作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Tdk;
  4 +
  5 +
  6 +use App\Http\Controllers\Bside\News\NewsController;
  7 +use App\Models\News\News;
  8 +use App\Models\News\NewsCategory;
  9 +use App\Models\Project\Project;
  10 +use App\Models\Project\ProjectUpdateTdk;
  11 +use App\Services\ProjectServer;
  12 +use Illuminate\Console\Command;
  13 +use Illuminate\Support\Facades\DB;
  14 +
  15 +/**
  16 + * 重跑异常tdk
  17 + * Class RerunSeoTdk
  18 + * @package App\Console\Commands\Tdk
  19 + * @author zbj
  20 + * @date 2025/4/12
  21 + */
  22 +class RerunSeoTdk extends Command
  23 +{
  24 + /**
  25 + * The name and signature of the console command.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $signature = 'rerun_seo_tdk';
  30 +
  31 + /**
  32 + * The console command description.
  33 + *
  34 + * @var string
  35 + */
  36 + protected $description = '重跑项目sdk';
  37 +
  38 + /**
  39 + * Create a new command instance.
  40 + *
  41 + * @return void
  42 + */
  43 + public function __construct()
  44 + {
  45 + parent::__construct();
  46 + }
  47 +
  48 + /**
  49 + * @return bool
  50 + */
  51 + public function handle()
  52 + {
  53 + $project_ids = Project::where('type', Project::TYPE_TWO)->pluck('id')->toArray();
  54 + foreach ($project_ids as $project_id){
  55 + try {
  56 + ProjectServer::useProject($project_id);
  57 + $this->judgeAnomalies($project_id);
  58 + DB::disconnect('custom_mysql');
  59 + }catch (\Exception $e){
  60 + dump($e->getMessage());
  61 + }
  62 + }
  63 + }
  64 +
  65 + /**
  66 + * 判断异常
  67 + * @author zbj
  68 + * @date 2025/4/12
  69 + */
  70 + public function judgeAnomalies($project_id){
  71 + //获取当前项目的所有分类
  72 + $categories = NewsCategory::pluck('name', 'id')->toArray();
  73 + //新闻 seo_keyword 和 分类名一样的
  74 + $news_ids = [];
  75 + foreach ($categories as $category){
  76 + $ids = News::WhereRaw("FIND_IN_SET('{$category}', `seo_keywords`)")->pluck('id')->toArray();
  77 + $news_ids = array_unique(array_merge($news_ids, $ids));
  78 + }
  79 +
  80 + $count = count($news_ids);
  81 + if($count){
  82 + echo "项目{$project_id},共{$count}条需要重跑";
  83 + News::whereIn('id', $news_ids)->update(['seo_keywords' => '']);
  84 + ProjectUpdateTdk::add_task($project_id);
  85 + }
  86 + }
  87 +}