RerunSeoTdk.php 2.2 KB
<?php

namespace App\Console\Commands\Tdk;


use App\Http\Controllers\Bside\News\NewsController;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Project\Project;
use App\Models\Project\ProjectUpdateTdk;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * 重跑异常tdk
 * Class RerunSeoTdk
 * @package App\Console\Commands\Tdk
 * @author zbj
 * @date 2025/4/12
 */
class RerunSeoTdk extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rerun_seo_tdk';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '重跑项目sdk';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return bool
     */
    public function handle()
    {
        $project_ids = Project::where('type', Project::TYPE_TWO)->pluck('id')->toArray();
        foreach ($project_ids as $project_id){
            try {
                ProjectServer::useProject($project_id);
                $this->judgeAnomalies($project_id);
                DB::disconnect('custom_mysql');
            }catch (\Exception $e){
                dump($e->getMessage());
            }
        }
    }

    /**
     * 判断异常
     * @author zbj
     * @date 2025/4/12
     */
    public function judgeAnomalies($project_id){
        //获取当前项目的所有分类
        $categories = NewsCategory::pluck('name', 'id')->toArray();
        //新闻 seo_keyword 和 分类名一样的
        $news_ids = [];
        foreach ($categories as $category){
            $ids = News::WhereRaw("FIND_IN_SET('{$category}', `seo_keywords`)")->pluck('id')->toArray();
            $news_ids = array_unique(array_merge($news_ids, $ids));
        }

        $count = count($news_ids);
        if($count){
            echo "项目{$project_id},共{$count}条需要重跑";
            News::whereIn('id', $news_ids)->update(['seo_keywords' => '']);
            ProjectUpdateTdk::add_task($project_id);
        }
    }
}