MonthProjectCount.php
8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* @remark :
* @name :CountProject.php
* @author :lyh
* @method :post
* @time :2024/1/8 9:03
*/
namespace App\Console\Commands\MonthlyCount;
use App\Helper\FormGlobalsoApi;
use App\Models\Com\UpdateOldInfo;
use App\Models\Domain\DomainInfo;
use App\Models\HomeCount\MonthCount;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Project\Project;
use App\Models\Visit\Visit;
use App\Models\Visit\VisitItem;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use App\Models\HomeCount\Count;
class MonthProjectCount extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'month_project {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = '升级项目统计';
public function handle(){
$project_id = $this->argument('project_id');
$oldModel = new UpdateOldInfo();
$info = $oldModel->read(['project_id'=>$project_id]);
if($info !== false){
$url = $info['old_domain_online'];
}else{
$domainModel = new DomainInfo();
$info = $domainModel->read(['project_id'=>$project_id]);
$url = $info['domain'];
}
$projectModel = new Project();
$projectInfo = $projectModel->read(['id'=>$project_id]);
ProjectServer::useProject($project_id);
$this->count($project_id,$url,$projectInfo['is_upgrade'] ?? 0);
DB::disconnect('custom_mysql');
}
/**
* @remark :日统计记录
* @name :count
* @author :lyh
* @method :post
* @time :2024/1/8 9:05
*/
public function count($project_id,$url,$is_upgrade = 0){
$data = [];
$list = DB::connection('custom_mysql')->table('gl_customer_visit')
->select(DB::raw('DATE_FORMAT(updated_date, "%Y-%m") as month'))
->orderBy('month', 'asc')
->groupBy('month')->get()->toArray();
foreach ($list as $v){
$data[] = $v->month;
}
$list = $this->fillMissingMonths($data);
foreach ($list as $v){
$arr = [];
$monthCountModel = new MonthCount();
$info = $monthCountModel->read(['month'=>$v,'project_id'=>$project_id]);
// 获取当月开始时间
$start = date('Y-m-01', strtotime($v));
// 获取当月结束时间
$end = date('Y-m-t', strtotime($v));
$arr['project_id'] = $project_id;
$res = (new FormGlobalsoApi())->getMonthInquiry($url,$v ,$is_upgrade ?? 0);
echo date('Y-m-d H:i:s') . '月份:'.$v. PHP_EOL;
$arr['total'] = $arr['month_total'] = 0;
if(isset($res['data']['count'])){
$arr['month_total'] = $res['data']['count'] + InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);
}
//获取上一个的count
$previousMonth = date('Y-m', strtotime($v . ' -1 month'));
$previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
if($previousInfo === false){
$arr['total'] = $arr['month_total'];
}else{
$arr['total'] = $arr['month_total'] + ($previousInfo['total'] ?? 0);
}
$country = [];
if(isset($res['data']['data'])){
$country = $res['data']['data'];
}
$countryData = InquiryFormData::getCountryCount([$start.' 00:00:00',$end.' 00:00:00']);
foreach ($countryData as $v1){
if(isset($country[$v1['country']])){
$country[$v1['country']] += $v1['count'];
}else{
$country[$v1['country']] = $v1['count'];
}
}
$arr['country'] = json_encode($country);
$arr['month'] = $v;
$arr = $this->pv_ip($arr,$start,$end,$project_id);
$arr = $this->sourceCount($arr,$start,$end);
if($info === false){
$selectedDate = $start;
$firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month"));
$arr['created_at'] = $firstDayOfNextMonth;
$arr['updated_at'] = $firstDayOfNextMonth;
$monthCountModel->insert($arr);
}else{
$monthCountModel->edit($arr,['id'=>$info['id']]);
}
}
echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
}
/**
* @remark :本月询盘总量
* @name :month_total
* @author :lyh
* @method :post
* @time :2024/1/8 11:02
*/
public function pv_ip(&$arr,$start,$end,$project_id){
$arr['pv'] = (new VisitItem())
->where('updated_date','>=',$start.' 00:00:00')
->where('updated_date','<=',$end.' 23:59:59')->count();
$arr['ip'] = (new Visit())
->where('updated_date','>=',$start.' 00:00:00')
->where('updated_date','<=',$end.' 23:59:59')->count();
if($arr['ip'] != 0){
$arr['rate'] = round(($arr['month_total'] / $arr['ip']) * 10,2);
}
return $arr;
}
/**
* @remark :来源访问前8
* @name :sourceCount
* @author :lyh
* @method :post
* @time :2023/6/30 16:14
*/
public function sourceCount(&$arr,$startTime,$endTime){
//访问来源前10
$source = DB::connection('custom_mysql')->table('gl_customer_visit')
->select('referrer_url', DB::raw('COUNT(*) as count'))
->groupBy('referrer_url')
->whereBetween('updated_date', [$startTime,$endTime])
->orderByDesc('count')->limit(10)->get()->toArray();
$arr['source'] = json_encode($source);
//访问国家前15
$source_country = DB::connection('custom_mysql')->table('gl_customer_visit')
->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
->groupBy('country')
->whereBetween('updated_date', [$startTime,$endTime])
->orderBy('ip','desc')->limit(15)->get()->toArray();
$arr['source_country'] = json_encode($source_country);
//受访界面前15
$referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit')
->select('url',DB::raw('COUNT(*) as num'))
->orderBy('num','desc')
->whereBetween('updated_date', [$startTime,$endTime])
->groupBy('url')
->limit(15)->get()->toArray();
$arr['referrer_url'] = json_encode($referrer_url);
//访问端口
$referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit')
->select('device_port',DB::raw('COUNT(*) as num'))
->orderBy('num','desc')
->whereBetween('updated_date', [$startTime,$endTime])
->groupBy('device_port')
->limit(15)->get()->toArray();
$arr['referrer_port'] = json_encode($referrer_port);
return $arr;
}
/**
* @remark :补齐月份
* @name :fillMissingMonths
* @author :lyh
* @method :post
* @time :2024/8/14 11:11
*/
public function fillMissingMonths($dates) {
// 将字符串日期转换为 Carbon 对象
$carbonDates = array_map(function($date) {
return Carbon::createFromFormat('Y-m', $date);
}, $dates);
// 排序日期,确保列表按时间顺序排列
usort($carbonDates, function($a, $b) {
return $a->gt($b);
});
// 用于存储完整日期的数组
$completeDates = [];
// 遍历日期列表,补齐中间缺失的月份
for ($i = 0; $i < count($carbonDates) - 1; $i++) {
$current = $carbonDates[$i];
$next = $carbonDates[$i + 1];
// 将当前月份加入完整日期数组
array_push($completeDates, $current->format('Y-m'));
// 循环补齐中间缺失的月份
while ($current->addMonth()->lt($next)) {
array_push($completeDates, $current->format('Y-m'));
}
}
// 加入最后一个月份
array_push($completeDates, $carbonDates[count($carbonDates) - 1]->format('Y-m'));
return $completeDates;
}
}