| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace addons\Store\common\services;
- use addons\ReservationService\common\models\ReservationServiceUser;
- use addons\Store\common\models\StoreReserveTechnicianScheduling;
- use addons\Store\common\models\StoreReserveTechnicianSchedulingTime;
- use common\enums\StatusEnum;
- use common\helpers\DateHelper;
- use Exception;
- use Yii;
- class SchedulingService extends BaseService
- {
- //获取技师数据
- public function getTechnicianData($get)
- {
- //获取所有技师数据
- $user_list_info = ReservationServiceUser::find()->where([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED])
- ->select('user_id,name')->asArray()->all();
- if (!empty($get['user_id'])) {
- $user_id = $get['user_id'];
- //获取所选技师数据
- $user_info = ReservationServiceUser::getDataOne([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED,
- 'user_id' => $get['user_id']
- ], 'user_id,name');
- } else {
- $user_info = $user_list_info[0] ?? [];
- $user_id = $user_info['user_id'];
- }
- $date_time = date('Y-m-d', time());
- $data = self::technicianSchedulingDetail(['user_id' => $user_id, 'date_time' => $date_time]);
- $user_info['detail'] = $data;
- return ['user_list' => $user_list_info, 'user_detail' => $user_info];
- }
- //获取选取日期排期详情
- public function technicianSchedulingDetail($params)
- {
- if (empty($params)) return [];
- $info = StoreReserveTechnicianScheduling::getDataOne(['mall_id' => $this->mall_id, 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'date_time' => strtotime($params['date_time'])]);
- if (empty($info)) {
- return [];
- }
- $info['date_time'] = date('Y-m-d', $info['date_time']);
- $time_data = StoreReserveTechnicianSchedulingTime::getDataAll(['sid' => $info['id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id, 'user_id' => $params['user_id']], 'id,start_time,end_time,sid');
- if (!empty($time_data)) {
- // 排序时间数据,按照 start_time 从小到大排序
- usort($time_data, function ($a, $b) {
- return $a['start_time'] <=> $b['start_time'];
- });
- foreach ($time_data as $key => $value) {
- $time_data[$key]['start_time'] = date('H:i:s', $value['start_time']);
- $time_data[$key]['end_time'] = date('H:i:s', $value['end_time']);
- $time_data[$key]['time_data'] = [date('Y-m-d H:i:s', $value['start_time']), date('Y-m-d H:i:s', $value['end_time'])];
- }
- $info['schedulingTime'] = $time_data;
- }
- return $info;
- }
- //获取选取月份排期
- public function technicianScheduling($params)
- {
- if (empty($params['month'])) $params['month'] = date('m', time());
- $year = $params['year'];
- $start_time = strtotime($year . '-' . $params['month']);
- $end_time = DateHelper::getMonthLastDayTime($start_time);
- $where = [
- ['mall_id' => $this->mall_id],
- ['user_id' => $params['user_id']],
- ['>=', 'date_time', $start_time],
- ['<', 'date_time', $end_time],
- ['status' => StatusEnum::ENABLED]
- ];
- $data = ['where' => $where, 'with' => ['schedulingTime'], 'limit' => 31, 'order' => 'date_time asc'];
- $list = StoreReserveTechnicianScheduling::listPage($data);
- if (!empty($list['list'])) {
- foreach ($list['list'] as $key => $value) {
- $list['list'][$key]['date_time'] = date('Y-m-d', $value['date_time']);
- if (!empty($value['schedulingTime'])) {
- foreach ($value['schedulingTime'] as $k => $val) {
- $list['list'][$key]['schedulingTime'][$k]['start_time'] = date('H:i:s', $val['start_time']);
- $list['list'][$key]['schedulingTime'][$k]['end_time'] = date('H:i:s', $val['end_time']);
- }
- }
- }
- }
- return $list['list'];
- }
- //设置排期
- public function setTechnicianScheduling($params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $params['mall_id'] = $this->mall_id;
- $params['date_time'] = strtotime($params['date_time']);
- $params['store_id'] = $this->store_id;
- if (empty($params['id'])) {
- //检查是否已经设置了当天的排班数据
- $check = StoreReserveTechnicianScheduling::getDataOne(['mall_id' => $this->mall_id,'store_id'=>$this->store_id, 'user_id' => $params['user_id'], 'date_time' => $params['date_time'], 'status' => StatusEnum::ENABLED]);
- if (!empty($check)) $params['id'] = $check['id'];
- }
- $res = StoreReserveTechnicianScheduling::setData($params);
- if (!$res) throw new Exception(StoreReserveTechnicianScheduling::getStaticError());
- //如果不是全天并且详细时间数据不为空
- if ($params['is_all_day'] != 1) {
- if (empty($params['time_data'])) throw new Exception('请选择时间');
- //是否是批量设置整个月的排班
- if ($params['is_month'] == 1 && $params['is_have'] == 1) {
- //如果已经存在排班时间的日期,则跳过设置该日期
- StoreReserveTechnicianSchedulingTime::updateAll(['status' => StatusEnum::DELETE],['sid' => $res->id, 'mall_id' => $this->mall_id, 'user_id' => $params['user_id'],]);
- }
- //如果详细时间数据不是数组,解json格式
- if (!is_array($params['time_data'])) $params['time_data'] = json_decode($params['time_data'], true);
- foreach ($params['time_data'] as $key => $value) {
- //格式化时间
- if (empty($value['time_data'])) {
- $start = date("Y-m-d", $params['date_time']) . $value['start_time'];
- $end = date("Y-m-d", $params['date_time']) . $value['end_time'];
- } else { // 批量设置月排班时间时,需重新格式化
- $start = date("Y-m-d", $params['date_time']) . ' ' . date('H:i:s', strtotime($value['time_data'][0]));
- $end = date("Y-m-d", $params['date_time']) . ' ' . date('H:i:s', strtotime($value['time_data'][1]));
- }
- $data = [
- 'sid' => $res->id,
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'user_id' => $params['user_id'],
- 'status' => StatusEnum::ENABLED,
- 'start_time' => strtotime($start),
- 'end_time' => strtotime($end),
- ];
- if (!empty($value['id'])) {
- $data['id'] = $value['id'];
- }
- $time_res = StoreReserveTechnicianSchedulingTime::setData($data);
- if (!$time_res) throw new \Exception('添加时间失败');
- }
- }
- $trans->commit();
- return ['id' => $res->id];
- } catch (Exception $e) {
- $trans->rollBack();
- return $e->getMessage();
- }
- }
- //修改排班时间段状态
- public function changeTechnicianSchedulingTimeStatus($params)
- {
- try {
- $check_data = StoreReserveTechnicianSchedulingTime::getDataOne(['id' => $params['id'], 'mall_id' => $this->mall_id,'store_id' => $this->store_id, 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($check_data)) throw new Exception('查无该排班时间');
- $res = StoreReserveTechnicianSchedulingTime::updateAll(['status' => $params['status']], ['id' => $params['id'], 'mall_id' => $this->mall_id, 'store_id' => $this->store_id,'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- if (!$res) throw new Exception('操作失败');
- return [];
- } catch (Exception $e) {
- return $e->getMessage();
- }
- }
- //批量设置当月排期
- public function setTechnicianSchedulingMonth($params)
- {
- try {
- $year = $params['year'];
- $start_time = strtotime($year . '-' . $params['month']);
- $end_time = DateHelper::getMonthLastDayTime($start_time);
- $i = 0;
- $data = [
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['user_id' => $params['user_id']],
- ['status' => StatusEnum::ENABLED],
- ['between', 'date_time', $start_time, $end_time],
- ],
- 'index' => 'date_time',
- 'with' => ['schedulingTime'],
- ];
- $list = StoreReserveTechnicianScheduling::lists($data);
- //循环所选月的日期
- while ($start_time + ($i * 24 * 60 * 60) < $end_time) {
- $data = [
- 'date_time' => date('Y-m-d', $start_time + ($i * 24 * 60 * 60)),
- 'is_all_day' => $params['is_all_day'],
- 'user_id' => $params['user_id'],
- 'is_month' => 1,// 表示该日期是由批量设置当月日期进行操作
- ];
- $scheduling_data = $list[strtotime($data['date_time'])];
- if (!empty($params['time_data'])) $data['time_data'] = $params['time_data'];
- if (!empty($scheduling_data)) {
- $data['id'] = $scheduling_data['id'];
- if (!empty($data['time_data']) && !empty($scheduling_data['schedulingTime'])) {
- $data['is_have'] = 1;
- foreach ($data['time_data'] as $key => $value) {
- foreach ($scheduling_data['schedulingTime'] as $k => $val) {
- if (strtotime($data['date_time'] . ' ' . date('H:i:s', strtotime($value['time_data'][0]))) == $val['start_time']) {
- continue;
- }
- }
- }
- };
- }
- $res = self::setTechnicianScheduling($data);
- if (is_string($res)) throw new Exception($data['date_time'] . $res);
- $i++;
- }
- return [];
- } catch (Exception $e) {
- return $e->getMessage();
- }
- }
- public function deleteTechnician($params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $data = [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['user_id' => $params['user_id']],
- ['id' => $params['id']],
- ['status' => StatusEnum::ENABLED]
- ];
- $check_data = StoreReserveTechnicianScheduling::getOne($data, true, ['schedulingTime']);
- if (empty($check_data)) throw new Exception('不存在该排期数据');
- $res = StoreReserveTechnicianScheduling::updateAll(['status' => StatusEnum::DELETE], ['id' => $check_data['id']]);
- if (!$res) throw new Exception('删除排期数据失败');
- //如果存在明细数据
- if (!empty($check_data['schedulingTime'])) {
- $res = StoreReserveTechnicianSchedulingTime::updateAll(['status' => StatusEnum::DELETE], ['sid' => $check_data['id']]);
- if (!$res) throw new Exception('删除失败');
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- return $e->getMessage();
- }
- }
- public function deleteScheduling($params)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $data = [
- ['mall_id' => $this->mall_id],
- ['user_id' => $params['user_id']],
- ['id' => $params['id']],
- ['status' => StatusEnum::ENABLED]
- ];
- $check_data = StoreReserveTechnicianScheduling::getOne($data, true, ['schedulingTime']);
- if (empty($check_data)) throw new Exception('不存在该排期数据');
- $res = StoreReserveTechnicianScheduling::updateAll(['status' => StatusEnum::DELETE], ['id' => $check_data['id']]);
- if (!$res) throw new Exception('删除排期数据失败');
- //如果存在明细数据
- if (!empty($check_data['schedulingTime'])) {
- $res = StoreReserveTechnicianSchedulingTime::updateAll(['status' => StatusEnum::DELETE], ['sid' => $check_data['id']]);
- if (!$res) throw new Exception('删除失败');
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- return $e->getMessage();
- }
- }
- }
|