SchedulingService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace addons\Store\common\services;
  3. use addons\ReservationService\common\models\ReservationServiceUser;
  4. use addons\Store\common\models\StoreReserveTechnicianScheduling;
  5. use addons\Store\common\models\StoreReserveTechnicianSchedulingTime;
  6. use common\enums\StatusEnum;
  7. use common\helpers\DateHelper;
  8. use Exception;
  9. use Yii;
  10. class SchedulingService extends BaseService
  11. {
  12. //获取技师数据
  13. public function getTechnicianData($get)
  14. {
  15. //获取所有技师数据
  16. $user_list_info = ReservationServiceUser::find()->where([
  17. 'mall_id' => $this->mall_id,
  18. 'store_id' => $this->store_id,
  19. 'status' => StatusEnum::ENABLED])
  20. ->select('user_id,name')->asArray()->all();
  21. if (!empty($get['user_id'])) {
  22. $user_id = $get['user_id'];
  23. //获取所选技师数据
  24. $user_info = ReservationServiceUser::getDataOne([
  25. 'mall_id' => $this->mall_id,
  26. 'store_id' => $this->store_id,
  27. 'status' => StatusEnum::ENABLED,
  28. 'user_id' => $get['user_id']
  29. ], 'user_id,name');
  30. } else {
  31. $user_info = $user_list_info[0] ?? [];
  32. $user_id = $user_info['user_id'];
  33. }
  34. $date_time = date('Y-m-d', time());
  35. $data = self::technicianSchedulingDetail(['user_id' => $user_id, 'date_time' => $date_time]);
  36. $user_info['detail'] = $data;
  37. return ['user_list' => $user_list_info, 'user_detail' => $user_info];
  38. }
  39. //获取选取日期排期详情
  40. public function technicianSchedulingDetail($params)
  41. {
  42. if (empty($params)) return [];
  43. $info = StoreReserveTechnicianScheduling::getDataOne(['mall_id' => $this->mall_id, 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'date_time' => strtotime($params['date_time'])]);
  44. if (empty($info)) {
  45. return [];
  46. }
  47. $info['date_time'] = date('Y-m-d', $info['date_time']);
  48. $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');
  49. if (!empty($time_data)) {
  50. // 排序时间数据,按照 start_time 从小到大排序
  51. usort($time_data, function ($a, $b) {
  52. return $a['start_time'] <=> $b['start_time'];
  53. });
  54. foreach ($time_data as $key => $value) {
  55. $time_data[$key]['start_time'] = date('H:i:s', $value['start_time']);
  56. $time_data[$key]['end_time'] = date('H:i:s', $value['end_time']);
  57. $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'])];
  58. }
  59. $info['schedulingTime'] = $time_data;
  60. }
  61. return $info;
  62. }
  63. //获取选取月份排期
  64. public function technicianScheduling($params)
  65. {
  66. if (empty($params['month'])) $params['month'] = date('m', time());
  67. $year = $params['year'];
  68. $start_time = strtotime($year . '-' . $params['month']);
  69. $end_time = DateHelper::getMonthLastDayTime($start_time);
  70. $where = [
  71. ['mall_id' => $this->mall_id],
  72. ['user_id' => $params['user_id']],
  73. ['>=', 'date_time', $start_time],
  74. ['<', 'date_time', $end_time],
  75. ['status' => StatusEnum::ENABLED]
  76. ];
  77. $data = ['where' => $where, 'with' => ['schedulingTime'], 'limit' => 31, 'order' => 'date_time asc'];
  78. $list = StoreReserveTechnicianScheduling::listPage($data);
  79. if (!empty($list['list'])) {
  80. foreach ($list['list'] as $key => $value) {
  81. $list['list'][$key]['date_time'] = date('Y-m-d', $value['date_time']);
  82. if (!empty($value['schedulingTime'])) {
  83. foreach ($value['schedulingTime'] as $k => $val) {
  84. $list['list'][$key]['schedulingTime'][$k]['start_time'] = date('H:i:s', $val['start_time']);
  85. $list['list'][$key]['schedulingTime'][$k]['end_time'] = date('H:i:s', $val['end_time']);
  86. }
  87. }
  88. }
  89. }
  90. return $list['list'];
  91. }
  92. //设置排期
  93. public function setTechnicianScheduling($params)
  94. {
  95. $trans = Yii::$app->db->beginTransaction();
  96. try {
  97. $params['mall_id'] = $this->mall_id;
  98. $params['date_time'] = strtotime($params['date_time']);
  99. $params['store_id'] = $this->store_id;
  100. if (empty($params['id'])) {
  101. //检查是否已经设置了当天的排班数据
  102. $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]);
  103. if (!empty($check)) $params['id'] = $check['id'];
  104. }
  105. $res = StoreReserveTechnicianScheduling::setData($params);
  106. if (!$res) throw new Exception(StoreReserveTechnicianScheduling::getStaticError());
  107. //如果不是全天并且详细时间数据不为空
  108. if ($params['is_all_day'] != 1) {
  109. if (empty($params['time_data'])) throw new Exception('请选择时间');
  110. //是否是批量设置整个月的排班
  111. if ($params['is_month'] == 1 && $params['is_have'] == 1) {
  112. //如果已经存在排班时间的日期,则跳过设置该日期
  113. StoreReserveTechnicianSchedulingTime::updateAll(['status' => StatusEnum::DELETE],['sid' => $res->id, 'mall_id' => $this->mall_id, 'user_id' => $params['user_id'],]);
  114. }
  115. //如果详细时间数据不是数组,解json格式
  116. if (!is_array($params['time_data'])) $params['time_data'] = json_decode($params['time_data'], true);
  117. foreach ($params['time_data'] as $key => $value) {
  118. //格式化时间
  119. if (empty($value['time_data'])) {
  120. $start = date("Y-m-d", $params['date_time']) . $value['start_time'];
  121. $end = date("Y-m-d", $params['date_time']) . $value['end_time'];
  122. } else { // 批量设置月排班时间时,需重新格式化
  123. $start = date("Y-m-d", $params['date_time']) . ' ' . date('H:i:s', strtotime($value['time_data'][0]));
  124. $end = date("Y-m-d", $params['date_time']) . ' ' . date('H:i:s', strtotime($value['time_data'][1]));
  125. }
  126. $data = [
  127. 'sid' => $res->id,
  128. 'mall_id' => $this->mall_id,
  129. 'store_id' => $this->store_id,
  130. 'user_id' => $params['user_id'],
  131. 'status' => StatusEnum::ENABLED,
  132. 'start_time' => strtotime($start),
  133. 'end_time' => strtotime($end),
  134. ];
  135. if (!empty($value['id'])) {
  136. $data['id'] = $value['id'];
  137. }
  138. $time_res = StoreReserveTechnicianSchedulingTime::setData($data);
  139. if (!$time_res) throw new \Exception('添加时间失败');
  140. }
  141. }
  142. $trans->commit();
  143. return ['id' => $res->id];
  144. } catch (Exception $e) {
  145. $trans->rollBack();
  146. return $e->getMessage();
  147. }
  148. }
  149. //修改排班时间段状态
  150. public function changeTechnicianSchedulingTimeStatus($params)
  151. {
  152. try {
  153. $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]);
  154. if (empty($check_data)) throw new Exception('查无该排班时间');
  155. $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]);
  156. if (!$res) throw new Exception('操作失败');
  157. return [];
  158. } catch (Exception $e) {
  159. return $e->getMessage();
  160. }
  161. }
  162. //批量设置当月排期
  163. public function setTechnicianSchedulingMonth($params)
  164. {
  165. try {
  166. $year = $params['year'];
  167. $start_time = strtotime($year . '-' . $params['month']);
  168. $end_time = DateHelper::getMonthLastDayTime($start_time);
  169. $i = 0;
  170. $data = [
  171. 'where' => [
  172. ['mall_id' => $this->mall_id],
  173. ['store_id' => $this->store_id],
  174. ['user_id' => $params['user_id']],
  175. ['status' => StatusEnum::ENABLED],
  176. ['between', 'date_time', $start_time, $end_time],
  177. ],
  178. 'index' => 'date_time',
  179. 'with' => ['schedulingTime'],
  180. ];
  181. $list = StoreReserveTechnicianScheduling::lists($data);
  182. //循环所选月的日期
  183. while ($start_time + ($i * 24 * 60 * 60) < $end_time) {
  184. $data = [
  185. 'date_time' => date('Y-m-d', $start_time + ($i * 24 * 60 * 60)),
  186. 'is_all_day' => $params['is_all_day'],
  187. 'user_id' => $params['user_id'],
  188. 'is_month' => 1,// 表示该日期是由批量设置当月日期进行操作
  189. ];
  190. $scheduling_data = $list[strtotime($data['date_time'])];
  191. if (!empty($params['time_data'])) $data['time_data'] = $params['time_data'];
  192. if (!empty($scheduling_data)) {
  193. $data['id'] = $scheduling_data['id'];
  194. if (!empty($data['time_data']) && !empty($scheduling_data['schedulingTime'])) {
  195. $data['is_have'] = 1;
  196. foreach ($data['time_data'] as $key => $value) {
  197. foreach ($scheduling_data['schedulingTime'] as $k => $val) {
  198. if (strtotime($data['date_time'] . ' ' . date('H:i:s', strtotime($value['time_data'][0]))) == $val['start_time']) {
  199. continue;
  200. }
  201. }
  202. }
  203. };
  204. }
  205. $res = self::setTechnicianScheduling($data);
  206. if (is_string($res)) throw new Exception($data['date_time'] . $res);
  207. $i++;
  208. }
  209. return [];
  210. } catch (Exception $e) {
  211. return $e->getMessage();
  212. }
  213. }
  214. public function deleteTechnician($params)
  215. {
  216. $trans = Yii::$app->db->beginTransaction();
  217. try {
  218. $data = [
  219. ['mall_id' => $this->mall_id],
  220. ['store_id' => $this->store_id],
  221. ['user_id' => $params['user_id']],
  222. ['id' => $params['id']],
  223. ['status' => StatusEnum::ENABLED]
  224. ];
  225. $check_data = StoreReserveTechnicianScheduling::getOne($data, true, ['schedulingTime']);
  226. if (empty($check_data)) throw new Exception('不存在该排期数据');
  227. $res = StoreReserveTechnicianScheduling::updateAll(['status' => StatusEnum::DELETE], ['id' => $check_data['id']]);
  228. if (!$res) throw new Exception('删除排期数据失败');
  229. //如果存在明细数据
  230. if (!empty($check_data['schedulingTime'])) {
  231. $res = StoreReserveTechnicianSchedulingTime::updateAll(['status' => StatusEnum::DELETE], ['sid' => $check_data['id']]);
  232. if (!$res) throw new Exception('删除失败');
  233. }
  234. $trans->commit();
  235. return true;
  236. } catch (\Exception $e) {
  237. $trans->rollBack();
  238. return $e->getMessage();
  239. }
  240. }
  241. public function deleteScheduling($params)
  242. {
  243. $trans = Yii::$app->db->beginTransaction();
  244. try {
  245. $data = [
  246. ['mall_id' => $this->mall_id],
  247. ['user_id' => $params['user_id']],
  248. ['id' => $params['id']],
  249. ['status' => StatusEnum::ENABLED]
  250. ];
  251. $check_data = StoreReserveTechnicianScheduling::getOne($data, true, ['schedulingTime']);
  252. if (empty($check_data)) throw new Exception('不存在该排期数据');
  253. $res = StoreReserveTechnicianScheduling::updateAll(['status' => StatusEnum::DELETE], ['id' => $check_data['id']]);
  254. if (!$res) throw new Exception('删除排期数据失败');
  255. //如果存在明细数据
  256. if (!empty($check_data['schedulingTime'])) {
  257. $res = StoreReserveTechnicianSchedulingTime::updateAll(['status' => StatusEnum::DELETE], ['sid' => $check_data['id']]);
  258. if (!$res) throw new Exception('删除失败');
  259. }
  260. $trans->commit();
  261. return true;
  262. } catch (\Exception $e) {
  263. $trans->rollBack();
  264. return $e->getMessage();
  265. }
  266. }
  267. }