作者 赵彬吉
  1 +<?php
  2 +
  3 +namespace App\Console\Commands\DayCount;
  4 +
  5 +use App\Helper\Common;
  6 +use App\Helper\FormGlobalsoApi;
  7 +use App\Models\Domain\DomainInfo;
  8 +use App\Models\Inquiry\InquiryForm;
  9 +use App\Models\Inquiry\InquiryFormData;
  10 +use App\Models\Project\DeployBuild;
  11 +use App\Models\Project\DeployOptimize;
  12 +use App\Models\Project\Project;
  13 +use App\Services\ProjectServer;
  14 +use Carbon\Carbon;
  15 +use Illuminate\Console\Command;
  16 +use Illuminate\Support\Facades\DB;
  17 +use Illuminate\Support\Facades\Log;
  18 +
  19 +class CountDate extends Command
  20 +{
  21 + const STATUS_ERROR = 400;
  22 + public $error = 0;
  23 + /**
  24 + * The name and signature of the console command.
  25 + *
  26 + * @var string
  27 + */
  28 + protected $signature = 'count_data {date}';
  29 +
  30 + /**
  31 + * The console command description.
  32 + *
  33 + * @var string
  34 + */
  35 + protected $description = '统计昨日数据';
  36 +
  37 + /**
  38 + * @name :(定时执行生成昨日数据统计)handle
  39 + * @author :lyh
  40 + * @method :post
  41 + * @time :2023/5/12 14:48
  42 + */
  43 + public function handle()
  44 + {
  45 + $date = $this->argument('date');
  46 + $list = DB::table('gl_project')->where('gl_project.extend_type','=',0)
  47 + ->whereIn('gl_project.type',[1,2,3,4,6])
  48 + ->leftJoin('gl_project_deploy_build', 'gl_project.id', '=', 'gl_project_deploy_build.project_id')
  49 + ->leftJoin('gl_project_deploy_optimize', 'gl_project.id', '=', 'gl_project_deploy_optimize.project_id')
  50 + ->select($this->selectParam())->get();
  51 + try {
  52 + if(!empty($list)){
  53 + $list = $list->toArray();
  54 +// $yesterday = Carbon::yesterday()->toDateString();
  55 + $yesterday = $date;
  56 + $domainInfo = new DomainInfo();
  57 + foreach ($list as $v){
  58 + $v = (array)$v;
  59 + echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
  60 + if($v['type'] == Project::TYPE_ZERO){
  61 + continue;
  62 + }
  63 + if(!empty($v['domain'])){
  64 + $info = $domainInfo->read(['id'=>$v['domain']]);
  65 + if($info !== false){
  66 + $v['test_domain'] = $info['domain'];
  67 + }
  68 + }
  69 + $arr = [];
  70 + //统计时间
  71 + $arr['date'] = $yesterday;
  72 + ProjectServer::useProject($v['id']);
  73 + //pv统计
  74 + $arr['pv_num'] = $this->pv_num($yesterday,$v['test_domain']);
  75 + //ip统计
  76 + $arr['ip_num'] = $this->ip_num($yesterday,$v['test_domain']);
  77 + DB::disconnect('custom_mysql');
  78 + //服务达标天数
  79 + $arr['compliance_day'] = $v['finish_remain_day'];
  80 + //剩余服务时常
  81 + $arr['service_day'] = ($v['remain_day'] - 1) < 0 ? 0: $v['remain_day'] - 1;
  82 + //项目id
  83 + $arr['project_id'] = $v['project_id'];
  84 + $arr['created_at'] = date('Y-m-d H:i:s');
  85 + $arr['updated_at'] = date('Y-m-d H:i:s');
  86 + //询盘统计
  87 + $arr = $this->inquiry($arr,$v['test_domain'], $v['id']);
  88 + if($arr === false){
  89 + continue;
  90 + }
  91 + //查询当天数据是否存在 存在则更新
  92 + $countModel = new \App\Models\HomeCount\Count();
  93 + $info = $countModel->read(['date'=>$arr['date'],'project_id'=>$v['id']]);
  94 + if($info === false){
  95 + DB::table('gl_count')->insert($arr);
  96 + }else{
  97 + $countModel->edit($arr,['id'=>$info['id']]);
  98 + }
  99 + Log::channel('day_count')->error('日期:'.$arr['created_at'].'success: ' .$v['test_domain']);
  100 + }
  101 + }
  102 + }catch (\Exception $e){
  103 + Log::channel('day_count')->error('day_count:->error ' . $e->getMessage());
  104 + }
  105 + Log::channel('day_count')->error('success:end');
  106 + echo $this->error;
  107 + }
  108 +
  109 + /**
  110 + * @name :(统计pv)pv_num
  111 + * @author :lyh
  112 + * @method :post
  113 + * @time :2023/6/14 15:40
  114 + */
  115 + public function pv_num($yesterday,$domain){
  116 + $pv = DB::connection('custom_mysql')->table('gl_customer_visit_item')->whereDate('updated_date', $yesterday)->count();
  117 + return $pv;
  118 + }
  119 +
  120 + /**
  121 + * @name :(统计ip)ip_num
  122 + * @author :lyh
  123 + * @method :post
  124 + * @time :2023/6/14 15:40
  125 + */
  126 + public function ip_num($yesterday,$domain){
  127 + $ip = DB::connection('custom_mysql')->table('gl_customer_visit')->whereDate('updated_date', $yesterday)->count();
  128 + return $ip;
  129 + }
  130 +
  131 + /**
  132 + * @param $arr
  133 + * @param $domain
  134 + * @name :(询盘统计)inquiry
  135 + * @author :lyh
  136 + * @method :post
  137 + * @time :2023/6/14 15:44
  138 + */
  139 + public function inquiry($arr,$domain,$project_id){
  140 + $inquiry_list = (new FormGlobalsoApi())->getInquiryAll($domain);
  141 + if($inquiry_list == false){
  142 + return false;
  143 + }
  144 + if($inquiry_list['status'] == self::STATUS_ERROR){
  145 + $arr['inquiry_num'] = 0;
  146 + $countryArr = [];
  147 + }else{
  148 + $arr['inquiry_num'] = $inquiry_list['data']['count'];
  149 + //询盘国家统计
  150 + $countryArr = $inquiry_list['data']['country'];
  151 + }
  152 + //加上其他询盘
  153 + ProjectServer::useProject($project_id);
  154 + $arr['inquiry_num'] += InquiryFormData::getCount();
  155 + $countryData = InquiryFormData::getCountryCount();
  156 + foreach ($countryData as $v1){
  157 + if(isset($countryArr[$v1['country']])){
  158 + $countryArr[$v1['country']] += $v1['count'];
  159 + }else{
  160 + $countryArr[$v1['country']] = $v1['count'];
  161 + }
  162 + }
  163 + arsort($countryArr);
  164 + $top20 = array_slice($countryArr, 0, 20, true);
  165 + $arr['country'] = json_encode($top20);
  166 + return $arr;
  167 + }
  168 +
  169 + /**
  170 + * @name :(查询参数设置)selectParam
  171 + * @author :lyh
  172 + * @method :post
  173 + * @time :2023/6/14 15:00
  174 + */
  175 + public function selectParam(){
  176 + $select = [
  177 + 'gl_project.id AS id',
  178 + 'gl_project.type AS type',
  179 + 'gl_project.extend_type AS extend_type',
  180 + 'gl_project.remain_day AS remain_day',
  181 + 'gl_project.finish_remain_day AS finish_remain_day',
  182 + 'gl_project_deploy_build.test_domain AS test_domain',
  183 + 'gl_project_deploy_optimize.domain AS domain',
  184 + 'gl_project_deploy_build.project_id AS project_id',
  185 + 'gl_project.cooperate_date AS cooperate_date',
  186 + 'gl_project_deploy_build.service_duration AS service_duration',
  187 + ];
  188 + return $select;
  189 + }
  190 +}
  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :CountProject.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2024/1/8 9:03
  8 + */
  9 +
  10 +namespace App\Console\Commands\MonthlyCount;
  11 +
  12 +use App\Helper\FormGlobalsoApi;
  13 +use App\Models\Com\UpdateOldInfo;
  14 +use App\Models\Domain\DomainInfo;
  15 +use App\Models\Inquiry\InquiryFormData;
  16 +use App\Models\Project\Project;
  17 +use App\Services\ProjectServer;
  18 +use Illuminate\Console\Command;
  19 +use Illuminate\Support\Facades\DB;
  20 +use App\Models\HomeCount\MonthCount AS MonthCountModel;
  21 +
  22 +class MonthCountDate extends Command
  23 +{
  24 + /**
  25 + * The name and signature of the console command.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $signature = 'month_counts_date {date}';
  30 +
  31 + /**
  32 + * The console command description.
  33 + *
  34 + * @var string
  35 + */
  36 + protected $description = '每天生成月统计记录';
  37 +
  38 + public function handle(){
  39 + $list = DB::table('gl_project')->where('gl_project.extend_type','=',0)
  40 + ->whereIn('gl_project.type',[1,2,3,4,6])
  41 + ->leftJoin('gl_project_deploy_build', 'gl_project.id', '=', 'gl_project_deploy_build.project_id')
  42 + ->leftJoin('gl_project_deploy_optimize', 'gl_project.id', '=', 'gl_project_deploy_optimize.project_id')
  43 + ->select($this->selectParam())->get()->toArray();
  44 + foreach ($list as $v) {
  45 + $v = (array)$v;
  46 + if($v['type'] == Project::TYPE_ZERO){
  47 + continue;
  48 + }
  49 + if($v['is_upgrade'] == 1){
  50 + $oldModel = new UpdateOldInfo();
  51 + $info = $oldModel->read(['project_id' => $v['id']]);
  52 + if ($info !== false) {
  53 + $url = $info['old_domain_online'];
  54 + }else{
  55 + continue;
  56 + }
  57 + }else{
  58 + $domainInfo = new DomainInfo();
  59 + if(!empty($v['domain'])){
  60 + $info = $domainInfo->read(['id'=>$v['domain']]);
  61 + if($info !== false){
  62 + $url = $info['domain'];
  63 + }
  64 + }else{
  65 + $url = $v['test_domain'];
  66 + }
  67 + }
  68 + ProjectServer::useProject($v['id']);
  69 + echo date('Y-m-d H:i:s') . '项目id:'.$v['id'] . PHP_EOL;
  70 + $this->count($v['id'], $url);
  71 + DB::disconnect('custom_mysql');
  72 + }
  73 + }
  74 +
  75 + /**
  76 + * @name :(查询参数设置)selectParam
  77 + * @author :lyh
  78 + * @method :post
  79 + * @time :2023/6/14 15:00
  80 + */
  81 + public function selectParam(){
  82 + $select = [
  83 + 'gl_project.id AS id',
  84 + 'gl_project.type AS type',
  85 + 'gl_project.extend_type AS extend_type',
  86 + 'gl_project_deploy_build.test_domain AS test_domain',
  87 + 'gl_project.is_upgrade AS is_upgrade',
  88 + 'gl_project_deploy_optimize.domain AS domain',
  89 + 'gl_project_deploy_build.project_id AS project_id',
  90 + 'gl_project.cooperate_date AS cooperate_date',
  91 + 'gl_project_deploy_build.service_duration AS service_duration',
  92 + ];
  93 + return $select;
  94 + }
  95 + /**
  96 + * @remark :日统计记录
  97 + * @name :count
  98 + * @author :lyh
  99 + * @method :post
  100 + * @time :2024/1/8 9:05
  101 + */
  102 + public function count($project_id,$url){
  103 + $arr = [];
  104 + $date = $this->argument('date');
  105 + $v = ['month'=>$date];
  106 + $monthCountModel = new MonthCountModel();
  107 + $info = $monthCountModel->read(['month'=>$v['month'],'project_id'=>$project_id]);
  108 + // 获取当月开始时间
  109 + $start = date('Y-m-01', strtotime($v['month']));
  110 + // 获取当月结束时间
  111 + $end = date('Y-m-t', strtotime($v['month']));
  112 + $arr['project_id'] = $project_id;
  113 + $res = (new FormGlobalsoApi())->getMonthInquiry($url,$v['month']);
  114 + $arr['total'] = $arr['month_total'] = 0;
  115 + if(isset($res['data']['count'])){
  116 + echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
  117 + $arr['month_total'] = $res['data']['count'] + InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);
  118 + }
  119 + //获取上一个的count
  120 + $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));
  121 + $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
  122 + if($previousInfo === false){
  123 + $arr['total'] = $arr['month_total'];
  124 + }else{
  125 + $arr['total'] = $arr['month_total'] + ($previousInfo['total'] ?? 0);
  126 + }
  127 + $country = [];
  128 + if(isset($res['data']['data'])){
  129 + $country = $res['data']['data'];
  130 + }
  131 + $countryData = InquiryFormData::getCountryCount([$start.' 00:00:00',$end.' 00:00:00']);
  132 + foreach ($countryData as $v1){
  133 + if(isset($country[$v1['country']])){
  134 + $country[$v1['country']] += $v1['count'];
  135 + }else{
  136 + $country[$v1['country']] = $v1['count'];
  137 + }
  138 + }
  139 + $arr['country'] = json_encode($country);
  140 + $arr['month'] = $v['month'];
  141 + $arr = $this->pv_ip($arr,$start,$end,$project_id);
  142 + $arr = $this->sourceCount($arr,$start,$end);
  143 + if($info === false){
  144 + $selectedDate = $start;
  145 + $firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month"));
  146 + $arr['created_at'] = $firstDayOfNextMonth;
  147 + $arr['updated_at'] = $firstDayOfNextMonth;
  148 + $monthCountModel->insert($arr);
  149 + }else{
  150 + $monthCountModel->edit($arr,['id'=>$info['id']]);
  151 + }
  152 + echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
  153 + }
  154 +
  155 + /**
  156 + * @remark :本月询盘总量
  157 + * @name :month_total
  158 + * @author :lyh
  159 + * @method :post
  160 + * @time :2024/1/8 11:02
  161 + */
  162 + public function pv_ip(&$arr,$start,$end,$project_id){
  163 + $pv_ip = DB::table('gl_count')
  164 + ->where(['project_id'=>$project_id])
  165 + ->where('date','>=',$start.' 00:00:00')
  166 + ->where('date','<=',$end.' 23:59:59')
  167 + ->select(DB::raw('SUM(pv_num) as pv_num'), DB::raw('SUM(ip_num) as ip_num'),DB::raw('SUM(inquiry_num) as inquiry_num'))
  168 + ->first();
  169 + $arr['pv'] = $pv_ip->pv_num;
  170 + $arr['ip'] = $pv_ip->ip_num;
  171 + if($arr['ip'] != 0){
  172 + $arr['rate'] = round((($arr['month_total'] ?? 0) / $arr['ip']) * 10,2);
  173 + }
  174 + return $arr;
  175 + }
  176 + /**
  177 + * @remark :来源访问前8
  178 + * @name :sourceCount
  179 + * @author :lyh
  180 + * @method :post
  181 + * @time :2023/6/30 16:14
  182 + */
  183 + public function sourceCount(&$arr,$startTime,$endTime){
  184 + //访问来源前10
  185 + $source = DB::connection('custom_mysql')->table('gl_customer_visit')
  186 + ->select('referrer_url', DB::raw('COUNT(*) as count'))
  187 + ->groupBy('referrer_url')
  188 + ->whereBetween('updated_date', [$startTime,$endTime])
  189 + ->orderByDesc('count')->limit(10)->get()->toArray();
  190 + $arr['source'] = json_encode($source);
  191 + //访问国家前15
  192 + $source_country = DB::connection('custom_mysql')->table('gl_customer_visit')
  193 + ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
  194 + ->groupBy('country')
  195 + ->whereBetween('updated_date', [$startTime,$endTime])
  196 + ->orderBy('ip','desc')->limit(15)->get()->toArray();
  197 + $arr['source_country'] = json_encode($source_country);
  198 + //受访界面前15
  199 + $referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit')
  200 + ->select('url',DB::raw('COUNT(*) as num'))
  201 + ->orderBy('num','desc')
  202 + ->whereBetween('updated_date', [$startTime,$endTime])
  203 + ->groupBy('url')
  204 + ->limit(15)->get()->toArray();
  205 + $arr['referrer_url'] = json_encode($referrer_url);
  206 + //访问端口
  207 + $referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit')
  208 + ->select('device_port',DB::raw('COUNT(*) as num'))
  209 + ->orderBy('num','desc')
  210 + ->whereBetween('updated_date', [$startTime,$endTime])
  211 + ->groupBy('device_port')
  212 + ->limit(15)->get()->toArray();
  213 + $arr['referrer_port'] = json_encode($referrer_port);
  214 + return $arr;
  215 + }
  216 +
  217 +}
