UserService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. <?php
  2. namespace addons\ReservationService\common\services;
  3. use addons\ReservationService\common\enums\ReservationServiceEnum;
  4. use addons\ReservationService\common\models\ReservationServiceOrder;
  5. use addons\ReservationService\common\models\ReservationServiceUser;
  6. use addons\ReservationService\common\models\ReservationServiceWorkTime;
  7. use addons\Store\common\models\Store;
  8. use addons\Store\common\models\StoreReserveSetting;
  9. use addons\Store\common\models\StoreReserveTechnicianScheduling;
  10. use addons\Store\common\models\StoreReserveTechnicianSchedulingTime;
  11. use addons\Store\common\models\StoreSettings;
  12. use common\enums\StatusEnum;
  13. use common\helpers\StringHelper;
  14. use common\models\common\CommissionLog;
  15. use common\models\user\User;
  16. use yii\db\Expression;
  17. use yii\helpers\Json;
  18. use Yii;
  19. class UserService
  20. {
  21. /**
  22. * @param int $mall_id
  23. * @param array $params
  24. * @return array|false|mixed|null
  25. */
  26. public function page(int $mall_id, array $params)
  27. {
  28. $where = [
  29. ['mall_id' => $mall_id],
  30. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  31. ];
  32. if (!empty($params['user_id'])) {
  33. $where[] = ['user_id' => $params['user_id']];
  34. } elseif (!empty($params['keyword'])) {
  35. $uids = User::find()->where(['mall_id' => $mall_id])
  36. ->andWhere(['status' => StatusEnum::ENABLED])
  37. ->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]])
  38. ->select('id')->column();
  39. if (!empty($uids)) {
  40. $where[] = ['user_id' => $uids];
  41. } else {
  42. return [];
  43. }
  44. }
  45. // 指定多个用户
  46. if (!empty($params['user_ids'])) {
  47. $where[] = ['user_id' => $params['user_ids']];
  48. }
  49. if (isset($params['user_id_arr'])) {
  50. $where[] = ['user_id' => $params['user_id_arr']];
  51. }
  52. // 真实姓名
  53. if (!empty($params['name'])) {
  54. $where[] = ['like', 'name', $params['name']];
  55. }
  56. // 手机号
  57. if (!empty($params['mobile'])) {
  58. $where[] = ['like', 'mobile', $params['mobile']];
  59. }
  60. // 所在地址
  61. if (!empty($params['address'])) {
  62. $where[] = ['like', 'address', $params['address']];
  63. }
  64. // 审核状态
  65. if (!empty($params['check_status'])) $where[] = ['check_status' => $params['check_status']];
  66. // 指定门店
  67. if (!empty($params['store_id'])) {
  68. $where[] = ['store_id' => $params['store_id']];
  69. }
  70. // 时间区间
  71. if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
  72. if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
  73. if (!empty($params['is_store'])) { // 仅限门店
  74. $where[] = ['>', 'store_id', '0'];
  75. }
  76. // 年龄区间
  77. if (!empty($params['age_min'])) $where[] = ['>=', 'age', $params['age_min']];
  78. if (!empty($params['age_max'])) $where[] = ['<=', 'age', $params['age_max']];
  79. // 性别
  80. if (!empty($params['gender'])) $where[] = ['gender' => $params['gender']];
  81. if (!empty($params['store_status'])) $where[] = ['store_status' => $params['store_status']];
  82. if (!empty($params['is_working'])) $where[] = ['is_working' => $params['is_working']];
  83. // 字段
  84. $select = 'id, mall_id, level, name, mobile, created_at, updated_at, idcard, idcard_front, idcard_back, address, longitude, latitude, '.
  85. 'qualification_certificate, commission, status, check_status, check_remark, store_id, store_status, service_rating, store_service_rating, store_lng, store_lat, ' .
  86. 'gender, age, desc, avatar, is_working, order_num, user_id, store_order_num';
  87. $fields = explode(', ', $select);
  88. // 技师经纬度
  89. if (!empty($params['lng']) && !empty($params['lat'])) {
  90. $lng = $params['lng'];
  91. $lat = $params['lat'];
  92. $lng_field = 'longitude';
  93. $lat_field = 'latitude';
  94. }
  95. // 门店经纬度
  96. if (!empty($params['store_lng']) && !empty($params['store_lat'])) {
  97. $lng = $params['store_lng'];
  98. $lat = $params['store_lat'];
  99. $lng_field = 'store_lng';
  100. $lat_field = 'store_lat';
  101. }
  102. // 距离
  103. if (!empty($lng)) {
  104. $fields[] = new Expression("round(
  105. 2 * 6378.137 * ASIN(
  106. SQRT(
  107. POW(
  108. SIN(
  109. PI() * ({$lng} - $lng_field) / 360
  110. ),
  111. 2
  112. ) + COS(PI() * {$lat} / 180) * COS($lat_field * PI() / 180) * POW(
  113. SIN(
  114. PI() * ({$lat} - $lat_field) / 360
  115. ),
  116. 2
  117. )
  118. )
  119. )
  120. ,1) AS distance");
  121. }
  122. $ret = ReservationServiceUser::listPage([
  123. 'where' => $where,
  124. 'with' => ['user' => function($query) {
  125. $query->select(['id', 'nickname', 'avatar_url', 'mobile']);
  126. }, 'level' => function($query) {
  127. $query->select('id, level, name');
  128. }],
  129. 'order' => $params['sort'] ?? 'id desc', // 排序
  130. 'limit' => $params['limit'] ?? 10,
  131. 'select'=> $fields,
  132. ]);
  133. $stats = ReservationServiceOrder::find()
  134. ->select(['staff_user_id', 'COUNT(*) as order_count'])
  135. ->where(['service_status' => 100, 'status' => StatusEnum::ENABLED])
  136. ->groupBy('staff_user_id')
  137. ->indexBy('staff_user_id')
  138. ->asArray()
  139. ->all();
  140. foreach($ret['list'] as $k => $v){
  141. $ret['list'][$k]['order_count'] = $stats[$v['user_id']]['order_count'];
  142. }
  143. if (!empty(ReservationServiceUser::getStaticError())) return ReservationServiceUser::getStaticCodeError();
  144. return $ret;
  145. }
  146. /**
  147. * @param int $mall_id
  148. * @param int $user_id
  149. * @param $store
  150. * @return true
  151. * @throws \Exception
  152. */
  153. public function bindStore(int $mall_id, int $user_id, $store)
  154. {
  155. $user = $this->fetch($mall_id, $user_id);
  156. $user->store_id = $store->id;
  157. $user->store_lng = $store->longitude;
  158. $user->store_lat = $store->latitude;
  159. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  160. return true;
  161. }
  162. /**
  163. * @param int $mall_id
  164. * @param int $user_id
  165. * @param int $store_id
  166. * @return ReservationServiceUser
  167. * @throws \Exception
  168. */
  169. public function fetch(int $mall_id, int $user_id, int $store_id = 0)
  170. {
  171. $where = [
  172. ['mall_id' => $mall_id],
  173. ['user_id' => $user_id],
  174. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  175. ['check_status' => StatusEnum::ENABLED]
  176. ];
  177. if (!empty($store_id)) $where[] = ['store_id' => $store_id];
  178. /**
  179. * @var $user ReservationServiceUser
  180. */
  181. $user = ReservationServiceUser::getOne($where);
  182. if (empty($user)) throw new \Exception('技师不存在!');
  183. return $user;
  184. }
  185. /**
  186. * @param int $mall_id
  187. * @param int $id
  188. * @return ReservationServiceUser|string
  189. */
  190. public function fetchById(int $mall_id, int $id)
  191. {
  192. $where = [
  193. ['mall_id' => $mall_id],
  194. ['id' => $id],
  195. ['status' => StatusEnum::ENABLED],
  196. ['check_status' => StatusEnum::ENABLED]
  197. ];
  198. $user = ReservationServiceUser::getOne($where, true, [], 'id desc', 'id, created_at, user_id, name, idcard, address,
  199. longitude, latitude, qualification_certificate, store_id, gender, age, desc, avatar, is_working, order_num, service_rating,idcard_front,idcard_back');
  200. if (empty($user)) return '技师记录不存在.';
  201. $user['store'] = null;
  202. if (!empty($user['store_id'])) {
  203. $user['store'] = Store::getOne([
  204. ['id' => $user['store_id']],
  205. ['status' => StatusEnum::ENABLED]
  206. ], true, [], 'id desc', 'id, store_name, logo_img');
  207. }
  208. $user['created_at'] = date('Y-m-d H:i:s', $user['created_at']);
  209. $user['qualification_certificate'] = !empty($user['qualification_certificate']) ? Json::decode($user['qualification_certificate']) : [];
  210. // 隐藏身份证号码
  211. // 是否可约
  212. // 距离
  213. $user['idcard'] = StringHelper::hideStr($user['idcard'], 1, 16);
  214. $is_real_name_authentication = 0;//实名认证
  215. $is_qualification_certificate = 0;//资质证书
  216. if(!empty($user['name']) && !empty($user['idcard']) && !empty($user['idcard_front']) && !empty($user['idcard_back'])){
  217. $is_real_name_authentication = 1;
  218. }
  219. if(!empty($user['qualification_certificate'])){
  220. $is_qualification_certificate= 1;
  221. }
  222. $user['is_real_name_authentication'] = $is_real_name_authentication;
  223. $user['is_qualification_certificate'] = $is_qualification_certificate;
  224. return $user;
  225. }
  226. /**
  227. * 详情
  228. * @param int $mall_id
  229. * @param int $user_id
  230. * @param int $store_id
  231. * @param bool $is_settle
  232. * @return mixed
  233. * @throws \Exception
  234. */
  235. public function detail(int $mall_id, int $user_id, int $store_id = 0, bool $is_settle = false)
  236. {
  237. $where = [
  238. ['mall_id' => $mall_id],
  239. ['user_id' => $user_id],
  240. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  241. ];
  242. if (!empty($store_id)) $where[] = ['store_id' => $store_id];
  243. $user = ReservationServiceUser::getOne($where, true, $is_settle ? [] : ['user' => function($query) {
  244. $query->select('id, nickname, mobile, avatar_url');
  245. }, 'level' => function($query) {
  246. $query->select('id, level, name');
  247. }]);
  248. if (empty($user) && !$is_settle) throw new \Exception('记录不存在!');
  249. $is_real_name_authentication = 0;//实名认证
  250. $is_qualification_certificate = 0;//资质证书
  251. if(!empty($user['name']) && !empty($user['idcard']) && !empty($user['idcard_front']) && !empty($user['idcard_back'])){
  252. $is_real_name_authentication = 1;
  253. }
  254. if(!empty($user['qualification_certificate'])){
  255. $is_qualification_certificate= 1;
  256. }
  257. $user['is_real_name_authentication'] = $is_real_name_authentication;
  258. $user['is_qualification_certificate'] = $is_qualification_certificate;
  259. return $user;
  260. }
  261. /**
  262. * 获取基础详情
  263. * @param int $mall_id
  264. * @param int $user_id
  265. * @return array|mixed
  266. * @throws \Exception
  267. */
  268. public function customByUserId(int $mall_id, int $user_id)
  269. {
  270. $where = [
  271. ['mall_id' => $mall_id],
  272. ['user_id' => $user_id],
  273. ['status' => StatusEnum::ENABLED],
  274. ];
  275. $user = ReservationServiceUser::getOne($where, true, [
  276. 'level' => function($query)use($mall_id) {
  277. $query->where(['mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED, StatusEnum::DISABLED]])->select('id, name');
  278. }
  279. ], 'id asc', 'level, store_id, commission');
  280. if (empty($user)) throw new \Exception("您还不是技师");
  281. if (!empty($user['store_id'])) {
  282. $user['store'] = Store::getOne([
  283. ['id' => $user['store_id']],
  284. ['status' => StatusEnum::ENABLED]
  285. ], true, [], 'id desc', 'id, store_name, logo_img');
  286. }
  287. return $user;
  288. }
  289. /**
  290. * 统计数据
  291. * @param int $mall_id
  292. * @param int $user_id
  293. * @param int $store_id
  294. * @return mixed|null
  295. */
  296. public function frontStatisticsByMonth(int $mall_id, int $user_id, int $store_id = 0)
  297. {
  298. $where = [
  299. ['mall_id' => $mall_id],
  300. ['staff_user_id' => $user_id],
  301. ['status' => StatusEnum::ENABLED],
  302. ['service_status' => [ReservationServiceEnum::RESERVE_ORDER_STATUS_FINISH, ReservationServiceEnum::RESERVE_ORDER_STATUS_UNDERWRITER]],
  303. ['>=', 'created_at', strtotime(date('Y-m', time()))]
  304. ];
  305. if (!empty($store_id)) {
  306. $where[] = ['store_id' => $store_id];
  307. }
  308. return ReservationServiceOrder::getOne($where, true, [], 'id asc', [new Expression("COUNT(*) as order_month")]);
  309. }
  310. /**
  311. * 移除绑定的门店
  312. * @param int $mall_id
  313. * @param int $user_id
  314. * @param int $store_id
  315. * @return true
  316. * @throws \Exception
  317. */
  318. public function removeStore(int $mall_id, int $user_id, int $store_id)
  319. {
  320. $where = [
  321. ['mall_id' => $mall_id],
  322. ['user_id' => $user_id],
  323. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  324. ['store_id' => $store_id]
  325. ];
  326. /**
  327. * @var $user ReservationServiceUser
  328. */
  329. $user = ReservationServiceUser::getOne($where);
  330. if (empty($user)) throw new \Exception("技师不存在");
  331. $user->store_id = 0;
  332. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  333. return true;
  334. }
  335. /**
  336. * 设置门店上班状态
  337. * @param int $mall_id
  338. * @param int $user_id
  339. * @param int $store_id
  340. * @return true
  341. * @throws \Exception
  342. */
  343. public function setStoreStatus(int $mall_id, int $user_id, int $store_id = 0)
  344. {
  345. $user = $this->fetch($mall_id, $user_id, $store_id);
  346. $user->store_status = $user->store_status == StatusEnum::ENABLED ? StatusEnum::DISABLED : StatusEnum::ENABLED;
  347. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  348. return true;
  349. }
  350. /**
  351. * 创建/修改
  352. * @param int $mall_id
  353. * @param int $user_id
  354. * @param array $data
  355. * @param bool $is_edit
  356. * @return mixed|string|true|null
  357. */
  358. public function save(int $mall_id, int $user_id, array $data, bool $is_edit = false)
  359. {
  360. $where = [
  361. ['mall_id' => $mall_id],
  362. ['user_id' => $user_id],
  363. ['status' => StatusEnum::ENABLED],
  364. ];
  365. /**
  366. * @var $user ReservationServiceUser
  367. */
  368. $user = ReservationServiceUser::getOne($where);
  369. if (empty($user)) {
  370. if ($is_edit) return '非法操作';
  371. $user = new ReservationServiceUser();
  372. $user->loadDefaultValues();
  373. $user->mall_id = $mall_id;
  374. $user->user_id = $user_id;
  375. } else {
  376. if (!in_array($user->check_status, [ReservationServiceEnum::CHECK_FAILED, ReservationServiceEnum::RESERVE_ORDER_STATUS_TALKING])) return '当前状态不可修改';
  377. }
  378. if (!$is_edit) {
  379. $user->check_status = StatusEnum::DISABLED;
  380. $user->created_at = time(); // 每次都设置为最新时间
  381. } else {
  382. $user->avatar = $data['avatar'];
  383. }
  384. if (empty($data['id'])) {
  385. $user->idcard = $data['idcard'];
  386. $user->idcard_front = $data['idcard_front'];
  387. $user->idcard_back = $data['idcard_back'];
  388. $user->name = $data['name'];
  389. $user->age = $data['age'] ?? 0;
  390. $user->gender = $data['gender'] ?? 0;
  391. }
  392. if (!empty($data['desc'])) {
  393. $user->desc = $data['desc'];
  394. }
  395. $user->qualification_certificate = Json::encode(!empty($data['qualification_certificate']) ? $data['qualification_certificate'] : []);
  396. $user->address = $data['address'];
  397. $user->longitude = $data['longitude'];
  398. $user->latitude = $data['latitude'];
  399. $user->mobile = $data['mobile'];
  400. $user->avatar = $data['avatar'] ?? '';
  401. if (!empty($data['province'])) {
  402. $user->province = $data['province'];
  403. }
  404. if (!empty($data['city'])) {
  405. $user->city = $data['city'];
  406. }
  407. if (!empty($data['district'])) {
  408. $user->district = $data['district'];
  409. }
  410. if (!$user->save()) return $user->getErrorMessage();
  411. return true;
  412. }
  413. /**
  414. * @param int $mall_id
  415. * @param int $user_id
  416. * @return true
  417. * @throws \Exception
  418. */
  419. public function setWorking(int $mall_id, int $user_id)
  420. {
  421. $user = $this->fetch($mall_id, $user_id);
  422. $user->is_working = $user->is_working == StatusEnum::ENABLED ? StatusEnum::DISABLED : StatusEnum::ENABLED;
  423. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  424. return true;
  425. }
  426. /**
  427. * 执行审核
  428. * @param int $mall_id
  429. * @param array $params
  430. * @return true
  431. * @throws \Exception
  432. */
  433. public function audit(int $mall_id, array $params)
  434. {
  435. $where = [
  436. ['mall_id' => $mall_id],
  437. ['id' => $params['id']],
  438. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  439. ['check_status' => StatusEnum::DISABLED]
  440. ];
  441. /**
  442. * @var $user ReservationServiceUser
  443. */
  444. $user = ReservationServiceUser::getOne($where);
  445. $user->check_status = $params['status'];
  446. $user->check_remark = !empty($params['remark']) ? $params['remark'] : '';
  447. $user->level = 1; // 默认为第一个级别
  448. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  449. return true;
  450. }
  451. /**
  452. * 保存接单时间
  453. * @param int $mall_id
  454. * @param int $user_id
  455. * @param $times_arr
  456. * @return
  457. */
  458. public function setWorkingTime(int $mall_id, int $user_id,$times_arr)
  459. {
  460. $trans = Yii::$app->db->beginTransaction();
  461. try{
  462. $user_times = ReservationServiceWorkTime::find()->where(['mall_id' => $mall_id,'user_id' => $user_id,'status'=>StatusEnum::ENABLED])
  463. ->orderBy('week_day asc')
  464. ->asArray()->all();
  465. $temp_times = [];
  466. foreach($user_times as $val){
  467. $temp_times[$val['week_day']][$val['text']] = $val;
  468. }
  469. $rows = [];
  470. $times_arr_new = [];
  471. foreach($times_arr as $time){
  472. $exist = $temp_times[$time['day']][$time['text']] ?? [];
  473. if(!$exist){
  474. //新增
  475. $rows[] = [
  476. 'mall_id' => $mall_id,
  477. 'user_id' => $user_id,
  478. 'week_day' => $time['day'],
  479. 'start_time' => $time['start_time'],
  480. 'end_time' => $time['end_time'],
  481. 'text' => $time['text'],
  482. 'created_at' => time(),
  483. 'updated_at' => time()
  484. ];
  485. }
  486. $times_arr_new[] = $time['day'].'_'.$time['text'];
  487. }
  488. $del_ids = [];
  489. foreach($user_times as $val){
  490. $key = $val['week_day'].'_'.$val['text'];
  491. if(!in_array($key,$times_arr_new)){
  492. $del_ids[] = $val['id'];
  493. }
  494. }
  495. if (!empty($rows)) {
  496. $field = ['mall_id','user_id','week_day','start_time','end_time','text','created_at','updated_at'];
  497. $res = \Yii::$app->db->createCommand()->batchInsert(ReservationServiceWorkTime::tableName(), $field, $rows)->execute();
  498. if(!$res) throw new \Exception(ReservationServiceWorkTime::getStaticError());
  499. }
  500. if($del_ids){
  501. $res = ReservationServiceWorkTime::updateAll(['status' => StatusEnum::DELETE], ['id'=>$del_ids]);
  502. if(!$res) throw new \Exception(ReservationServiceWorkTime::getStaticError());
  503. }
  504. $trans->commit();
  505. return true;
  506. }catch(\Exception $e){
  507. $trans->rollBack();
  508. return $e->getMessage();
  509. }
  510. }
  511. /**
  512. * 获取技师工作时间
  513. * @param int $mall_id
  514. * @param int $user_id
  515. * @return array
  516. */
  517. public function getWorkingTime(int $mall_id, int $user_id)
  518. {
  519. $user_times = ReservationServiceWorkTime::find()->where(['mall_id' => $mall_id,'user_id' => $user_id,'status'=>StatusEnum::ENABLED])
  520. ->select('week_day,start_time,end_time,text')
  521. ->orderBy('week_day asc')
  522. ->asArray()->all();
  523. $temp_times = [];
  524. foreach($user_times as $val){
  525. $temp_times[$val['week_day']][] = $val;
  526. }
  527. return $temp_times;
  528. }
  529. /**
  530. * 技师的项目
  531. * @param int $mall_id
  532. * @param int $id
  533. * @return array|false|mixed|string
  534. */
  535. public function goods(int $mall_id, int $id)
  536. {
  537. /**
  538. * @var $user ReservationServiceUser
  539. */
  540. $user = ReservationServiceUser::getOne([
  541. ['mall_id' => $mall_id],
  542. ['id' => $id],
  543. ['status' => StatusEnum::ENABLED],
  544. ['check_status' => StatusEnum::ENABLED]
  545. ], false, [], 'id asc', 'user_id');
  546. if (empty($user)) return '技师不存在';
  547. return (new GoodsService())->goods($mall_id, $user->user_id);
  548. }
  549. /**
  550. * 接单排行榜
  551. * @return array|false|mixed|null
  552. */
  553. public function orderRank(int $mall_id, int $store_id = 0)
  554. {
  555. if (!empty($store_id)) $params['store_id'] = $store_id;
  556. $params['sort'] = 'order_num desc';
  557. $params['limit'] = 10;
  558. $params['page'] = 1;
  559. return (new UserService())->page($mall_id, $params);
  560. }
  561. /**
  562. * 服务评分排行榜
  563. * @return array|false|mixed|null
  564. */
  565. public function ratingRand(int $mall_id, int $store_id = 0)
  566. {
  567. if (!empty($store_id)) $params['store_id'] = $store_id;
  568. $params['sort'] = 'service_rating desc';
  569. $params['limit'] = 10;
  570. $params['page'] = 1;
  571. return (new UserService())->page($mall_id, $params);
  572. }
  573. /**
  574. * 数据统计
  575. * @param int $mall_id
  576. * @param int $user_id
  577. * @param int $store_id
  578. * @return array
  579. */
  580. public function statisticsByStaff(int $mall_id, int $user_id, int $store_id = 0)
  581. {
  582. $month = $this->frontStatisticsByMonth($mall_id, $user_id, $store_id);
  583. $where = ['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  584. $staff = ReservationServiceUser::findOne($where);
  585. $income_month = CommissionLog::find()->where(['user_id' => $user_id])
  586. ->andWhere(['>=', 'created_at', strtotime(date('Y-m', time()))])
  587. ->andWhere(['change_type' => 'add'])
  588. ->andWhere(['from_type' => ReservationServiceEnum::ADDONS_NAME])
  589. ->sum('change_num');
  590. return [
  591. 'total_order' => !empty($store_id) ? $staff->store_order_num : $staff->order_num,
  592. 'total_commission'=> $staff->commission,
  593. 'month_order' => $month['order_month'],
  594. 'month_commission'=> !empty($income_month) ? bcmul($income_month, 1, 2) : 0
  595. ];
  596. }
  597. /**
  598. * @param int $user_id
  599. * @param $commission
  600. * @return void
  601. * @throws \Exception
  602. */
  603. public function addCommission(int $user_id, $commission)
  604. {
  605. $user = ReservationServiceUser::findOne(['user_id' => $user_id]);
  606. if($user){
  607. $user->commission += $commission;
  608. if (!$user->save()) throw new \Exception($user->getErrorMessage());
  609. }
  610. }
  611. public function checkScan($params)
  612. {
  613. $reservation_service_user = ReservationServiceUser::getOne([
  614. ['status' => StatusEnum::ENABLED],
  615. ['user_id'=>$params['user_id']],
  616. ['mall_id'=>$params['mall_id']],
  617. ]);
  618. if(empty($reservation_service_user)){
  619. throw new \Exception('技师不存在');
  620. }
  621. if(empty($reservation_service_user['store_id'])){
  622. return true;
  623. }
  624. $store_reserve_settings = StoreReserveSetting::getOne([
  625. ['status' => StatusEnum::ENABLED],
  626. ['store_id'=>$reservation_service_user['store_id']],
  627. ['mall_id'=>$params['mall_id']],
  628. ]);
  629. if(empty($store_reserve_settings['setting'])){
  630. return true;
  631. }
  632. $store_reserve_settings = json_decode($store_reserve_settings['setting'],true);
  633. if(!empty($store_reserve_settings['technician_verification_switch'])){
  634. return true;
  635. }
  636. //关闭后,需要验证技师在门店的排班时间内不能核销 todo
  637. $time = time();
  638. $where = [
  639. ['user_id'=>$params['user_id']],
  640. ['store_id'=>$reservation_service_user['store_id']],
  641. ['mall_id'=>$params['mall_id']],
  642. ['date_time'=>strtotime(date('Y-m-d',$time))]
  643. ];
  644. $result = StoreReserveTechnicianScheduling::getOne($where);
  645. if (empty($result)) {
  646. return true;
  647. }
  648. if(!empty($result['is_all_day'])){
  649. throw new \Exception('该时段无核销权限');
  650. }
  651. $where = [];
  652. $where[] = [
  653. 'sid'=>$result['id'],
  654. ];
  655. $list = StoreReserveTechnicianSchedulingTime::lists([
  656. 'where'=>$where
  657. ]);
  658. if(empty($list)){
  659. return true;
  660. }
  661. foreach ($list as $item){
  662. if($item['start_time']<=$time && $time<=$item['end_time']){
  663. throw new \Exception('该时段无核销权限');
  664. }
  665. }
  666. return true;
  667. }
  668. public function orderStastics($params)
  669. {
  670. $where = ['mall_id'=>$params['mall_id'], 'staff_user_id'=>$params['staff_user_id'], 'service_status'=>ReservationServiceEnum::RESERVE_ORDER_STATUS_NORMAL];
  671. $pending_orders = ReservationServiceOrder::find()->where($where)->count();
  672. $where['service_status'] = ReservationServiceEnum::RESERVE_ORDER_STATUS_TALKING;
  673. $waiting_service = ReservationServiceOrder::find()->where($where)->count();
  674. $where['service_status'] = ReservationServiceEnum::RESERVE_ORDER_STATUS_UNDERWRITER;
  675. $to_be_accepted = ReservationServiceOrder::find()->where($where)->count();
  676. return ['pending_orders'=>$pending_orders??0,'waiting_service'=>$waiting_service??0,'to_be_accepted'=>$to_be_accepted??0];
  677. }
  678. }