Quellcode durchsuchen

分团队成单率

sunhao vor 5 Jahren
Ursprung
Commit
3ceee7f440

+ 92 - 0
app/Console/Commands/TeamOrderRateByDay.php

@@ -0,0 +1,92 @@
1
+<?php 
2
+namespace App\Console\Commands;
3
+
4
+use Illuminate\Console\Command;
5
+use DB;
6
+class TeamOrderRateByDay extends Command {
7
+
8
+    protected $signature = 'TeamOrderRateByDay';
9
+
10
+    /**
11
+     * The console command description.
12
+     *
13
+     * @var string
14
+     */
15
+    protected $description = '每日团队综合成单率日志脚本';
16
+
17
+
18
+    public function handle()
19
+    {
20
+        $this->TeamOrderRateByDay();
21
+    }
22
+    public function TeamOrderRateByDay(){
23
+        $team_ids = [1,3,5];
24
+        foreach($team_ids as $team_id){
25
+            #团队成员:
26
+            $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
27
+
28
+            #昨天日期:
29
+            $date = date('Y-m-d', strtotime('-1 day'));
30
+            //第一步: 统计昨日数据
31
+            #昨日加粉 and 昨日成单 
32
+            $phones = DB::table('customers')->where('fanTime', $date)->lists('phone');
33
+            $order_count = DB::table('order')->where('team_id', $team_id)->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime','<=',$date.' 23:59:59')->where('is_del', 0)->count();
34
+            #昨日加粉
35
+            $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $date)->sum('fan_add');
36
+            #计算昨日成单率
37
+            $rate = $fan_add>0 ? round($order_count / $fan_add, 4) * 100  : 0;
38
+            //存入数据表
39
+            $result = $this->updateData($date, $rate, $date, $team_id);
40
+            echo "\nDate:".$date." fan:".$fan_add." order_count:".$order_count." rate:".$rate." 处理结果:".$result;
41
+            
42
+            //第二布:统计往期数据
43
+            #查日期列表
44
+            $dates = DB::table('team_cust_day_remain')->select('idate')->where('team_id', $team_id)->where('idate','<', $date)->groupBy('idate')->orderBy('idate', 'desc')->get();
45
+            if(!empty($dates)){
46
+                $dates = json_decode(json_encode($dates), true);
47
+                foreach($dates as $k=>$v){
48
+                    #当前日期加粉 and 昨日成单 
49
+                    $phones = DB::table('customers')->where('fanTime', $v['idate'])->lists('phone');
50
+                    $order_count = DB::table('order')->where('team_id', $team_id)->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime','<=',$date.' 23:59:59')->where('is_del', 0)->count();
51
+                    #当前日期加粉
52
+                    $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $v['idate'])->sum('fan_add');
53
+                    #计算当前日期加粉在昨日成单率
54
+                    $rate = $fan_add>0 ? round($order_count / $fan_add, 4) * 100  : 0;
55
+                    //存入数据表
56
+                    $result = $this->updateData($v['idate'], $rate, $date, $team_id);
57
+                    echo "\nDate:".$v['idate']." fan:".$fan_add." order_count:".$order_count." rate:".$rate." 处理结果:".$result;
58
+                }
59
+            }            
60
+        }
61
+    }
62
+
63
+    /**
64
+     * @param idate:统计日期 rate:成单率 date:昨日
65
+     *
66
+     */
67
+    public function updateData($idate, $rate, $date, $team_id){
68
+        #当前月
69
+        $month = date('Y-m', strtotime($date));
70
+        #当月1号
71
+        $month_first = date('Y-m-01', strtotime($date));
72
+        $data = array();
73
+        #如果是昨天的数据(或昨天是当月一号)直接插入
74
+        if($idate == $date || $date == $month_first){
75
+            $data['team_id'] = $team_id;
76
+            $data['idate'] = $idate;
77
+            $data['month_time'] = $month;
78
+            $data['order_rate_data'] = json_encode([0=>['date'=>$date, 'rate'=>$rate]]);
79
+            $res = DB::table('team_cust_day_remain')->insert($data);
80
+        }else{
81
+            //往期成单
82
+            $odata = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('idate', $idate)->where('month_time', $month)->first();
83
+            $order_rate_data = json_decode($odata->order_rate_data, true);
84
+            $order_rate_data[] = ['date'=>$date, 'rate'=>$rate];
85
+            $data['order_rate_data'] = json_encode($order_rate_data);
86
+            $res = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('idate', $idate)->where('month_time', $month)->update($data);
87
+        }
88
+
89
+        return $res;
90
+    }
91
+
92
+}

+ 93 - 0
app/Console/Commands/TeamOrderRateByDayHistory.php

