DeptPremisesController.php 9.0 KB

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