OptimizeSetKeywordSync.php 2.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2025/3/26
 * Time: 14:29
 */
namespace App\Console\Commands\Product;

use App\Models\Product\Keyword;
use App\Models\Project\AggregateKeyword;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * Class OptimizeSetKeywordSync
 * @package App\Console\Commands\Product
 */
class OptimizeSetKeywordSync extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'optimize_set_keyword_sync';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '优化设置聚合页关键词,同步至对应项目产品关键词';

    /**
     * OptimizeSetKeywordSync constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 数据源:App\Http\Logic\Aside\Project\AggregateKeywordLogic::saveKeyword;
     * @return bool
     */
    public function handle()
    {
        $task = AggregateKeyword::where(['status' => AggregateKeyword::STATUS_INIT])->where('created_at', '<', date('Y-m-d H:i:s'))->get();
        foreach ($task as $item) {
            Log::info('同步ID:' . $item->id);
            echo $item->project_id . PHP_EOL;
            $this->syncKeyword($item->project_id, $item->keywords);
            $item->status = AggregateKeyword::STATUS_FINISH;
            $item->save();
        }
        return true;
    }

    /**
     * 关键词同步
     * @param $project_id
     * @param $keywords
     * @return bool
     */
    public function syncKeyword($project_id, $keywords)
    {
        $keywords = explode("\n", $keywords);
        $keywords = array_unique(array_filter($keywords));

        ProjectServer::useProject($project_id);
        $keywordModel = new Keyword();
        $keywordModel->saveBKeyword($project_id,$keywords);
        DB::disconnect('custom_mysql');
        return true;
    }
}