sunhao 5 years ago
parent
commit
e053e4a698
1 changed files with 102 additions and 0 deletions
  1. 102 0
      app/Console/Commands/RoiSalerTotalHistory.php

+ 102 - 0
app/Console/Commands/RoiSalerTotalHistory.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
+class RoiSalerTotalHistory extends Command {
9
+
10
+    protected $signature = 'RoiSalerTotalHistory';
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->RoiSalerTotalHistory();
23
+    }
24
+    public function RoiSalerTotalHistory(){
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=69; $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
+                $custTotal = CustTotal::select('total_cost', 'total_fan_add', 'team_id')->whereIn('team_id', [1,3,5])->where('is_del',0)->where('dtime', $stime)->get();
43
+                $custTotal = json_decode(json_encode($custTotal), true);  
44
+
45
+                foreach($custTotal as $k=>$v){
46
+                    $saler_ids = $salerIds[$v['team_id']];
47
+
48
+                    foreach($result as $k=>$v){
49
+                        $data = array();
50
+                        $data['ad_time'] = $stime;  
51
+                        $data['type'] = $i;            
52
+                        //新粉收入
53
+                        //当日加粉
54
+                        $phones = DB::table('customers')->where('fanTime', $stime)->lists('phone');
55
+                        #当日加粉15日订单总计:
56
+                        $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();
57
+                        #当日新粉成单:            
58
+                        $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();
59
+                       
60
+                        // 1.当日新粉成单数
61
+                        $data['new_fan_order_count'] = $new_order->order_count;
62
+                        // 2.当日新粉成交额 
63
+                        $data['new_fan_order_amount'] = $new_order->order_amount;
64
+                        // 3.当日粉丝总成交额
65
+                        $data['order_amount'] = $order->order_amount;
66
+                        // 15日总单数
67
+                        $data['order_count'] = $order->order_count;
68
+
69
+                        $data['cust_count'] = $order->cust_count;
70
+
71
+                        //销售
72
+                        $saler_ids = $salerIds[$v['team_id']];
73
+                        $data['fan_count'] = CustDetail::where('dtime', $stime)->where('is_del', 0)->whereIn('admin_id', $saler_ids)->sum('fan_add');
74
+                        $data['gzh_count'] = $v['total_fan_add'];
75
+                        $data['cost'] = $v['total_cost'];
76
+                        $data['team_id'] = $v['team_id'];
77
+                        //插入数据
78
+                        $res = $this->insertData($data);
79
+                    }
80
+                }
81
+                
82
+            }
83
+        }
84
+    }
85
+
86
+    /**
87
+     * @param idate:统计日期 rate:成单率 date:昨日
88
+     *
89
+     */
90
+    public function insertData($data){        
91
+        return DB::table('roi_team_total')->insert($data);
92
+    }
93
+
94
+    public function getTeamSalers(){
95
+        $data = array();
96
+        $data[1] = DB::table('admin')->where('team_id', 1)->lists('id');
97
+        $data[3] = DB::table('admin')->where('team_id', 3)->lists('id');
98
+        $data[5] = DB::table('admin')->where('team_id', 5)->lists('id');
99
+        return $data;
100
+    }
101
+
102
+}