作者 lyh

gx

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :UpgradeProjectCount.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\Models\Com\UpdateOldInfo;
  13 +use App\Models\Domain\DomainInfo;
  14 +use App\Models\Project\Project;
  15 +use App\Services\ProjectServer;
  16 +use Illuminate\Console\Command;
  17 +use Illuminate\Support\Facades\DB;
  18 +
  19 +class MonthCount extends Command
  20 +{
  21 + /**
  22 + * The name and signature of the console command.
  23 + *
  24 + * @var string
  25 + */
  26 + protected $signature = 'month_counts';
  27 +
  28 + /**
  29 + * The console command description.
  30 + *
  31 + * @var string
  32 + */
  33 + protected $description = '每天生成月统计记录';
  34 +
  35 + public function handle(){
  36 + $list = DB::table('gl_project')->where('gl_project.extend_type','=',0)
  37 + ->where('gl_project.type','!=',0)
  38 + ->leftJoin('gl_project_deploy_build', 'gl_project.id', '=', 'gl_project_deploy_build.project_id')
  39 + ->leftJoin('gl_project_deploy_optimize', 'gl_project.id', '=', 'gl_project_deploy_optimize.project_id')
  40 + ->select($this->selectParam())->get()->toArray();
  41 + foreach ($list as $v) {
  42 + $v = (array)$v;
  43 + if($v['type'] == Project::TYPE_ZERO){
  44 + continue;
  45 + }
  46 + if($v['is_upgrade'] == 1){
  47 + $oldModel = new UpdateOldInfo();
  48 + $info = $oldModel->read(['project_id' => $v['id']]);
  49 + if ($info !== false) {
  50 + $url = $info['old_domain_online'];
  51 + }else{
  52 + continue;
  53 + }
  54 + }else{
  55 + $domainInfo = new DomainInfo();
  56 + if(!empty($v['domain'])){
  57 + $info = $domainInfo->read(['id'=>$v['domain']]);
  58 + if($info !== false){
  59 + $url = $info['domain'];
  60 + }
  61 + }else{
  62 + $url = $v['test_domain'];
  63 + }
  64 + }
  65 + ProjectServer::useProject($v['id']);
  66 + echo date('Y-m-d H:i:s') . '项目id:'.$v['id'] . PHP_EOL;
  67 + $this->count($v['id'], $url);
  68 + DB::disconnect('custom_mysql');
  69 + }
  70 + }
  71 +
  72 + /**
  73 + * @name :(查询参数设置)selectParam
  74 + * @author :lyh
  75 + * @method :post
  76 + * @time :2023/6/14 15:00
  77 + */
  78 + public function selectParam(){
  79 + $select = [
  80 + 'gl_project.id AS id',
  81 + 'gl_project.type AS type',
  82 + 'gl_project.extend_type AS extend_type',
  83 + 'gl_project_deploy_build.test_domain AS test_domain',
  84 + 'gl_project_deploy_optimize.domain AS domain',
  85 + 'gl_project_deploy_build.project_id AS project_id',
  86 + 'gl_project.cooperate_date AS cooperate_date',
  87 + 'gl_project_deploy_build.service_duration AS service_duration',
  88 + ];
  89 + return $select;
  90 + }
  91 + /**
  92 + * @remark :日统计记录
  93 + * @name :count
  94 + * @author :lyh
  95 + * @method :post
  96 + * @time :2024/1/8 9:05
  97 + */
  98 + public function count($project_id,$url){
  99 + $list = DB::connection('custom_mysql')->table('gl_customer_visit')
  100 + ->select(DB::raw('DATE_FORMAT(updated_date, "%Y-%m") as month'))
  101 + ->groupBy('month')->get()->toArray();
  102 + foreach ($list as $k=>$v){
  103 + $v = (array)$v;
  104 + if($v['month'] == date('Y-m')){
  105 + continue;
  106 + }
  107 + $monthCountModel = new MonthCount();
  108 + $info = $monthCountModel->read(['month'=>$v['month'],'project_id'=>$project_id]);
  109 + // 获取当月开始时间
  110 + $start = date('Y-m-01', strtotime($v['month']));
  111 + // 获取当月结束时间
  112 + $end = date('Y-m-t', strtotime($v['month']));
  113 + $arr['project_id'] = $project_id;
  114 + $res = $this->inquiry($url,$v['month']);
  115 +// $arr['month_total'] = 0;
  116 + if(isset($res['data']['count'])){
  117 + echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
  118 + $arr['month_total'] = $res['data']['count'];
  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'] = $res['data']['count'] + ($previousInfo['total'] ?? 0);
  126 + }
  127 + }
  128 + if(isset($res['data']['data'])){
  129 + $arr['country'] = json_encode($res['data']['data']);
  130 + }
  131 + $arr['month'] = $v['month'];
  132 + $arr = $this->pv_ip($arr,$start,$end,$project_id);
  133 + $arr = $this->sourceCount($arr,$start,$end);
  134 + if($info === false){
  135 + $selectedDate = $start;
  136 + $firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month"));
  137 + $arr['created_at'] = $firstDayOfNextMonth;
  138 + $arr['updated_at'] = $firstDayOfNextMonth;
  139 +// echo date('Y-m-d H:i:s') . '数据:'.json_encode($arr) . PHP_EOL;
  140 + $monthCountModel->insert($arr);
  141 + }else{
  142 + $monthCountModel->edit($arr,['id'=>$info['id']]);
  143 + }
  144 + }
  145 + echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
  146 + }
  147 +
  148 + /**
  149 + * @remark :本月询盘总量
  150 + * @name :month_total
  151 + * @author :lyh
  152 + * @method :post
  153 + * @time :2024/1/8 11:02
  154 + */
  155 + public function pv_ip(&$arr,$start,$end,$project_id){
  156 + $pv_ip = DB::table('gl_count')
  157 + ->where(['project_id'=>$project_id])
  158 + ->where('date','>=',$start.' 00:00:00')
  159 + ->where('date','<=',$end.' 23:59:59')
  160 + ->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'))
  161 + ->first();
  162 + $arr['pv'] = $pv_ip->pv_num;
  163 + $arr['ip'] = $pv_ip->ip_num;
  164 + if($arr['ip'] != 0){
  165 + $arr['rate'] = round((($arr['month_total'] ?? 0) / $arr['ip']) * 10,2);
  166 + }
  167 + return $arr;
  168 + }
  169 + /**
  170 + * @remark :来源访问前8
  171 + * @name :sourceCount
  172 + * @author :lyh
  173 + * @method :post
  174 + * @time :2023/6/30 16:14
  175 + */
  176 + public function sourceCount(&$arr,$startTime,$endTime){
  177 + //访问来源前10
  178 + $source = DB::connection('custom_mysql')->table('gl_customer_visit')
  179 + ->select('referrer_url', DB::raw('COUNT(*) as count'))
  180 + ->groupBy('referrer_url')
  181 + ->whereBetween('updated_date', [$startTime,$endTime])
  182 + ->orderByDesc('count')->limit(10)->get()->toArray();
  183 + $arr['source'] = json_encode($source);
  184 + //访问国家前15
  185 + $source_country = DB::connection('custom_mysql')->table('gl_customer_visit')
  186 + ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
  187 + ->groupBy('country')
  188 + ->whereBetween('updated_date', [$startTime,$endTime])
  189 + ->orderBy('ip','desc')->limit(15)->get()->toArray();
  190 + $arr['source_country'] = json_encode($source_country);
  191 + //受访界面前15
  192 + $referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit')
  193 + ->select('url',DB::raw('COUNT(*) as num'))
  194 + ->orderBy('num','desc')
  195 + ->whereBetween('updated_date', [$startTime,$endTime])
  196 + ->groupBy('url')
  197 + ->limit(15)->get()->toArray();
  198 + $arr['referrer_url'] = json_encode($referrer_url);
  199 + //访问端口
  200 + $referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit')
  201 + ->select('device_port',DB::raw('COUNT(*) as num'))
  202 + ->orderBy('num','desc')
  203 + ->whereBetween('updated_date', [$startTime,$endTime])
  204 + ->groupBy('device_port')
  205 + ->limit(15)->get()->toArray();
  206 + $arr['referrer_port'] = json_encode($referrer_port);
  207 + return $arr;
  208 + }
  209 +
  210 + public function inquiry($url,$month){
  211 + $url = 'https://'.$url.'/';
  212 + $token = md5($url.date("Y-m-d"));
  213 + $url = 'https://form.globalso.com/api/external-interface/country_con/15243d63ed5a5738?domain='.$url.'&token='.$token.'&source=1,2,3,4&model=month&sta_date='.$month;
  214 + $res = http_get($url,['charset=utf-8']);
  215 + echo date('Y-m-d H:i:s') . '数据:'.json_encode($res) . PHP_EOL;
  216 + return $res;
  217 + }
  218 +
  219 +}
