|
@@ -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
|
+}
|