sunhao преди 5 години
родител
ревизия
1c3743987e
променени са 3 файла, в които са добавени 203 реда и са изтрити 0 реда
  1. 103 0
      app/Console/Commands/RoiTeamTotal.php
  2. 98 0
      app/Console/Commands/RoiTeamTotalHistory.php
  3. 2 0
      app/Console/Kernel.php

+ 103 - 0
app/Console/Commands/RoiTeamTotal.php

@@ -0,0 +1,103 @@
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 RoiTeamTotal extends Command {
9
+
10
+    protected $signature = 'RoiTeamTotal';
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->RoiTeamTotal();
23
+    }
24
+    public function RoiTeamTotal(){
25
+        $timeArr = array(
26
+            1 => '-7 day',
27
+            2 => '-15 day',
28
+            3 => '-30 day',
29
+            4 => '-45 day',
30
+            5 => '-60 day',
31
+        );
32
+        //获取各个团队销售id
33
+        $salerIds = $this->getTeamSalers();
34
+        for($i=1;$i<6;$i++){
35
+            $stime = date('Y-m-d', strtotime($timeArr[$i]));   
36
+            $etime = $date = date('Y-m-d');
37
+            //计算date i天内的roi    
38
+            $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, team_id'))->whereIn('team_id', [1,3,5])->where('is_del',0)->where('dtime', $stime)->groupBy('team_id')->get();
39
+
40
+            $result = json_decode(json_encode($result), true);           
41
+            
42
+            foreach($result as $k=>$v){
43
+                $data = array();
44
+                $data['ad_time'] = $stime;  
45
+                $data['type'] = $i;            
46
+                //新粉收入
47
+                //当日加粉
48
+                $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');               
49
+                #当日加粉n日订单总计:
50
+                $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)->where('team_id', $v['team_id'])->first();
51
+                #当日新粉成单:            
52
+                $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)->where('team_id', $v['team_id'])->first();
53
+               
54
+                if(!empty($new_order)){
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
+                }else{
60
+                    $data['new_fan_order_count'] = 0;
61
+                    $data['new_fan_order_amount'] = 0;
62
+                }
63
+                if(!empty($order)){
64
+                    // 3.当日粉丝总成交额
65
+                    $data['order_amount'] = $order->order_amount;
66
+                    // 15日总单数
67
+                    $data['order_count'] = $order->order_count;
68
+                    $data['cust_count'] = $order->cust_count;
69
+                }else{
70
+                    $data['order_amount'] = 0;
71
+                    $data['order_count'] = 0;
72
+                    $data['cust_count'] = 0;
73
+                }
74
+                //销售
75
+                $saler_ids = $salerIds[$v['team_id']];
76
+                $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
77
+                $data['gzh_count'] = $v['total_fan_add'];
78
+                $data['cost'] = $v['total_cost'];
79
+                $data['team_id'] = $v['team_id'];
80
+                //插入数据
81
+                $res = $this->insertData($data);
82
+            }
83
+        }
84
+  
85
+    }
86
+
87
+    /**
88
+     * @param idate:统计日期 rate:成单率 date:昨日
89
+     *
90
+     */
91
+    public function insertData($data){        
92
+        return DB::table('roi_team_total')->insert($data);
93
+    }
94
+
95
+    public function getTeamSalers(){
96
+        $data = array();
97
+        $data[1] = DB::table('admin')->where('team_id', 1)->lists('id');
98
+        $data[3] = DB::table('admin')->where('team_id', 3)->lists('id');
99
+        $data[5] = DB::table('admin')->where('team_id', 5)->lists('id');
100
+        return $data;
101
+    }
102
+
103
+}

+ 98 - 0
app/Console/Commands/RoiTeamTotalHistory.php

@@ -0,0 +1,98 @@
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 RoiTeamTotalHistory extends Command {
9
+
10
+    protected $signature = 'RoiTeamTotalHistory';
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->RoiTeamTotalHistory();
23
+    }
24
+    public function RoiTeamTotalHistory(){
25
+        $timeArr = array(
26
+            1 => '7',
27
+            2 => '15',
28
+            3 => '30',
29
+            4 => '45',
30
+            5 => '60',
31
+        );
32
+        //获取各个团队销售id
33
+        $salerIds = $this->getTeamSalers();
34
+        for($i=1;$i<6;$i++){
35
+            $j_min = $timeArr[$i];
36
+            for($j=64; $j>=$j_min; $j--){
37
+                $stime = date('Y-m-d', strtotime('-'.$j.' day'));
38
+                $e = $j-$j_min; 
39
+                $etime = $date = date('Y-m-d', strtotime('-'.$e.' day'));
40
+
41
+               //计算date i天内的roi    
42
+                $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add'))->whereIn('team_id', [1,3,5])->where('is_del',0)->where('dtime', $stime)->groupBy('team_id')->get();
43
+
44
+                $result = json_decode(json_encode($result), true);           
45
+                foreach($result as $k=>$v){
46
+                    $data = array();
47
+                    $data['ad_time'] = $stime;  
48
+                    $data['type'] = $i;            
49
+                    //新粉收入
50
+                    //当日加粉
51
+                    $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
52
+                    #当日加粉15日订单总计:
53
+                    $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)->where('team_id', $v['team_id'])->first();
54
+                    #当日新粉成单:            
55
+                    $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)->where('team_id', $v['team_id'])->first();
56
+                   
57
+                    // 1.当日新粉成单数
58
+                    $data['new_fan_order_count'] = $new_order->order_count;
59
+                    // 2.当日新粉成交额 
60
+                    $data['new_fan_order_amount'] = $new_order->order_amount;
61
+                    // 3.当日粉丝总成交额
62
+                    $data['order_amount'] = $order->order_amount;
63
+                    // 15日总单数
64
+                    $data['order_count'] = $order->order_count;
65
+
66
+                    $data['cust_count'] = $order->cust_count;
67
+
68
+                    //销售
69
+                    $saler_ids = $salerIds[$v['team_id']];
70
+                    $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
71
+                    $data['gzh_count'] = $v['total_fan_add'];
72
+                    $data['cost'] = $v['total_cost'];
73
+                    $data['team_id'] = $v['team_id'];
74
+                    //插入数据
75
+                    $res = $this->insertData($data);
76
+                }
77
+                
78
+            }
79
+        }
80
+    }
81
+
82
+    /**
83
+     * @param idate:统计日期 rate:成单率 date:昨日
84
+     *
85
+     */
86
+    public function insertData($data){        
87
+        return DB::table('roi_team_total')->insert($data);
88
+    }
89
+
90
+    public function getTeamSalers(){
91
+        $data = array();
92
+        $data[1] = DB::table('admin')->where('team_id', 1)->lists('id');
93
+        $data[3] = DB::table('admin')->where('team_id', 3)->lists('id');
94
+        $data[5] = DB::table('admin')->where('team_id', 5)->lists('id');
95
+        return $data;
96
+    }
97
+
98
+}

+ 2 - 0
app/Console/Kernel.php

@@ -38,6 +38,8 @@ class Kernel extends ConsoleKernel {
38 38
         'App\Console\Commands\RoiTotal',
39 39
         'App\Console\Commands\RoiTotalHistory',
40 40
         'App\Console\Commands\DayGrandRoiHistory',
41
+        'App\Console\Commands\RoiTeamTotalHistory',
42
+        'App\Console\Commands\RoiTeamTotal',
41 43
 
42 44
         
43 45
     ];