| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- namespace addons\Happiness\common\service;
- use addons\Happiness\common\models\HappinessClerk;
- use addons\Happiness\common\models\HappinessClerkLog;
- use addons\Happiness\common\models\HappinessStore;
- use addons\Happiness\common\models\HappinessStoreGoods;
- use common\enums\StatusEnum;
- use common\helpers\ApiCode;
- use common\enums\AddonsEnum;
- use common\models\user\User;
- use yii\helpers\Json;
- use Exception;
- use Money\Exchange;
- use Swoole\Http\Status;
- use Yii;
- class ClerkService extends BaseService
- {
-
- //获取核销员列表
- public function getClerkMemberList($params)
- {
- try {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ];
- if (!empty($params['user_id'])) $where[] = ['user_id' => $params['user_id']];
- if (!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];
- }
- if (!empty($params['store_id'])) $where[] = ['store_id' => $params['store_id']];
- $clerk_params = [
- 'where' => $where,
- 'page' => $params['page'],
- 'limig' => $params['limit'],
- 'order' => 'id desc',
- 'with' => ['user', 'store'],
- ];
- $list = HappinessClerk::listPage($clerk_params);
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //获取用户列表
- public function searchUser($params)
- {
- try {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED],
- ];
- if (!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']], ['id' => $params['keyword']]])
- ->select('id')->column();
- if (!empty($uids)) $where[] = ['id' => $uids];
- }
- //获取已被添加的核销员user_id
- $clerk_user = HappinessClerk::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED]
- ],
- ]);
- if (!empty($clerk_user)) {
- $user_id = array_column($clerk_user, 'user_id', 'user_id');
- $where[] = ['not in', 'id', $user_id];
- }
- $list = User::listPage([
- 'where' => $where,
- 'page' => 1,
- 'limit' => 10,
- ]);
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //查询门店数据
- public function searchStore($params)
- {
- try {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED],
- ];
- if (!empty($params['keyword'])) {
- $where[] = ['or', ['like', 'store_name', $params['keyword']], ['id' => $params['keyword']]];
- }
- $store_params = [
- 'where' => $where,
- 'page' => $params['page'],
- 'limit' => $params['limit'],
- 'order' => 'id desc'
- ];
- $list = HappinessStore::listPage($store_params);
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //添加、编辑核销员
- public function addClerk($params)
- {
- try {
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::ENABLED;
- $res = HappinessClerk::setData($params);
- if (!$res) throw new Exception(HappinessClerk::$static_error);
- return true;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //删除核销员
- public function deleteClerk($params)
- {
- try {
- //查询是否存在该核销员
- $info = HappinessClerk::getOne([
- ['mall_id' => $this->mall_id],
- ['id' => $params['id']],
- ]);
- if (empty($info)) throw new Exception('不存在该核销员');
- $info->status = StatusEnum::DELETE;
- if (!$info->save()) throw new Exception('删除失败');
- return true;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function clerkLogList($params)
- {
- try {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED],
- ];
- if (!empty($params['store_id'])) $where[] = ['store_id' => $params['store_id']];
- if (!empty($params['order_no'])) $where[] = ['order_no' => $params['order_no']];
- if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
- if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
- if (!empty($params['user_keyword'])) {
- //获取用户数据
- $uids = User::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['or', ['like', 'nickname', $params['user_keyword']], ['like', 'mobile', $params['user_keyword']], ['like', 'id', $params['user_keyword']]])
- ->select('id')->column();
- if (empty($uids)) {
- $where[] = ['user_id' => 0];
- } else {
- $where[] = ['user_id' => $uids];
- }
- }
- if (!empty($params['clerk_keyword'])) {
- //获取用户数据
- $uids = User::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['or', ['like', 'nickname', $params['clerk_keyword']], ['like', 'mobile', $params['clerk_keyword']], ['like', 'id', $params['clerk_keyword']]])
- ->select('id')->column();
- if (empty($uids)) {
- $where[] = ['user_id' => 0];
- } else {
- $clerk_ids = HappinessClerk::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['user_id' => $uids])
- ->select('id')->column();
- if (empty($clerk_ids)) {
- $where[] = ['clerk_id' => 0];
- } else {
- $where[] = ['clerk_id' => $clerk_ids];
- }
- }
- }
- $clerk_params = [
- 'where' => $where,
- 'page' => $params['page'],
- 'limit' => $params['limit'],
- 'order' => 'id desc',
- 'with' => ['store', 'clerkUser', 'user'],
- ];
- $list = HappinessClerkLog::listPage($clerk_params);
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //获取门店核销记录数据列表
- public function storeClerkLog($params)
- {
- try {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED],
- ['store_id' => $this->store_id],
- ];
- if (!empty($params['order_no'])) $where[] = ['order_no' => $params['order_no']];
- if (!empty($params['user_id'])) $where[] = ['user_id' => $params['user_id']];
- if (!empty($params['clerk_id'])) {
- //获取用户数据
- $clerk_ids = HappinessClerk::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['user_id' => $params['clerk_id']])
- ->select('id')->column();
- if (empty($clerk_ids)) {
- $where[] = ['clerk_ids' => 0];
- } else {
- $where[] = ['clerk_id' => $clerk_ids];
- }
- }
- $clerk_params = [
- 'where' => $where,
- 'order' => 'id desc',
- 'page' => $params['page'],
- 'limit' => $params['limit'],
- 'with' => ['clerkUser', 'user', 'detail']
- ];
- $list = HappinessClerkLog::listPage($clerk_params);
- if (!empty($list['list'])) {
- //获取门店商品
- $goods_list = HappinessStoreGoods::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['status' => StatusEnum::ENABLED],
- ['is_on_sale' => 1],
- ],
- 'with' => ['good'],
- 'index' => 'goods_id'
- ]);
- foreach ($list['list'] as &$item) {
- foreach ($item['detail'] as $key => $value) {
- $item['detail'][$key]['cover_pic'] = $goods_list[$value['goods_id']]['good']['cover_pic'];
- }
- }
- }
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //获取核销记录详情
- public function clerkLogDetail($params)
- {
- try {
- $where = [
- 'mall_id' => $params['mall_id'],
- 'store_id' => $params['store_id'],
- 'id' => $params['id'],
- ];
- $info = HappinessClerkLog::find()->where($where)->with(['clerkUser', 'user', 'detail'])->asArray()->one();
- if (!empty($info)) {
- $goods_list = HappinessStoreGoods::lists([
- 'where' => [
- ['mall_id' => $params['mall_id']],
- ['store_id' => $params['store_id']],
- ['status' => StatusEnum::ENABLED],
- ['is_on_sale' => 1],
- ],
- 'with' => ['good'],
- 'index' => 'goods_id'
- ]);
- foreach ($info['detail'] as &$item) {
- $item['cover_pic'] = $goods_list[$item['goods_id']]['good']['cover_pic'];
- }
- }
- return $info;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function changeClerkStatus($params)
- {
- try {
- //查询核销员
- $clerk_info = HappinessClerk::getOne([
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['id' => $params['id']],
- ]);
- if (empty($clerk_info)) throw new Exception('查无此核销员');
- $clerk_info->status = $params['status'];
- if (!$clerk_info->save()) throw new Exception('操作失败');
- return true;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- }
|