Parcourir la source

task orderstats update

xiaoyu il y a 3 ans
Parent
commit
dd2c36879a
3 fichiers modifiés avec 132 ajouts et 32 suppressions
  1. 2 1
      admin/control/refill_order.php
  2. 76 31
      helper/task/handler.php
  3. 54 0
      test/TestTask.php

+ 2 - 1
admin/control/refill_order.php

@@ -222,7 +222,8 @@ class refill_orderControl extends SystemControl
             [$start,$end] = $scope[$time_type];
             $period = $periodor($start,$end);
             $manager = new task\manager();
-            $task = $manager->add_task('refill_order_stat',$cond,0,$period);
+            $task = $manager->add_task('refill_order_stat', ['normal' => $normal,'time_scope' => $scope],0,$period);
+
 
             $task_id = $task->task_id();
             if($task->waiting()) {

+ 76 - 31
helper/task/handler.php

@@ -11,45 +11,90 @@ use statistics\stat_refill;
 
 class handler
 {
-    public function refill_order_stat($condition)
+    public function refill_order_stat($cond)
     {
-        try
+        $tmcond_gen = function ($time_scope,$cur_start,$cur_end)
         {
-            $items = Model('')->table('refill_order,vr_order')
-                ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
-                ->join('inner')
-                ->on('refill_order.order_id=vr_order.order_id')
-                ->where($condition)
-                ->group('order_state')
-                ->select();
-
-            $all = [];
-            $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
-            $sending = $success = $cancel = $data;
-            foreach ($items as $item)
+            $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
+
+            [$start, $end] = $time_scope['add_time'];
+            $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
+
+            return $cond;
+        };
+        $normal_cond = $cond['normal'];
+        $time_scope = $cond['time_scope'];
+
+        $order_reader = function ($normal_cond, $time_scope) use ($tmcond_gen)
+        {
+            [$start, $end] = $time_scope['order_time'];
+
+            for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
             {
-                if ($item['order_state'] == ORDER_STATE_SEND) {
-                    $sending = $item;
-                } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
-                    $success = $item;
-                } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
-                    $cancel = $item;
+                if ($cur_start + 3600 >= $end) {
+                    $cur_end = $end;
+                } else {
+                    $cur_end = $cur_start + 3600;
                 }
 
-                $all['order_count'] += $item['order_count'];
-                $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
-                $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
-                $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
-            }
+                $tmcond = $tmcond_gen($time_scope, $cur_start, $cur_end);
+                $cond = array_merge($normal_cond, $tmcond);
 
-            $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
+                $mod = Model();
+                $items = $mod->table('refill_order,vr_order')
+                    ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
+                    ->join('inner')
+                    ->on('refill_order.order_id=vr_order.order_id')
+                    ->where($cond)
+                    ->group('order_state')
+                    ->select();
 
-            return [true,$result];
+                $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
+                $all = $sending = $success = $cancel = $data;
+                foreach ($items as $item) {
+                    if ($item['order_state'] == ORDER_STATE_SEND) {
+                        $sending = $item;
+                    } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
+                        $success = $item;
+                    } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
+                        $cancel = $item;
+                    }
 
-        }
-        catch (Exception $ex)
-        {
-            return [false,false];
+                    $all['order_count'] += $item['order_count'];
+                    $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
+                    $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
+                    $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
+                }
+                yield ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
+            }
+        };
+
+        $array_add = function ($a, $b) {
+            $arr = array_intersect_key($a, $b);
+            foreach ($b as $key => $value) {
+                if (!array_key_exists($key, $a)) {
+                    $a[$key] = $value;
+                }
+            }
+            foreach ($arr as $key => $value) {
+                $a[$key] = $a[$key] + $b[$key];
+            }
+            return $a;
+        };
+
+        try {
+            $stats = $order_reader($normal_cond, $time_scope);
+            $all = $sending = $success = $cancel = [];
+            foreach ($stats as $stat) {
+                $all = $array_add($all, $stat['all']);
+                $sending = $array_add($sending, $stat['sending']);
+                $success = $array_add($success, $stat['success']);
+                $cancel = $array_add($cancel, $stat['cancel']);
+            }
+            $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
+            return [true, $result];
+        } catch (Exception $ex) {
+            return [false, false];
         }
     }
 

+ 54 - 0
test/TestTask.php

@@ -1,6 +1,7 @@
 <?php declare(strict_types=1);
 
 use PHPUnit\Framework\TestCase;
+use task\handler;
 
 define('APP_ID', 'test');
 define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
@@ -8,6 +9,7 @@ define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
 require_once(BASE_ROOT_PATH . '/global.php');
 require_once(BASE_CORE_PATH . '/lrlz.php');
 require_once(BASE_ROOT_PATH . '/fooder.php');
+require_once(BASE_HELPER_PATH . '/task/task_helper.php');
 
 class TestTask extends TestCase
 {
@@ -69,4 +71,56 @@ class TestTask extends TestCase
         $ret = $gen->send('ret2');
         Log::record($ret,Log::DEBUG);
     }
+
+    public function testOrderStats()
+    {
+        $normal = [
+            'inner_status' => 0,
+            'refill_order.mchid' => 1093,
+            'vr_order.order_state' => 40
+        ];
+        $scope = [
+            'order_time' => [1617206400, 1617724800],
+            'add_time' => [1617206400, 1618156800]
+        ];
+        $handler = new handler();
+        [$succ,$result] = $handler->refill_order_stat(['normal' => $normal, 'time_scope' => $scope]);
+    }
+
+    public function testArrayMerge()
+    {
+        function array_add($a, $b)
+        {
+            $arr = array_intersect_key($a, $b);
+            foreach ($b as $key => $value) {
+                if (!array_key_exists($key, $a)) {
+                    $a[$key] = $value;
+                }
+            }
+            foreach ($arr as $key => $value) {
+                $a[$key] = $a[$key] + $b[$key];
+            }
+            return $a;
+        }
+        $a = [
+            'order_count' => 0,
+            'refill_amounts' => 0,
+            'channel_amounts' => 0,
+            'mch_amounts' => 0
+        ];
+        $b = [
+            'order_count' => 1,
+            'refill_amounts' => 2,
+            'channel_amounts' => 3,
+            'mch_amounts' => 4
+        ];
+        $c = array_add($a,$b);
+        $d = [
+            'order_count' => 4,
+            'refill_amounts' => 3,
+            'channel_amounts' => 2,
+            'mch_amounts' => 1
+        ];
+        $e = array_add($c,$d);
+    }
 }