| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- <?php
- namespace addons\Store\common\services;
- use addons\ReservationService\common\models\ReservationServiceStoreGoods;
- use addons\ReservationService\common\models\ReservationServiceStoreSecondaryCard;
- use addons\ReservationService\common\models\ReservationServiceUser;
- use addons\ReservationService\common\services\OrderService;
- use addons\ReservationService\common\services\UserService;
- use addons\SecondaryCard\common\enums\SecondaryCardEnum;
- use addons\SecondaryCard\common\models\SecondaryCard;
- use addons\SecondaryCard\common\models\SecondaryCardRelateStore;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreStaffApply;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use yii\helpers\Json;
- use function Clue\StreamFilter\fun;
- /**
- * 技师管理
- */
- class StaffService extends BaseService
- {
- public $user_id = 0;
- public function init()
- {
- parent::init(); // TODO: Change the autogenerated stub
- // 判断是否有安装技师插件
- }
- /**
- *
- * @param array $params
- * @return array|false|mixed|null
- */
- public function page(array $params)
- {
- // 技师列表
- if (!empty($this->store_id)) $params['store_id'] = $this->store_id;
- $params['is_store'] = StatusEnum::ENABLED;
- // $params['store_status'] = StatusEnum::ENABLED;
- return (new UserService())->page($this->mall_id, $params);
- }
- /**
- * 待审核列表
- * @param array $params
- * @return array|false|mixed|null
- */
- public function applyPage(array $params)
- {
- $where = [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ];
- if (!empty($params['user_id'])) {
- $where[] = ['user_id' => $params['user_id']];
- } elseif (!empty($params['keyword'])) {
- $uids = User::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]])
- ->select('id')->column();
- if (!empty($uids)) {
- $where[] = ['user_id' => $uids];
- } else {
- return [];
- }
- }
- if (!empty($params['name'])) {
- $where[] = ['like', 'name', $params['name']];
- }
- if (!empty($params['mobile'])) {
- $where[] = ['like', 'phone', $params['mobile']];
- }
- if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
- if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
- $l_user = StoreStaffApply::lists([
- 'where' => $where,
- 'order' => 'id desc',
- 'index' => 'user_id'
- ]);
- if (empty($l_user)) return [];
- $user_ids = array_column($l_user, 'user_id');
- $ret = (new UserService())->page($this->mall_id, [
- 'user_ids' => $user_ids,
- 'limit' => $params['limit'] ?? 10,
- 'address' => $params['address'] ?? '',
- 'page' => 1,
- ]);
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- $item['settle'] = $l_user[$item['user_id']] ?? null;
- }
- }
- return $ret;
- }
- /**
- * 审核
- * @param array $params
- * @return string|true
- */
- public function audit(array $params)
- {
- // 审核
- /**
- * @var $staff StoreStaffApply
- */
- $staff = StoreStaffApply::getOne([
- ['mall_id' => $this->mall_id],
- ['id' => $params['id']],
- ['store_id' => $this->store_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ]);
- if (empty($staff)) return '记录不存在';
- if ($staff->check_status != 0) return '当前记录不可审核';
- $t = \Yii::$app->db->beginTransaction();
- try {
- $staff->check_status = $params['status'];
- $staff->check_remark = !empty($params['remark']) ? $params['remark'] : '';
- if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
- /**
- * @var $store Store
- */
- $store = Store::getOne([[
- 'id' => $staff->store_id
- ]]);
- if (empty($store)) throw new \Exception('门店不存在');
- // 那边需要绑定store_id
- if ($staff->check_status == StatusEnum::ENABLED) {
- (new UserService())->bindStore($this->mall_id, $staff->user_id, $store);
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * 设置接单状态
- * @param int $id
- * @return string|true
- */
- public function setStatus(int $id)
- {
- try {
- return (new UserService())->setStoreStatus($this->mall_id, $id, $this->store_id);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 技师详情
- * @param int $id
- * @return mixed|string
- */
- public function detail(int $id)
- {
- // 技师详情
- try {
- $ret = (new UserService())->detail($this->mall_id, $id, $this->store_id);
- if (!empty($ret)) {
- $store = Store::getOne([['id' => $this->store_id]], true, [], false, 'id, store_name, shop_owner, shop_mobile, logo_img');
- $ret['settle'] = ['store' => $store, 'updated_at' => $ret['created_at']];
- $ret['qualification_certificate'] = Json::decode($ret['qualification_certificate']);
- $ret['idcard_images'] = [
- $ret['idcard_front'],
- $ret['idcard_back'],
- ];
- }
- return $ret;
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 移除数据
- * @param int $id
- * @return string|true
- */
- public function delete(int $id)
- {
- /**
- * @var $staff StoreStaffApply
- */
- $staff = StoreStaffApply::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ]);
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($staff)) {
- $staff->status = StatusEnum::DELETE;
- if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
- }
- (new UserService())->removeStore($this->mall_id, $id, $this->store_id);
- ReservationServiceStoreGoods::updateAll(['status' => StatusEnum::DELETE], ['user_id' => $id, 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * 接单排行榜
- * @return array
- */
- public function orderRank()
- {
- // 单量排行榜
- $params['store_id'] = $this->store_id;
- $params['sort'] = 'store_order_num desc';
- $params['limit'] = 10;
- $params['page'] = 1;
- return (new UserService())->page($this->mall_id, $params);
- }
- /**
- * 服务评分排行榜
- * @return array|false|mixed|null
- */
- public function ratingRand()
- {
- $params['store_id'] = $this->store_id;
- $params['sort'] = 'store_service_rating desc';
- $params['limit'] = 10;
- $params['page'] = 1;
- return (new UserService())->page($this->mall_id, $params);
- }
- /**
- * @return int[]
- */
- public function statistics()
- {
- // 累计服务数量
- // 昨日服务数量
- return (new OrderService())->dataStatistics($this->mall_id, $this->store_id);
- }
- /**
- * 数据统计
- * @param int $id
- * @return array
- */
- public function statisticsByStaff(int $id)
- {
- // 累计订单
- // 累计收益
- // 本月收益
- // 本月接单
- return (new UserService())->statisticsByStaff($this->mall_id, $id, $this->store_id);
- }
- public function create(array $params)
- {
- // 需要判断是否有申请技师
- try {
- (new UserService())->fetch($this->mall_id, $this->user_id);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- /**
- * @var $staff StoreStaffApply
- */
- $staff = StoreStaffApply::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $this->user_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ]);
- if (empty($staff)) {
- $staff = new StoreStaffApply();
- $staff->mall_id = $this->mall_id;
- $staff->user_id = $this->user_id;
- } else {
- if ($staff->check_status == StatusEnum::ENABLED) return '您不可再次申请';
- }
- $reservation_service_user = ReservationServiceUser::findOne(['user_id' => $this->user_id]);
- if (empty($reservation_service_user)) {
- return '请先申请成为平台技师';
- }
- $staff->check_status = StatusEnum::DISABLED;
- $staff->phone = $reservation_service_user['mobile'];
- $staff->store_id = $params['store_id'];
- $staff->pic = $reservation_service_user['avatar'];
- $staff->name = $reservation_service_user['name'];
- $staff->created_at = time(); // 每次提交都是最新的时间
- if (!$staff->save()) return $staff->getErrorMessage();
- // 技师申请
- return $staff;
- }
- /**
- * 获取申请详情
- * @return mixed|null
- */
- public function frontDetail()
- {
- return StoreStaffApply::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $this->user_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ], true, [], 'id asc');
- }
- /**
- * 添加技师
- * @param array $params
- * @return StoreStaffApply|mixed|string|null
- */
- public function add(array $params)
- {
- try {
- $user = ReservationServiceUser::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
- $store = Store::findOne(['id' => $this->store_id]);
- if (empty($user)) throw new \Exception('请先前往总后台预约服务插件添加该技师');
- if (empty($store)) throw new \Exception('门店不存在');
- if (!empty($user)) {
- $user->store_id = $this->store_id;
- $user->store_lat = $store->latitude;
- $user->store_lng = $store->longitude;
- if (!$user->save()) throw new \Exception($user->getErrorMessage());
- }
- /**
- * @var $staff StoreStaffApply
- */
- $staff = StoreStaffApply::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $params['user_id']],
- ['store_id' => $params['store_id']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ]);
- if (empty($staff)) {
- $staff = new StoreStaffApply();
- $staff->mall_id = $this->mall_id;
- $staff->user_id = $params['user_id'];
- } else {
- throw new \Exception("当前技师已存在");
- }
- $staff->phone = $user->mobile;
- $staff->store_id = $params['store_id'];
- $staff->pic = $user->avatar;
- $staff->name = $user->name;
- $staff->check_status = StatusEnum::ENABLED;
- if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
- return $staff;
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- /**
- * 获取技师关联的门店预约商品列表
- * @param $params
- * @return false|mixed
- * @throws \Exception
- */
- public function getAssociationStoreGoods($params)
- {
- if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
- throw new \Exception('未安装预约服务插件');
- }
- $list = ReservationServiceStoreGoods::lists([
- 'where' => [
- ['user_id' => $params['user_id']],
- ['status' => StatusEnum::ENABLED]
- ],
- 'with' => [
- 'goods' => function ($q) {
- $q->select('id,goods_name,price,cover_pic,created_at');
- }
- ],
- 'select' => 'goods_id'
- ]
- );
- $goods_ids = array_column($list, 'goods_id');
- $list = StoreGoods::lists([
- 'where' => [['id' => $goods_ids]],
- 'select' => 'id,goods_name,price,cover_pic,created_at'
- ]
- );
- return $list;
- }
- /**
- * 获取门店预约商品列表
- * @param $params
- * @return false|mixed
- * @throws \Exception
- */
- public function getStoreGoods($params)
- {
- if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
- throw new \Exception('未安装预约服务插件');
- }
- $where = [
- ['store_id' => $this->store_id],
- ['status' => StatusEnum::ENABLED],
- ['goods_type' => GoodsTypeEnum::GoodsTypeReserved]
- ];
- if (!empty($params['keyword'])) {
- $keyword = trim($params['keyword']);
- $where[] = [
- 'or',
- ['like', 'goods_name', $keyword],
- ['id' => $keyword]
- ];
- }
- $params['where'] = $where;
- $params['select'] = 'id,goods_name,price,cover_pic,created_at';
- $list = StoreGoods::listPage($params);
- return $list;
- }
- /**
- * 操作技师关联的门店预约商品列表
- * @param $params
- * @return false|mixed
- * @throws \Exception
- */
- public function handleAssociationStoreGoods($params)
- {
- if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
- throw new \Exception('未安装预约服务插件');
- }
- $params['store_id'] = $this->store_id;
- $params['mall_id'] = $this->mall_id;
- ReservationServiceStoreGoods::setData($params);
- return [];
- }
- public function addStallUser(array $params)
- {
- try {
- $user = ReservationServiceUser::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
- $store = Store::findOne(['id' => $this->store_id]);
- if (empty($user)) throw new \Exception('请先前往总后台预约服务插件添加该技师');
- if (empty($store)) throw new \Exception('门店不存在');
- if ($user['store_id'] && $user['store_id'] != $this->store_id) {
- throw new \Exception('该技师已经绑定其他门店,无法添加');
- }
- $user->load($params, '');
- if (!$user->save()) throw new \Exception($user->getErrorMessage());
- $staff = StoreStaffApply::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $params['user_id']],
- ['store_id' => $params['store_id']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ]);
- if (empty($staff)) {
- $staff = new StoreStaffApply();
- $staff->mall_id = $this->mall_id;
- $staff->user_id = $params['user_id'];
- }
- $staff->phone = $user->mobile;
- $staff->store_id = $params['store_id'];
- $staff->pic = $user->avatar;
- $staff->name = $user->name;
- $staff->check_status = StatusEnum::ENABLED;
- if (!$staff->save()) throw new \Exception($staff->getErrorMessage());
- return $user;
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- /**
- * 获取技师关联的门店预约商品列表-分页
- * @param $params
- * @return false|mixed
- * @throws \Exception
- */
- public function getAssociationStoreGoodsPage($params)
- {
- if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
- throw new \Exception('未安装预约服务插件');
- }
- $list = ReservationServiceStoreGoods::lists([
- 'where' => [
- ['user_id' => $params['user_id']],
- ['status' => StatusEnum::ENABLED]
- ],
- 'select' => 'goods_id'
- ]
- );
- $goods_ids = array_column($list, 'goods_id');
- $where = [['id' => $goods_ids]];
- $params['where'] = $where;
- $params['with'] = 'cats';
- $params['select'] = 'id,goods_name,price,cover_pic,created_at';
- $list = StoreGoods::listPage($params);
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$item) {
- $item['cate_name'] = '';
- foreach ($item['cats'] as $val) {
- $item['cate_name'] .= $val['name'] . ',';
- }
- $item['cate_name'] = trim($item['cate_name'], ',');
- }
- }
- return $list;
- }
- /**
- * 获取关联的次卡
- * @param $params
- * @return false|mixed
- * @throws \Exception
- */
- public function getAssociationStoreSecondaryCard($params)
- {
- if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
- throw new \Exception('未安装预约服务插件');
- }
- $list = ReservationServiceStoreSecondaryCard::lists([
- 'where' => [
- ['user_id' => $params['user_id']],
- ['status' => StatusEnum::ENABLED]
- ],
- 'select' => 'secondary_card_id'
- ]
- );
- $secondary_card_ids = array_column($list, 'secondary_card_id');
- $list = SecondaryCard::lists([
- 'where' => [['id' => $secondary_card_ids]],
- 'select' => 'id,name,create_source_type,created_at'
- ]
- );
- foreach ($list as &$item) {
- $item['create_source_type_text'] = $item['create_source_type'] == 0 ? '平台' : '门店';
- }
- return $list;
- }
- /**
- * 获取门店次卡列表
- * @param $params
- * @return array|false|mixed|null
- * @throws \Exception
- */
- public function getStoreSecondaryCard($params)
- {
- $list = ReservationServiceStoreSecondaryCard::lists([
- 'where' => [
- ['user_id' => $params['user_id']],
- ['status' => StatusEnum::ENABLED]
- ],
- 'select' => 'secondary_card_id'
- ]
- );
- $list = array_column($list, 'secondary_card_id');
- $params['where'][] = ['not in', 'sc.id', $list];
- $params['where'][] = ['sc.mall_id' => $this->mall_id];
- $params['where'][] = ['sc.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['where'][] = ['sc.reservation_model' => SecondaryCardEnum::RESERVATION_PEOPLE];
- if (!empty($params['keyword'])) {
- $keyword = trim($params['keyword']);
- $params['where'][] = [
- 'or',
- ['like', 'sc.name', $keyword],
- ['sc.id' => $keyword]
- ];
- }
- $params['where'][] = [
- 'or',
- ['sc.use_type' => 0],
- ['scrs.store_id' => $this->store_id, 'scrs.store_type' => 1]
- ];
- $params['join'] = [['left join', SecondaryCardRelateStore::tableName() . ' as scrs', 'sc.id = scrs.secondary_card_id']];
- $params['order'] = 'sc.id DESC';
- $params['alias'] = 'sc';
- $params['select'] = 'sc.id,sc.name,sc.create_source_type,sc.created_at';
- $pageData = SecondaryCard::listPage($params, false);
- if (!empty($pageData['list'])) {
- foreach ($pageData['list'] as &$item) {
- $item['create_source_type_text'] = $item['create_source_type'] == 0 ? '平台' : '门店';
- }
- }
- return $pageData;
- }
- public function handleAssociationStoreSecondaryCard($params)
- {
- if (!MallAddons::isInstall($this->mall_id, 'ReservationService')) {
- throw new \Exception('未安装预约服务插件');
- }
- $params['store_id'] = $this->store_id;
- $params['mall_id'] = $this->mall_id;
- ReservationServiceStoreSecondaryCard::setData($params);
- return [];
- }
- }
|