| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- namespace addons\Store\common\services;
- use addons\ReservationService\common\enums\ReservationServiceEnum;
- use addons\ReservationService\common\models\ReservationServiceOccupy;
- use addons\ReservationService\common\models\ReservationServiceOrder;
- use addons\ReservationService\common\models\ReservationServiceUser;
- use addons\SecondaryCard\common\models\SecondaryCard;
- use addons\Store\common\enums\ReserveServiceEnum;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreGoodsReserveSku;
- use addons\Store\common\models\StoreReserveTechnicianScheduling;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- class ReserveBoardService extends BaseService
- {
- public function index($params)
- {
- if (empty($params['date'])) {
- $date = date('Y-m-d');
- } else {
- $date = date('Y-m-d', strtotime($params['date']));
- }
- $technician_display = empty($params['technician_display']) ? 0 : 1;
- $date_time_start = strtotime($date . ' 00:00:00');
- $date_time_end = strtotime($date . ' 23:59:59');
- $unassigned = $this->getUnassigned($date_time_start, $date_time_end);
- $tech_user_ids = [];
- $staffs = $this->getStaffs($technician_display, $date, $tech_user_ids);
- $staffs_user_list = $this->getStaffsUser($tech_user_ids);
- $appointments = $this->getAppointments($staffs, $date_time_start, $date_time_end);
- $store = $params['store'];
- $business_time_start = strtotime($date . ' ' . $store['business_time_start'] . ':00');
- $business_time_end = strtotime($date . ' ' . $store['business_time_end'] . ':00');
- $H = date('H', $business_time_start);
- $start_time = $H * 60;
- $H = date('H', $business_time_end);
- $minute = date('i', $business_time_end);
- $end_time = $H * 60 + $minute;
- return ['unassigned' => $unassigned, 'staffs' => $staffs, 'staffs_user_list' => $staffs_user_list, 'appointments' => $appointments,
- 'start_time' => $start_time,
- 'end_time' => $end_time,
- 'store_name' => $store['store_name'],
- ReservationServiceEnum::SERVER_STATUS_MAP
- ];
- }
- protected function getUnassigned($date_time_start, $date_time_end)
- {
- $unassigned = [];//未分配
- $unassigned_list = ReservationServiceOrder::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['staff_user_id' => 0],
- ['!=', 'service_status', ReserveServiceEnum::RESERVE_ORDER_STATUS_CLOSED],
- ['>=', 'service_start_at', $date_time_start],
- ['<=', 'service_start_at', $date_time_end],
- [
- 'or',
- ['>', 'goods_id', 0],
- [
- 'and',
- ['>', 'secondary_card_id', 0],
- ['reservation_model' => 2]
- ],
- ]
- ],
- 'with' => ['user', 'orderDetail'],
- ]);
- $time = time();
- $goods_ids = array_column($unassigned_list, 'goods_id');
- $goods_list = StoreGoods::lists(['where' => [['id' => $goods_ids]], 'select' => 'id,goods_name', 'index' => 'id']);
- $service_type_map = ReserveServiceEnum::SERVICE_TYPE_MAP;
- foreach ($unassigned_list as $item) {
- $row['id'] = $item['id'];
- $row['name'] = $item['user']['nickname'];
- $row['phone'] = $item['user']['mobile'];
- $row['schedule'] = ['startTime' => date('H:i', $item['service_start_at']), 'endTime' => date('H:i', $item['service_end_at'])];
- $row['late'] = 0;
- $row['project'] = $goods_list[$item['goods_id']]['goods_name'] ?? '';
- if ($time > $item['service_start_at']) {
- $row['late'] = intval(($time - $item['service_start_at']) / 60);
- }
- $row['service_type_text'] = $service_type_map[$item['service_type']] ?? '';
- $row['server_status'] = $item['server_status'];
- $row['create_type'] = $item['create_type'];
- $row['order_no'] = $item['order_no'];
- $unassigned[] = $row;
- }
- return $unassigned;
- }
- protected function getStaffs($technician_display, $date, &$tech_user_ids)
- {
- $date_time = strtotime($date);
- $where = [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['status' => StatusEnum::ENABLED],
- ];
- if (!empty($technician_display)) {
- $scheduling_list = StoreReserveTechnicianScheduling::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['date_time' => $date_time],
- ['status' => StatusEnum::ENABLED]
- ],
- ]);
- $user_ids = array_column($scheduling_list, 'user_id');
- $where[] = ['user_id' => $user_ids];
- }
- $list = ReservationServiceUser::lists([
- 'where' => $where,
- 'select' => 'id,name,user_id',
- 'order' => 'id desc'
- ]);
- $rows = [];
- foreach ($list as $item) {
- $tech_user_ids[] = $item['user_id'];
- $rows[] = $item['user_id'] . ':' . $item['name'];
- }
- return $rows;
- }
- protected function getStaffsUser($tech_user_ids)
- {
- $list = ReservationServiceUser::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['user_id' => $tech_user_ids]
- ],
- 'select' => 'user_id,name',
- 'order' => 'user_id desc'
- ]);
- return $list;
- }
- protected function getAppointments($staffs, $date_time_start, $date_time_end)
- {
- $appointments_list = ReservationServiceOrder::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['!=', 'staff_user_id', 0],
- ['!=', 'server_status', ReservationServiceEnum::CANCELED],
- ['>=', 'service_start_at', $date_time_start],
- ['<=', 'service_start_at', $date_time_end],
- ],
- 'with' => ['user', 'staff' => function ($q) {
- $q->where(['status' => StatusEnum::ENABLED]);
- }, 'orderDetail'],
- 'order' => 'service_start_at asc',
- ]);
- $occupy_list = ReservationServiceOccupy::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['status' => StatusEnum::ENABLED],
- ['!=', 'staff_user_id', 0],
- ['>=', 'start_at', $date_time_start],
- ['<=', 'start_at', $date_time_end],
- ],
- 'with' => ['staff' => function ($q) {
- $q->where(['status' => StatusEnum::ENABLED]);
- }],
- 'order' => 'start_at asc',
- ]);
- $appointments = [];
- $rows = [];
- $goods_reserve_sku_id = array_column($appointments_list, 'goods_reserve_sku_id');
- $goods_sku_list = StoreGoodsReserveSku::lists(['where' => [['id' => $goods_reserve_sku_id]], 'with' => ['goods' => function ($q) {
- $q->select('id,goods_name');
- }], 'select' => 'id,name,goods_id', 'index' => 'id']);
- if (MallAddons::isInstall($this->mall_id, 'SecondaryCard')) {
- $secondary_card_id = array_column($appointments_list, 'secondary_card_id');
- $secondary_card_list = SecondaryCard::lists(['where' => [['id' => $secondary_card_id]], 'select' => 'id,name', 'index' => 'id']);
- }
- $service_type_map = ReserveServiceEnum::SERVICE_TYPE_MAP;
- foreach ($appointments_list as $item) {
- $reserve_time = date('H:i', $item['service_start_at']);
- $key = $item['staff']['user_id'] . ':' . $item['staff']['name'];
- $occupy_time = ($item['service_end_at'] - $item['service_start_at']) / 60;
- $project = '';
- if (isset($goods_sku_list[$item['goods_reserve_sku_id']])) {
- $project = $goods_sku_list[$item['goods_reserve_sku_id']]['name'];
- if (empty($project)) {
- $project = $goods_sku_list[$item['goods_reserve_sku_id']]['goods']['goods_name'];
- }
- }
- if (isset($secondary_card_list[$item['secondary_card_id']])) {
- $project = $secondary_card_list[$item['secondary_card_id']]['name'];
- }
- $rows[$reserve_time][$key][] = [
- 'name' => $item['user']['nickname'],
- 'phone' => $item['user']['mobile'],
- 'staff' => $item['staff']['name'],
- 'schedule' => ['startTime' => date('H:i', $item['service_start_at']), 'endTime' => date('H:i', $item['service_end_at'])],
- 'project' => $project,
- 'service_type_text' => $service_type_map[$item['service_type']] ?? '',
- 'duration' => $occupy_time . '分钟',
- 'id' => $item['id'],
- 'server_status' => $item['server_status'],
- 'create_type' => $item['create_type'],
- 'order_no' => $item['order_no'],
- ];
- }
- foreach ($occupy_list as $val) {
- $reserve_time = date('H:i', $val['start_at']);
- $key = $val['staff']['user_id'] . ':' . $val['staff']['name'];
- $occupy_time = ($val['end_at'] - $val['start_at']) / 60;
- $rows[$reserve_time][$key][] = [
- 'name' => '',
- 'phone' => '',
- 'staff' => $val['staff']['name'],
- 'schedule' => ['startTime' => date('H:i', $val['start_at']), 'endTime' => date('H:i', $val['end_at'])],
- 'project' => '',
- 'service_type_text' => '',
- 'duration' => $occupy_time . '分钟',
- 'id' => $val['id'],
- 'server_status' => ReservationServiceEnum::OCCUPY,
- 'create_type' => 3,
- ];
- }
- foreach ($rows as $k => $v) {
- $temp = $v;
- $temp['time'] = $k;
- $appointments[] = $temp;
- }
- return $appointments;
- }
- public function getTimeRange($params)
- {
- $date = $params['date'] ?? date('Y-m-d');
- $store = $params['store'];
- $business_time_start = strtotime($date . ' ' . $store['business_time_start'] . ':00');
- $business_time_end = strtotime($date . ' ' . $store['business_time_end'] . ':00');
- $time = time();
- if ($time > $business_time_start) {
- $start = $time;
- } else {
- $start = $business_time_start;
- }
- $rows = [];
- $minute = date('i', $start);
- $H = date('H', $start);
- $minute_temp = '00';
- switch (true) {
- case $minute >= 0 && $minute < 10:
- $minute_temp = '10';
- break;
- case $minute > 10 && $minute < 19:
- $minute_temp = '20';
- break;
- case $minute > 20 && $minute < 29:
- $minute_temp = '30';
- break;
- case $minute > 30 && $minute < 39:
- $minute_temp = '40';
- break;
- case $minute > 40 && $minute < 49:
- $minute_temp = '50';
- break;
- case $minute > 50 && $minute < 59:
- $H++;
- $minute_temp = '00';
- break;
- }
- $date = date('Y-m-d', $start);
- $start = strtotime($date . ' ' . $H . ':' . $minute_temp . ':00');
- for ($i = $start; $i <= $business_time_end; $i += 10 * 60) {
- $rows[] = ['time' => date('H:i', $i)];
- }
- return $rows;
- }
- /**
- * 设置占用
- * @param $params
- * @return array
- * @throws \Exception
- */
- public function setOccupyData($params)
- {
- $date = date('Y-m-d', strtotime($params['date']));
- $start_time = strtotime($date . ' ' . $params['form']['start_at'] . ':00');
- $staff_user_id = $params['form']['staff_user_id'];
- $occupy_hour = !empty($params['form']['occupy_hour']) ? $params['form']['occupy_hour'] : 0;
- $occupy_minute = !empty($params['form']['occupy_minute']) ? $params['form']['occupy_minute'] : 0;
- $end_time = $start_time + $occupy_hour * 3600 + $occupy_minute * 60;
- $res = ReservationServiceOrder::find()->where([
- 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'staff_user_id' => $staff_user_id
- ])->andWhere(['!=', 'server_status', ReservationServiceEnum::CANCELED])
- ->andWhere([
- 'or',
- [
- 'and',
- ['>=', 'service_start_at', $start_time],
- ['<=', 'service_start_at', $end_time],
- ],
- [
- 'and',
- ['>', 'service_end_at', $start_time],
- ['<=', 'service_end_at', $end_time],
- ]
- ])
- ->one();
- $reservation_service_user = ReservationServiceUser::findOne(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'user_id' => $staff_user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($reservation_service_user)) {
- throw new \Exception('技师不存在');
- }
- if (!empty($res)) {
- throw new \Exception($reservation_service_user['name'] . '技师在该时间有预约,请重新选择');
- }
- $model = ReservationServiceOccupy::find()->where([
- 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'staff_user_id' => $staff_user_id,
- 'start_at' => $start_time, 'status' => StatusEnum::ENABLED
- ])->andWhere([
- 'or',
- [
- 'and',
- ['>=', 'start_at', $start_time],
- ['<=', 'start_at', $end_time],
- ],
- [
- 'and',
- ['>=', 'end_at', $start_time],
- ['<=', 'end_at', $end_time],
- ]
- ])->one();
- if (!empty($model)) {
- throw new \Exception('该时间段已被占用');
- }
- $model = new ReservationServiceOccupy();
- $model->loadDefaultValues();
- $model->mall_id = $this->mall_id;
- $model->store_id = $this->store_id;
- $model->staff_user_id = $staff_user_id;
- $model->start_at = $start_time;
- $model->end_at = $end_time;
- $model->save();
- return ['id' => $model['id']];
- }
- public function canceledOccupy($params)
- {
- $model = ReservationServiceOccupy::findOne(['id' => $params['id'], 'store_id' => $this->store_id, 'mall_id' => $this->mall_id]);
- if (empty($model)) {
- throw new \Exception('占用记录不存在');
- }
- $model->status = StatusEnum::DISABLED;
- $res = $model->save();
- if ($res == false) {
- throw new \Exception($model->getErrorMessage());
- }
- return ['id' => $model['id']];
- }
- public function getSkuList()
- {
- $goods_list = StoreGoods::lists([
- 'where' => [
- ['store_id' => $this->store_id],
- ['status' => StatusEnum::ENABLED],
- ['check_status' => StatusEnum::ENABLED],
- ['is_on_sale' => StatusEnum::ENABLED],
- ['goods_type' => GoodsTypeEnum::GoodsTypeReserved],
- ],
- 'select' => 'id,goods_name',
- 'index' => 'id'
- ]);
- $goods_ids = array_column($goods_list, 'id');
- $sku_list = StoreGoodsReserveSku::lists([
- 'where' => [
- ['store_id' => $this->store_id],
- ['goods_id' => $goods_ids],
- ],
- 'select' => 'id,goods_id,service_min,name',
- ]);
- foreach ($sku_list as &$sku) {
- $sku['goods_name'] = $goods_list[$sku['goods_id']]['goods_name'] . ':' . $sku['name'];
- }
- return $sku_list;
- }
- public function setEditData($params)
- {
- $params['sku_id'] = $params['master'][0]['sku_id'];
- $params['nickname'] = $params['username'] ?? '';
- $params['reservation_model'] = 2;
- $ret = (new ReserveOrderService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->setEditData($params);
- return ['id' => $ret['id']];
- }
- }
|