You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Scheduling.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\controller\store\appointment;
  3. use app\common\logic\DataLogic;
  4. use app\controller\store\AuthController;
  5. use app\model\store\appointment\AppointmentModel;
  6. use app\model\store\appointment\TechnicianModel;
  7. use app\model\store\appointment\TechnicianProjectModel;
  8. use app\model\store\appointment\TechnicianSchedulingModel;
  9. use app\model\store\project\ProjectModel;
  10. use app\model\store\SystemStore;
  11. class Scheduling extends AuthController
  12. {
  13. public function show()
  14. {
  15. $day = $this->request->param('day');
  16. if (empty($day)) return $this->fail('参数错误');
  17. if (false === strtotime($day . ' 00:00:00')) return $this->fail('参数错误');
  18. $store_id = $this->request->param('store_id/d', 0);
  19. if ($store_id == 0) return $this->fail('门店不存在');
  20. $where = [
  21. ['store_id', '=', $store_id],
  22. ['status', '=', 1],
  23. ['is_delete', '=', 0]
  24. ];
  25. $technician_model = new TechnicianModel();
  26. $technician_model = $technician_model->with(['schedulings' => function ($query) use ($day) {
  27. $query->where([['is_delete', '=', 0], ['day', '=', $day]]);
  28. }]);
  29. $order = ['add_time' => 'desc'];
  30. $field = '*';
  31. $append = ['add_time_format'];
  32. $return = DataLogic::getDataList($technician_model, $where, $order, $field, $append);
  33. return $this->success($return);
  34. }
  35. public function showTechnicianFromScheduling()
  36. {
  37. $technician_id = $this->request->param('technician_id/d', 0);
  38. $day = $this->request->param('day');
  39. if ($technician_id == 0) return $this->fail('技师不存在');
  40. if (empty($day)) return $this->fail('参数错误');
  41. if (false === strtotime($day . ' 00:00:00')) return $this->fail('参数错误');
  42. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail('技师不存在');
  43. if ($technician->is_delete != 0) return $this->fail('技师不存在');
  44. if ($technician->status != 1) return $this->fail('技师已经不在职了');
  45. $scheduling_time = TechnicianSchedulingModel::where([
  46. ['store_id', '=', $technician->store_id],
  47. ['technician_id', '=', $technician_id],
  48. ['day', '=', $day],
  49. ['is_delete', '=', 0]
  50. ])->field('start_time,end_time')->order(['start_time' => 'asc'])->select()->toArray();
  51. $info = [
  52. 'face' => $technician->face,
  53. 'name' => $technician->name,
  54. 'mobile' => $technician->mobile,
  55. 'scheduling_time' => $scheduling_time
  56. ];
  57. return $this->success(['info' => $info]);
  58. }
  59. public function getProjectFromScheduling()
  60. {
  61. $technician_id = $this->request->param('technician_id/d', 0);
  62. if ($technician_id == 0) return $this->fail('技师不存在');
  63. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail('技师不存在');
  64. if ($technician->is_delete != 0) return $this->fail('技师不存在');
  65. if ($technician->status != 1) return $this->fail('技师已经不在职了');
  66. $where = [
  67. ['is_delete', '=', 0],
  68. ];
  69. $bind_where = [
  70. ['store_id', '=', $technician->store_id],
  71. ['technician_id', '=', $technician_id],
  72. ['is_delete', '=', 0],
  73. ];
  74. $project_ids = TechnicianProjectModel::where($bind_where)->column('project_id') ?? [];
  75. $where[] = ['project_id', 'in', $project_ids];
  76. $project_model = new ProjectModel();
  77. $order = ['add_time' => 'desc'];
  78. $field = '*';
  79. $append = ['add_time_format'];
  80. $return = DataLogic::getDataList($project_model, $where, $order, $field, $append);
  81. return $this->success($return);
  82. }
  83. public function save()
  84. {
  85. $today = date('Y-m-d');
  86. $technician_id = $this->request->param('technician_id/d', 0);
  87. $day = $this->request->param('day');
  88. $scheduling_time = $this->request->param('scheduling_time/a', []);
  89. $appointment_num = $this->request->param('appointment_num/d', 0);
  90. $appointment_interval = $this->request->param('appointment_interval/d', 30);
  91. $store_id = $this->request->param('store_id/d', 0);
  92. if ($store_id == 0) return $this->fail('门店不存在');
  93. //判断门店是否存在
  94. $store = new SystemStore();
  95. $check = $store->where('id', $store_id)->value('id');
  96. if (empty($check)) {
  97. return $this->fail('门店不存在');
  98. }
  99. if ($technician_id == 0) return $this->fail('技师不存在');
  100. if (empty($day)) return $this->fail('请选择预约日期');
  101. if (false === strtotime($day . ' 00.00')) return $this->fail('请选择预约日期');
  102. if (empty($scheduling_time) || !is_array($scheduling_time) || count($scheduling_time) != 2) return $this->fail('请选择工作时间');
  103. $start_time = $scheduling_time[0];
  104. $end_time = $scheduling_time[1];
  105. if (false === $start_time_unix = strtotime($day . ' ' . $start_time)) return $this->fail('请选择工作开始时间');
  106. if (false === $end_time_unix = strtotime($day . ' ' . $end_time)) return $this->fail('请选择工作结束时间');
  107. if ($appointment_interval < 30 || $appointment_interval > 120) {
  108. return $this->fail('请选择排班步长');
  109. }
  110. $unix = $appointment_interval * 60;
  111. $interval = $end_time_unix - $start_time_unix;
  112. if ($interval <= 0) return $this->fail('预约开始时间不能大于结束时间');
  113. if ($interval % 1800 != 0) return $this->fail('预约时间间隔必须是30分钟的整倍');
  114. if (empty($appointment_num) || $appointment_num <= 0) return $this->fail('请输入同时段预约人数,且不能小于0');
  115. if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail('技师不存在');
  116. if ($technician->is_delete != 0) return $this->fail('技师不存在');
  117. if ($technician->status != 1) return $this->fail('技师已经不在职了');
  118. $timeArr = [];
  119. for ($i = $start_time_unix; $i < $end_time_unix; $i += $unix) {
  120. $timeArr[] = ['st' => date('H:i', $i), 'et' => date('H:i', $i + $unix)];
  121. }
  122. if (empty($timeArr)) {
  123. return $this->fail('该时间选择后未能生成有效时间排班');
  124. }
  125. $copy = $this->request->param('copy/d', 0);
  126. $days = [$day];
  127. for ($i = 1; $i <= $copy; $i++) {
  128. $time = strtotime($day) + 86400 * $i;
  129. $days[] = date('Y-m-d', $time);
  130. }
  131. $where = [
  132. ['store_id', '=', $store_id],
  133. ['is_delete', '=', 0],
  134. ['technician_id', '=', $technician_id],
  135. ['day', 'IN', $days],
  136. ];
  137. //这里删除这些日期的排版
  138. TechnicianSchedulingModel::where($where)->delete();//已经预约的没办法管了 批量的数据没办法操作
  139. $technician_scheduling_model = new TechnicianSchedulingModel();
  140. $saveAll = [];
  141. foreach ($days as $val) {
  142. foreach ($timeArr as $val2) {
  143. $saveAll[] = [
  144. 'store_id' => $store_id,
  145. 'technician_id' => $technician_id,
  146. 'day' => $val,
  147. 'start_time' => $val2['st'],
  148. 'end_time' => $val2['et'],
  149. 'appointment_num' => $appointment_num,
  150. ];
  151. }
  152. }
  153. $technician_scheduling_model->saveAll($saveAll);
  154. return $this->success('添加成功');
  155. }
  156. public function del()
  157. {
  158. $scheduling_id = $this->request->param('scheduling_id/d', 0);
  159. if ($scheduling_id == 0) return $this->fail('排班不存在');
  160. if (null === $technician_scheduling = TechnicianSchedulingModel::find($scheduling_id)) return $this->fail('排班不存在');
  161. if ($technician_scheduling->is_delete != 0) return $this->fail('排班不存在');
  162. $technician_scheduling->is_delete = 1;
  163. $technician_scheduling->save();
  164. $appointment = AppointmentModel::where([
  165. ['store_id', '=', $technician_scheduling->store_id],
  166. ['scheduling_id', '=', $scheduling_id],
  167. ['status', 'in', [1, 4]]
  168. ])->select()->toArray();
  169. if (!empty($appointment)) {
  170. $mobiles = [];
  171. foreach ($appointment as $k => $v) {
  172. $appointment[$k]['status'] = -1;
  173. $appointment[$k]['cancel_time'] = $this->request->time();
  174. $appointment[$k]['reason'] = '技师排班删除导致的取消';
  175. $appointment[$k]['operate_type'] = 2;
  176. $appointment[$k]['operate_id'] = $this->storeStaffId;
  177. $appointment[$k]['operate_name'] = $this->storeStaffInfo['staff_name'];
  178. $appointment[$k]['operate_mobile'] = $this->storeStaffInfo['phone'];
  179. $mobiles[] = $v['member_mobile'];
  180. }
  181. $appointment_model = new AppointmentModel();
  182. $appointment_model->saveAll($appointment);
  183. //发送短信给用户
  184. // try {
  185. // $sms_setting = SettingSmsModel::where([['store_id', '=', $store_id]])->find();
  186. // if (null !== $sms_setting) {
  187. // if ($sms_setting->cancel_appointment == 1) {
  188. // $sms_logic = new SmsLogic();
  189. // $params = [
  190. // 'xm' => $appointment[0]['project_name'],
  191. // ];
  192. // $sms_logic->send($store_id, $mobiles, $params, 'cancelAppointment');
  193. // }
  194. // }
  195. // } catch (\Exception $e) {
  196. //
  197. // }
  198. }
  199. return $this->success('删除成功');
  200. }
  201. }