瀏覽代碼

加快查询速度

cqp 10 月之前
父節點
當前提交
b3e92777a9
共有 1 個文件被更改,包括 19 次插入35 次删除
  1. 19 35
      app/Service/ReportFormsService.php

+ 19 - 35
app/Service/ReportFormsService.php

@@ -27,7 +27,7 @@ class ReportFormsService extends Service
         if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
 
         //检索条件 生产订单主表----------------
-        $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity');
+        $model = OrdersProduct::where('del_time',0)->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity');
         $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
         if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
         if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
@@ -36,11 +36,7 @@ class ReportFormsService extends Service
         if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
         $orderList = $model->get()->toArray();
         //生产订单主表----------------
-
-        //筛选出制单日期 分表的依据
-        $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
-        //制单日期
-        $out_time = $this->checkSameQuarter($out_order_no_time);
+        $id = array_column($orderList,'id');
 
         //分组以后的订单列表
         $list = [];
@@ -53,45 +49,33 @@ class ReportFormsService extends Service
             }
         }unset($orderList);
 
-        //查询分表数据
-        $production_no = array_column($list,'production_no');
         $detail = [];
-        $process_id = $data['process_id'] ?? [];
-        foreach ($out_time as $value){
-            //子表搜索
-            $models = new OrdersProductProcess(['channel' => $value]);
-            $tmp = $models->whereIn('production_no',$production_no)
-                ->where('del_time',0)
-                ->whereIn('status',array(1,2))
-                ->when(!empty($process_id), function ($query) use ($process_id) {
-                    return $query->whereIn('process_id', $process_id);
-                })
-                ->select('production_no','process_id',DB::raw('SUM(CASE WHEN status >= 1 THEN 1 ELSE 0 END) as dispatch_count'),DB::raw('SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as finish_count'))
-                ->groupBy('production_no','process_id')
-                ->get()->toArray();//派工和完工数据
-
-            foreach ($tmp as $t){
-                $keys = $t['production_no'] . "|" .$t['process_id'];
-                if(isset($detail[$keys])){
-                    $detail[$keys]['dispatch_count'] += $t['dispatch_count'];
-                    $detail[$keys]['finish_count'] += $t['finish_count'];
-                }else{
-                    $detail[$keys] = $t;
-                }
+        $dispatchList = DispatchSub::where('del_time',0)
+            ->whereIn('order_product_id',$id)
+            ->select('order_product_id','process_id',DB::raw('SUM(dispatch_quantity) as dispatch_count'),DB::raw('SUM(finished_num) as finish_count'))
+            ->groupBy('order_product_id','process_id')
+            ->get()->toArray();//派工和完工数据
+        foreach ($dispatchList as $t){
+            $keys = $t['order_product_id'] . "|" .$t['process_id'];
+            if(isset($detail[$keys])){
+                $detail[$keys]['dispatch_count'] += $t['dispatch_count'];
+                $detail[$keys]['finish_count'] += $t['finish_count'];
+            }else{
+                $detail[$keys] = $t;
             }
         }
 
         //返回统计数据
-
         foreach ($list as $key => $value) {
             $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
             $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
 
-            $detail_key = $value['production_no'] . "|";
+            $detail_key = $value['id'] . "|";
             foreach ($detail as $key_son => $value_son) {
-                if (strpos($key_son,$detail_key) !== false) {
-                    $dispatch_count = bcdiv($value_son['dispatch_count'],1000,3);
-                    $finish_count = bcdiv($value_son['finish_count'],1000,3);
+                if (strpos($key_son, $detail_key) !== false) {
+                    $value_son['production_no'] = $value['production_no'];
+                    $dispatch_count = $value_son['dispatch_count'];
+                    $finish_count = $value_son['finish_count'];
                     $value_son['dispatch_count'] = $dispatch_count;
                     $value_son['finish_count'] = $finish_count;
                     $value_son['rate'] = number_format($value_son['finish_count'] / $value['order_quantity'] * 100, 2);