GeoCountAll.php 3.4 KB
<?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;
    }
}