@@ -198,6 +198,17 @@ class DomainInfoLogic extends BaseLogic @@ -198,6 +198,17 @@ class DomainInfoLogic extends BaseLogic
198 } 198 }
199 } 199 }
200 200
  201 + $not_allow_ip = array_filter($this->param['not_allow_ip'] ?? []);
  202 + if($not_allow_ip){
  203 + //判断禁止访问IP是否填写正确
  204 + foreach ($not_allow_ip as $vn){
  205 + $vn_count = count(explode('.',$vn));
  206 + if($vn_count != 4){
  207 + $this->fail('禁止访问IP填写有误');
  208 + }
  209 + }
  210 + }
  211 +
201 //保存301跳转数据+其他域名 212 //保存301跳转数据+其他域名
202 $data = [ 213 $data = [
203 'other_domain'=>json_encode(array_filter($this->param['other_domain'] ?? [])), 214 'other_domain'=>json_encode(array_filter($this->param['other_domain'] ?? [])),
@@ -211,7 +222,7 @@ class DomainInfoLogic extends BaseLogic @@ -211,7 +222,7 @@ class DomainInfoLogic extends BaseLogic
211 'amp_private_key' => $this->param['amp_key'] ?? '', 222 'amp_private_key' => $this->param['amp_key'] ?? '',
212 'amp_private_cert' => $this->param['amp_cert'] ?? '', 223 'amp_private_cert' => $this->param['amp_cert'] ?? '',
213 'not_allow_country'=>json_encode(array_filter($this->param['not_allow_country'] ?? [])), 224 'not_allow_country'=>json_encode(array_filter($this->param['not_allow_country'] ?? [])),
214 - 'not_allow_ip'=>json_encode(array_filter($this->param['not_allow_ip'] ?? [])), 225 + 'not_allow_ip'=>json_encode($not_allow_ip),
215 'is_redirect' => $this->param['is_redirect'] ?? 0, 226 'is_redirect' => $this->param['is_redirect'] ?? 0,
216 ]; 227 ];
217 $this->model->edit($data,['id'=>$this->param['id']]); 228 $this->model->edit($data,['id'=>$this->param['id']]);