作者 lyh

变更数据

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :GeoCount.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/10/13 16:05
  8 + */
  9 +
  10 +namespace App\Console\Commands\Geo;
  11 +
  12 +use App\Models\Geo\GeoQuestionLog;
  13 +use App\Models\Geo\GeoQuestionResult;
  14 +use Illuminate\Console\Command;
  15 +use Illuminate\Support\Carbon;
  16 +use App\Models\Geo\GeoCount as GeoCountModel;
  17 +
  18 +class GeoCountAll extends Command
  19 +{
  20 + /**
  21 + * The name and signature of the console command.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $signature = 'geo_count_all';
  26 +
  27 + public $porject_id;//记录当时执行的project_id
  28 +
  29 + /**
  30 + * The console command description.
  31 + *
  32 + * @var string
  33 + */
  34 + protected $description = 'geo统计数据';
  35 +
  36 +
  37 +
  38 + public function handle(){
  39 + $this->output('start');
  40 + $this->_action();
  41 + $this->output('end');
  42 + return true;
  43 + }
  44 +
  45 + /**
  46 + * @remark :方法
  47 + * @name :_action
  48 + * @author :lyh
  49 + * @method :post
  50 + * @time :2025/10/13 16:20
  51 + */
  52 + public function _action()
  53 + {
  54 + $geoCountModel = new GeoCountModel();
  55 + //获取前一天的项目id
  56 + $dates = [];
  57 + for ($i = 0; $i < 30; $i++) {
  58 + $dates[] = date('Y-m-d', strtotime("-{$i} days"));
  59 + }
  60 + foreach ($dates as $date) {
  61 + $start = $date.' 00:00:00';
  62 + $end = $date.' 23:59:59';
  63 + $geoLogModel = new GeoQuestionLog();
  64 + $project_id = $geoLogModel->formatQuery(['created_at' => ['between',[$start,$end]]])->distinct()->pluck('project_id');
  65 + if(empty($project_id)){
  66 + return true;
  67 + }
  68 + $geoQuestionResModel = new GeoQuestionResult();
  69 + $platforms = ['gemini','openai','deepseek','poe','perplexity','google_ai_overview','openai-not-network','claude'];
  70 + foreach ($project_id as $item){
  71 + $this->output('执行的项目ID----'.$item);
  72 + //收录总数
  73 + $total = $geoQuestionResModel->counts(['project_id' => $item,'hit'=>['!=',0],'created_at' => ['between',[$start,$end]]]);
  74 + $data = [
  75 + 'project_id' => $item,
  76 + 'date' => $date,
  77 + 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  78 + 'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
  79 + 'total'=>$total,//收录总数
  80 + ];
  81 + foreach ($platforms as $platform){
  82 + if($platform == 'openai-not-network'){
  83 + $data['openai_not_network'] = $geoQuestionResModel->counts(['project_id' => $item,'hit'=>['!=',0],'platform'=>$platform,'created_at' => ['between',[$start,$end]]]);
  84 + }else{
  85 + $data[$platform] = $geoQuestionResModel->counts(['project_id' => $item,'hit'=>['!=',0],'platform'=>$platform,'created_at' => ['between',[$start,$end]]]);
  86 + }
  87 + }
  88 + $info = $geoCountModel->read(['project_id' => $item,'date'=>$date]);
  89 + if($info === false){
  90 + //新增一条数据
  91 + $geoCountModel->addReturnId($data);
  92 + }
  93 + }
  94 + }
  95 +
  96 + return true;
  97 + }
  98 +
  99 + /**
  100 + * 输出日志
  101 + * @param $message
  102 + * @return bool
  103 + */
  104 + public function output($message)
  105 + {
  106 + echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  107 + return true;
  108 + }
  109 +}