DeptPremisesController.php 11 KB

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