DeptPremisesController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. return $data;
  135. }
  136. /**
  137. * @Desc 更新
  138. * @Author Gorden
  139. * @Date 2024/3/7 13:14
  140. *
  141. * @param Request $request
  142. * @return Response
  143. */
  144. public function update(Request $request): Response
  145. {
  146. if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {
  147. return json_fail($this->validateClass->getError());
  148. }
  149. try {
  150. [$id, $data] = $this->updateInput($request);
  151. $this->doUpdate($id, $data);
  152. } catch (BusinessException $customException) {
  153. return json_fail($customException->getMessage());
  154. } catch (\Exception $e) {
  155. return json_fail('数据更新失败');
  156. }
  157. return json_success('success');
  158. }
  159. /**
  160. * 更新前置方法
  161. * @param Request $request
  162. * @return array
  163. * @throws BusinessException
  164. */
  165. protected function updateInput(Request $request): array
  166. {
  167. $work = $request->post('dept_work', []);
  168. $dateWeek = $request->post('dept_week', []);
  169. $primary_key = $this->model->getKeyName();
  170. $id = $request->post($primary_key);
  171. $data = $this->inputFilter($request->post());
  172. $model = $this->model->find($id);
  173. if (!$model) {
  174. throw new BusinessException('记录不存在', 2);
  175. }
  176. $extendJson = [];
  177. if (!empty($model->dept_extend_json)) {
  178. $extendJson = json_decode($model->dept_extend_json, true);
  179. }
  180. if (isset($work[0]) && isset($work[1])) {
  181. $start = date('H:i', strtotime($work[0]));
  182. $end = date('H:i', strtotime($work[1]));
  183. $extendJson['times'] = $start . '~' . $end;
  184. } elseif (!isset($work[0]) && isset($extendJson['times'])) {
  185. unset($extendJson['times']);
  186. }
  187. if ($dateWeek) {
  188. $newDates = [];
  189. foreach ($dateWeek as $date) {
  190. $key = self::$week[$date];
  191. $newDates[$key] = $date;
  192. }
  193. ksort($newDates);
  194. $currentDate = current($newDates);
  195. $lastDate = end($newDates);
  196. $weekStr = '';
  197. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  198. $weekStr = implode(',', $newDates);
  199. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  200. $weekStr = $currentDate . '至' . $lastDate;
  201. }
  202. $extendJson['weeks'] = $weekStr;
  203. $extendJson['week'] = $dateWeek;
  204. }
  205. $data['dept_extend_json'] = json_encode($extendJson);
  206. unset($data[$primary_key]);
  207. return [$id, $data];
  208. }
  209. public static $week = [
  210. '周一' => 1,
  211. '周二' => 2,
  212. '周三' => 3,
  213. '周四' => 4,
  214. '周五' => 5,
  215. '周六' => 6,
  216. '周日' => 7,
  217. ];
  218. }