TdkTest.php 3.1 KB
<?php

namespace App\Console\Commands;

use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Models\Ai\AiCommand;
use App\Models\Mail\Mail;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Models\Project\ProjectUpdateTdk;
use App\Models\User\User;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 初始化项目
 * Class InitProject
 * @package App\Console\Commands
 * @author zbj
 * @date 2023/10/8
 */
class TdkTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tdk_test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'keywords ,分割加空格';

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

    /**
     * '表' => [
     *      '指令key' => '表字段'
     * ]
     * @return array
     * @author zbj
     * @date 2023/11/3
     */
    protected $maps = [
        'gl_web_custom_template' => 'keywords',
        'gl_product' => 'seo_mate.keyword',
        'gl_product_category' => 'seo_keywords',
        'gl_blog' => 'seo_keywords',
        'gl_blog_category' => 'seo_keywords',
        'gl_news' => 'seo_keywords',
        'gl_news_category' => 'seo_keywords',
        'gl_product_keyword' => 'seo_keywords',
    ];

    /**
     * @return bool
     */
    public function handle()
    {
        $project_ids = Project::where('type', Project::TYPE_TWO)->pluck('id')->toArray();
        foreach ($project_ids as $project_id){
            echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL;
            ProjectServer::useProject($project_id);
            foreach ($this->maps as $table=>$field){
                $list = DB::connection('custom_mysql')->table($table)->get();
                foreach ($list as $item){
                   $item = (array) $item;
                    $field_arr = explode('.', $field);
                    if ($field == 'seo_mate.keyword') {
                        $data = json_decode($item[$field_arr[0]], true);
                        $value = $data['keyword'] ?? '';
                        if(!$value){
                            continue;
                        }
                        $data['keyword'] = implode(', ', array_map('trim', explode(',', $value)));
                        DB::connection('custom_mysql')->table($table)->where('id', $item['id'])->update(['seo_mate' => json_encode($data)]);
                    } else {
                        $value = $item[$field];
                        if(!$value){
                            continue;
                        }
                        $value = implode(', ', array_map('trim', explode(',', $value)));
                        DB::connection('custom_mysql')->table($table)->where('id', $item['id'])->update([$field => $value]);
                    }
               }
            }
        }
    }
}