StoreTeamListForm.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: kaifa
  5. * Date: 2020-05-10
  6. * Time: 17:57
  7. */
  8. namespace addons\Store\models;
  9. use common\models\user\User;
  10. use common\models\user\UserChildren;
  11. use addons\Store\common\models\Boss;
  12. use addons\Store\common\models\BaseModel;
  13. class TeamListForm extends BaseModel
  14. {
  15. public $limit = 10;
  16. public $page = 1;
  17. public $level=0;
  18. public function rules()
  19. {
  20. return [
  21. [['limit', 'page','level'], 'integer'],
  22. ];
  23. }
  24. public function getList()
  25. {
  26. if (!$this->validate()) {
  27. return $this->responseErrorInfo();
  28. }
  29. $mall = \Yii::$app->mall;
  30. $pagination = null;
  31. $query = UserChildren::find()
  32. ->alias('uc')
  33. ->leftJoin(['b' => Boss::tableName()], 'uc.child_id=b.user_id')
  34. ->where(['b.is_delete' => 0, 'b.mall_id' => $mall->id])
  35. ->leftJoin(['u' => User::tableName()], 'u.id=uc.child_id')
  36. ->andWhere(['uc.is_delete' => 0, 'uc.user_id' => \Yii::$app->user->identity->id, 'b.is_delete' => 0]);
  37. if($this->level){
  38. $query->andWhere(['b.level'=>$this->level]);
  39. }
  40. $list = $query->page($pagination, $this->limit, $this->page)
  41. ->select('b.*,u.nickname,u.avatar_url,u.mobile')
  42. ->orderBy('b.id desc')->asArray()->all();
  43. $newList = [];
  44. foreach ($list as $item) {
  45. $newItem['id'] = $item['id'];
  46. $newItem['user_id'] = $item['user_id'];
  47. $newItem['nickname'] = $item['nickname'];
  48. $newItem['avatar_url'] = $item['avatar_url'];
  49. $newItem['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  50. $newItem['mobile'] = $item['mobile'];
  51. $newItem['total_price'] = $item['total_price'];
  52. $team_count = UserChildren::find()->where(['user_id' => $item['user_id'], 'is_delete' => 0])->count();
  53. $newItem['team_count'] = $team_count ?? 0;
  54. $first_team_count = UserChildren::find()->where(['user_id' => $item['user_id'], 'is_delete' => 0, 'level' => 1])->count();
  55. $newItem['first_team_count'] = $first_team_count ?? 0;
  56. $newItem['other_team_count'] = $newItem['team_count'] - $newItem['first_team_count'];
  57. $newList[] = $newItem;
  58. }
  59. return [
  60. 'code' => 0,
  61. 'msg' => '',
  62. 'data' => [
  63. 'list' => $newList,
  64. 'pagination' => $pagination,
  65. ]
  66. ];
  67. }
  68. }