Bläddra i källkod

客户查询慢

cqp 2 månader sedan
förälder
incheckning
2aa4a125a0
1 ändrade filer med 19 tillägg och 7 borttagningar
  1. 19 7
      app/Service/RangeService.php

+ 19 - 7
app/Service/RangeService.php

@@ -168,14 +168,26 @@ class RangeService extends Service
         $return_id = array_unique(array_merge_recursive($return_id,$rang_id));
 
         if(! empty($search['top_depart_id']) && ! empty($user['is_all_depart'])){
-            $id = DB::table('customer')
-                ->where('del_time',0)
-                ->where('top_depart_id',$search['top_depart_id'])
-                ->select('id')->get()->toArray();
-            $id = array_column($id,'id');
-            foreach ($return_id as $key => $value){
-                if(! in_array($value,$id)) unset($return_id[$key]);
+            // 分批查询数据库,避免 IN 查询太长
+            $filtered_ids = [];
+            foreach (array_chunk($return_id, 500) as $chunk) {
+                $filtered_ids = array_merge($filtered_ids, Customer::whereIn('id', $chunk)
+                    ->where('top_depart_id', $search['top_depart_id'])
+                    ->where('del_time', 0)
+                    ->pluck('id')
+                    ->toArray());
             }
+
+            $return_id = $filtered_ids;
+
+//            $id = DB::table('customer')
+//                ->where('del_time',0)
+//                ->where('top_depart_id',$search['top_depart_id'])
+//                ->select('id')->get()->toArray();
+//            $id = array_column($id,'id');
+//            foreach ($return_id as $key => $value){
+//                if(! in_array($value,$id)) unset($return_id[$key]);
+//            }
         }
 
         return $return_id;