123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
-
- namespace app\controller\store\appointment;
-
- use app\common\logic\DataLogic;
- use app\controller\store\AuthController;
- use app\model\store\appointment\AppointmentModel;
- use app\model\store\appointment\TechnicianModel;
- use app\model\store\appointment\TechnicianProjectModel;
- use app\model\store\appointment\TechnicianSchedulingModel;
- use app\model\store\project\ProjectModel;
- use app\model\store\SystemStore;
-
- class Scheduling extends AuthController
- {
-
- public function show()
- {
- $day = $this->request->param('day');
- if (empty($day)) return $this->fail('参数错误');
- if (false === strtotime($day . ' 00:00:00')) return $this->fail('参数错误');
- $store_id = $this->request->param('store_id/d', 0);
- if ($store_id == 0) return $this->fail('门店不存在');
-
- $where = [
- ['store_id', '=', $store_id],
- ['status', '=', 1],
- ['is_delete', '=', 0]
- ];
- $technician_model = new TechnicianModel();
- $technician_model = $technician_model->with(['schedulings' => function ($query) use ($day) {
- $query->where([['is_delete', '=', 0], ['day', '=', $day]]);
- }]);
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format'];
- $return = DataLogic::getDataList($technician_model, $where, $order, $field, $append);
- return $this->success($return);
- }
-
- public function showTechnicianFromScheduling()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- $day = $this->request->param('day');
-
- if ($technician_id == 0) return $this->fail('技师不存在');
- if (empty($day)) return $this->fail('参数错误');
- if (false === strtotime($day . ' 00:00:00')) return $this->fail('参数错误');
-
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail('技师不存在');
- if ($technician->is_delete != 0) return $this->fail('技师不存在');
- if ($technician->status != 1) return $this->fail('技师已经不在职了');
-
- $scheduling_time = TechnicianSchedulingModel::where([
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ['day', '=', $day],
- ['is_delete', '=', 0]
- ])->field('start_time,end_time')->order(['start_time' => 'asc'])->select()->toArray();
-
- $info = [
- 'face' => $technician->face,
- 'name' => $technician->name,
- 'mobile' => $technician->mobile,
- 'scheduling_time' => $scheduling_time
- ];
-
- return $this->success(['info' => $info]);
- }
-
- public function getProjectFromScheduling()
- {
- $technician_id = $this->request->param('technician_id/d', 0);
- if ($technician_id == 0) return $this->fail('技师不存在');
-
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail('技师不存在');
- if ($technician->is_delete != 0) return $this->fail('技师不存在');
- if ($technician->status != 1) return $this->fail('技师已经不在职了');
-
- $where = [
- ['is_delete', '=', 0],
- ];
-
- $bind_where = [
- ['store_id', '=', $technician->store_id],
- ['technician_id', '=', $technician_id],
- ['is_delete', '=', 0],
- ];
-
- $project_ids = TechnicianProjectModel::where($bind_where)->column('project_id') ?? [];
- $where[] = ['project_id', 'in', $project_ids];
- $project_model = new ProjectModel();
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format'];
-
- $return = DataLogic::getDataList($project_model, $where, $order, $field, $append);
-
- return $this->success($return);
- }
-
- public function save()
- {
- $today = date('Y-m-d');
- $technician_id = $this->request->param('technician_id/d', 0);
- $day = $this->request->param('day');
- $scheduling_time = $this->request->param('scheduling_time/a', []);
- $appointment_num = $this->request->param('appointment_num/d', 0);
- $appointment_interval = $this->request->param('appointment_interval/d', 30);
- $store_id = $this->request->param('store_id/d', 0);
-
- if ($store_id == 0) return $this->fail('门店不存在');
- //判断门店是否存在
- $store = new SystemStore();
- $check = $store->where('id', $store_id)->value('id');
- if (empty($check)) {
- return $this->fail('门店不存在');
- }
-
- if ($technician_id == 0) return $this->fail('技师不存在');
- if (empty($day)) return $this->fail('请选择预约日期');
- if (false === strtotime($day . ' 00.00')) return $this->fail('请选择预约日期');
- if (empty($scheduling_time) || !is_array($scheduling_time) || count($scheduling_time) != 2) return $this->fail('请选择工作时间');
- $start_time = $scheduling_time[0];
- $end_time = $scheduling_time[1];
- if (false === $start_time_unix = strtotime($day . ' ' . $start_time)) return $this->fail('请选择工作开始时间');
- if (false === $end_time_unix = strtotime($day . ' ' . $end_time)) return $this->fail('请选择工作结束时间');
- if ($appointment_interval < 30 || $appointment_interval > 120) {
- return $this->fail('请选择排班步长');
- }
- $unix = $appointment_interval * 60;
- $interval = $end_time_unix - $start_time_unix;
- if ($interval <= 0) return $this->fail('预约开始时间不能大于结束时间');
- if ($interval % 1800 != 0) return $this->fail('预约时间间隔必须是30分钟的整倍');
- if (empty($appointment_num) || $appointment_num <= 0) return $this->fail('请输入同时段预约人数,且不能小于0');
-
- if (null === $technician = TechnicianModel::find($technician_id)) return $this->fail('技师不存在');
- if ($technician->is_delete != 0) return $this->fail('技师不存在');
- if ($technician->status != 1) return $this->fail('技师已经不在职了');
- $timeArr = [];
- for ($i = $start_time_unix; $i < $end_time_unix; $i += $unix) {
- $timeArr[] = ['st' => date('H:i', $i), 'et' => date('H:i', $i + $unix)];
- }
- if (empty($timeArr)) {
- return $this->fail('该时间选择后未能生成有效时间排班');
- }
- $copy = $this->request->param('copy/d', 0);
- $days = [$day];
- for ($i = 1; $i <= $copy; $i++) {
- $time = strtotime($day) + 86400 * $i;
- $days[] = date('Y-m-d', $time);
- }
-
- $where = [
- ['store_id', '=', $store_id],
- ['is_delete', '=', 0],
- ['technician_id', '=', $technician_id],
- ['day', 'IN', $days],
- ];
- //这里删除这些日期的排版
- TechnicianSchedulingModel::where($where)->delete();//已经预约的没办法管了 批量的数据没办法操作
- $technician_scheduling_model = new TechnicianSchedulingModel();
- $saveAll = [];
- foreach ($days as $val) {
- foreach ($timeArr as $val2) {
- $saveAll[] = [
- 'store_id' => $store_id,
- 'technician_id' => $technician_id,
- 'day' => $val,
- 'start_time' => $val2['st'],
- 'end_time' => $val2['et'],
- 'appointment_num' => $appointment_num,
- ];
- }
- }
- $technician_scheduling_model->saveAll($saveAll);
- return $this->success('添加成功');
- }
-
- public function del()
- {
- $scheduling_id = $this->request->param('scheduling_id/d', 0);
- if ($scheduling_id == 0) return $this->fail('排班不存在');
-
- if (null === $technician_scheduling = TechnicianSchedulingModel::find($scheduling_id)) return $this->fail('排班不存在');
- if ($technician_scheduling->is_delete != 0) return $this->fail('排班不存在');
-
- $technician_scheduling->is_delete = 1;
- $technician_scheduling->save();
-
- $appointment = AppointmentModel::where([
- ['store_id', '=', $technician_scheduling->store_id],
- ['scheduling_id', '=', $scheduling_id],
- ['status', 'in', [1, 4]]
- ])->select()->toArray();
- if (!empty($appointment)) {
- $mobiles = [];
- foreach ($appointment as $k => $v) {
- $appointment[$k]['status'] = -1;
- $appointment[$k]['cancel_time'] = $this->request->time();
- $appointment[$k]['reason'] = '技师排班删除导致的取消';
- $appointment[$k]['operate_type'] = 2;
- $appointment[$k]['operate_id'] = $this->storeStaffId;
- $appointment[$k]['operate_name'] = $this->storeStaffInfo['staff_name'];
- $appointment[$k]['operate_mobile'] = $this->storeStaffInfo['phone'];
- $mobiles[] = $v['member_mobile'];
- }
- $appointment_model = new AppointmentModel();
- $appointment_model->saveAll($appointment);
-
- //发送短信给用户
- // try {
- // $sms_setting = SettingSmsModel::where([['store_id', '=', $store_id]])->find();
- // if (null !== $sms_setting) {
- // if ($sms_setting->cancel_appointment == 1) {
- // $sms_logic = new SmsLogic();
- // $params = [
- // 'xm' => $appointment[0]['project_name'],
- // ];
- // $sms_logic->send($store_id, $mobiles, $params, 'cancelAppointment');
- // }
- // }
- // } catch (\Exception $e) {
- //
- // }
- }
- return $this->success('删除成功');
- }
-
- }
|