|
@@ -0,0 +1,111 @@
|
|
1
|
+<?php namespace App\Console\Commands;
|
|
2
|
+
|
|
3
|
+use Illuminate\Console\Command;
|
|
4
|
+use Illuminate\Support\Facades\DB;
|
|
5
|
+use App\Admin;
|
|
6
|
+use App\Distributors;
|
|
7
|
+
|
|
8
|
+class GroupFanAmountByMonth extends Command {
|
|
9
|
+
|
|
10
|
+ /**
|
|
11
|
+ * The console command name.
|
|
12
|
+ *
|
|
13
|
+ * @var string
|
|
14
|
+ */
|
|
15
|
+ protected $name = 'GroupFanAmountByMonth';
|
|
16
|
+ protected $date;
|
|
17
|
+
|
|
18
|
+ /**
|
|
19
|
+ * The console command description.
|
|
20
|
+ *
|
|
21
|
+ * @var string
|
|
22
|
+ */
|
|
23
|
+ protected $description = '新粉月销售额梯形图';
|
|
24
|
+
|
|
25
|
+ public function handle()
|
|
26
|
+ {
|
|
27
|
+ try{
|
|
28
|
+ $this->date = date('Y-m-01',strtotime('-1 months'));
|
|
29
|
+ $this->TeamFanAmountByMonth();
|
|
30
|
+ } catch (\Exception $e) {
|
|
31
|
+ echo 'line:'.$e->getLine().' message:'.$e->getMessage();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ public function TeamFanAmountByMonth() {
|
|
37
|
+ $team_ids = Admin::getTeams();
|
|
38
|
+ foreach ($team_ids as $team_id) {
|
|
39
|
+ $date = $this->date;
|
|
40
|
+ $date_first = $this->date;
|
|
41
|
+ $date_end = $this->getthemonth($date);
|
|
42
|
+ #团队成员:
|
|
43
|
+ $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
|
|
44
|
+ //第一步: 统计上月数据
|
|
45
|
+ #上月加粉
|
|
46
|
+ $phones = DB::table('customers')->whereIn('admin_id',$saler_ids)->where('fanTime', '>=', $date_first)->where('fanTime', '<=', $date_end)->lists('phone');
|
|
47
|
+ $amount = DB::table('order')->whereIn('receiverMobile', $phones)->where('createTime','>=',$date)->where('createTime', '<=', $date_end. '23:59:59')->where('is_del', 0)->where('team_id', $team_id)->sum('receivedAmount');
|
|
48
|
+ //存入数据表
|
|
49
|
+ $result = $this->updateData($date, $amount, $date, $team_id);
|
|
50
|
+ echo "\nDate:".$date." team_id:".$team_id." amount:".$amount." 处理结果:".$result;
|
|
51
|
+ //第二布:统计往期数据
|
|
52
|
+ #查日期列表
|
|
53
|
+ $dates = DB::table('cust_month_remain')->select('idate')->where('team_id', $team_id)->where('idate','<', $date)->groupBy('idate')->orderBy('idate', 'desc')->get();
|
|
54
|
+ if( !empty($dates) ) {
|
|
55
|
+ $dates = json_decode(json_encode($dates),true);
|
|
56
|
+ foreach ($dates as $k=>&$v) {
|
|
57
|
+ $idate_first = date('Y-m-01',strtotime($v['idate']));
|
|
58
|
+ $idate_end = $this->getthemonth($v['idate']);
|
|
59
|
+ $phones = DB::table('customers')->whereIn('admin_id',$saler_ids)->where('fanTime', '>=', $idate_first)->where('fanTime', '<=', $idate_end)->lists('phone');
|
|
60
|
+ $amount = DB::table('order')->whereIn('receiverMobile', $phones)->where('createTime','>=', $date_first)->where('createTime', '<', $date_end. '23:59:59')->where('is_del', 0)->where('team_id', $team_id)->sum('receivedAmount');
|
|
61
|
+ //存入数据表
|
|
62
|
+ $result = $this->updateData($v['idate'], $amount, $date, $team_id);
|
|
63
|
+ echo "\nDate:".$v['idate']." team_id:".$team_id." amount:".$amount." 处理结果:".$result;
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public function updateData($idate, $amount, $date, $team_id){
|
|
70
|
+ #当前年份
|
|
71
|
+ $month = date('Y-01', strtotime($date));
|
|
72
|
+ $data = array();
|
|
73
|
+ #如果是上个月的数据直接插入
|
|
74
|
+ if($idate == $date ){
|
|
75
|
+ $data['team_id'] = $team_id;
|
|
76
|
+ $data['idate'] = $idate;
|
|
77
|
+ $data['month_time'] = $month;
|
|
78
|
+ $data['amount_data'] = json_encode([0=>['date'=>$date, 'amount'=>$amount]]);
|
|
79
|
+ $data['create_time'] = date('Y-m-d H:i:s',time());
|
|
80
|
+ $res = DB::table('cust_month_remain')->insert($data);
|
|
81
|
+ }else{
|
|
82
|
+ if(12 < $this->getMonthNum( $idate, $date)){
|
|
83
|
+ return 0;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ //往期成单
|
|
87
|
+ $odata = DB::table('cust_month_remain')->where('team_id', $team_id)->where('idate', $idate)->first();
|
|
88
|
+ $amount_data = json_decode($odata->amount_data, true);
|
|
89
|
+ $amount_data[] = ['date'=>$date, 'amount'=>$amount];
|
|
90
|
+ $data['amount_data'] = json_encode($amount_data);
|
|
91
|
+ $res = DB::table('cust_month_remain')->where('team_id', $team_id)->where('idate', $idate)->update($data);
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ return $res;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ public function getthemonth($date)
|
|
98
|
+ {
|
|
99
|
+ $firstday = date('Y-m-01', strtotime($date));
|
|
100
|
+ $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
|
|
101
|
+ return $lastday;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public function getMonthNum($date1,$date2){
|
|
105
|
+ $date1_stamp=strtotime($date1);
|
|
106
|
+ $date2_stamp=strtotime($date2);
|
|
107
|
+ list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
|
|
108
|
+ list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
|
|
109
|
+ return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m'];
|
|
110
|
+ }
|
|
111
|
+}
|