DeptPremisesController.php 6.8 KB

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