| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- /*
- * @Descripttion:
- * @version:
- * @Author: ewgof
- * @Date: 2022-04-29 10:31:21
- * @LastEditors: Please set LastEditors
- * @LastEditTime: 2024-07-15 15:26:41
- */
- namespace addons\Store\backend\controllers;
- use common\enums\StatusEnum;
- use addons\Store\common\models\StoreAssociatedUser;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use Yii;
- class StoreUserAssociatedController extends BaseController
- {
- /**
- * @name:
- * @msg: 获取门店关联会员列表
- * @param {*}
- * @return {*}
- */
- public function actionIndex()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store-user-associated/index');
- }
- if (\Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $store_id = $get['store_id'];
- $where = [
- ['mall_id' => $this->mall_id],
- ['store_id' => $store_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ];
- $user_where = [];
- if (!empty($get['keyword'])) {
- $user_where[] = [
- 'OR',
- ['id' => $get['keyword']],
- ['like', 'nickname', $get['keyword']]
- ];
- }
- if (!empty($get['related_start'])) $where[] = ['>=', 'updated_at', strtotime($get['related_start'])];
- if (!empty($get['related_end'])) $where[] = ['<=', 'updated_at', strtotime($get['related_end'])];
- if (!empty($user_where)) {
- $user_list = User::lists(['where' => $user_where, 'select' => 'id']);
- $user_id_arr = array_column($user_list, 'id');
- $where[] = ['user_id' => $user_id_arr];
- }
- $params = [
- 'where' => $where,
- 'with' => ['user'],
- 'order' => 'id desc',
- 'select' => ['id', 'mall_id', 'store_id', 'user_id', 'updated_at'], // 指定要查询的字段
- ];
- !empty($get['limit']) && $params['limit'] = $get['limit'];
- $list = StoreAssociatedUser::listPage($params);
- if ($list === false) return $this->error(StoreAssociatedUser::getStaticError());
- return $this->success('ok', $list);
- }
- }
- /**
- * @name:
- * @msg: 获取会员列表
- * @param {*}
- * @return {*}
- */
- public function actionGetUsers()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $keyword = \Yii::$app->request->get('keyword');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- if (!empty($user_id)) {
- $wheres[] = ['id' => $user_id];
- }
- if (!empty($keyword)) {
- $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
- }
- $store_user_list = StoreAssociatedUser::lists(['where' => [
- 'mall_id' => $this->mall_id
- ], 'select' => 'user_id']);
- $store_user_ids = array_column($store_user_list, 'user_id');
- $wheres[] = ['not in', 'id', $store_user_ids];
- $params = [
- // 'alias' => 'user',
- 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
- 'where' => $wheres,
- 'order' => 'created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- // 'join' => [['left join',StoreUser::tableName().' as su','su.user_id=user.id and mall_id' => $this->mall_id]],
- ];
- $user_list = User::listPage($params);
- if (empty($user_list['list'])) {
- if (false === $user_list['list'] || $user_list === false) {
- return $this->error(User::getStaticError());
- }
- } else {
- $levels = UserLevel::find()
- ->select('id,level,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('level')
- ->all();
- $parent_ids = array_column($user_list['list'], 'parent_id');
- $parent_users = User::find()
- ->select('id,nickname,avatar_url,mobile,level,parent_id')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($user_list['list'] as &$user) {
- if (empty($user['avatar_url'])) {
- $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- }
- $level = $levels[$user['level']] ?? [];
- $parent_user = $parent_users[$user['parent_id']] ?? [];
- $user['level_name'] = $level['name'] ?? '';
- $user['parent_nickname'] = $parent_user['nickname'] ?? '';
- }
- }
- return $this->success('success', $user_list);
- }
- /**
- * @name:
- * @msg: 编辑会员关联门店
- * @param {*}
- * @return {*}
- */
- public function actionAddUserStore()
- {
- try {
- if (\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $post =\Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post, [
- [['user_id', 'store_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id = $post['user_id'];
- $store_id = $post['store_id'];
- StoreAssociatedUser::setData([
- 'mall_id' => $this->mall_id,
- 'user_id' => $user_id,
- 'store_id' => $store_id,
- ]);
- return $this->success('操作成功', []);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 编辑会员关联门店
- * @param {*}
- * @return {*}
- */
- public function actionEditUserStore()
- {
- try {
- if (\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $post =\Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post, [
- [['user_id', 'store_id', 'old_store_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_ids = explode(',', $post['user_id']);
- $store_id = $post['store_id'];
- $old_store_id = $post['old_store_id'];
- $db = Yii::$app->db->beginTransaction();
- foreach ($user_ids as $user_id) {
- StoreAssociatedUser::setData([
- 'mall_id' => $this->mall_id,
- 'user_id' => $user_id,
- 'store_id' => $old_store_id,
- 'new_store_id' => $store_id
- ]);
- }
- $db->commit();
- return $this->success('操作成功', []);
- } catch (\Exception $e) {
- $db->rollBack();
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 批量编辑会员关联门店
- * @param {*}
- * @return {*}
- */
- public function actionBatchEditUserStore()
- {
- try {
- if (\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $post =\Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post, [
- [['store_id', 'old_store_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $old_store_id = $post['old_store_id'];
- $store_id = $post['store_id'];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $old_store_id];
- $store_user_list = StoreAssociatedUser::lists(['where' => $where , 'select' => 'user_id']);
- if (empty($store_user_list)) {
- return $this->success('操作成功', []);
- }
- $user_ids = array_column($store_user_list, 'user_id');
-
- $db = Yii::$app->db->beginTransaction();
- foreach ($user_ids as $user_id) {
- StoreAssociatedUser::setData([
- 'mall_id' => $this->mall_id,
- 'user_id' => $user_id,
- 'store_id' => $old_store_id,
- 'new_store_id' => $store_id
- ]);
- }
- $db->commit();
- return $this->success('操作成功', []);
- } catch (\Exception $e) {
- $db->rollBack();
- return $this->error($e->getMessage());
- }
- }
- }
|