Browse Source

优化查询

zgg 2 weeks ago
parent
commit
5bc1d0d7e0
1 changed files with 88 additions and 34 deletions
  1. 88 34
      application/admin/controller/Orders.php

+ 88 - 34
application/admin/controller/Orders.php

@@ -1300,53 +1300,107 @@ class Orders extends Auth {
             }
             $newUser = arraySequence($newUser,'pinyin');
             $mm = new \app\common\model\User();
+            //旧版,速度太慢,抛弃
+//            foreach ($newUser as $key => $value) {
+//                $nums = Db::name('todo')
+//                    ->where(['to_user_id' => $value['id'], 'work_type_mode' => 3])
+//                    ->where('create_yyyymmdd', date('Ymd'))
+//                    ->where('todo_mode', 'in', [1, 2, 3])
+//                    ->count();
+//                $newUser[$key]['nums'] = $nums;
+//
+//                $newUser[$key]['cur_status'] = 0; // 空闲
+//                $nums2 = Db::name('todo')
+//                    ->where(['to_user_id' => $value['id'], 'work_type_mode' => 3])
+//                    ->where('todo_mode', 'in', [1, 2])
+//                    ->count();
+//                if($nums2 > 0){
+//                    $newUser[$key]['cur_status'] = 1; // 任务中
+//                }
+//
+//
+//                $addr = Db::name('order_convey')
+//                    ->alias('cpr')
+//                    ->join('todo os', 'cpr.order_id = os.order_id')
+//                    ->join('orders ods', 'cpr.order_id = ods.id')
+//                    ->join('address ca', 'ca.id = cpr.start')
+//                    ->join('address cad', 'cad.id = cpr.end')
+//                    ->field('os.todo_mode,ca.title as start_name,cad.title as end_name,os.create_time')
+//                    ->where('os.to_user_id', $value['id'])
+//                    ->where('os.del', 0)
+//                    ->where('ods.del', 0)
+//                    ->order('os.id desc')
+//                    ->find();
+//                $newUser[$key]['title'] = '';
+//                $newUser[$key]['addr_time'] = '';
+//                if($addr){
+//                    if(in_array($addr['todo_mode'],[1,2])){
+//                        $newUser[$key]['title'] = $addr['start_name'];
+//                    }else{
+//                        $newUser[$key]['title'] = $addr['end_name'];
+//                    }
+//                    $newUser[$key]['addr_time'] = $addr['create_time'];
+//                }
+//                $u = Db::name('user')
+//                    ->where('id',$value['id'])
+//                    ->value('sex');
+//                $newUser[$key]['sex_name'] = isset($mm->sex[$u])?$mm->sex[$u]:"";
+//
+//            }
+            $userIds = array_column($newUser, 'id');
+            $todoCounts = Db::name('todo')
+                ->where('to_user_id', 'in', $userIds)
+                ->where('work_type_mode', 3)
+                ->where('create_yyyymmdd', date('Ymd'))
+                ->where('todo_mode', 'in', [1, 2, 3])
+                ->group('to_user_id')
+                ->column('to_user_id, COUNT(*) as nums');
+            $todoStatusCounts = Db::name('todo')
+                ->where('to_user_id', 'in', $userIds)
+                ->where('work_type_mode', 3)
+                ->where('todo_mode', 'in', [1, 2])
+                ->group('to_user_id')
+                ->column('to_user_id, COUNT(*) as nums2');
+            $addrInfo = Db::name('order_convey')
+                ->alias('cpr')
+                ->join('todo os', 'cpr.order_id = os.order_id')
+                ->join('orders ods', 'cpr.order_id = ods.id')
+                ->join('address ca', 'ca.id = cpr.start')
+                ->join('address cad', 'cad.id = cpr.end')
+                ->field('os.to_user_id, os.todo_mode, ca.title as start_name, cad.title as end_name, os.create_time')
+                ->where('os.to_user_id', 'in', $userIds)
+                ->where('os.del', 0)
+                ->where('ods.del', 0)
+                ->select();
+            $addrMap = [];
+            foreach ($addrInfo as $addr) {
+                $addrMap[$addr['to_user_id']] = $addr;
+            }
+            $userSex = Db::name('user')
+                ->where('id', 'in', $userIds)
+                ->column('id, sex');
             foreach ($newUser as $key => $value) {
-                $nums = Db::name('todo')
-                    ->where(['to_user_id' => $value['id'], 'work_type_mode' => 3])
-                    ->where('create_yyyymmdd', date('Ymd'))
-                    ->where('todo_mode', 'in', [1, 2, 3])
-                    ->count();
-                $newUser[$key]['nums'] = $nums;
-
-                $newUser[$key]['cur_status'] = 0; // 空闲
-                $nums2 = Db::name('todo')
-                    ->where(['to_user_id' => $value['id'], 'work_type_mode' => 3])
-                    ->where('todo_mode', 'in', [1, 2])
-                    ->count();
-                if($nums2 > 0){
+                $userId = $value['id'];
+                $newUser[$key]['nums'] = isset($todoCounts[$userId]) ? $todoCounts[$userId] : 0;
+                $newUser[$key]['cur_status'] = 0; // 默认空闲
+                if (isset($todoStatusCounts[$userId]) && $todoStatusCounts[$userId] > 0) {
                     $newUser[$key]['cur_status'] = 1; // 任务中
                 }
 
-
-                $addr = Db::name('order_convey')
-                    ->alias('cpr')
-                    ->join('todo os', 'cpr.order_id = os.order_id')
-                    ->join('orders ods', 'cpr.order_id = ods.id')
-                    ->join('address ca', 'ca.id = cpr.start')
-                    ->join('address cad', 'cad.id = cpr.end')
-                    ->field('os.todo_mode,ca.title as start_name,cad.title as end_name,os.create_time')
-                    ->where('os.to_user_id', $value['id'])
-                    ->where('os.del', 0)
-                    ->where('ods.del', 0)
-                    ->order('os.id desc')
-                    ->find();
                 $newUser[$key]['title'] = '';
                 $newUser[$key]['addr_time'] = '';
-                if($addr){
-                    if(in_array($addr['todo_mode'],[1,2])){
+                if (isset($addrMap[$userId])) {
+                    $addr = $addrMap[$userId];
+                    if (in_array($addr['todo_mode'], [1, 2])) {
                         $newUser[$key]['title'] = $addr['start_name'];
-                    }else{
+                    } else {
                         $newUser[$key]['title'] = $addr['end_name'];
                     }
                     $newUser[$key]['addr_time'] = $addr['create_time'];
                 }
-                $u = Db::name('user')
-                    ->where('id',$value['id'])
-                    ->value('sex');
-                $newUser[$key]['sex_name'] = isset($mm->sex[$u])?$mm->sex[$u]:"";
 
+                $newUser[$key]['sex_name'] = isset($mm->sex[$userSex[$userId]]) ? $mm->sex[$userSex[$userId]] : "";
             }
-
             $newUser = list_sort_by($newUser,"addr_time", 'asc');
             if($this->orgId==3){
 //                echo "<pre/>";