RestaurantController.php 13 KB

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