forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.php
More file actions
157 lines (130 loc) · 4.11 KB
/
Copy pathAccount.php
File metadata and controls
157 lines (130 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace app\models;
use Yii;
use app\widgets\LinkPager;
use yii\data\Pagination;
class Account extends User
{
const USER_TYPE = 10; // 普通用户类型
const ADMIN_TYPE = 20; // 管理员类型
/**
* 通过email查找用户
* @param $email
* @return Account|User|null
*/
public static function findByEmail($email)
{
return self::findOne(['email' => $email]);
}
/**
* 获取用户全名(昵称+邮箱)
* @return string
*/
public function getFullName()
{
return $this->name . '(' . $this->email . ')';
}
/**
* 获取最近登录
* @return array|null|\yii\db\ActiveRecord
*/
public function getLastLogin()
{
$filter = [
'user_id' => $this->id,
];
$sort = [
'id' => SORT_DESC
];
return LoginLog::find()->where($filter)->orderBy($sort)->one();
}
/**
* 获取创建项目
* @param null $type 项目类型
* @return \yii\db\ActiveQuery
*/
public function getCreatedProjects($type = null)
{
return $this->hasMany(Project::className(),['creater_id'=>'id'])
->where(['status' => self::ACTIVE_STATUS])
->andFilterWhere(['type' => $type])
->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC]);
}
/**
* 获取参与项目
* @param null $type 项目类型
* @return \yii\db\ActiveQuery
* @throws \yii\base\InvalidConfigException
*/
public function getJoinedProjects($type = null)
{
return $this->hasMany(Project::className(), ['id' => 'project_id'])
->viaTable('{{%member}}', ['user_id' => 'id'])
->where(['status' => self::ACTIVE_STATUS])
->andFilterWhere(['type' => $type])
->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC]);
}
/**
* 判断是否是系统管理员
* @return bool
*/
public function getIsAdmin()
{
return $this->type == self::ADMIN_TYPE ? true : false;
}
/**
* 账户搜索
* @param array $params
* @return $this
* @throws \Exception
*/
public function search($params = [])
{
$this->params = array2object($params);
$query = self::find();
$query->andFilterWhere([
'type' => $this->params->type
]);
$this->params->start_date && $query->andFilterWhere(['>=', '{{%user}}.created_at', $this->params->start_date . ' 00:00:00']);
$this->params->end_date && $query->andFilterWhere(['<=', '{{%user}}.created_at', $this->params->end_date . ' 23:59:59']);
$this->params->status && $query->andFilterWhere([
'status' => $this->params->status,
]);
if($this->params->project_id){
$project = Project::findModel(['encode_id' => $this->params->project_id]);
$user_ids = Member::find()->where(['project_id' => $project->id])->select('user_id')->column();
if(!$user_ids){
$user_ids = [0];
}
$query->andFilterWhere(['in', 'id', $user_ids]);
}
$query->andFilterWhere([
'or',
['like','name', $this->params->name],
['like','email', $this->params->name],
]);
$this->count = $query->count();
$pagination = new Pagination([
'pageSizeParam' => false,
'totalCount' => $this->count,
'pageSize' => $this->pageSize,
'validatePage' => false,
]);
$this->models = $query
->offset($pagination->offset)
->limit($pagination->limit)
->orderBy('id DESC')
->all();
$this->sql = $query->createCommand()->getRawSql();
$this->pages = LinkPager::widget([
'pagination' => $pagination,
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
'hideOnSinglePage' => true,
'maxButtonCount' => 5,
]);
return $this;
}
}