WIN-2308041133\Administrator 1 day ago
parent
commit
a100c7f0dc
1 changed files with 49 additions and 37 deletions
  1. 49 37
      app/model/api/User.php

+ 49 - 37
app/model/api/User.php

@@ -432,19 +432,16 @@ class User extends BaseModel
         $post["pageSize"] = $post["pageSize"] > 20 ? 20 : (int)$post["pageSize"];
         $post["pageSize"] = $post["pageSize"] > 20 ? 20 : (int)$post["pageSize"];
         $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
         $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
 
 
-        // 构建基础条件
-        $where = [["u.work_type_id", ">", 0], ["u.status", "=", 1]];
-        if (!empty($post['work_type_id'])) {
-            $where[] = ["u.work_type_id", "=", $post['work_type_id']];
-        }
+        // info_audit 表的筛选条件(需要在获取 uid 时应用)
+        $infoAuditWhere = [["status", "=", 1], ["is_show", "=", 1]];
         if ($post['servicePrice'] > 0) {
         if ($post['servicePrice'] > 0) {
-            $where[] = ["a.service_min_price", "<=", $post['servicePrice']];
+            $infoAuditWhere[] = ["service_min_price", "<=", $post['servicePrice']];
         }
         }
         if (!empty($post['timetype'])) {
         if (!empty($post['timetype'])) {
-            $where[] = ["a.service_type", "=", $post['timetype']];
+            $infoAuditWhere[] = ["service_type", "=", $post['timetype']];
         }
         }
         if ($post['is_china'] != '') {
         if ($post['is_china'] != '') {
-            $where[] = ["a.is_china", '=', $post['is_china']];
+            $infoAuditWhere[] = ["is_china", '=', $post['is_china']];
         }
         }
         
         
         // 城市查询条件优化:预先获取 cityIds
         // 城市查询条件优化:预先获取 cityIds
@@ -477,34 +474,44 @@ class User extends BaseModel
             }
             }
         }
         }
 
 
-        // 分步查询优化:先查 user 表获取符合条件的 uid 和基本信息
-        $userQuery = $this->alias("u")
-            ->where($where);
+        // 构建 info_audit 查询条件(用于获取符合条件的 uid)
+        $infoAuditQuery = (new \app\model\api\InfoAudit())->where($infoAuditWhere);
         
         
-        // 如果有城市筛选,先查 info_audit 获取 uid 列表
+        // 城市筛选:使用 find_in_set
         if (!empty($cityIds)) {
         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)";
-                        }
+            $infoAuditQuery->where(function ($query) use ($cityIds) {
+                $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');
+                }
+                $query->whereRaw("({$orWhere})");
+            });
+        }
+        
+        // 直接在 info_audit 上筛选 uid(如果有 info_audit 筛选条件)
+        $hasInfoAuditFilter = $post['servicePrice'] > 0 || !empty($post['timetype']) || $post['is_china'] != '' || !empty($cityIds);
+        
+        if ($hasInfoAuditFilter) {
+            // 通过 info_audit 获取符合条件的 uid
+            $infoAuditUids = $infoAuditQuery->column('uid');
             
             
-            if (!empty($infoAuditUids)) {
-                $userQuery->where('u.uid', 'in', $infoAuditUids);
-            } else {
+            if (empty($infoAuditUids)) {
                 return ["list" => [], "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => 0];
                 return ["list" => [], "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => 0];
             }
             }
+            
+            // user 查询以此 uid 列表为基础
+            $userQuery = $this->alias("u")
+                ->where($where)
+                ->where('u.uid', 'in', $infoAuditUids);
+        } else {
+            // 没有 info_audit 筛选条件,直接查 user
+            $userQuery = $this->alias("u")
+                ->where($where)
+                ->whereRaw("EXISTS (SELECT 1 FROM info_audit ia WHERE ia.uid = u.uid AND ia.status = 1 AND ia.is_show = 1)");
         }
         }
         
         
         // 获取总数
         // 获取总数
@@ -519,13 +526,18 @@ class User extends BaseModel
                 ->column('u.uid');
                 ->column('u.uid');
             
             
             if (!empty($userList)) {
             if (!empty($userList)) {
-                // 批量获取 info_audit 信息
-                $infoAuditList = (new \app\model\api\InfoAudit())
-                    ->where('uid', 'in', $userList)
-                    ->where('status', '=', 1)
-                    ->where('is_show', '=', 1)
-                    ->select()
-                    ->toArray();
+                // 批量获取 info_audit 信息(带所有筛选条件)
+                $infoAuditQuery2 = (new \app\model\api\InfoAudit())->where($infoAuditWhere);
+                if (!empty($cityIds)) {
+                    $infoAuditQuery2->where(function ($query) use ($cityIds) {
+                        $orWhere = '';
+                        foreach ($cityIds as $idx => $cid) {
+                            $orWhere .= ($idx == 0 ? "" : " OR ") . "FIND_IN_SET({$cid}, service_area)";
+                        }
+                        $query->whereRaw("({$orWhere})");
+                    });
+                }
+                $infoAuditList = $infoAuditQuery2->where('uid', 'in', $userList)->select()->toArray();
                 
                 
                 $infoAuditMap = [];
                 $infoAuditMap = [];
                 foreach ($infoAuditList as $item) {
                 foreach ($infoAuditList as $item) {