stanley-king 3 years ago
parent
commit
6e61073730
1 changed files with 43 additions and 32 deletions
  1. 43 32
      helper/task/handler.php

+ 43 - 32
helper/task/handler.php

@@ -91,49 +91,60 @@ class handler
                     ->group('order_state')
                     ->select();
 
-                $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;
-                    }
+                if(empty($items)) continue;
 
-                    $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];
+                yield $items;
             }
         };
 
-        $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;
+
+        $summer = function ($items,&$result)
+        {
+            foreach ($items as $item)
+            {
+                $order_state = $item['order_state'];
+                if(!array_key_exists($order_state,$result)) {
+                    $val = [];
+                    $val['order_count'] = $item['order_count'];
+                    $val['refill_amounts'] = ncPriceFormat($item['refill_amounts']);
+                    $val['channel_amounts'] = ncPriceFormat($item['channel_amounts']);
+                    $val['mch_amounts'] = ncPriceFormat($item['mch_amounts']);
+
+                    $result[$order_state] = $val;
+                }
+                else {
+                    $result[$order_state]['order_count'] += $item['order_count'];
+                    $result[$order_state]['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
+                    $result[$order_state]['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
+                    $result[$order_state]['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
                 }
             }
-            foreach ($arr as $key => $value) {
-                $a[$key] = $a[$key] + $b[$key];
+        };
+
+        $all_summer = function ($records)
+        {
+            $result = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0];
+            foreach ($records as $order_state => $item) {
+                $result['order_count'] += $item['order_count'];
+                $result['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
+                $result['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
+                $result['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
             }
-            return $a;
+
+            return $result;
         };
 
-        try {
+
+        try
+        {
+            $records = [];
             $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']);
+            foreach ($stats as $items) {
+                $summer($items,$records);
             }
-            $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
+            $all = $all_summer($records);
+
+            $result = ['all' => $all, 'sending' => $records[ORDER_STATE_SEND], 'success' => $records[ORDER_STATE_SUCCESS], 'cancel' => $records[ORDER_STATE_CANCEL]];
             return [true, $result];
         } catch (Exception $ex) {
             return [false, false];