@@ -0,0 +1,93 @@
1
+<?php 
2
+namespace App\Console\Commands;
3
+
4
+use Illuminate\Console\Command;
5
+use DB;
6
+class TeamOrderRateByDayHistory extends Command {
7
+
8
+    protected $signature = 'TeamOrderRateByDayHistory';
9
+
10
+    /**
11
+     * The console command description.
12
+     *
13
+     * @var string
14
+     */
15
+    protected $description = '团队综合成单率日志脚本';
16
+
17
+
18
+    public function handle()
19
+    {
20
+        $this->TeamOrderRateByDayHistory();
21
+    }
22
+    public function TeamOrderRateByDayHistory(){
23
+        $team_ids = [1,3,5];
24
+        foreach($team_ids as $team_id){
25
+            #团队成员:
26
+            $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
27
+            for($i=70; $i>1; $i--){
28
+                #昨天日期:
29
+                $date = date('Y-m-d', strtotime('-'.$i.' day'));
30
+                //第一步: 统计昨日数据
31
+                #昨日加粉 and 昨日成单 
32
+                $phones = DB::table('customers')->where('fanTime', $date)->lists('phone');
33
+                $order_count = DB::table('order')->where('team_id', $team_id)->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime','<=',$date.' 23:59:59')->where('is_del', 0)->count();
34
+                #昨日加粉
35
+                $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $date)->sum('fan_add');
36
+                #计算昨日成单率
37
+                $rate = $fan_add>0 ? round($order_count / $fan_add, 4) * 100  : 0;
38
+                //存入数据表
39
+                $result = $this->updateData($date, $rate, $date, $team_id);
40
+                echo "\nDate:".$date." fan:".$fan_add." order_count:".$order_count." rate:".$rate." 处理结果:".$result;
41
+                
42
+                //第二布:统计往期数据
43
+                #查日期列表
44
+                $dates = DB::table('team_cust_day_remain')->select('idate')->where('team_id', $team_id)->where('idate','<', $date)->groupBy('idate')->orderBy('idate', 'desc')->get();
45
+                if(!empty($dates)){
46
+                    $dates = json_decode(json_encode($dates), true);
47
+                    foreach($dates as $k=>$v){
48
+                        #当前日期加粉 and 昨日成单 
49
+                        $phones = DB::table('customers')->where('fanTime', $v['idate'])->lists('phone');
50
+                        $order_count = DB::table('order')->where('team_id', $team_id)->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime','<=',$date.' 23:59:59')->where('is_del', 0)->count();
51
+                        #当前日期加粉
52
+                        $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime', $v['idate'])->sum('fan_add');
53
+                        #计算当前日期加粉在昨日成单率
54
+                        $rate = $fan_add>0 ? round($order_count / $fan_add, 4) * 100  : 0;
55
+                        //存入数据表
56
+                        $result = $this->updateData($v['idate'], $rate, $date, $team_id);
57
+                        echo "\nDate:".$v['idate']." fan:".$fan_add." order_count:".$order_count." rate:".$rate." 处理结果:".$result;
58
+                    }
59
+                }
60
+            }
61
+        }
62
+    }
63
+
64
+    /**
65
+     * @param idate:统计日期 rate:成单率 date:昨日
66
+     *
67
+     */
68
+    public function updateData($idate, $rate, $date, $team_id){
69
+        #当前月
70
+        $month = date('Y-m', strtotime($date));
71
+        #当月1号
72
+        $month_first = date('Y-m-01', strtotime($date));
73
+        $data = array();
74
+        #如果是昨天的数据(或昨天是当月一号)直接插入
75
+        if($idate == $date || $date == $month_first){
76
+            $data['team_id'] = $team_id;
77
+            $data['idate'] = $idate;
78
+            $data['month_time'] = $month;
79
+            $data['order_rate_data'] = json_encode([0=>['date'=>$date, 'rate'=>$rate]]);
80
+            $res = DB::table('team_cust_day_remain')->insert($data);
81
+        }else{
82
+            //往期成单
83
+            $odata = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('idate', $idate)->where('month_time', $month)->first();
84
+            $order_rate_data = json_decode($odata->order_rate_data, true);
85
+            $order_rate_data[] = ['date'=>$date, 'rate'=>$rate];
86
+            $data['order_rate_data'] = json_encode($order_rate_data);
87
+            $res = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('idate', $idate)->where('month_time', $month)->update($data);
88
+        }
89
+
90
+        return $res;
91
+    }
92
+
93
+}

+ 2 - 0
app/Console/Kernel.php

@@ -16,6 +16,8 @@ class Kernel extends ConsoleKernel {
16 16
         'App\Console\Commands\OrderRateByDay',
17 17
         'App\Console\Commands\OrderExcelAdd',
18 18
         'App\Console\Commands\OrderRateByDayHistory',
19
+        'App\Console\Commands\TeamOrderRateByDay',
20
+        'App\Console\Commands\TeamOrderRateByDayHistory',
19 21
         
20 22
     ];
21 23