sunhao 5 years ago
parent
commit
40b9bd94ca
2 changed files with 84 additions and 0 deletions
  1. 83 0
      app/Console/Commands/RoiTotalHistory.php
  2. 1 0
      app/Console/Kernel.php

+ 83 - 0
app/Console/Commands/RoiTotalHistory.php

@@ -0,0 +1,83 @@
1
+<?php 
2
+namespace App\Console\Commands;
3
+
4
+use Illuminate\Console\Command;
5
+use DB;
6
+use App\CustTotal;
7
+use App\CustDetail;
8
+class RoiTotalHistory extends Command {
9
+
10
+    protected $signature = 'RoiTotalHistory';
11
+
12
+    /**
13
+     * The console command description.
14
+     *
15
+     * @var string
16
+     */
17
+    protected $description = 'Roi汇总历史';
18
+
19
+
20
+    public function handle()
21
+    {
22
+        $this->RoiTotalHistory();
23
+    }
24
+    public function RoiTotalHistory(){
25
+        $timeArr = array(
26
+            1 => '7',
27
+            2 => '15',
28
+            3 => '30',
29
+            4 => '45',
30
+            5 => '60',
31
+        );
32
+        for($i=1;$i<6;$i++){
33
+            $j_min = $timeArr[$i];
34
+            for($j=64; $j>=$j_min; $j--){
35
+                $stime = date('Y-m-d', strtotime('-'.$j.' day'));
36
+                $e = $j-$j_min; 
37
+                $etime = $date = date('Y-m-d', strtotime('-'.$e.' day'));
38
+
39
+               //计算date i天内的roi    
40
+                $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add'))->where('is_del',0)->where('dtime', $stime)->first();
41
+
42
+                $result = json_decode(json_encode($result), true);           
43
+                
44
+                $data = array();
45
+                $data['ad_time'] = $stime;  
46
+                $data['type'] = $i;            
47
+                //新粉收入
48
+                //当日加粉
49
+                $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
50
+                #当日加粉15日订单总计:
51
+                $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, count(distinct(receiverMobile)) as cust_count'))->whereIn('receiverMobile', $phones)->where('createTime', '>=', $stime)->where('createTime', '<', $etime)->where('is_del', 0)->first();
52
+                #当日新粉成单:            
53
+                $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $stime)->where('createTime','<=', $stime.' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
54
+               
55
+                // 1.当日新粉成单数
56
+                $data['new_fan_order_count'] = $new_order->order_count;
57
+                // 2.当日新粉成交额 
58
+                $data['new_fan_order_amount'] = $new_order->order_amount;
59
+                // 3.当日粉丝总成交额
60
+                $data['order_amount'] = $order->order_amount;
61
+                // 15日总单数
62
+                $data['order_count'] = $order->order_count;
63
+
64
+                $data['cust_count'] = $order->cust_count;
65
+                $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->sum('fan_add');
66
+                $data['gzh_count'] = $result['total_fan_add'];
67
+                $data['cost'] = $result['total_cost'];
68
+                //插入数据
69
+                $res = $this->insertData($data);
70
+                
71
+            }
72
+        }
73
+    }
74
+
75
+    /**
76
+     * @param idate:统计日期 rate:成单率 date:昨日
77
+     *
78
+     */
79
+    public function insertData($data){        
80
+        return DB::table('roi_total')->insert($data);
81
+    }
82
+
83
+}

+ 1 - 0
app/Console/Kernel.php

@@ -36,6 +36,7 @@ class Kernel extends ConsoleKernel {
36 36
         'App\Console\Commands\DayGrandTotal',
37 37
         'App\Console\Commands\AfreshOrderCost',
38 38
         'App\Console\Commands\RoiTotal',
39
+        'App\Console\Commands\RoiTotalHistory',
39 40
 
40 41
         
41 42
     ];