GeoCountAll.php
3.4 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
<?php
/**
* @remark :
* @name :GeoCount.php
* @author :lyh
* @method :post
* @time :2025/10/13 16:05
*/
namespace App\Console\Commands\Geo;
use App\Models\Geo\GeoQuestionLog;
use App\Models\Geo\GeoQuestionResult;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use App\Models\Geo\GeoCount as GeoCountModel;
class GeoCountAll extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'geo_count_all';
public $porject_id;//记录当时执行的project_id
/**
* The console command description.
*
* @var string
*/
protected $description = 'geo统计数据';
public function handle(){
$this->output('start');
$this->_action();
$this->output('end');
return true;
}
/**
* @remark :方法
* @name :_action
* @author :lyh
* @method :post
* @time :2025/10/13 16:20
*/
public function _action()
{
$geoCountModel = new GeoCountModel();
//获取前一天的项目id
$dates = [];
for ($i = 0; $i < 100; $i++) {
$dates[] = date('Y-m-d', strtotime("-{$i} days"));
}
foreach ($dates as $date) {
$start = $date.' 00:00:00';
$end = $date.' 23:59:59';
$geoLogModel = new GeoQuestionLog();
$project_id = $geoLogModel->formatQuery(['created_at' => ['between',[$start,$end]]])->distinct()->pluck('project_id');
if(empty($project_id)){
return true;
}
$geoQuestionResModel = new GeoQuestionLog();
$platforms = ['gemini','openai','deepseek','poe','perplexity','google_ai_overview','openai-not-network','claude'];
foreach ($project_id as $item){
$this->output('执行的项目ID----'.$item);
//收录总数
$total = $geoQuestionResModel->counts(['project_id' => $item,'hit'=>['!=',0],'created_at' => ['between',[$start,$end]]]);
$data = [
'project_id' => $item,
'date' => $date,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
'total'=>$total,//收录总数
];
foreach ($platforms as $platform){
if($platform == 'openai-not-network'){
$data['openai_not_network'] = $geoQuestionResModel->counts(['project_id' => $item,'hit'=>['!=',0],'platform'=>$platform,'created_at' => ['between',[$start,$end]]]);
}else{
$data[$platform] = $geoQuestionResModel->counts(['project_id' => $item,'hit'=>['!=',0],'platform'=>$platform,'created_at' => ['between',[$start,$end]]]);
}
}
$info = $geoCountModel->read(['project_id' => $item,'date'=>$date]);
if($info === false){
//新增一条数据
$geoCountModel->addReturnId($data);
}else{
$geoCountModel->edit($data,['id'=>$info['id']]);
}
}
}
return true;
}
/**
* 输出日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
return true;
}
}