cxfoot/models/common/auth/CommonRoleUserListForm.php
2023-10-24 14:54:18 +08:00

111 lines
3.2 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年9月13日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\auth;
use app\models\auth\Role;
use app\models\auth\RoleUser;
use app\models\auth\RolePermission;
use app\models\User;
use app\models\SysAdmin;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonRoleUserListForm extends Model
{
public $keywords;
public $limit;
public $page;
public $cx_mch_id;
public $creator_user_id;
public $type;
public function rules()
{
return [
[['keywords'], 'trim'],
[['keywords'], 'string'],
[['page', 'limit', 'cx_mch_id', 'creator_user_id', 'type'], 'integer'],
[['page'], 'default', 'value' => 1],
[['limit'], 'default', 'value' => 20],
[['type'], 'required']
];
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$query = User::find()->alias('u')
->leftJoin(['sa' => SysAdmin::tableName()], 'sa.user_id=u.id')
->where([
'u.cx_mch_id' => $this->cx_mch_id,
'u.is_delete' => 0,
// 'u.type' => $this->type,
])
->andWhere(['in','u.type',[User::TYPE_STAFF,User::TYPE_BOSS_STAFF,User::TYPE_ADMIN_STAFF]])
->andFilterWhere([
'OR',
['LIKE', 'u.nickname', $this->keywords],
])
->andFilterWhere([
'sa.creator_user_id' => $this->creator_user_id
])
->select('u.id,u.nickname,u.username,u.avatar_url,u.created_at,u.updated_at');
$count_query = clone $query;
$count = $count_query->count();
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['u.created_at' => SORT_DESC])->asArray()->all();
foreach ($list as $index => $item){
$item['created_at_cn'] = date('Y-m-d H:i:s',$item['created_at']);
$item['updated_at_cn'] = date('Y-m-d H:i:s',$item['updated_at']);
$item['roles'] = $this->getUserRoles($item['id']);
$list[$index] = $item;
}
//是否已经全部加载
$end_flag = $this->page > $pagination->pageCount ? true : false;
return [
'code' => 0,
'msg' => 'ok',
'data' => $list,
'count' => $count,
'page_size' => $this->limit,
'page_count' => $pagination->pageCount,
'page_no' => $this->page,
'end_flag' => $end_flag
];
}
private function getUserRoles($user_id)
{
$list = RoleUser::find()->alias('aru')
->leftJoin(['ar' => Role::tableName()],'ar.id=aru.role_id')
->where([
'aru.user_id' => $user_id,
'aru.is_delete' => 0,
'ar.is_delete' => 0
])
->select('aru.role_id,ar.name as role_name')
->asArray()->all();
return $list;
}
}