DeptPremisesController.php 9.5 KB

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