ReserveStaffController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use addons\ReservationService\common\enums\ReservationServiceEnum;
  4. use addons\ReservationService\common\models\ReservationServiceUser;
  5. use addons\ReservationService\common\services\BackendService;
  6. use addons\Store\common\services\SchedulingService;
  7. use addons\Store\common\services\StaffService;
  8. use common\enums\ApiStatusEnum;
  9. use common\enums\StatusEnum;
  10. use common\models\user\User;
  11. use yii\helpers\Json;
  12. use Yii;
  13. use Exception;
  14. class ReserveStaffController extends BaseController
  15. {
  16. /**
  17. * 技师列表
  18. * @return mixed|null
  19. */
  20. public function actionIndex()
  21. {
  22. if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
  23. $data = \Yii::$app->request->get();
  24. $rules = [
  25. [['name','address', 'mobile', 'date_start', 'date_end', 'keyword'], 'string'],
  26. [['user_id', 'page', 'limit'], 'integer'],
  27. ];
  28. $res = \Yii::$app->services->validate->validate($data, $rules);
  29. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  30. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->page($data);
  31. if (!empty($ret['list'])) {
  32. foreach ($ret['list'] as &$item) {
  33. $item['idcard_images'] = [
  34. $item['idcard_front'],
  35. $item['idcard_back'],
  36. ];
  37. $item['qualification_certificate'] = Json::decode($item['qualification_certificate']);
  38. }
  39. }
  40. $ret['custom_text'] = BackendService::getCustomText($this->mall_id);
  41. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  42. }
  43. return $this->render('index');
  44. }
  45. /**
  46. * 技师详情
  47. * @return mixed|null
  48. */
  49. public function actionDetail()
  50. {
  51. if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
  52. $data = \Yii::$app->request->get();
  53. $rules = [
  54. [['id'], 'integer'],
  55. ];
  56. $res = \Yii::$app->services->validate->validate($data, $rules);
  57. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  58. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->detail($data['id']);
  59. $ret['custom_text'] = BackendService::getCustomText($this->mall_id);
  60. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret, ApiStatusEnum::CODE_SUCCESS, JSON_BIGINT_AS_STRING);
  61. }
  62. return $this->render('detail');
  63. }
  64. /**
  65. * 移除技师
  66. * @return mixed|void|null
  67. */
  68. public function actionDelete()
  69. {
  70. if (\Yii::$app->request->isAjax && \Yii::$app->request->isPost) {
  71. $data = \Yii::$app->request->post();
  72. $rules = [
  73. [['id'], 'integer'],
  74. ];
  75. $res = \Yii::$app->services->validate->validate($data, $rules);
  76. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  77. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->delete($data['id']);
  78. return is_string($ret) ? $this->error($ret) : $this->success('操作成功');
  79. }
  80. }
  81. /**
  82. * 技师数据统计
  83. * @return mixed|null
  84. */
  85. public function actionStatistics()
  86. {
  87. $data = \Yii::$app->request->get();
  88. $rules = [
  89. [['id'], 'integer'],
  90. ];
  91. $res = \Yii::$app->services->validate->validate($data, $rules);
  92. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  93. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->statisticsByStaff($data['id']);
  94. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  95. }
  96. /**
  97. * 设置上下班状态
  98. * @return mixed|null
  99. */
  100. public function actionSetStatus()
  101. {
  102. $data = \Yii::$app->request->post();
  103. $rules = [
  104. [['id'], 'integer'],
  105. ];
  106. $res = \Yii::$app->services->validate->validate($data, $rules);
  107. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  108. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->setStatus($data['id']);
  109. return is_string($ret) ? $this->error($ret) : $this->success('操作成功', $ret);
  110. }
  111. public function actionEdit()
  112. {
  113. $request = \Yii::$app->request;
  114. $id = $request->get('id', 0);
  115. try {
  116. if ($request->isAjax) {
  117. // 查看等级详情
  118. if ($request->isGet) {
  119. $data = [];
  120. if (!empty($id)) {
  121. // 获取等级信息
  122. $model = ReservationServiceUser::getOne([['id' => $id, 'mall_id' => $this->mall_id]], true, []);
  123. $data['data'] = $model;
  124. }
  125. return $this->success('操作成功', $data);
  126. } elseif ($request->isPost) {
  127. $params = \Yii::$app->request->post()['form'];
  128. $res = \Yii::$app->services->validate->validate($params, [
  129. [['idcard', 'level', 'user_id'], 'required'],
  130. [['level', 'user_id', 'id', 'mobile'], 'integer'],
  131. [['idcard_back', 'idcard_front', 'address', 'longitude', 'latitude', 'name'], 'string'],
  132. ]);
  133. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  134. $params['mall_id'] = $this->mall_id;
  135. $params['check_status'] = ReservationServiceEnum::CHECK_ADOPT;
  136. $params['store_id'] = $this->store_id;
  137. $res = (new StaffService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->add($params);
  138. // if ($res === false) throw new \Exception(ReservationServiceUser::getStaticCodeError()['error']);
  139. return $this->success('操作成功');
  140. }
  141. }
  142. } catch (\Exception $e) {
  143. return $this->error($e->getMessage(), []);
  144. }
  145. return $this->render($this->action->id);
  146. }
  147. public function actionGetLevels()
  148. {
  149. return $this->success('获取成功', BackendService::getLevels($this->mall_id));
  150. }
  151. public function actionSearchUser()
  152. {
  153. $keyword = \Yii::$app->request->get('keyword', '');
  154. $list['list'] = User::searchUser(['keyword' => $keyword, 'mall_id' => $this->mall_id, 'no_inviter' => 1]);
  155. return $this->success('操作成功', $list);
  156. }
  157. public function actionGetAssociationStoreGoods()
  158. {
  159. try {
  160. $data = \Yii::$app->request->get();
  161. $rules = [
  162. [['user_id'], 'integer'],
  163. ];
  164. $res = \Yii::$app->services->validate->validate($data, $rules);
  165. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  166. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->getAssociationStoreGoods($data);
  167. return $this->success('获取成功', $ret);
  168. }catch (\Exception $e){
  169. return $this->error($e->getMessage());
  170. }
  171. }
  172. public function actionGetStoreGoods()
  173. {
  174. try {
  175. $data = \Yii::$app->request->get();
  176. $rules = [
  177. [['keyword'], 'safe'],
  178. ];
  179. $res = \Yii::$app->services->validate->validate($data, $rules);
  180. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  181. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->getStoreGoods($data);
  182. return $this->success('获取成功', $ret);
  183. }catch (\Exception $e){
  184. return $this->error($e->getMessage());
  185. }
  186. }
  187. public function actionHandleAssociationStoreGoods()
  188. {
  189. try {
  190. $data = \Yii::$app->request->post();
  191. $rules = [
  192. [['user_id'], 'required'],
  193. [['goods_ids'], 'safe'],
  194. ];
  195. $res = \Yii::$app->services->validate->validate($data, $rules);
  196. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  197. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->handleAssociationStoreGoods($data);
  198. return $this->success('获取成功', $ret);
  199. }catch (\Exception $e){
  200. return $this->error($e->getMessage());
  201. }
  202. }
  203. public function actionGetAssociationStoreGoodsPage()
  204. {
  205. try {
  206. $data = \Yii::$app->request->get();
  207. $rules = [
  208. [['user_id'], 'integer'],
  209. [['page','limit'], 'integer'],
  210. ];
  211. $res = \Yii::$app->services->validate->validate($data, $rules);
  212. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  213. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->getAssociationStoreGoodsPage($data);
  214. return $this->success('获取成功', $ret);
  215. }catch (\Exception $e){
  216. return $this->error($e->getMessage());
  217. }
  218. }
  219. public function actionAdd()
  220. {
  221. try {
  222. if (\Yii::$app->request->isAjax) {
  223. if(\Yii::$app->request->isGet){
  224. $ret['custom_text'] = BackendService::getCustomText($this->mall_id);
  225. return $this->success('获取成功', $ret);
  226. }elseif (\Yii::$app->request->isPost) {
  227. $params = \Yii::$app->request->post()['form'];
  228. $res = \Yii::$app->services->validate->validate($params, [
  229. [['idcard', 'level', 'user_id','gender','age'], 'required'],
  230. [['level', 'user_id', 'id', 'mobile'], 'integer'],
  231. [['idcard_back', 'idcard_front', 'address', 'longitude', 'latitude', 'name'], 'string'],
  232. [['qualification_certificate','avatar'], 'safe'],
  233. ]);
  234. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  235. $params['mall_id'] = $this->mall_id;
  236. $params['check_status'] = ReservationServiceEnum::CHECK_ADOPT;
  237. $params['store_id'] = $this->store_id;
  238. if(empty($params['qualification_certificate'])){
  239. $params['qualification_certificate'] = [];
  240. }
  241. $params['qualification_certificate'] = json_encode($params['qualification_certificate'], JSON_UNESCAPED_UNICODE);
  242. $res = (new StaffService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->addStallUser($params);
  243. return $this->success('操作成功');
  244. }
  245. }
  246. return $this->render('add');
  247. }catch (\Exception $e){
  248. return $this->error($e->getMessage());
  249. }
  250. }
  251. public function actionCheckStaffUser()
  252. {
  253. try {
  254. if (\Yii::$app->request->isAjax) {
  255. if(\Yii::$app->request->isGet){
  256. $params = \Yii::$app->request->get();
  257. $user = ReservationServiceUser::getOne(
  258. [['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]], true, 'user'
  259. );
  260. if (empty($user)) {
  261. return $this->error('平台不存在该技师');
  262. }
  263. if (!empty($user['store_id'])) {
  264. return $this->error('该技师已经绑定其他门店,无法添加');
  265. }
  266. $user['nickname'] = $user['user']['nickname'];
  267. if(!empty($user['qualification_certificate'])){
  268. $qualification_certificate = json_decode($user['qualification_certificate'],true);
  269. $user['qualification_certificate'] = $qualification_certificate;
  270. }else{
  271. $user['qualification_certificate'] = [];
  272. }
  273. return $this->success('获取成功',['user'=>$user]);
  274. }
  275. }
  276. }catch (\Exception $e){
  277. return $this->error($e->getMessage());
  278. }
  279. }
  280. //排期管理
  281. public function actionScheduling()
  282. {
  283. if (Yii::$app->request->isAjax) {
  284. if (Yii::$app->request->isGet) {
  285. $get = Yii::$app->request->get();
  286. $res = Yii::$app->services->validate->validate($get, [
  287. [['user_id', 'month'], 'integer'],
  288. [['date'], 'safe'],
  289. ]);
  290. if (!$res) return $this->error('获取失败');
  291. $config = [
  292. 'mall_id' => $this->mall_id,
  293. 'store_id' => $this->store_id,
  294. ];
  295. $year = date('Y');
  296. if (!empty($get['date'])) {
  297. $year = date('Y', strtotime($get['date']));
  298. }
  299. $get['year'] = $year;
  300. $data = (new SchedulingService($config))->getTechnicianData($get);
  301. $get['user_id'] = $data['user_detail']['user_id'];
  302. $time_data = (new SchedulingService($config))->technicianScheduling($get);
  303. $data['time_data'] = $time_data;
  304. $store = $this->store;
  305. $business_time_start = $store['business_time_start'];
  306. $business_time_end = $store['business_time_end'];
  307. $date = date('Y-m-d');
  308. $default_start_time = $date.' '.$business_time_start.':00';
  309. $default_end_time = $date . ' ' . $business_time_end . ':00';
  310. $data['default_start_time'] = $default_start_time;
  311. $data['default_end_time'] = $default_end_time;
  312. return $this->success('获取成功',$data);
  313. }
  314. if (Yii::$app->request->isPost) {
  315. $post = Yii::$app->request->post();
  316. try {
  317. $res = Yii::$app->services->validate->validate($post, [
  318. [['user_id', 'is_all_day', 'id', 'type'], 'integer'],
  319. [['date_time'], 'string'],
  320. [['schedulingTime'], 'safe']
  321. ]);
  322. if (!$res) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  323. $config = [
  324. 'mall_id' => $this->mall_id,
  325. 'store_id' => $this->store_id,
  326. ];
  327. $post['time_data'] = $post['schedulingTime'];
  328. $ret = (new SchedulingService($config))->setTechnicianScheduling($post);
  329. if (is_string($ret)) throw new Exception($ret);
  330. return $this->success('操作成功');
  331. } catch (\Exception $e) {
  332. return $this->error($e->getMessage());
  333. }
  334. }
  335. }
  336. return $this->render('scheduling',[]);
  337. }
  338. //获取所选日期排期详情
  339. public function actionTechnicianSchedulingDetail()
  340. {
  341. $request = Yii::$app->request;
  342. if ($request->isAjax && $request->isGet) {
  343. try{
  344. $params = $request->get();
  345. $res = Yii::$app->services->validate->validate($params, [
  346. [['user_id'], 'integer'],
  347. [['date_time'], 'string']
  348. ]);
  349. if (!$res) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  350. $res = (new SchedulingService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->technicianSchedulingDetail($params);
  351. return $this->success('获取成功', $res);
  352. } catch (\Exception $e) {
  353. return $this->error($e->getMessage());
  354. }
  355. }
  356. }
  357. //删除排班时间
  358. public function actionDelSchedulingTime()
  359. {
  360. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  361. try {
  362. $post = Yii::$app->request->post();
  363. $res = Yii::$app->services->validate->validate($post, [
  364. [['id', 'user_id', 'status'], 'integer'],
  365. ]);
  366. if (!$res) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  367. $config = [
  368. 'mall_id' => $this->mall_id,
  369. 'store_id' => $this->store_id,
  370. ];
  371. (new SchedulingService($config))->changeTechnicianSchedulingTimeStatus($post);
  372. return $this->success('删除成功');
  373. } catch (\Exception $e) {
  374. return $this->error($e->getMessage());
  375. }
  376. }
  377. }
  378. //批量设置当月排班
  379. public function actionSetAllMonth()
  380. {
  381. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  382. $post = Yii::$app->request->post();
  383. try {
  384. $res = Yii::$app->services->validate->validate($post, [
  385. [['user_id', 'is_all_day', 'type'], 'integer'],
  386. [['month'], 'string'],
  387. [['schedulingTime', 'date'], 'safe']
  388. ]);
  389. if (!$res) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  390. $year = date('Y');
  391. if (!empty($post['date'])) {
  392. $year = date('Y', strtotime($post['date']));
  393. }
  394. $post['year'] = $year;
  395. //如果详细时间数据不是数组,解json格式
  396. $config = [
  397. 'mall_id' => $this->mall_id,
  398. 'store_id' => $this->store_id,
  399. ];
  400. $post['time_data'] = $post['schedulingTime'];
  401. $ret = (new SchedulingService($config))->setTechnicianSchedulingMonth($post);
  402. return $this->success('操作成功');
  403. } catch (\Exception $e) {
  404. return $this->error($e->getMessage());
  405. }
  406. }
  407. }
  408. //删除当天排期
  409. public function actionDeleteScheduling()
  410. {
  411. $request = Yii::$app->request;
  412. if ($request->isAjax && $request->isPost) {
  413. try {
  414. $post = $request->post();
  415. $res = Yii::$app->services->validate->validate($post, [
  416. [['id'], 'required'],
  417. [['user_id'], 'integer'],
  418. ]);
  419. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  420. $config = [
  421. 'mall_id' => $this->mall_id,
  422. 'store_id' => $post['store_id'],
  423. ];
  424. $ret = (new SchedulingService($config))->deleteScheduling($post);
  425. return $this->success('操作成功');
  426. } catch (\Exception $e) {
  427. return $this->error($e->getMessage());
  428. }
  429. }
  430. }
  431. /**
  432. * 获取关联的次卡
  433. * @return mixed|null
  434. */
  435. public function actionGetAssociationStoreSecondaryCard()
  436. {
  437. try {
  438. $data = \Yii::$app->request->get();
  439. $rules = [
  440. [['user_id'], 'integer'],
  441. ];
  442. $res = \Yii::$app->services->validate->validate($data, $rules);
  443. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  444. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->getAssociationStoreSecondaryCard($data);
  445. return $this->success('获取成功', $ret);
  446. } catch (\Exception $e) {
  447. return $this->error($e->getMessage());
  448. }
  449. }
  450. /**
  451. * 获取门店次卡列表
  452. * @return mixed|null
  453. */
  454. public function actionGetStoreSecondaryCard()
  455. {
  456. try {
  457. $data = \Yii::$app->request->get();
  458. $rules = [
  459. [['keyword'], 'safe'],
  460. [['user_id'], 'safe'],
  461. ];
  462. $res = \Yii::$app->services->validate->validate($data, $rules);
  463. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  464. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->getStoreSecondaryCard($data);
  465. return $this->success('获取成功', $ret);
  466. } catch (\Exception $e) {
  467. return $this->error($e->getMessage());
  468. }
  469. }
  470. /**
  471. * 操作关联次卡
  472. * @return mixed|null
  473. */
  474. public function actionHandleAssociationStoreSecondaryCard()
  475. {
  476. try {
  477. $data = \Yii::$app->request->post();
  478. $rules = [
  479. [['user_id'], 'required'],
  480. [['secondary_card_ids'], 'safe'],
  481. ];
  482. $res = \Yii::$app->services->validate->validate($data, $rules);
  483. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  484. $ret = (new StaffService(['store_id' => $this->store_id, 'mall_id' => $this->mall_id]))->handleAssociationStoreSecondaryCard($data);
  485. return $this->success('获取成功', $ret);
  486. } catch (\Exception $e) {
  487. return $this->error($e->getMessage());
  488. }
  489. }
  490. }