stanley-king 2 years ago
parent
commit
9009ec8c39
1 changed files with 47 additions and 26 deletions
  1. 47 26
      helper/task/handler.php

+ 47 - 26
helper/task/handler.php

@@ -205,12 +205,8 @@ class handler
                 [$start,$end] = $time_scope['order_time'];
                 [$start,$end] = $time_scope['order_time'];
             }
             }
 
 
-            $task_id = $this->mTtaskId;
             $total_stage = ceil(($end - $start) / 3600);
             $total_stage = ceil(($end - $start) / 3600);
-            $cur_stage = 0;
-            $expected_time = 0;
-            $expected_curtime = time();
-            $mod_task = Model('task');
+            $cur_stage = 1;
 
 
             for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
             for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
             {
             {
@@ -241,25 +237,10 @@ class handler
                     $i++;
                     $i++;
 
 
                     if(empty($items)) break;
                     if(empty($items)) break;
-                    foreach ($items as $item) {
-                        yield $item;
-                    }
-                }
-                if(!empty($task_id)){
-                    if($cur_stage === 0) {
-                        $now = time();
-                        if($now == $expected_curtime) {
-                            $expected_time = 10;
-                        }else{
-                            $expected_time = ($now - $expected_curtime) * ($total_stage / $cur_stage);
-                        }
-                        $expected_time = date("Y-m-d H:i:s", ($now + $expected_time));
 
 
-                    }
-                    $cur_stage++;
-                    $stage = "导出进度:{$cur_stage}/{$total_stage}, 预计完成时间:{$expected_time}";
-                    $mod_task->where(['task_id' => $task_id])->update(['stage' => $stage]);
+                    yield [$items,$total_stage,$cur_stage];
                 }
                 }
+                $cur_stage++;
             }
             }
         };
         };
 
 
@@ -273,6 +254,7 @@ class handler
             $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']);
             $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']);
             $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['channel_amount']);
             $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['channel_amount']);
         }
         }
+
         $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
         $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
         foreach ($merchant_list as $value) {
         foreach ($merchant_list as $value) {
             $merchants[$value['mchid']] = $value;
             $merchants[$value['mchid']] = $value;
@@ -293,9 +275,7 @@ class handler
         }
         }
         $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
         $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
 
 
-        $orders = $order_reader($normal_cond,$time_scope);
-        $index = 0;
-        foreach ($orders as $order)
+        $execl_writer = function ($order,$index) use ($card_type_texts, $column_keys,$objPHPExcel,$data_keys)
         {
         {
             if(!empty($merchants)) {
             if(!empty($merchants)) {
                 $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
                 $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
@@ -314,8 +294,48 @@ class handler
                 $field = $column_key . ($index + 2);
                 $field = $column_key . ($index + 2);
                 $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
                 $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
             }
             }
+        };
 
 
-            $index += 1;
+        $start_time = time();
+        $percentor = function ($total_stage, $cur_stage,$used_time) use ($start_time)
+        {
+            $task_id = $this->mTtaskId;
+            $expected_time = 0;
+            $expected_curtime = time();
+            $mod_task = Model('task');
+
+            if(!empty($task_id)){
+                if($cur_stage === 0) {
+                    $now = time();
+                    if($now == $expected_curtime) {
+                        $expected_time = 10;
+                    }else{
+                        $expected_time = ($now - $expected_curtime) * ($total_stage / $cur_stage);
+                    }
+                    $expected_time = date("Y-m-d H:i:s", ($now + $expected_time));
+
+                }
+                $cur_stage++;
+                $stage = "导出进度:{$cur_stage}/{$total_stage}, 预计完成时间:{$expected_time}";
+                $mod_task->where(['task_id' => $task_id])->update(['stage' => $stage]);
+            }
+        };
+
+        $reader = $order_reader($normal_cond,$time_scope);
+        $index = 0;
+
+        foreach ($reader as $result)
+        {
+            $cur_start = time();
+            [$items,$total_stage,$cur_stage] = $result;
+            foreach ($items as $order)
+            {
+                $execl_writer($order,$index);
+                $index += 1;
+            }
+
+            $cur_end = time();
+            $percentor($total_stage,$cur_stage,$cur_end - $cur_start);
         }
         }
 
 
         try {
         try {
@@ -331,6 +351,7 @@ class handler
             $objWriter->save($file_path);
             $objWriter->save($file_path);
             return [true, $filename];
             return [true, $filename];
         } catch (Exception $e) {
         } catch (Exception $e) {
+
             return [false, false];
             return [false, false];
         }
         }
     }
     }