StoreBossListForm.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: kaifa
  5. * Date: 2020-05-10
  6. * Time: 17:57
  7. */
  8. namespace addons\Store\common\models;
  9. use common\enums\StatusEnum;
  10. use yii\helpers\Json;
  11. use common\helpers\ArrayHelper;
  12. use common\models\user\User;
  13. use addons\Store\common\models\StoreBoss;
  14. use addons\Store\common\models\StoreBossSetting;
  15. use yii\data\Pagination;
  16. class StoreBossListForm extends BaseModel
  17. {
  18. public $keyword;
  19. public $platform;
  20. public $limit = 10;
  21. public $page = 1;
  22. public $sort;
  23. public $level;
  24. public $fields;
  25. public $flag;
  26. public $store_id = 0;
  27. public function rules()
  28. {
  29. return [
  30. [['keyword', 'platform'], 'trim'],
  31. [['keyword', 'platform', 'flag'], 'string'],
  32. [['limit', 'page', 'level', 'store_id'], 'integer'],
  33. [['fields'], 'safe'],
  34. [['sort'], 'default', 'value' => ['d.created_at' => SORT_DESC]],
  35. ];
  36. }
  37. public function getList()
  38. {
  39. if (!$this->validate()) {
  40. return $this->responseErrorInfo();
  41. }
  42. if($this->store_id){
  43. $store = Store::getOne([['id'=>$this->store_id]],true);
  44. }else{
  45. $store = \Yii::$app->session->get('o2o');
  46. }
  47. $pagination = null;
  48. $query = StoreBoss::find()->alias('d')
  49. ->where(['d.status' => 1,'d.store_id' => $store['id'], 'd.mall_id' => $store['mall_id']]);
  50. //->leftJoin(['u' => User::tableName()], 'u.id = d.user_id');
  51. if ($this->keyword) {
  52. $query->andWhere([
  53. 'or',
  54. ['like', 'd.user_id', $this->keyword],
  55. // ['like', 'u.nickname', $this->keyword]
  56. ]);
  57. }
  58. if ($this->level) {
  59. $query->andWhere(['d.level' => $this->level]);
  60. }
  61. //$list = $query->orderBy($this->sort)->all();
  62. $pagination = new Pagination(['totalCount' => $query->count()]);
  63. $pagination->setPageSize('20');
  64. $query->offset($pagination->offset);
  65. $list=$query->orderBy($this->sort)->asArray()->all();
  66. $newList = [];
  67. if($list){
  68. $user_ids = array_unique(array_column($list, 'user_id'));
  69. $users = User::find()
  70. ->select('id,nickname,mobile,avatar_url,parent_id')
  71. ->where(['id' => $user_ids, 'mall_id' => $store['mall_id'], 'status' => StatusEnum::ENABLED])
  72. ->asArray()
  73. ->indexBy('id')
  74. ->all();
  75. // 获取用户推荐人
  76. $parent_ids = array_column($users, 'parent_id');
  77. $parent_list = User::getUserList([['in', 'id', $parent_ids]], 'id,nickname,avatar_url', [], 1);
  78. $parent_list = array_column($parent_list,null,'id');
  79. }
  80. $level_list = StoreBossLevel::find()->where([
  81. 'mall_id' => $store['mall_id'],'store_id' => $store['id'], 'is_delete' => 0, 'is_enable' => 1,
  82. ])->select(['id', 'level', 'name'])->asArray()->all();
  83. $level_list = array_column($level_list,null,'level');
  84. foreach ($list as $item) {
  85. $newItem = $item;
  86. /* @var User $user */
  87. $user = $users[$item['user_id']] ?? [];
  88. $parent = $parent_list[$user['parent_id']] ?? [];
  89. $newItem = array_merge($newItem, [
  90. 'nickname' => $user['nickname'] ?? '',
  91. 'avatar_url' =>$user['avatar_url'] ?? '',
  92. 'parent_name' => $parent['nickname'] ?? '平台'
  93. ]);
  94. $newItem['userInfo'] = $user;
  95. $newItem['level_name'] = $level_list[$item['level']]['name'] ?? '默认等级';
  96. $newList[] = $newItem;
  97. }
  98. return [
  99. 'code' => 0,
  100. 'msg' => '',
  101. 'data' => [
  102. 'list' => $newList,
  103. 'pagination' => $pagination,
  104. 'bossLevelList' => $level_list,
  105. ]
  106. ];
  107. }
  108. }