|
|
@@ -447,58 +447,100 @@ class User extends BaseModel
|
|
|
$where[] = ["a.is_china", '=', $post['is_china']];
|
|
|
}
|
|
|
|
|
|
- // 城市查询条件
|
|
|
- $serviceAreaWhere = null;
|
|
|
+ // 城市查询条件优化:预先获取 cityIds
|
|
|
+ $cityIds = [];
|
|
|
if (!empty($post["service_area"]) && is_array($post["service_area"])) {
|
|
|
- $cityIds = [];
|
|
|
$cityModel = new CityModel();
|
|
|
+ $areaNames = [];
|
|
|
foreach ($post["service_area"] as $v) {
|
|
|
$stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
|
|
|
$str = str_replace(['辖'], ['市辖'], $stc);
|
|
|
$arr = explode(",", $str);
|
|
|
- $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
|
|
|
- if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
|
|
|
- if ($city_id) $cityIds[] = $city_id;
|
|
|
- }
|
|
|
- if (!empty($cityIds)) {
|
|
|
- $serviceAreaWhere = function ($query) use ($cityIds) {
|
|
|
- foreach ($cityIds as $cid) {
|
|
|
- $query->whereOr('find_in_set(' . intval($cid) . ',a.service_area)');
|
|
|
+ $areaNames[] = ["arr1" => $arr[1] ?? '', "arr2" => $arr[2] ?? '', "arr0" => $arr[0] ?? ''];
|
|
|
+ }
|
|
|
+ // 批量查询 city_ids
|
|
|
+ if (!empty($areaNames)) {
|
|
|
+ $cityIdMap = [];
|
|
|
+ foreach ($areaNames as $item) {
|
|
|
+ if (!isset($cityIdMap[$item['arr1']])) {
|
|
|
+ $id = $cityModel->where('merger_name', 'like', "%" . $item['arr1'] . "," . $item['arr2'])->value('id');
|
|
|
+ if (!$id) {
|
|
|
+ $id = $cityModel->where('merger_name', 'like', "%" . $item['arr0'] . "," . $item['arr1'])->value('id');
|
|
|
+ }
|
|
|
+ $cityIdMap[$item['arr1']] = $id;
|
|
|
}
|
|
|
- };
|
|
|
+ if ($cityIdMap[$item['arr1']]) {
|
|
|
+ $cityIds[] = $cityIdMap[$item['arr1']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $cityIds = array_unique($cityIds);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 构建查询(不设置 field,用默认查所有,减少解析开销)
|
|
|
- $query = $this
|
|
|
- ->alias("u")
|
|
|
- ->leftJoin("info_audit a", "u.uid=a.uid and a.status = 1 and a.is_show = 1")
|
|
|
-// ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")
|
|
|
+ // 分步查询优化:先查 user 表获取符合条件的 uid 和基本信息
|
|
|
+ $userQuery = $this->alias("u")
|
|
|
->where($where);
|
|
|
- if ($serviceAreaWhere) {
|
|
|
- $query->where($serviceAreaWhere);
|
|
|
+
|
|
|
+ // 如果有城市筛选,先查 info_audit 获取 uid 列表
|
|
|
+ if (!empty($cityIds)) {
|
|
|
+ $infoAuditUids = (new \app\model\api\InfoAudit())
|
|
|
+ ->where('status', '=', 1)
|
|
|
+ ->where('is_show', '=', 1)
|
|
|
+ ->where(function ($query) use ($cityIds) {
|
|
|
+ // 优化:使用 OR 拼接 find_in_set,减少函数调用次数
|
|
|
+ $orWhere = '';
|
|
|
+ foreach ($cityIds as $idx => $cid) {
|
|
|
+ if ($idx == 0) {
|
|
|
+ $orWhere .= "FIND_IN_SET({$cid}, service_area)";
|
|
|
+ } else {
|
|
|
+ $orWhere .= " OR FIND_IN_SET({$cid}, service_area)";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $query->whereRaw("({$orWhere})");
|
|
|
+ })
|
|
|
+ ->column('uid');
|
|
|
+
|
|
|
+ if (!empty($infoAuditUids)) {
|
|
|
+ $userQuery->where('u.uid', 'in', $infoAuditUids);
|
|
|
+ } else {
|
|
|
+ return ["list" => [], "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => 0];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 获取总数
|
|
|
- $totalCount = $query->count();
|
|
|
+ $totalCount = $userQuery->count();
|
|
|
|
|
|
// 分页查询
|
|
|
$data = [];
|
|
|
if ($totalCount > 0) {
|
|
|
$offset = ($post["page"] - 1) * $post["pageSize"];
|
|
|
- $result = $query->order("u.uid", "desc")
|
|
|
+ $userList = $userQuery->order("u.uid", "desc")
|
|
|
->limit($offset, $post["pageSize"])
|
|
|
- ->select();
|
|
|
- if (!empty($result)) {
|
|
|
- $result = $result->toArray();
|
|
|
- foreach ($result as $v) {
|
|
|
+ ->column('u.uid');
|
|
|
+
|
|
|
+ if (!empty($userList)) {
|
|
|
+ // 批量获取 info_audit 信息
|
|
|
+ $infoAuditList = (new \app\model\api\InfoAudit())
|
|
|
+ ->where('uid', 'in', $userList)
|
|
|
+ ->where('status', '=', 1)
|
|
|
+ ->where('is_show', '=', 1)
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ $infoAuditMap = [];
|
|
|
+ foreach ($infoAuditList as $item) {
|
|
|
+ $infoAuditMap[$item['uid']] = $item;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($userList as $uid) {
|
|
|
+ $audit = $infoAuditMap[$uid] ?? [];
|
|
|
$data[] = [
|
|
|
- 'uid' => $v['uid'],
|
|
|
- 'show_template_id' => $v['show_template_id'] ?? 0,
|
|
|
- 'name' => $v['name'] ?? '',
|
|
|
- 'avatar' => $v['avatar'] ?? '',
|
|
|
- 'longitude' => $v['longitude'] ?? 0,
|
|
|
- 'latitude' => $v['latitude'] ?? 0,
|
|
|
+ 'uid' => $uid,
|
|
|
+ 'show_template_id' => $audit['show_template_id'] ?? 0,
|
|
|
+ 'name' => $audit['name'] ?? '',
|
|
|
+ 'avatar' => $audit['avatar'] ?? '',
|
|
|
+ 'longitude' => $audit['longitude'] ?? 0,
|
|
|
+ 'latitude' => $audit['latitude'] ?? 0,
|
|
|
'user_work_type_title' => ''
|
|
|
];
|
|
|
}
|