StoreUserAssociatedController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @version:
  5. * @Author: ewgof
  6. * @Date: 2022-04-29 10:31:21
  7. * @LastEditors: Please set LastEditors
  8. * @LastEditTime: 2024-07-15 15:26:41
  9. */
  10. namespace addons\Store\backend\controllers;
  11. use common\enums\StatusEnum;
  12. use addons\Store\common\models\StoreAssociatedUser;
  13. use common\models\user\User;
  14. use common\models\user\UserLevel;
  15. use Yii;
  16. class StoreUserAssociatedController extends BaseController
  17. {
  18. /**
  19. * @name:
  20. * @msg: 获取门店关联会员列表
  21. * @param {*}
  22. * @return {*}
  23. */
  24. public function actionIndex()
  25. {
  26. if (!\Yii::$app->request->isAjax) {
  27. return $this->render('/store-user-associated/index');
  28. }
  29. if (\Yii::$app->request->isGet) {
  30. $get = \Yii::$app->request->get();
  31. $store_id = $get['store_id'];
  32. $where = [
  33. ['mall_id' => $this->mall_id],
  34. ['store_id' => $store_id],
  35. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  36. ];
  37. $user_where = [];
  38. if (!empty($get['keyword'])) {
  39. $user_where[] = [
  40. 'OR',
  41. ['id' => $get['keyword']],
  42. ['like', 'nickname', $get['keyword']]
  43. ];
  44. }
  45. if (!empty($get['related_start'])) $where[] = ['>=', 'updated_at', strtotime($get['related_start'])];
  46. if (!empty($get['related_end'])) $where[] = ['<=', 'updated_at', strtotime($get['related_end'])];
  47. if (!empty($user_where)) {
  48. $user_list = User::lists(['where' => $user_where, 'select' => 'id']);
  49. $user_id_arr = array_column($user_list, 'id');
  50. $where[] = ['user_id' => $user_id_arr];
  51. }
  52. $params = [
  53. 'where' => $where,
  54. 'with' => ['user'],
  55. 'order' => 'id desc',
  56. 'select' => ['id', 'mall_id', 'store_id', 'user_id', 'updated_at'], // 指定要查询的字段
  57. ];
  58. !empty($get['limit']) && $params['limit'] = $get['limit'];
  59. $list = StoreAssociatedUser::listPage($params);
  60. if ($list === false) return $this->error(StoreAssociatedUser::getStaticError());
  61. return $this->success('ok', $list);
  62. }
  63. }
  64. /**
  65. * @name:
  66. * @msg: 获取会员列表
  67. * @param {*}
  68. * @return {*}
  69. */
  70. public function actionGetUsers()
  71. {
  72. if (!\Yii::$app->request->isGet) {
  73. return $this->error('请求方式错误');
  74. }
  75. $user_id = \Yii::$app->request->get('user_id');
  76. $keyword = \Yii::$app->request->get('keyword');
  77. $page = \Yii::$app->request->get('page', 1);
  78. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  79. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  80. if (!empty($user_id)) {
  81. $wheres[] = ['id' => $user_id];
  82. }
  83. if (!empty($keyword)) {
  84. $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
  85. }
  86. $store_user_list = StoreAssociatedUser::lists(['where' => [
  87. 'mall_id' => $this->mall_id
  88. ], 'select' => 'user_id']);
  89. $store_user_ids = array_column($store_user_list, 'user_id');
  90. $wheres[] = ['not in', 'id', $store_user_ids];
  91. $params = [
  92. // 'alias' => 'user',
  93. 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
  94. 'where' => $wheres,
  95. 'order' => 'created_at desc',
  96. 'page' => $page,
  97. 'limit' => $limit,
  98. // 'join' => [['left join',StoreUser::tableName().' as su','su.user_id=user.id and mall_id' => $this->mall_id]],
  99. ];
  100. $user_list = User::listPage($params);
  101. if (empty($user_list['list'])) {
  102. if (false === $user_list['list'] || $user_list === false) {
  103. return $this->error(User::getStaticError());
  104. }
  105. } else {
  106. $levels = UserLevel::find()
  107. ->select('id,level,name')
  108. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  109. ->asArray()
  110. ->indexBy('level')
  111. ->all();
  112. $parent_ids = array_column($user_list['list'], 'parent_id');
  113. $parent_users = User::find()
  114. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  115. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  116. ->asArray()
  117. ->indexBy('id')
  118. ->all();
  119. foreach ($user_list['list'] as &$user) {
  120. if (empty($user['avatar_url'])) {
  121. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  122. }
  123. $level = $levels[$user['level']] ?? [];
  124. $parent_user = $parent_users[$user['parent_id']] ?? [];
  125. $user['level_name'] = $level['name'] ?? '';
  126. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  127. }
  128. }
  129. return $this->success('success', $user_list);
  130. }
  131. /**
  132. * @name:
  133. * @msg: 编辑会员关联门店
  134. * @param {*}
  135. * @return {*}
  136. */
  137. public function actionAddUserStore()
  138. {
  139. try {
  140. if (\Yii::$app->request->isGet) {
  141. return $this->error('请求方式错误');
  142. }
  143. $post =\Yii::$app->request->post();
  144. // 检查提交的参数
  145. $res = Yii::$app->services->validate->validate($post, [
  146. [['user_id', 'store_id'], 'required'],
  147. ]);
  148. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  149. $user_id = $post['user_id'];
  150. $store_id = $post['store_id'];
  151. StoreAssociatedUser::setData([
  152. 'mall_id' => $this->mall_id,
  153. 'user_id' => $user_id,
  154. 'store_id' => $store_id,
  155. ]);
  156. return $this->success('操作成功', []);
  157. } catch (\Exception $e) {
  158. return $this->error($e->getMessage());
  159. }
  160. }
  161. /**
  162. * @name:
  163. * @msg: 编辑会员关联门店
  164. * @param {*}
  165. * @return {*}
  166. */
  167. public function actionEditUserStore()
  168. {
  169. try {
  170. if (\Yii::$app->request->isGet) {
  171. return $this->error('请求方式错误');
  172. }
  173. $post =\Yii::$app->request->post();
  174. // 检查提交的参数
  175. $res = Yii::$app->services->validate->validate($post, [
  176. [['user_id', 'store_id', 'old_store_id'], 'required'],
  177. ]);
  178. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  179. $user_ids = explode(',', $post['user_id']);
  180. $store_id = $post['store_id'];
  181. $old_store_id = $post['old_store_id'];
  182. $db = Yii::$app->db->beginTransaction();
  183. foreach ($user_ids as $user_id) {
  184. StoreAssociatedUser::setData([
  185. 'mall_id' => $this->mall_id,
  186. 'user_id' => $user_id,
  187. 'store_id' => $old_store_id,
  188. 'new_store_id' => $store_id
  189. ]);
  190. }
  191. $db->commit();
  192. return $this->success('操作成功', []);
  193. } catch (\Exception $e) {
  194. $db->rollBack();
  195. return $this->error($e->getMessage());
  196. }
  197. }
  198. /**
  199. * @name:
  200. * @msg: 批量编辑会员关联门店
  201. * @param {*}
  202. * @return {*}
  203. */
  204. public function actionBatchEditUserStore()
  205. {
  206. try {
  207. if (\Yii::$app->request->isGet) {
  208. return $this->error('请求方式错误');
  209. }
  210. $post =\Yii::$app->request->post();
  211. // 检查提交的参数
  212. $res = Yii::$app->services->validate->validate($post, [
  213. [['store_id', 'old_store_id'], 'required'],
  214. ]);
  215. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  216. $old_store_id = $post['old_store_id'];
  217. $store_id = $post['store_id'];
  218. $where[] = ['mall_id' => $this->mall_id];
  219. $where[] = ['store_id' => $old_store_id];
  220. $store_user_list = StoreAssociatedUser::lists(['where' => $where , 'select' => 'user_id']);
  221. if (empty($store_user_list)) {
  222. return $this->success('操作成功', []);
  223. }
  224. $user_ids = array_column($store_user_list, 'user_id');
  225. $db = Yii::$app->db->beginTransaction();
  226. foreach ($user_ids as $user_id) {
  227. StoreAssociatedUser::setData([
  228. 'mall_id' => $this->mall_id,
  229. 'user_id' => $user_id,
  230. 'store_id' => $old_store_id,
  231. 'new_store_id' => $store_id
  232. ]);
  233. }
  234. $db->commit();
  235. return $this->success('操作成功', []);
  236. } catch (\Exception $e) {
  237. $db->rollBack();
  238. return $this->error($e->getMessage());
  239. }
  240. }
  241. }