DeptPremisesController.php 10 KB

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