Browse Source

历史汇总数据脚本

sunhao 5 years ago
parent
commit
391156f2e9
2 changed files with 103 additions and 0 deletions
  1. 102 0
      app/Console/Commands/DayGrandTotalHistory.php
  2. 1 0
      app/Console/Kernel.php

+ 102 - 0
app/Console/Commands/DayGrandTotalHistory.php

@@ -0,0 +1,102 @@
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
+use App\Order;
9
+class DayGrandTotalHistory extends Command {
10
+
11
+    protected $signature = 'DayGrandTotalHistory';
12
+
13
+    /**
14
+     * The console command description.
15
+     *
16
+     * @var string
17
+     */
18
+    protected $description = '每日汇总投放,成本,毛利等数据';
19
+
20
+
21
+    public function handle()
22
+    {
23
+        $this->dayGrandTotal();
24
+    }
25
+
26
+    
27
+    public function dayGrandTotal(){
28
+        $_start = '2019-09-04';
29
+        for($i=79;$i>=1;$i--){
30
+            $m = '-'.($i-1).' day';
31
+            $n = '-'.$i.' day';
32
+            $_end = date('Y-m-d', strtotime($m));
33
+            $idate = date('Y-m-d', strtotime($n));
34
+
35
+            $data = array();
36
+            //查看是否已有数据
37
+            $if_e = DB::table('day_grand_total')->where('idate', $idate)->first();
38
+            if(!empty($if_e)){
39
+                echo "\n日期:".$idate." 已存在";
40
+                return false;
41
+            }
42
+            $data['idate'] = $idate;
43
+            //总投入
44
+            $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('total_cost');
45
+            //订单信息
46
+            $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('is_del',0)->where('createTime','<',$_end)->where('createTime','>=',$_start)->first();
47
+            $data['goods_cost'] = $order->goods_cost;
48
+            $data['freight_cost'] = $order->freight_cost;
49
+            $data['aftersale_cost'] = $order->aftersale_cost;
50
+            $data['refund_fee'] = $order->refund_fee;
51
+            $data['order_count'] = $order->order_count;
52
+            $data['order_amount'] = $order->order_amount;
53
+            $data['cust_count'] = $order->cust_count;
54
+            //加粉
55
+            $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');    
56
+            //毛利 = 总销售额-总商品成本-总运费-总售后
57
+            $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
58
+            //总新粉单数            
59
+            $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.createTime','<',$_end)->where('order.createTime','>=',$_start)->first();
60
+            $data['new_order_count'] = $new_order->order_count;
61
+            //总老粉单数
62
+            $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
63
+            //总复购订单
64
+            $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
65
+
66
+            //7 - 60 roi汇总
67
+
68
+            $s7 = $idate. ' -7 day';
69
+            $date = date('Y-m-d', strtotime($s7));
70
+            $data['cost7'] = DB::table('roi_total')->where('type', 1)->where('ad_time', '<=', $date)->sum('cost');
71
+            $data['order_count7'] = DB::table('roi_total')->where('type', 1)->where('ad_time', '<=', $date)->sum('order_count');
72
+            $data['order_amount7'] = DB::table('roi_total')->where('type', 1)->where('ad_time', '<=', $date)->sum('order_amount');
73
+
74
+            $s15 = $idate. ' -15 day';
75
+            $date = date('Y-m-d', strtotime($s15));
76
+            $data['cost15'] = DB::table('roi_total')->where('type', 2)->where('ad_time', '<=', $date)->sum('cost');
77
+            $data['order_count15'] = DB::table('roi_total')->where('type', 2)->where('ad_time', '<=', $date)->sum('order_count');
78
+            $data['order_amount15'] = DB::table('roi_total')->where('type', 2)->where('ad_time', '<=', $date)->sum('order_amount');
79
+
80
+            $s30 = $idate. ' -30 day';
81
+            $date = date('Y-m-d', strtotime($s30));
82
+            $data['cost30'] = DB::table('roi_total')->where('type', 3)->where('ad_time', '<=', $date)->sum('cost');
83
+            $data['order_count30'] = DB::table('roi_total')->where('type', 3)->where('ad_time', '<=', $date)->sum('order_count');
84
+            $data['order_amount30'] = DB::table('roi_total')->where('type', 3)->where('ad_time', '<=', $date)->sum('order_amount');
85
+
86
+            $s45 = $idate. ' -45 day';
87
+            $date = date('Y-m-d', strtotime($s45));
88
+            $data['cost45'] = DB::table('roi_total')->where('type', 4)->where('ad_time', '<=', $date)->sum('cost');
89
+            $data['order_count45'] = DB::table('roi_total')->where('type', 4)->where('ad_time', '<=', $date)->sum('order_count');
90
+            $data['order_amount45'] = DB::table('roi_total')->where('type', 4)->where('ad_time', '<=', $date)->sum('order_amount');
91
+
92
+            $s60 = $idate. ' -60 day';
93
+            $date = date('Y-m-d', strtotime($s60));
94
+            $data['cost60'] = DB::table('roi_total')->where('type', 5)->where('ad_time', '<=', $date)->sum('cost');
95
+            $data['order_count60'] = DB::table('roi_total')->where('type', 5)->where('ad_time', '<=', $date)->sum('order_count');
96
+            $data['order_amount60'] = DB::table('roi_total')->where('type', 5)->where('ad_time', '<=', $date)->sum('order_amount');
97
+            $res = DB::table('day_grand_total')->insert($data);
98
+            echo "\n日期:".$idate." 插入结果:".$res;
99
+        }
100
+    }
101
+
102
+}

+ 1 - 0
app/Console/Kernel.php

@@ -34,6 +34,7 @@ class Kernel extends ConsoleKernel {
34 34
         'App\Console\Commands\DisRoiByDay60',
35 35
         'App\Console\Commands\SyncCustSaler',
36 36
         'App\Console\Commands\DayGrandTotal',
37
+        'App\Console\Commands\DayGrandTotalHistory',
37 38
         'App\Console\Commands\AfreshOrderCost',
38 39
         'App\Console\Commands\RoiTotal',
39 40
         'App\Console\Commands\RoiTotalHistory',