modified 性能优化提升查询速度

This commit is contained in:
zhaoxiang 2019-01-30 14:58:15 +08:00
parent 4c4e60e4f9
commit 3bb28c738f

View File

@ -13,6 +13,7 @@ use app\model\AdminUser;
use app\model\AdminUserData; use app\model\AdminUserData;
use app\util\ReturnCode; use app\util\ReturnCode;
use app\util\Tools; use app\util\Tools;
use think\Db;
class User extends Base { class User extends Base {
@ -112,32 +113,28 @@ class User extends Base {
/** /**
* 获取当前组的全部用户 * 获取当前组的全部用户
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array * @return array
* @throws \think\db\exception\DataNotFoundException * @throws \think\Exception
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException * @throws \think\exception\DbException
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function getUsers() { public function getUsers() {
$limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT')); $limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
$start = $this->request->get('page', 1); $page = $this->request->get('page', 1);
$gid = $this->request->get('gid', 0); $gid = $this->request->get('gid', 0);
if (!$gid) { if (!$gid) {
return $this->buildFailed(ReturnCode::PARAM_INVALID, '非法操作'); return $this->buildFailed(ReturnCode::PARAM_INVALID, '非法操作');
} }
$listInfo = (new AdminAuthGroupAccess())->where(['groupId' => ['like', "%{$gid}%"]])->select(); $totalNum = (new AdminAuthGroupAccess())->where('find_in_set("' . $gid . '", `groupId`)')->count();
$listInfo = Tools::buildArrFromObj($listInfo); $start = $limit * ($page - 1);
$uidArr = array_column($listInfo, 'uid'); $sql = "SELECT au.* FROM admin_user as au LEFT JOIN admin_auth_group_access as aaga " .
" ON aaga.`uid` = au.`id` WHERE find_in_set('{$gid}', aaga.`groupId`) " .
" ORDER BY au.regTime DESC LIMIT {$start}, {$limit}";
$userInfo = Db::query($sql);
$listObj = (new AdminUser())->whereIn('id', $uidArr)->order('regTime DESC') $uidArr = array_column($userInfo, 'id');
->paginate($limit, false, ['page' => $start])->toArray(); $userData = (new AdminUserData())->whereIn('uid', $uidArr)->select();
$userInfo = $listObj['data'];
$userData = AdminUserData::all(function($query) use ($uidArr) {
$query->whereIn('uid', $uidArr);
});
$userData = Tools::buildArrFromObj($userData);
$userData = Tools::buildArrByNewKey($userData, 'uid'); $userData = Tools::buildArrByNewKey($userData, 'uid');
foreach ($userInfo as $key => $value) { foreach ($userInfo as $key => $value) {
@ -151,7 +148,7 @@ class User extends Base {
return $this->buildSuccess([ return $this->buildSuccess([
'list' => $userInfo, 'list' => $userInfo,
'count' => $listObj['total'] 'count' => $totalNum
]); ]);
} }