Browse Source

update home stats

ayHaru 4 years ago
parent
commit
e8383e717f
1 changed files with 90 additions and 43 deletions
  1. 90 43
      mobile/control/merchant_info.php

+ 90 - 43
mobile/control/merchant_info.php

@@ -49,64 +49,111 @@ class merchant_infoControl extends mbMerchantControl
         $statistics = $this->statistics();
         $statistics = $this->statistics();
 
 
         $result['merchant_info'] = $merchant_info;
         $result['merchant_info'] = $merchant_info;
-        $result['todayStatistics'] = $statistics[6];
+        $result['todayStatistics'] = $statistics[date("Y-m-d")];
         $result['weeksStatistics'] = $statistics;
         $result['weeksStatistics'] = $statistics;
         return self::outsuccess($result);
         return self::outsuccess($result);
     }
     }
 
 
     private function statistics()
     private function statistics()
     {
     {
-        $cond['mchid'] = $this->mchid();
-        $cond['inner_status'] = 0;
-        $begin_time = $this->date_time();
-        $cond['order_time'] = ['egt', $begin_time];
-
-        $result = [];
-        for ($ftime = $begin_time, $i = 0; $ftime < time(); $ftime += 86400, $i += 1) {
-            $result[$i] = ['count' => 0, 'successCount' => 0, 'errorCount' => 0, 'otherCount' => 0, 'amountCount' => 0];
-        }
-
-
-        $model_refill_order = Model('refill_order');
-
-        $i = 0;
-        while (true)
-        {
-            $start = $i * 10000;
-            $orders = $model_refill_order->getMerchantOrderList($cond,'','refill_order.order_id,refill_order.order_time,refill_order.mch_amount,vr_order.order_state', 'refill_order.order_id desc',"{$start},10000");
-            $i++;
-
-            if (empty($orders)) {
-                break;
+        $result = $this->datarcache(rcache($this->mchid() , 'refillstat-'));
+        if(empty($result)) {
+            for ($i = 6; $i > 0; $i--) {
+                $begin = mktime(0, 0, 0, date('m'), date('d') - $i, date('Y'));
+                $end = mktime(0, 0, 0, date('m'), date('d') - $i + 1, date('Y')) - 1;
+                $cond['refill_order.order_time'] = ['between', [$begin, $end]];
+                $stats[date("Y-m-d",$begin)] = $this->refillStatistics($cond);
+            }
+            $result = $this->refillCountDispose($stats);
+            wcache($this->mchid(),$this->datawcache($result),'refillstat-');
+            $begin = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
+            $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
+            $cond['refill_order.order_time'] = ['between', [$begin, $end]];
+            $stats[date("Y-m-d",$begin)] = $this->refillStatistics($cond);
+            $result = $this->refillCountDispose($stats);
+        }else{
+            ksort($result);
+            //检测前六天缓存数据
+            for ($i = 1; $i < 7; $i++) {
+                $dates[] = date("Y-m-d" , strtotime("-{$i}day"));
+            }
+            foreach ($dates as $date) {
+                //前六天不存在的重新读表缓存
+                if(empty($result[$date])) {
+                    $begin = strtotime($date);
+                    $end = strtotime($date) + 86400 - 1;
+                    $cond['refill_order.order_time'] = ['between', [$begin, $end]];
+                    $data[$date] = $this->refillStatistics($cond);
+                    $result = array_merge($result);
+                    wcache($this->mchid(),$this->datawcache($result),'refillstat-');
+                }
             }
             }
 
 
-            $days_calc = function ($order_time) use ($begin_time) {
-                $period = $order_time - $begin_time;
-                $days = intval($period / 86400);
-                return $days;
-            };
-
-            foreach ($orders as $order)
-            {
-                $order_day = $days_calc($order['order_time']);
+            $begin = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
+            $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
+            $cond['refill_order.order_time'] = ['between', [$begin, $end]];
+            $stats[date("Y-m-d",$begin)] = $this->refillStatistics($cond);
+            $todaycount = $this->refillCountDispose($stats);
+            $result = array_merge($result , $todaycount);
+        }
+        return $result;
+    }
 
 
-                $result[$order_day]['count'] += 1;
-                if ($order['order_state'] == ORDER_STATE_CANCEL) {
-                    $result[$order_day]['errorCount'] += 1;
-                } elseif ($order['order_state'] == ORDER_STATE_SUCCESS) {
-                    $result[$order_day]['successCount'] += 1;
-                    $result[$order_day]['amountCount'] +=  ncPriceFormat($order['mch_amount']);
-                } else {
-                    $result[$order_day]['otherCount'] += 1;
+    private function refillStatistics($cond)
+    {
+        $cond['mchid'] = $this->mchid();
+        $cond['inner_status'] = 0;
+        $stat = Model('')->table('refill_order,vr_order')->join('inner')
+            ->on('refill_order.order_id=vr_order.order_id')
+            ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
+            ->group('order_state')
+            ->where($cond)->select();
+        return $stat;
+    }
+    private function refillCountDispose($stats)
+    {
+        foreach ($stats as $date => $stat) {
+            $result[$date]['count'] = $result[$date]['otherCount'] = $result[$date]['errorCount'] = $result[$date]['successCount'] = $result[$date]['amountCount'] = 0;
+            $result[$date]['is_cache'] = true;
+            foreach ($stat as $count) {
+                if($count['order_state'] == ORDER_STATE_SEND) {
+                    $result[$date]['otherCount'] = $count['order_count'];
+                    $result[$date]['count'] ++;
+                    $result[$date]['is_cache'] = false;
+                }
+                if($count['order_state'] == ORDER_STATE_CANCEL) {
+                    $result[$date]['errorCount'] = $count['order_count'];
+                    $result[$date]['count'] ++;
+                }
+                if($count['order_state'] == ORDER_STATE_SUCCESS) {
+                    $result[$date]['successCount'] = $count['order_count'];
+                    $result[$date]['amountCount'] = $count['mch_amounts'];
+                    $result[$date]['count'] ++;
                 }
                 }
             }
             }
+        }
+        return $result;
+    }
 
 
-            if (count($orders) < 10000) {
-                break;
+    private function datawcache($data)
+    {
+        foreach ($data as $key => $value)
+        {
+            if(!$value['is_cache']) {
+                continue;
             }
             }
+            $cache[$key] = serialize($value);
         }
         }
+        return $cache;
+    }
 
 
-        return $result;
+    private function datarcache($data)
+    {
+        foreach ($data as $key => $value)
+        {
+            $data[$key] = unserialize($value);
+        }
+        return $data;
     }
     }
 
 
     private function date_time($days = 6)
     private function date_time($days = 6)