Browse Source

60日roi脚本

sunhao 5 years ago
parent
commit
604e0a5048
2 changed files with 76 additions and 0 deletions
  1. 75 0
      app/Console/Commands/DisRoiByDay60.php
  2. 1 0
      app/Console/Kernel.php

+ 75 - 0
app/Console/Commands/DisRoiByDay60.php

@@ -0,0 +1,75 @@
1
+<?php 
2
+namespace App\Console\Commands;
3
+
4
+use Illuminate\Console\Command;
5
+use DB;
6
+use App\AdCost;
7
+class DisRoiByDay60 extends Command {
8
+
9
+    protected $signature = 'DisRoiByDay60';
10
+
11
+    /**
12
+     * The console command description.
13
+     *
14
+     * @var string
15
+     */
16
+    protected $description = '地域60日roi';
17
+
18
+
19
+    public function handle()
20
+    {
21
+        $this->DisRoiByDay60();
22
+    }
23
+    public function DisRoiByDay60(){
24
+
25
+            $stime = date('Y-m-d', strtotime('-60 day'));   
26
+            $etime = $date = date('Y-m-d');
27
+            //计算date 60天内的roi    
28
+            $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where('ad_time', $stime)->groupBy('city')->get();
29
+            if(empty($result)){
30
+                return false;
31
+            }
32
+            $result = json_decode(json_encode($result), true);           
33
+            foreach($result as $k=>&$v){
34
+                $data = array();
35
+                $data['ad_time'] = $stime;
36
+                $data['city'] = $v['city'];               
37
+                //新粉收入
38
+                //当日加粉
39
+                $city_name = str_replace('市', '', $v['city']);
40
+                $phones = DB::table('customers')->where('fanTime', $stime)->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
41
+                #当日加粉60日订单总计:
42
+                $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('createTime', '>=', $stime)->where('createTime', '<', $etime)->where('is_del', 0)->first();
43
+                #当日新粉成单:            
44
+                $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();
45
+               
46
+                // 1.当日新粉成单数
47
+                $data['new_fan_order_count'] = $new_order->order_count;
48
+                // 2.当日新粉成交额 
49
+                $data['new_fan_order_amount'] = $new_order->order_amount;
50
+                // 3.当日粉丝总成交额
51
+                $data['order_amount'] = $order->order_amount;
52
+                // 60日总单数
53
+                $data['order_count'] = $order->order_count;
54
+                //新粉roi
55
+                //$v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
56
+                //60日累计roi
57
+                //$v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
58
+                $data['fan_count'] = count($phones);
59
+                $data['gzh_count'] = $v['conversion_times'];
60
+                $data['cost'] = $v['total_cost'];
61
+                //插入数据
62
+                $res = $this->insertData($data);
63
+            }
64
+  
65
+    }
66
+
67
+    /**
68
+     * @param idate:统计日期 rate:成单率 date:昨日
69
+     *
70
+     */
71
+    public function insertData($data){        
72
+        return DB::table('district_roi_60')->insert($data);
73
+    }
74
+
75
+}

+ 1 - 0
app/Console/Kernel.php

@@ -31,6 +31,7 @@ class Kernel extends ConsoleKernel {
31 31
         'App\Console\Commands\SyncMjOrderStatus',
32 32
         'App\Console\Commands\SyncMjWarehouse',
33 33
         'App\Console\Commands\SyncOrderToMj',
34
+        'App\Console\Commands\DisRoiByDay60',
34 35
 
35 36
         
36 37
     ];