StaffService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php
  2. namespace addons\Store\common\services;
  3. use addons\ReservationService\common\models\ReservationServiceStoreGoods;
  4. use addons\ReservationService\common\models\ReservationServiceStoreSecondaryCard;
  5. use addons\ReservationService\common\models\ReservationServiceUser;
  6. use addons\ReservationService\common\services\OrderService;
  7. use addons\ReservationService\common\services\UserService;
  8. use addons\SecondaryCard\common\enums\SecondaryCardEnum;
  9. use addons\SecondaryCard\common\models\SecondaryCard;
  10. use addons\SecondaryCard\common\models\SecondaryCardRelateStore;
  11. use addons\Store\common\models\Store;
  12. use addons\Store\common\models\StoreGoods;
  13. use addons\Store\common\models\StoreStaffApply;
  14. use common\enums\GoodsTypeEnum;
  15. use common\enums\StatusEnum;
  16. use common\models\mall\MallAddons;
  17. use common\models\user\User;
  18. use yii\helpers\Json;
  19. use function Clue\StreamFilter\fun;
  20. /**
  21. * 技师管理
  22. */
  23. class StaffService extends BaseService
  24. {
  25. public $user_id = 0;
  26. public function init()
  27. {
  28. parent::init(); // TODO: Change the autogenerated stub
  29. // 判断是否有安装技师插件
  30. }
  31. /**
  32. *
  33. * @param array $params
  34. * @return array|false|mixed|null
  35. */
  36. public function page(array $params)
  37. {
  38. // 技师列表
  39. if (!empty($this->store_id)) $params['store_id'] = $this->store_id;
  40. $params['is_store'] = StatusEnum::ENABLED;
  41. // $params['store_status'] = StatusEnum::ENABLED;
  42. return (new UserService())->page($this->mall_id, $params);
  43. }
  44. /**
  45. * 待审核列表
  46. * @param array $params
  47. * @return array|false|mixed|null
  48. */
  49. public function applyPage(array $params)
  50. {
  51. $where = [
  52. ['mall_id' => $this->mall_id],
  53. ['store_id' => $this->store_id],
  54. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  55. ];
  56. if (!empty($params['user_id'])) {
  57. $where[] = ['user_id' => $params['user_id']];
  58. } elseif (!empty($params['keyword'])) {
  59. $uids = User::find()->where(['mall_id' => $this->mall_id])
  60. ->andWhere(['status' => StatusEnum::ENABLED])
  61. ->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]])
  62. ->select('id')->column();
  63. if (!empty($uids)) {
  64. $where[] = ['user_id' => $uids];
  65. } else {
  66. return [];
  67. }
  68. }
  69. if (!empty($params['name'])) {
  70. $where[] = ['like', 'name', $params['name']];
  71. }
  72. if (!empty($params['mobile'])) {
  73. $where[] = ['like', 'phone', $params['mobile']];
  74. }
  75. if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
  76. if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
  77. $l_user = StoreStaffApply::lists([
  78. 'where' => $where,
  79. 'order' => 'id desc',
  80. 'index' => 'user_id'
  81. ]);
  82. if (empty($l_user)) return [];
  83. $user_ids = array_column($l_user, 'user_id');
  84. $ret = (new UserService())->page($this->mall_id, [
  85. 'user_ids' => $user_ids,
  86. 'limit' => $params['limit'] ?? 10,
  87. 'address' => $params['address'] ?? '',
  88. 'page' => 1,
  89. ]);
  90. if (!empty($ret['list'])) {
  91. foreach ($ret['list'] as &$item) {
  92. $item['settle'] = $l_user[$item['user_id']] ?? null;
  93. }
  94. }
  95. return $ret;
  96. }
  97. /**
  98. * 审核
  99. * @param array $params
  100. * @return string|true
  101. */
  102. public function audit(array $params)
  103. {
  104. // 审核
  105. /**
  106. * @var $staff StoreStaffApply
  107. */
  108. $staff = StoreStaffApply::getOne([
  109. ['mall_id' => $this->mall_id],
  110. ['id' => $params['id']],
  111. ['store_id' => $this->store_id],
  112. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  113. ]);
  114. if (empty($staff)) return '记录不存在';
  115. if ($staff->check_status != 0) return '当前记录不可审核';
  116. $t = \Yii::$app->db->beginTransaction();
  117. try {
  118. $staff->check_status = $params['status'];
  119. $staff->check_remark = !empty($params['remark']) ? $params['remark'] : '';
  120. if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
  121. /**
  122. * @var $store Store
  123. */
  124. $store = Store::getOne([[
  125. 'id' => $staff->store_id
  126. ]]);
  127. if (empty($store)) throw new \Exception('门店不存在');
  128. // 那边需要绑定store_id
  129. if ($staff->check_status == StatusEnum::ENABLED) {
  130. (new UserService())->bindStore($this->mall_id, $staff->user_id, $store);
  131. }
  132. $t->commit();
  133. } catch (\Exception $e) {
  134. $t->rollBack();
  135. return $e->getMessage();
  136. }
  137. return true;
  138. }
  139. /**
  140. * 设置接单状态
  141. * @param int $id
  142. * @return string|true
  143. */
  144. public function setStatus(int $id)
  145. {
  146. try {
  147. return (new UserService())->setStoreStatus($this->mall_id, $id, $this->store_id);
  148. } catch (\Exception $e) {
  149. return $e->getMessage();
  150. }
  151. }
  152. /**
  153. * 技师详情
  154. * @param int $id
  155. * @return mixed|string
  156. */
  157. public function detail(int $id)
  158. {
  159. // 技师详情
  160. try {
  161. $ret = (new UserService())->detail($this->mall_id, $id, $this->store_id);
  162. if (!empty($ret)) {
  163. $store = Store::getOne([['id' => $this->store_id]], true, [], false, 'id, store_name, shop_owner, shop_mobile, logo_img');
  164. $ret['settle'] = ['store' => $store, 'updated_at' => $ret['created_at']];
  165. $ret['qualification_certificate'] = Json::decode($ret['qualification_certificate']);
  166. $ret['idcard_images'] = [
  167. $ret['idcard_front'],
  168. $ret['idcard_back'],
  169. ];
  170. }
  171. return $ret;
  172. } catch (\Exception $e) {
  173. return $e->getMessage();
  174. }
  175. }
  176. /**
  177. * 移除数据
  178. * @param int $id
  179. * @return string|true
  180. */
  181. public function delete(int $id)
  182. {
  183. /**
  184. * @var $staff StoreStaffApply
  185. */
  186. $staff = StoreStaffApply::getOne([
  187. ['mall_id' => $this->mall_id],
  188. ['user_id' => $id],
  189. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  190. ]);
  191. $t = \Yii::$app->db->beginTransaction();
  192. try {
  193. if (!empty($staff)) {
  194. $staff->status = StatusEnum::DELETE;
  195. if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
  196. }
  197. (new UserService())->removeStore($this->mall_id, $id, $this->store_id);
  198. ReservationServiceStoreGoods::updateAll(['status' => StatusEnum::DELETE], ['user_id' => $id, 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
  199. $t->commit();
  200. } catch (\Exception $e) {
  201. $t->rollBack();
  202. return $e->getMessage();
  203. }
  204. return true;
  205. }
  206. /**
  207. * 接单排行榜
  208. * @return array
  209. */
  210. public function orderRank()
  211. {
  212. // 单量排行榜
  213. $params['store_id'] = $this->store_id;
  214. $params['sort'] = 'store_order_num desc';
  215. $params['limit'] = 10;
  216. $params['page'] = 1;
  217. return (new UserService())->page($this->mall_id, $params);
  218. }
  219. /**
  220. * 服务评分排行榜
  221. * @return array|false|mixed|null
  222. */
  223. public function ratingRand()
  224. {
  225. $params['store_id'] = $this->store_id;
  226. $params['sort'] = 'store_service_rating desc';
  227. $params['limit'] = 10;
  228. $params['page'] = 1;
  229. return (new UserService())->page($this->mall_id, $params);
  230. }
  231. /**
  232. * @return int[]
  233. */
  234. public function statistics()
  235. {
  236. // 累计服务数量
  237. // 昨日服务数量
  238. return (new OrderService())->dataStatistics($this->mall_id, $this->store_id);
  239. }
  240. /**
  241. * 数据统计
  242. * @param int $id
  243. * @return array
  244. */
  245. public function statisticsByStaff(int $id)
  246. {
  247. // 累计订单
  248. // 累计收益
  249. // 本月收益
  250. // 本月接单
  251. return (new UserService())->statisticsByStaff($this->mall_id, $id, $this->store_id);
  252. }
  253. public function create(array $params)
  254. {
  255. // 需要判断是否有申请技师
  256. try {
  257. (new UserService())->fetch($this->mall_id, $this->user_id);
  258. } catch (\Exception $e) {
  259. return $e->getMessage();
  260. }
  261. /**
  262. * @var $staff StoreStaffApply
  263. */
  264. $staff = StoreStaffApply::getOne([
  265. ['mall_id' => $this->mall_id],
  266. ['user_id' => $this->user_id],
  267. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  268. ]);
  269. if (empty($staff)) {
  270. $staff = new StoreStaffApply();
  271. $staff->mall_id = $this->mall_id;
  272. $staff->user_id = $this->user_id;
  273. } else {
  274. if ($staff->check_status == StatusEnum::ENABLED) return '您不可再次申请';
  275. }
  276. $reservation_service_user = ReservationServiceUser::findOne(['user_id' => $this->user_id]);
  277. if (empty($reservation_service_user)) {
  278. return '请先申请成为平台技师';
  279. }
  280. $staff->check_status = StatusEnum::DISABLED;
  281. $staff->phone = $reservation_service_user['mobile'];
  282. $staff->store_id = $params['store_id'];
  283. $staff->pic = $reservation_service_user['avatar'];
  284. $staff->name = $reservation_service_user['name'];
  285. $staff->created_at = time(); // 每次提交都是最新的时间
  286. if (!$staff->save()) return $staff->getErrorMessage();
  287. // 技师申请
  288. return $staff;
  289. }
  290. /**
  291. * 获取申请详情
  292. * @return mixed|null
  293. */
  294. public function frontDetail()
  295. {
  296. return StoreStaffApply::getOne([
  297. ['mall_id' => $this->mall_id],
  298. ['user_id' => $this->user_id],
  299. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  300. ], true, [], 'id asc');
  301. }
  302. /**
  303. * 添加技师
  304. * @param array $params
  305. * @return StoreStaffApply|mixed|string|null
  306. */
  307. public function add(array $params)
  308. {
  309. try {
  310. $user = ReservationServiceUser::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
  311. $store = Store::findOne(['id' => $this->store_id]);
  312. if (empty($user)) throw new \Exception('请先前往总后台预约服务插件添加该技师');
  313. if (empty($store)) throw new \Exception('门店不存在');
  314. if (!empty($user)) {
  315. $user->store_id = $this->store_id;
  316. $user->store_lat = $store->latitude;
  317. $user->store_lng = $store->longitude;
  318. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  319. }
  320. /**
  321. * @var $staff StoreStaffApply
  322. */
  323. $staff = StoreStaffApply::getOne([
  324. ['mall_id' => $this->mall_id],
  325. ['user_id' => $params['user_id']],
  326. ['store_id' => $params['store_id']],
  327. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  328. ]);
  329. if (empty($staff)) {
  330. $staff = new StoreStaffApply();
  331. $staff->mall_id = $this->mall_id;
  332. $staff->user_id = $params['user_id'];
  333. } else {
  334. throw new \Exception("当前技师已存在");
  335. }
  336. $staff->phone = $user->mobile;
  337. $staff->store_id = $params['store_id'];
  338. $staff->pic = $user->avatar;
  339. $staff->name = $user->name;
  340. $staff->check_status = StatusEnum::ENABLED;
  341. if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
  342. return $staff;
  343. } catch (\Exception $e) {
  344. throw new \Exception($e->getMessage());
  345. }
  346. }
  347. /**
  348. * 获取技师关联的门店预约商品列表
  349. * @param $params
  350. * @return false|mixed
  351. * @throws \Exception
  352. */
  353. public function getAssociationStoreGoods($params)
  354. {
  355. if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
  356. throw new \Exception('未安装预约服务插件');
  357. }
  358. $list = ReservationServiceStoreGoods::lists([
  359. 'where' => [
  360. ['user_id' => $params['user_id']],
  361. ['status' => StatusEnum::ENABLED]
  362. ],
  363. 'with' => [
  364. 'goods' => function ($q) {
  365. $q->select('id,goods_name,price,cover_pic,created_at');
  366. }
  367. ],
  368. 'select' => 'goods_id'
  369. ]
  370. );
  371. $goods_ids = array_column($list, 'goods_id');
  372. $list = StoreGoods::lists([
  373. 'where' => [['id' => $goods_ids]],
  374. 'select' => 'id,goods_name,price,cover_pic,created_at'
  375. ]
  376. );
  377. return $list;
  378. }
  379. /**
  380. * 获取门店预约商品列表
  381. * @param $params
  382. * @return false|mixed
  383. * @throws \Exception
  384. */
  385. public function getStoreGoods($params)
  386. {
  387. if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
  388. throw new \Exception('未安装预约服务插件');
  389. }
  390. $where = [
  391. ['store_id' => $this->store_id],
  392. ['status' => StatusEnum::ENABLED],
  393. ['goods_type' => GoodsTypeEnum::GoodsTypeReserved]
  394. ];
  395. if (!empty($params['keyword'])) {
  396. $keyword = trim($params['keyword']);
  397. $where[] = [
  398. 'or',
  399. ['like', 'goods_name', $keyword],
  400. ['id' => $keyword]
  401. ];
  402. }
  403. $params['where'] = $where;
  404. $params['select'] = 'id,goods_name,price,cover_pic,created_at';
  405. $list = StoreGoods::listPage($params);
  406. return $list;
  407. }
  408. /**
  409. * 操作技师关联的门店预约商品列表
  410. * @param $params
  411. * @return false|mixed
  412. * @throws \Exception
  413. */
  414. public function handleAssociationStoreGoods($params)
  415. {
  416. if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
  417. throw new \Exception('未安装预约服务插件');
  418. }
  419. $params['store_id'] = $this->store_id;
  420. $params['mall_id'] = $this->mall_id;
  421. ReservationServiceStoreGoods::setData($params);
  422. return [];
  423. }
  424. public function addStallUser(array $params)
  425. {
  426. try {
  427. $user = ReservationServiceUser::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
  428. $store = Store::findOne(['id' => $this->store_id]);
  429. if (empty($user)) throw new \Exception('请先前往总后台预约服务插件添加该技师');
  430. if (empty($store)) throw new \Exception('门店不存在');
  431. if ($user['store_id'] && $user['store_id'] != $this->store_id) {
  432. throw new \Exception('该技师已经绑定其他门店,无法添加');
  433. }
  434. $user->load($params, '');
  435. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  436. $staff = StoreStaffApply::getOne([
  437. ['mall_id' => $this->mall_id],
  438. ['user_id' => $params['user_id']],
  439. ['store_id' => $params['store_id']],
  440. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  441. ]);
  442. if (empty($staff)) {
  443. $staff = new StoreStaffApply();
  444. $staff->mall_id = $this->mall_id;
  445. $staff->user_id = $params['user_id'];
  446. }
  447. $staff->phone = $user->mobile;
  448. $staff->store_id = $params['store_id'];
  449. $staff->pic = $user->avatar;
  450. $staff->name = $user->name;
  451. $staff->check_status = StatusEnum::ENABLED;
  452. if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
  453. return $user;
  454. } catch (\Exception $e) {
  455. throw new \Exception($e->getMessage());
  456. }
  457. }
  458. /**
  459. * 获取技师关联的门店预约商品列表-分页
  460. * @param $params
  461. * @return false|mixed
  462. * @throws \Exception
  463. */
  464. public function getAssociationStoreGoodsPage($params)
  465. {
  466. if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
  467. throw new \Exception('未安装预约服务插件');
  468. }
  469. $list = ReservationServiceStoreGoods::lists([
  470. 'where' => [
  471. ['user_id' => $params['user_id']],
  472. ['status' => StatusEnum::ENABLED]
  473. ],
  474. 'select' => 'goods_id'
  475. ]
  476. );
  477. $goods_ids = array_column($list, 'goods_id');
  478. $where = [['id' => $goods_ids]];
  479. $params['where'] = $where;
  480. $params['with'] = 'cats';
  481. $params['select'] = 'id,goods_name,price,cover_pic,created_at';
  482. $list = StoreGoods::listPage($params);
  483. if (!empty($list['list'])) {
  484. foreach ($list['list'] as &$item) {
  485. $item['cate_name'] = '';
  486. foreach ($item['cats'] as $val) {
  487. $item['cate_name'] .= $val['name'] . ',';
  488. }
  489. $item['cate_name'] = trim($item['cate_name'], ',');
  490. }
  491. }
  492. return $list;
  493. }
  494. /**
  495. * 获取关联的次卡
  496. * @param $params
  497. * @return false|mixed
  498. * @throws \Exception
  499. */
  500. public function getAssociationStoreSecondaryCard($params)
  501. {
  502. if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
  503. throw new \Exception('未安装预约服务插件');
  504. }
  505. $list = ReservationServiceStoreSecondaryCard::lists([
  506. 'where' => [
  507. ['user_id' => $params['user_id']],
  508. ['status' => StatusEnum::ENABLED]
  509. ],
  510. 'select' => 'secondary_card_id'
  511. ]
  512. );
  513. $secondary_card_ids = array_column($list, 'secondary_card_id');
  514. $list = SecondaryCard::lists([
  515. 'where' => [['id' => $secondary_card_ids]],
  516. 'select' => 'id,name,create_source_type,created_at'
  517. ]
  518. );
  519. foreach ($list as &$item) {
  520. $item['create_source_type_text'] = $item['create_source_type'] == 0 ? '平台' : '门店';
  521. }
  522. return $list;
  523. }
  524. /**
  525. * 获取门店次卡列表
  526. * @param $params
  527. * @return array|false|mixed|null
  528. * @throws \Exception
  529. */
  530. public function getStoreSecondaryCard($params)
  531. {
  532. $list = ReservationServiceStoreSecondaryCard::lists([
  533. 'where' => [
  534. ['user_id' => $params['user_id']],
  535. ['status' => StatusEnum::ENABLED]
  536. ],
  537. 'select' => 'secondary_card_id'
  538. ]
  539. );
  540. $list = array_column($list, 'secondary_card_id');
  541. $params['where'][] = ['not in', 'sc.id', $list];
  542. $params['where'][] = ['sc.mall_id' => $this->mall_id];
  543. $params['where'][] = ['sc.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  544. $params['where'][] = ['sc.reservation_model' => SecondaryCardEnum::RESERVATION_PEOPLE];
  545. if (!empty($params['keyword'])) {
  546. $keyword = trim($params['keyword']);
  547. $params['where'][] = [
  548. 'or',
  549. ['like', 'sc.name', $keyword],
  550. ['sc.id' => $keyword]
  551. ];
  552. }
  553. $params['where'][] = [
  554. 'or',
  555. ['sc.use_type' => 0],
  556. ['scrs.store_id' => $this->store_id, 'scrs.store_type' => 1]
  557. ];
  558. $params['join'] = [['left join', SecondaryCardRelateStore::tableName() . ' as scrs', 'sc.id = scrs.secondary_card_id']];
  559. $params['order'] = 'sc.id DESC';
  560. $params['alias'] = 'sc';
  561. $params['select'] = 'sc.id,sc.name,sc.create_source_type,sc.created_at';
  562. $pageData = SecondaryCard::listPage($params, false);
  563. if (!empty($pageData['list'])) {
  564. foreach ($pageData['list'] as &$item) {
  565. $item['create_source_type_text'] = $item['create_source_type'] == 0 ? '平台' : '门店';
  566. }
  567. }
  568. return $pageData;
  569. }
  570. public function handleAssociationStoreSecondaryCard($params)
  571. {
  572. if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
  573. throw new \Exception('未安装预约服务插件');
  574. }
  575. $params['store_id'] = $this->store_id;
  576. $params['mall_id'] = $this->mall_id;
  577. ReservationServiceStoreSecondaryCard::setData($params);
  578. return [];
  579. }
  580. }