作者 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\Helper\FormGlobalsoApi;
  13 +use App\Models\Com\UpdateOldInfo;
  14 +use App\Models\Domain\DomainInfo;
  15 +use App\Models\HomeCount\MonthCount;
  16 +use App\Models\Project\Project;
  17 +use App\Models\Visit\Visit;
  18 +use App\Services\ProjectServer;
  19 +use Illuminate\Console\Command;
  20 +use Illuminate\Support\Facades\DB;
  21 +use App\Models\HomeCount\Count;
  22 +
  23 +class UpgradeCount extends Command
  24 +{
  25 + /**
  26 + * The name and signature of the console command.
  27 + *
  28 + * @var string
  29 + */
  30 + protected $signature = 'upgrade_month_count';
  31 +
  32 + /**
  33 + * The console command description.
  34 + *
  35 + * @var string
  36 + */
  37 + protected $description = '升级项目统计-月统计';
  38 +
  39 + public function handle(){
  40 + $projectModel = new Project();
  41 + $list = $projectModel->list(['is_upgrade'=>1,'delete_status'=>0]);
  42 + foreach ($list as $v) {
  43 + $oldModel = new UpdateOldInfo();
  44 + $info = $oldModel->read(['project_id' => $v['id']]);
  45 + if ($info !== false) {
  46 + $url = $info['old_domain_online'];
  47 + } else {
  48 + $domainModel = new DomainInfo();
  49 + $info = $domainModel->read(['project_id' => $v['id']]);
  50 + $url = $info['domain'];
  51 + }
  52 + ProjectServer::useProject($v['id']);
  53 + $this->count($v['id'], $url);
  54 + DB::disconnect('custom_mysql');
  55 + }
  56 + }
  57 +
  58 + /**
  59 + * @remark :日统计记录
  60 + * @name :count
  61 + * @author :lyh
  62 + * @method :post
  63 + * @time :2024/1/8 9:05
  64 + */
  65 + public function count($project_id,$url){
  66 + $list = DB::connection('custom_mysql')->table('gl_customer_visit')
  67 + ->select(DB::raw('DATE_FORMAT(updated_date, "%Y-%m") as month'))
  68 + ->groupBy('month')->get()->toArray();
  69 + foreach ($list as $k=>$v){
  70 + $v = (array)$v;
  71 + if($v['updated_date'] == date('Y-m')){
  72 + continue;
  73 + }
  74 + $monthCountModel = new MonthCount();
  75 + $info = $monthCountModel->read(['month'=>$v['month'],'project_id'=>$project_id]);
  76 + // 获取当月开始时间
  77 + $start = date('Y-m-01', strtotime($v['month']));
  78 + // 获取当月结束时间
  79 + $end = date('Y-m-t', strtotime($v['month']));
  80 + $arr['project_id'] = $project_id;
  81 + $res = $this->inquiry($url,$v['month']);
  82 +// $arr['month_total'] = 0;
  83 + if(isset($res['data']['count'])){
  84 + echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
  85 + $arr['month_total'] = $res['data']['count'];
  86 + //获取上一个的count
  87 + $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));
  88 + $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
  89 + if($previousInfo === false){
  90 + $arr['total'] = $arr['month_total'];
  91 + }else{
  92 + $arr['total'] = $res['data']['count'] + ($previousInfo['total'] ?? 0);
  93 + }
  94 + }
  95 + if(isset($res['data']['data'])){
  96 + $arr['country'] = json_encode($res['data']['data']);
  97 + }
  98 + $arr['month'] = $v['month'];
  99 + $arr = $this->pv_ip($arr,$start,$end,$project_id);
  100 + $arr = $this->sourceCount($arr,$start,$end);
  101 + if($info === false){
  102 + $selectedDate = $start;
  103 + $firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month"));
  104 + $arr['created_at'] = $firstDayOfNextMonth;
  105 + $arr['updated_at'] = $firstDayOfNextMonth;
  106 +// echo date('Y-m-d H:i:s') . '数据:'.json_encode($arr) . PHP_EOL;
  107 + $monthCountModel->insert($arr);
  108 + }else{
  109 + $monthCountModel->edit($arr,['id'=>$info['id']]);
  110 + }
  111 + }
  112 + echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
  113 + }
  114 +
  115 + /**
  116 + * @remark :本月询盘总量
  117 + * @name :month_total
  118 + * @author :lyh
  119 + * @method :post
  120 + * @time :2024/1/8 11:02
  121 + */
  122 + public function pv_ip(&$arr,$start,$end,$project_id){
  123 + $pv_ip = DB::table('gl_count')
  124 + ->where(['project_id'=>$project_id])
  125 + ->where('date','>=',$start.' 00:00:00')
  126 + ->where('date','<=',$end.' 23:59:59')
  127 + ->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'))
  128 + ->first();
  129 + $arr['pv'] = $pv_ip->pv_num;
  130 + $arr['ip'] = $pv_ip->ip_num;
  131 + if($arr['ip'] != 0){
  132 + $arr['rate'] = round(($arr['month_total'] / $arr['ip']) * 10,2);
  133 + }
  134 + return $arr;
  135 + }
  136 + /**
  137 + * @remark :来源访问前8
  138 + * @name :sourceCount
  139 + * @author :lyh
  140 + * @method :post
  141 + * @time :2023/6/30 16:14
  142 + */
  143 + public function sourceCount(&$arr,$startTime,$endTime){
  144 + //访问来源前10
  145 + $source = DB::connection('custom_mysql')->table('gl_customer_visit')
  146 + ->select('referrer_url', DB::raw('COUNT(*) as count'))
  147 + ->groupBy('referrer_url')
  148 + ->whereBetween('updated_date', [$startTime,$endTime])
  149 + ->orderByDesc('count')->limit(10)->get()->toArray();
  150 + $arr['source'] = json_encode($source);
  151 + //访问国家前15
  152 + $source_country = DB::connection('custom_mysql')->table('gl_customer_visit')
  153 + ->select('country',DB::raw('COUNT(*) as ip'),DB::raw('SUM(depth) as pv'))
  154 + ->groupBy('country')
  155 + ->whereBetween('updated_date', [$startTime,$endTime])
  156 + ->orderBy('ip','desc')->limit(15)->get()->toArray();
  157 + $arr['source_country'] = json_encode($source_country);
  158 + //受访界面前15
  159 + $referrer_url = DB::connection('custom_mysql')->table('gl_customer_visit')
  160 + ->select('url',DB::raw('COUNT(*) as num'))
  161 + ->orderBy('num','desc')
  162 + ->whereBetween('updated_date', [$startTime,$endTime])
  163 + ->groupBy('url')
  164 + ->limit(15)->get()->toArray();
  165 + $arr['referrer_url'] = json_encode($referrer_url);
  166 + //访问端口
  167 + $referrer_port = DB::connection('custom_mysql')->table('gl_customer_visit')
  168 + ->select('device_port',DB::raw('COUNT(*) as num'))
  169 + ->orderBy('num','desc')
  170 + ->whereBetween('updated_date', [$startTime,$endTime])
  171 + ->groupBy('device_port')
  172 + ->limit(15)->get()->toArray();
  173 + $arr['referrer_port'] = json_encode($referrer_port);
  174 + return $arr;
  175 + }
  176 +
  177 + public function inquiry($url,$month){
  178 + $url = 'https://'.$url.'/';
  179 + $token = md5($url.date("Y-m-d"));
  180 + $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;
  181 + $res = http_get($url,['charset=utf-8']);
  182 + echo date('Y-m-d H:i:s') . '数据:'.json_encode($res) . PHP_EOL;
  183 + return $res;
  184 + }
  185 +
  186 +}