| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * Created by PhpStorm.
- * User: kaifa
- * Date: 2020-05-10
- * Time: 17:57
- */
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use yii\helpers\Json;
- use common\helpers\ArrayHelper;
- use common\models\user\User;
- use addons\Store\common\models\StoreBoss;
- use addons\Store\common\models\StoreBossSetting;
- use yii\data\Pagination;
- class StoreBossListForm extends BaseModel
- {
- public $keyword;
- public $platform;
- public $limit = 10;
- public $page = 1;
- public $sort;
- public $level;
- public $fields;
- public $flag;
- public $store_id = 0;
- public function rules()
- {
- return [
- [['keyword', 'platform'], 'trim'],
- [['keyword', 'platform', 'flag'], 'string'],
- [['limit', 'page', 'level', 'store_id'], 'integer'],
- [['fields'], 'safe'],
- [['sort'], 'default', 'value' => ['d.created_at' => SORT_DESC]],
- ];
- }
- public function getList()
- {
- if (!$this->validate()) {
- return $this->responseErrorInfo();
- }
- if($this->store_id){
- $store = Store::getOne([['id'=>$this->store_id]],true);
- }else{
- $store = \Yii::$app->session->get('o2o');
- }
- $pagination = null;
- $query = StoreBoss::find()->alias('d')
- ->where(['d.status' => 1,'d.store_id' => $store['id'], 'd.mall_id' => $store['mall_id']]);
- //->leftJoin(['u' => User::tableName()], 'u.id = d.user_id');
- if ($this->keyword) {
- $query->andWhere([
- 'or',
- ['like', 'd.user_id', $this->keyword],
- // ['like', 'u.nickname', $this->keyword]
- ]);
- }
- if ($this->level) {
- $query->andWhere(['d.level' => $this->level]);
- }
- //$list = $query->orderBy($this->sort)->all();
- $pagination = new Pagination(['totalCount' => $query->count()]);
- $pagination->setPageSize('20');
- $query->offset($pagination->offset);
- $list=$query->orderBy($this->sort)->asArray()->all();
- $newList = [];
- if($list){
- $user_ids = array_unique(array_column($list, 'user_id'));
- $users = User::find()
- ->select('id,nickname,mobile,avatar_url,parent_id')
- ->where(['id' => $user_ids, 'mall_id' => $store['mall_id'], 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->indexBy('id')
- ->all();
- // 获取用户推荐人
- $parent_ids = array_column($users, 'parent_id');
- $parent_list = User::getUserList([['in', 'id', $parent_ids]], 'id,nickname,avatar_url', [], 1);
- $parent_list = array_column($parent_list,null,'id');
- }
- $level_list = StoreBossLevel::find()->where([
- 'mall_id' => $store['mall_id'],'store_id' => $store['id'], 'is_delete' => 0, 'is_enable' => 1,
- ])->select(['id', 'level', 'name'])->asArray()->all();
- $level_list = array_column($level_list,null,'level');
- foreach ($list as $item) {
- $newItem = $item;
- /* @var User $user */
- $user = $users[$item['user_id']] ?? [];
- $parent = $parent_list[$user['parent_id']] ?? [];
- $newItem = array_merge($newItem, [
- 'nickname' => $user['nickname'] ?? '',
- 'avatar_url' =>$user['avatar_url'] ?? '',
- 'parent_name' => $parent['nickname'] ?? '平台'
- ]);
- $newItem['userInfo'] = $user;
- $newItem['level_name'] = $level_list[$item['level']]['name'] ?? '默认等级';
- $newList[] = $newItem;
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'list' => $newList,
- 'pagination' => $pagination,
- 'bossLevelList' => $level_list,
- ]
- ];
- }
- }
|