@@ -30,7 +30,6 @@ class MonthCountLogic extends BaseLogic @@ -30,7 +30,6 @@ class MonthCountLogic extends BaseLogic
30 */ 30 */
31 public function getCountLists($map,$order = 'created_at',$filed = ['*']){ 31 public function getCountLists($map,$order = 'created_at',$filed = ['*']){
32 $map['project_id'] = $this->user['project_id']; 32 $map['project_id'] = $this->user['project_id'];
33 - $new = $this->currentMonthCount();  
34 $lists = $this->model->list($map,$order,$filed,'desc',10); 33 $lists = $this->model->list($map,$order,$filed,'desc',10);
35 if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){ 34 if(isset($this->project['is_record_china_visit']) && ($this->project['is_record_china_visit'] == 0)){
36 foreach ($lists as $k => $v){ 35 foreach ($lists as $k => $v){
@@ -49,7 +48,6 @@ class MonthCountLogic extends BaseLogic @@ -49,7 +48,6 @@ class MonthCountLogic extends BaseLogic
49 $lists[$k] = $v; 48 $lists[$k] = $v;
50 } 49 }
51 } 50 }
52 - $lists['new'] = $new;  
53 return $this->success($lists); 51 return $this->success($lists);
54 } 52 }
55 53
@@ -156,7 +154,7 @@ class MonthCountLogic extends BaseLogic @@ -156,7 +154,7 @@ class MonthCountLogic extends BaseLogic
156 ->where('referrer_url','!=','') 154 ->where('referrer_url','!=','')
157 ->whereBetween('updated_date', [$startTime,$endTime]) 155 ->whereBetween('updated_date', [$startTime,$endTime])
158 ->orderByDesc('count')->limit(10)->get()->toArray(); 156 ->orderByDesc('count')->limit(10)->get()->toArray();
159 - $arr['source'] = $source; 157 + $arr['source'] = $source ?? [];
160 //访问国家前15 158 //访问国家前15
161 $query = DB::connection('custom_mysql')->table('gl_customer_visit') 159 $query = DB::connection('custom_mysql')->table('gl_customer_visit')
162 ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv')) 160 ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
@@ -166,7 +164,7 @@ class MonthCountLogic extends BaseLogic @@ -166,7 +164,7 @@ class MonthCountLogic extends BaseLogic
166 } 164 }
167 $source_country = $query->whereBetween('updated_date', [$startTime,$endTime]) 165 $source_country = $query->whereBetween('updated_date', [$startTime,$endTime])
168 ->orderBy('ip','desc')->limit(15)->get()->toArray(); 166 ->orderBy('ip','desc')->limit(15)->get()->toArray();
169 - $arr['source_country'] = $source_country; 167 + $arr['source_country'] = $source_country ?? [];
170 //受访界面前15 168 //受访界面前15
171 $referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit') 169 $referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit')
172 ->select('url',DB::raw('COUNT(*) as num')) 170 ->select('url',DB::raw('COUNT(*) as num'))
@@ -174,15 +172,15 @@ class MonthCountLogic extends BaseLogic @@ -174,15 +172,15 @@ class MonthCountLogic extends BaseLogic
174 ->whereBetween('updated_date', [$startTime,$endTime]) 172 ->whereBetween('updated_date', [$startTime,$endTime])
175 ->groupBy('url') 173 ->groupBy('url')
176 ->limit(15)->get()->toArray(); 174 ->limit(15)->get()->toArray();
177 - $arr['referrer_url'] = $referrer_url;  
178 - //访问断后 175 + $arr['referrer_url'] = $referrer_url ?? [];
  176 + //访问端口
179 $referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit') 177 $referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit')
180 ->select('device_port',DB::raw('COUNT(*) as num')) 178 ->select('device_port',DB::raw('COUNT(*) as num'))
181 ->orderBy('num','desc') 179 ->orderBy('num','desc')
182 ->whereBetween('updated_date', [$startTime,$endTime]) 180 ->whereBetween('updated_date', [$startTime,$endTime])
183 ->groupBy('device_port') 181 ->groupBy('device_port')
184 ->limit(15)->get()->toArray(); 182 ->limit(15)->get()->toArray();
185 - $arr['referrer_port'] = $referrer_port; 183 + $arr['referrer_port'] = $referrer_port ?? [];
186 return $arr; 184 return $arr;
187 } 185 }
188 186