Browse Source

优化查询速度

zgg 1 month ago
parent
commit
06682772e4
4 changed files with 79 additions and 4 deletions
  1. 0 0
      .env
  2. 2 2
      .gitignore
  3. 75 0
      application/admin/controller/OrderStatistics.php
  4. 2 2
      thinkphp/library/think/App.php

.env.example → .env


+ 2 - 2
.gitignore

@@ -1,6 +1,6 @@
 /.idea
 /.vscode
 *.log
-.env.example
+.env
 .DS_Store
-/.env.example
+/.env

+ 75 - 0
application/admin/controller/OrderStatistics.php

@@ -299,6 +299,81 @@ class OrderStatistics extends Auth
 
     public function repairWorkloadData($start,$end,$mode){
 
+        $sTime = $start;
+        $eTime = $end;
+
+        $list = Db::name('todo')
+            ->field('id,to_user_id')
+            ->where('del',0)
+            ->where('org_id',$this->orgId)
+            ->where('create_time','>=',$sTime)
+            ->where('create_time','<=',$eTime)
+            ->where('work_type_mode',$mode)
+            ->group('to_user_id')
+            ->select();
+
+        $userIds = array_unique(array_column($list, 'to_user_id'));
+        $userNames = Db::name('user')->where('id', 'in', $userIds)->column('real_name', 'id');
+        $todoData = Db::name('todo')
+            ->alias('a')
+            ->join('order_convey b', 'a.order_id=b.order_id')
+            ->field('a.to_user_id, COUNT(*) as total,
+             SUM(CASE WHEN a.todo_mode = 1 THEN 1 ELSE 0 END) as nums1, 
+             SUM(CASE WHEN a.todo_mode = 2 THEN 1 ELSE 0 END) as nums2, 
+             SUM(CASE WHEN a.todo_mode = 3 THEN 1 ELSE 0 END) as nums3, 
+             SUM(CASE WHEN a.todo_mode = 4 THEN 1 ELSE 0 END) as nums4, 
+             SUM(CASE WHEN a.todo_mode = 5 THEN 1 ELSE 0 END) as nums5, 
+             SUM(CASE WHEN a.todo_mode = 6 THEN 1 ELSE 0 END) as nums6,
+             SUM(CASE WHEN a.todo_mode = 3 THEN b.score ELSE 0 END) as score')
+            ->where('a.del', 0)
+            ->where('a.org_id', $this->orgId)
+            ->where('a.create_time', '>=', $sTime)
+            ->where('a.create_time', '<=', $eTime)
+            ->where('a.work_type_mode', $mode)
+            ->group('a.to_user_id')
+            ->select();
+        foreach ($list as $k => $v) {
+            $list[$k]['user_name'] = $userNames[$v['to_user_id']] ?? '';
+
+            $todoInfo = $todoData ? array_filter($todoData, function ($item) use ($v) { return $item['to_user_id'] == $v['to_user_id']; }) : [];
+            $todoInfo = array_values($todoInfo)[0] ?? [];
+
+            $list[$k] = array_merge($list[$k], [
+                'score' => $mode == 3 ? $todoInfo['score']: 0,
+                'nums' => $todoInfo['total'] ?? 0,
+                'nums1' => $todoInfo['nums1'] ?? 0,
+                'nums2' => $todoInfo['nums2'] ?? 0,
+                'nums3' => $todoInfo['nums3'] ?? 0,
+                'nums4' => $todoInfo['nums4'] ?? 0,
+                'nums5' => $todoInfo['nums5'] ?? 0,
+                'nums6' => $todoInfo['nums6'] ?? 0,
+            ]);
+        }
+
+        $list =  $list ? list_sort_by($list,'nums','desc'):[];
+
+
+        $totalList = [
+            array_sum(array_column($list,'score')),
+            array_sum(array_column($list,'nums')),
+            array_sum(array_column($list,'nums1')),
+            array_sum(array_column($list,'nums2')),
+            array_sum(array_column($list,'nums3')),
+            array_sum(array_column($list,'nums4')),
+            array_sum(array_column($list,'nums5')),
+            array_sum(array_column($list,'nums6')),
+        ];
+        if($mode!=3){
+            unset($totalList[0]);
+        }
+
+
+        return ['list'=>$list,'totalList'=>$totalList];
+    }
+
+    //查询时间巨长!!!
+    public function repairWorkloadDataOld($start,$end,$mode){
+
 //        $sTime = $start.' 00:00:00';
 //        $eTime = $end.' 23:59:59';
         $sTime = $start;

+ 2 - 2
thinkphp/library/think/App.php

@@ -180,8 +180,8 @@ class App extends Container
         $this->instance('app', $this);
 
         // 加载环境变量配置文件
-        if (is_file($this->rootPath . '.env.example')) {
-            $this->env->load($this->rootPath . '.env.example');
+        if (is_file($this->rootPath . '.env')) {
+            $this->env->load($this->rootPath . '.env');
         }
 
         $this->configExt = $this->env->get('config_ext', '.php');