Skip to content

Commit 3bb28c7

Browse files
author
zhaoxiang
committed
modified 性能优化提升查询速度
1 parent 4c4e60e commit 3bb28c7

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

application/admin/controller/User.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use app\model\AdminUserData;
1414
use app\util\ReturnCode;
1515
use app\util\Tools;
16+
use think\Db;
1617

1718
class User extends Base {
1819

@@ -112,32 +113,28 @@ public function add() {
112113

113114
/**
114115
* 获取当前组的全部用户
115-
* @author zhaoxiang <zhaoxiang051405@gmail.com>
116116
* @return array
117-
* @throws \think\db\exception\DataNotFoundException
118-
* @throws \think\db\exception\ModelNotFoundException
117+
* @throws \think\Exception
119118
* @throws \think\exception\DbException
119+
* @author zhaoxiang <zhaoxiang051405@gmail.com>
120120
*/
121121
public function getUsers() {
122122
$limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
123-
$start = $this->request->get('page', 1);
123+
$page = $this->request->get('page', 1);
124124
$gid = $this->request->get('gid', 0);
125125
if (!$gid) {
126126
return $this->buildFailed(ReturnCode::PARAM_INVALID, '非法操作');
127127
}
128128

129-
$listInfo = (new AdminAuthGroupAccess())->where(['groupId' => ['like', "%{$gid}%"]])->select();
130-
$listInfo = Tools::buildArrFromObj($listInfo);
131-
$uidArr = array_column($listInfo, 'uid');
132-
133-
$listObj = (new AdminUser())->whereIn('id', $uidArr)->order('regTime DESC')
134-
->paginate($limit, false, ['page' => $start])->toArray();
135-
$userInfo = $listObj['data'];
129+
$totalNum = (new AdminAuthGroupAccess())->where('find_in_set("' . $gid . '", `groupId`)')->count();
130+
$start = $limit * ($page - 1);
131+
$sql = "SELECT au.* FROM admin_user as au LEFT JOIN admin_auth_group_access as aaga " .
132+
" ON aaga.`uid` = au.`id` WHERE find_in_set('{$gid}', aaga.`groupId`) " .
133+
" ORDER BY au.regTime DESC LIMIT {$start}, {$limit}";
134+
$userInfo = Db::query($sql);
136135

137-
$userData = AdminUserData::all(function($query) use ($uidArr) {
138-
$query->whereIn('uid', $uidArr);
139-
});
140-
$userData = Tools::buildArrFromObj($userData);
136+
$uidArr = array_column($userInfo, 'id');
137+
$userData = (new AdminUserData())->whereIn('uid', $uidArr)->select();
141138
$userData = Tools::buildArrByNewKey($userData, 'uid');
142139

143140
foreach ($userInfo as $key => $value) {
@@ -151,7 +148,7 @@ public function getUsers() {
151148

152149
return $this->buildSuccess([
153150
'list' => $userInfo,
154-
'count' => $listObj['total']
151+
'count' => $totalNum
155152
]);
156153
}
157154

0 commit comments

Comments
 (0)