RestaurantController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\controller\Curd;
  4. use app\model\Supplier;
  5. use app\model\SysDept;
  6. use app\model\SysUser;
  7. use support\Db;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. use Tinywan\Jwt\JwtToken;
  12. class RestaurantController extends Curd{
  13. public function __construct()
  14. {
  15. $this->model = new SysDept();
  16. }
  17. public function select(Request $request):Response
  18. {
  19. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  20. $where['dept_category'] = '餐厅';
  21. $order = $request->get('order', 'desc');
  22. $field = $field ?? 'dept_sort';
  23. $query = $this->doSelect($where, $field, $order);
  24. return $this->doFormat($query, $format, $limit);
  25. }
  26. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  27. {
  28. $model = $this->model->with([
  29. 'premises' => function ($query) {
  30. $query->select('dept_id', 'dept_name');
  31. },
  32. ]);
  33. foreach ($where as $column => $value) {
  34. if (is_array($value)) {
  35. if ($value[0] === 'like' || $value[0] === 'not like') {
  36. $model = $model->where($column, $value[0], "%$value[1]%");
  37. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  38. $model = $model->where($column, $value[0], $value[1]);
  39. } elseif ($value[0] == 'in' && !empty($value[1])) {
  40. $valArr = $value[1];
  41. if (is_string($value[1])) {
  42. $valArr = explode(",", trim($value[1]));
  43. }
  44. $model = $model->whereIn($column, $valArr);
  45. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  46. $valArr = $value[1];
  47. if (is_string($value[1])) {
  48. $valArr = explode(",", trim($value[1]));
  49. }
  50. $model = $model->whereNotIn($column, $valArr);
  51. } elseif ($value[0] == 'null') {
  52. $model = $model->whereNull($column);
  53. } elseif ($value[0] == 'not null') {
  54. $model = $model->whereNotNull($column);
  55. } elseif ($value[0] !== '' || $value[1] !== '') {
  56. $model = $model->whereBetween($column, $value);
  57. }
  58. } else {
  59. $model = $model->where($column, $value);
  60. }
  61. }
  62. if ($field) {
  63. $model = $model->orderBy($field, $order);
  64. }
  65. return $model;
  66. }
  67. public function afterQuery($items)
  68. {
  69. foreach ($items as &$item) {
  70. if (!empty($item->dept_extend_json)) {
  71. $extendJson = json_decode($item->dept_extend_json, true);
  72. $item->dept_week = $extendJson['week'] ?? [];
  73. $item->dept_work = [];
  74. $deptWork = [];
  75. if (!empty($extendJson['times'])) {
  76. $times = explode('~', $extendJson['times']);
  77. $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[0]));
  78. $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[1]));
  79. }
  80. $item->dept_work = $deptWork;
  81. }
  82. }
  83. return $items;
  84. }
  85. public function insert(Request $request): Response
  86. {
  87. if ($this->validate && !$this->validateClass->scene('add_premises')->check($request->post())) {
  88. return json_fail($this->validateClass->getError());
  89. }
  90. // 验证编号是否存在
  91. if (!empty($request->post('dept_code')) && SysDept::checkExist('餐厅', 'dept_code', $request->post('dept_code'))) {
  92. return json_fail('编号重复,请重新输入');
  93. }
  94. Db::beginTransaction();
  95. try {
  96. $data = $this->insertInput($request);
  97. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  98. if (!$premises){
  99. Db::rollBack();
  100. return json_fail('营销场所不存在');
  101. }
  102. $data['dept_category'] = '餐厅';
  103. $deptId = $this->doInsert($data);
  104. if (!$deptId) {
  105. throw new BusinessException('创建餐厅失败');
  106. }
  107. // 更新部门path
  108. $path = $premises-> dept_super_path . $deptId . '/';
  109. if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {
  110. throw new BusinessException('创建餐厅失败');
  111. }
  112. // 新餐厅到供应商里
  113. $supplierData = [
  114. 'join_supplier_category_id'=>55,
  115. 'join_supplier_dept_id'=>$deptId,
  116. 'supplier_status'=>'ACTIVED',
  117. 'supplier_category'=>'DEPT',
  118. 'supplier_name' => $data['dept_name'],
  119. 'supplier_addtimes'=>time()
  120. ];
  121. Supplier::insert($supplierData);
  122. Db::commit();
  123. } catch (BusinessException $customException) {
  124. Db::rollBack();
  125. return json_fail($customException->getMessage());
  126. } catch (\Exception $exception) {
  127. dump($exception->getMessage());
  128. Db::rollBack();
  129. return json_fail('创建餐厅失败');
  130. }
  131. return json_success('success');
  132. }
  133. protected function insertInput(Request $request): array
  134. {
  135. $data = $this->inputFilter($request->post());
  136. $work = $request->post('dept_work', []);
  137. $dateWeek = $request->post('dept_week', []);
  138. $extendJson = [];
  139. if (isset($work[0]) && isset($work[1])) {
  140. $start = date('H:i', strtotime($work[0]));
  141. $end = date('H:i', strtotime($work[1]));
  142. $extendJson['times'] = $start . '~' . $end;
  143. } elseif (!isset($work[0]) && isset($extendJson['times'])) {
  144. unset($extendJson['times']);
  145. }
  146. if ($dateWeek) {
  147. $newDates = [];
  148. foreach ($dateWeek as $date) {
  149. $key = self::$week[$date];
  150. $newDates[$key] = $date;
  151. }
  152. ksort($newDates);
  153. $currentDate = current($newDates);
  154. $lastDate = end($newDates);
  155. $weekStr = '';
  156. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  157. $weekStr = implode(',', $newDates);
  158. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  159. $weekStr = $currentDate . '至' . $lastDate;
  160. }
  161. $extendJson['weeks'] = $weekStr;
  162. $extendJson['week'] = $dateWeek;
  163. }
  164. $data['dept_extend_json'] = json_encode($extendJson);
  165. return $data;
  166. }
  167. /**
  168. * 更新
  169. * @param Request $request
  170. * @return Response
  171. */
  172. public function update(Request $request): Response
  173. {
  174. if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {
  175. return json_fail($this->validateClass->getError());
  176. }
  177. try {
  178. [$id, $data] = $this->updateInput($request);
  179. $premises = SysDept::where('dept_id',$data['dept_super_id'])->where('dept_category','营业场所')->first();
  180. if (!$premises){
  181. Db::rollBack();
  182. return json_fail('营销场所不存在');
  183. }
  184. // 更新部门path
  185. $data['dept_super_path'] = $premises-> dept_super_path . $id . '/';
  186. $this->doUpdate($id, $data);
  187. } catch (BusinessException $customException) {
  188. return json_fail($customException->getMessage());
  189. } catch (\Exception $e) {
  190. return json_fail('数据更新失败');
  191. }
  192. return json_success('success');
  193. }
  194. /**
  195. * 更新前置方法
  196. * @param Request $request
  197. * @return array
  198. * @throws BusinessException
  199. */
  200. protected function updateInput(Request $request): array
  201. {
  202. $work = $request->post('dept_work', []);
  203. $dateWeek = $request->post('dept_week', []);
  204. $primary_key = $this->model->getKeyName();
  205. $id = $request->post($primary_key);
  206. $data = $this->inputFilter($request->post());
  207. $model = $this->model->find($id);
  208. if (!$model) {
  209. throw new BusinessException('记录不存在', 2);
  210. }
  211. $extendJson = [];
  212. if (!empty($model->dept_extend_json)) {
  213. $extendJson = json_decode($model->dept_extend_json, true);
  214. }
  215. if (isset($work[0]) && isset($work[1])) {
  216. $start = date('H:i', strtotime($work[0]));
  217. $end = date('H:i', strtotime($work[1]));
  218. $extendJson['times'] = $start . '~' . $end;
  219. } elseif (!isset($work[0]) && isset($extendJson['times'])) {
  220. unset($extendJson['times']);
  221. }
  222. if ($dateWeek) {
  223. $newDates = [];
  224. foreach ($dateWeek as $date) {
  225. $key = self::$week[$date];
  226. $newDates[$key] = $date;
  227. }
  228. ksort($newDates);
  229. $currentDate = current($newDates);
  230. $lastDate = end($newDates);
  231. $weekStr = '';
  232. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  233. $weekStr = implode(',', $newDates);
  234. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  235. $weekStr = $currentDate . '至' . $lastDate;
  236. }
  237. $extendJson['weeks'] = $weekStr;
  238. $extendJson['week'] = $dateWeek;
  239. }
  240. $data['dept_extend_json'] = json_encode($extendJson);
  241. unset($data[$primary_key]);
  242. return [$id, $data];
  243. }
  244. public static $week = [
  245. '周一' => 1,
  246. '周二' => 2,
  247. '周三' => 3,
  248. '周四' => 4,
  249. '周五' => 5,
  250. '周六' => 6,
  251. '周日' => 7,
  252. ];
  253. /**
  254. * 场所名获取餐厅打印机
  255. */
  256. public function getPrinterByPremiseName(Request $request)
  257. {
  258. $premiseName = $request->get('premise','');
  259. $printerList = [];
  260. try{
  261. $resraurant = SysDept::where('dept_name',$premiseName)->where('dept_category','餐厅')->first();
  262. if (!$resraurant){
  263. $premise = SysDept::where('dept_name',$premiseName)->where('dept_category','营业场所')->first();
  264. $resraurant = SysDept::where('dept_super_id',$premise->dept_id)->where('dept_category','餐厅')->first();
  265. }
  266. if (!empty($resraurant->dept_extend_json)){
  267. $deptExtendJson = json_decode($resraurant->dept_extend_json,true);
  268. if (isset($deptExtendJson['printer'])){
  269. $printerList = $deptExtendJson['printer'];
  270. }
  271. }
  272. }catch(\Exception $e){}
  273. return json_success('',$printerList);
  274. }
  275. /**
  276. * 获取用户所在餐厅
  277. */
  278. public function getByUser(Request $request)
  279. {
  280. $userId = $request->get('user_id',JwtToken::getCurrentId());
  281. $user = SysUser::where('user_id',$userId)->first();
  282. $restaurant = SysDept::where('dept_id',$user->join_user_dept_id)->where('dept_category','餐厅')->get()->toArray();
  283. if(empty($restaurant)){
  284. $premise = SysDept::where('dept_id',$user->join_user_dept_id)->where('dept_category','营业场所')->first();
  285. if (!empty($premise)){
  286. $restaurant = SysDept::where('dept_super_id',$premise->dept_id)->where('dept_category','餐厅')->get()->toArray();
  287. }
  288. }
  289. $data = [];
  290. foreach ($restaurant as $item){
  291. $data[] = [
  292. 'key'=>$item['dept_id'],
  293. 'label' => $item['dept_name']
  294. ];
  295. }
  296. return json_success('success',$data);
  297. }
  298. }