DeptPremisesController.php 7.6 KB

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