CountDate.php
6.7 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
<?php
namespace App\Console\Commands\DayCount;
use App\Helper\FormGlobalsoApi;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CountDate extends Command
{
const STATUS_ERROR = 400;
public $error = 0;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'count_data {date}';
/**
* The console command description.
*
* @var string
*/
protected $description = '按时间统计所有项目记录';
/**
* @name :(定时执行生成昨日数据统计)handle
* @author :lyh
* @method :post
* @time :2023/5/12 14:48
*/
public function handle()
{
$date = $this->argument('date');
$list = DB::table('gl_project')->where('gl_project.extend_type','=',0)
->whereIn('gl_project.type',[1,2,3,4,6])
->leftJoin('gl_project_deploy_build', 'gl_project.id', '=', 'gl_project_deploy_build.project_id')
->leftJoin('gl_project_deploy_optimize', 'gl_project.id', '=', 'gl_project_deploy_optimize.project_id')
->select($this->selectParam())->get();
try {
if(!empty($list)){
$list = $list->toArray();
// $yesterday = Carbon::yesterday()->toDateString();
$yesterday = $date;
$domainInfo = new DomainInfo();
foreach ($list as $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
if($v['type'] == Project::TYPE_ZERO){
continue;
}
if(!empty($v['domain'])){
$info = $domainInfo->read(['id'=>$v['domain']]);
if($info !== false){
$v['test_domain'] = $info['domain'];
}
}
$arr = [];
//统计时间
$arr['date'] = $yesterday;
ProjectServer::useProject($v['id']);
//pv统计
$arr['pv_num'] = $this->pv_num($yesterday);
//ip统计
$arr['ip_num'] = $this->ip_num($yesterday);
DB::disconnect('custom_mysql');
//服务达标天数
$arr['compliance_day'] = $v['finish_remain_day'];
//剩余服务时常
$arr['service_day'] = ($v['remain_day'] - 1) < 0 ? 0: $v['remain_day'] - 1;
//项目id
$arr['project_id'] = $v['project_id'];
$arr['created_at'] = date('Y-m-d H:i:s');
$arr['updated_at'] = date('Y-m-d H:i:s');
//询盘统计
$arr = $this->inquiry($arr,$v['test_domain'], $v['id'],$v['is_upgrade'] ?? 0);
if($arr === false){
continue;
}
//查询当天数据是否存在 存在则更新
$countModel = new \App\Models\HomeCount\Count();
$info = $countModel->read(['date'=>$arr['date'],'project_id'=>$v['id']]);
if($info === false){
DB::table('gl_count')->insert($arr);
}else{
$countModel->edit($arr,['id'=>$info['id']]);
}
Log::channel('day_count')->error('日期:'.$arr['created_at'].'success: ' .$v['test_domain']);
}
}
}catch (\Exception $e){
Log::channel('day_count')->error('day_count:->error ' . $e->getMessage());
}
Log::channel('day_count')->error('success:end');
echo $this->error;
}
/**
* @name :(统计pv)pv_num
* @author :lyh
* @method :post
* @time :2023/6/14 15:40
*/
public function pv_num($yesterday){
$pv = DB::connection('custom_mysql')->table('gl_customer_visit_item')->whereDate('updated_date', $yesterday)->count();
return $pv;
}
/**
* @name :(统计ip)ip_num
* @author :lyh
* @method :post
* @time :2023/6/14 15:40
*/
public function ip_num($yesterday){
$ip = DB::connection('custom_mysql')->table('gl_customer_visit')->whereDate('updated_date', $yesterday)->count();
return $ip;
}
/**
* @param $arr
* @param $domain
* @name :(询盘统计)inquiry
* @author :lyh
* @method :post
* @time :2023/6/14 15:44
*/
public function inquiry($arr,$domain,$project_id,$is_upgrade = 0){
$inquiry_list = (new FormGlobalsoApi())->getInquiryAll($domain,$is_upgrade);
if($inquiry_list == false){
return $arr;
}
if($inquiry_list['status'] == self::STATUS_ERROR){
$arr['inquiry_num'] = 0;
$countryArr = [];
}else{
$arr['inquiry_num'] = $inquiry_list['data']['count'];
//询盘国家统计
$countryArr = $inquiry_list['data']['country'];
}
//加上其他询盘
ProjectServer::useProject($project_id);
$arr['inquiry_num'] += InquiryFormData::getCount();
$countryData = InquiryFormData::getCountryCount();
foreach ($countryData as $v1){
if(isset($countryArr[$v1['country']])){
$countryArr[$v1['country']] += $v1['count'];
}else{
$countryArr[$v1['country']] = $v1['count'];
}
}
arsort($countryArr);
$top20 = array_slice($countryArr, 0, 20, true);
$arr['country'] = json_encode($top20);
return $arr;
}
/**
* @name :(查询参数设置)selectParam
* @author :lyh
* @method :post
* @time :2023/6/14 15:00
*/
public function selectParam(){
$select = [
'gl_project.id AS id',
'gl_project.type AS type',
'gl_project.extend_type AS extend_type',
'gl_project.is_upgrade AS is_upgrade',
'gl_project.remain_day AS remain_day',
'gl_project.finish_remain_day AS finish_remain_day',
'gl_project_deploy_build.test_domain AS test_domain',
'gl_project_deploy_optimize.domain AS domain',
'gl_project_deploy_build.project_id AS project_id',
'gl_project.cooperate_date AS cooperate_date',
'gl_project_deploy_build.service_duration AS service_duration',
];
return $select;
}
}