RerunSeoTdk.php 8.1 KB
<?php

namespace App\Console\Commands\Tdk;


use App\Helper\Arr;
use App\Models\Blog\BlogCategory;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Keyword;
use App\Models\Project\KeywordPrefix;
use App\Models\Project\Project;
use App\Models\Project\ProjectUpdateTdk;
use App\Models\Template\BCustomTemplate;
use App\Services\ProjectServer;
use App\Utils\LogUtils;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
 * 重跑异常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)->where('site_status',0)->where('extend_type',0)->where('delete_status',0)->where('tag_page_version', '>' ,1)->where('uptime', '<=', date('Y-m-d H:i:s'))->pluck('id')->toArray();
        foreach ($project_ids as $project_id){
            try {
                $project = ProjectServer::useProject($project_id);

                //小语种
                if($project['main_lang_id'] != 1){
                    $this->needTransKeywordPage($project_id);
                }
                DB::disconnect('custom_mysql');
            }catch (\Exception $e){
                dump($e->getMessage());
            }
        }
    }

    public function needTransKeywordPage($project_id)
    {
        $row = Keyword::whereNotNull('sale_title')->update(['sale_title' => null, 'sale_content' => null, 'count_title'=>null, 'table_html'=>null, 'count_html' => null]);
        dump($project_id .' - ' .$row);
    }


    /**
     * 换了公司英文名的
     * @author zbj
     * @date 2025/7/18
     */
    public function changeCompanyName($project_id){
        $row1 = BCustomTemplate::where('description', 'like', '%BlueQ Biotechnology%')->update(['description' => '']);
        $row2 = Category::where('seo_des', 'like', '%BlueQ Biotechnology%')->update(['seo_des' => '']);
        $row3 = Keyword::where('seo_description', 'like', '%BlueQ Biotechnology%')->update(['seo_description' => '']);
        $row4 = Keyword::where('keyword_content', 'like', '%BlueQ Biotechnology%')->update(['keyword_content' => '']);
        $row5 = BlogCategory::where('seo_des', 'like', '%BlueQ Biotechnology%')->update(['seo_des' => '']);
        $row6 = NewsCategory::where('seo_des', 'like', '%BlueQ Biotechnology%')->update(['seo_des' => '']);
        $row7 = CustomModuleCategory::where('seo_description', 'like', '%BlueQ Biotechnology%')->update(['seo_description' => '']);


        dump($row1,$row2,$row3,$row4,$row5,$row6,$row7);
    }


    /**
     * 判断seo_title  前缀有wholesale或cheap或buy的词,后缀也有 manufacturer,factory,exporter,company
     * 判断关键词最后一个词是前缀的词,前后缀都不拼
     * @author zbj
     * @date 2025/4/12
     */
//    public function judgeAnomalies($project_id){
//        dump($project_id);
//        $all_prefixes = $this->getAllPrefix(1, $project_id);
//        $all_prefixes = array_map('strtolower', $all_prefixes);
//
//        //获取当前项目的所有分类
//        $list = Keyword::select('title', 'seo_title', 'id')->get()->toArray();
//        //新闻 seo_keyword 和 分类名一样的
//        $ids = [];
//        foreach ($list as $k=>$item){
//            $seo_title = $item['seo_title'];
//            $id = $item['id'];
//            $title = $item['title'];
//            if(Str::startsWith(strtolower($seo_title), ['wholesale', 'cheap', 'buy']) && Str::contains(strtolower($seo_title), ['manufacturer', 'manufacturers', 'factory', 'factories', 'exporter', 'exporters', 'company', 'companies', 'supplier', 'suppliers'])){
//                $ids[] = $id;
//                dump($seo_title);
//                continue;
//            }
//            $topic_words = explode(' ', strtolower($title));
//            //关键词最后一个是前缀 且 有前后缀
//            if(in_array(Arr::last($topic_words), $all_prefixes) && $title != $seo_title){
//                $ids[] = $id;
//                dump($seo_title);
//                continue;
//            }
//        }
//
//        $count = count($ids);
//        if($count){
//            echo "项目{$project_id},共{$count}条需要重跑";
//            LogUtils::info("RerunSeoTdk: 项目{$project_id},共{$count}条需要重跑");
//            Keyword::whereIn('id', $ids)->update(['seo_title' => '']);
//            ProjectUpdateTdk::add_task($project_id);
//        }
//    }

//    /**
//     * 判断seo_title后缀重复
//     * @author zbj
//     * @date 2025/4/12
//     */
//    public function judgeAnomalies($project_id){
//        //获取当前项目的所有分类
//        $seo_titles = Keyword::pluck('seo_title', 'id')->toArray();
//        //新闻 seo_keyword 和 分类名一样的
//        $ids = [];
//        //Suppliers, Manufacturer
//        foreach ($seo_titles as $id=>$seo_title){
//            if(!Str::contains($seo_title, ', ')){
//                continue;
//            }
//            $arr = explode(', ', $seo_title);
//            $suffix1 = $arr[1];
//            $arr = explode(' ', $arr[0]);
//            $suffix2 = Arr::last($arr);
//            if(Str::singular($suffix1) == Str::singular($suffix2)){
//                $ids[] = $id;
//            }
//        }
//
//        $count = count($ids);
//        if($count){
//            echo "项目{$project_id},共{$count}条需要重跑";
//            Keyword::whereIn('id', $ids)->update(['seo_title' => '']);
//            ProjectUpdateTdk::add_task($project_id);
//        }
//    }

//    /**
//     * 判断异常
//     * @author zbj
//     * @date 2025/4/12
//     */
//    public function judgeAnomalies($project_id){
//        //获取当前项目的所有分类
//        $categories = NewsCategory::pluck('alias', '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);
//        }
//    }

    public function judgeAnomalies($project_id)
    {
        $page_title = Keyword::where('keyword_title', 'like', '%qz_1%')->orWhere('keyword_title', 'like', '%qz_2%')->update(['keyword_title' => '']);
        $page_content = Keyword::where('keyword_content', 'like', '%qz_1%')->orWhere('keyword_content', 'like', '%qz_2%')->update(['keyword_content' => '']);
        $seo_description = Keyword::where('seo_description', 'like', '%qz_1%')->orWhere('seo_description', 'like', '%qz_2%')->update(['seo_description' => '']);

        if($page_title || $page_content || $seo_description){
            echo "项目{$project_id},需要重跑";
            dump($page_title);
            dump($page_content);
            dump($seo_description);
        }
    }



    public function getAllPrefix($type, int $project_id = 0){
        $cache_key = 'AllPrefix_' . $type . '_' . $project_id;
        $data = Cache::get($cache_key);
        if(!$data){
            $data = KeywordPrefix::whereIn('project_id', [0, $project_id])->where('type', $type)->pluck('keyword')->toArray();
            Cache::put($cache_key, $data, 600);
        }
        return $data;
    }


}