作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Helper\Common;
  7 +use App\Helper\Gpt;
  8 +use App\Helper\Translate;
  9 +use App\Models\Ai\AiCommand;
  10 +use App\Models\Mail\Mail;
  11 +use App\Models\Project\DeployOptimize;
  12 +use App\Models\Project\Project;
  13 +use App\Models\Project\ProjectUpdateTdk;
  14 +use App\Models\User\User;
  15 +use App\Services\ProjectServer;
  16 +use Illuminate\Console\Command;
  17 +use Illuminate\Support\Facades\Cache;
  18 +use Illuminate\Support\Facades\DB;
  19 +use Illuminate\Support\Facades\Redis;
  20 +
  21 +/**
  22 + * 初始化项目
  23 + * Class InitProject
  24 + * @package App\Console\Commands
  25 + * @author zbj
  26 + * @date 2023/10/8
  27 + */
  28 +class TdkTest extends Command
  29 +{
  30 + /**
  31 + * The name and signature of the console command.
  32 + *
  33 + * @var string
  34 + */
  35 + protected $signature = 'tdk_test';
  36 +
  37 + /**
  38 + * The console command description.
  39 + *
  40 + * @var string
  41 + */
  42 + protected $description = 'keywords ,分割加空格';
  43 +
  44 + /**
  45 + * Create a new command instance.
  46 + *
  47 + * @return void
  48 + */
  49 + public function __construct()
  50 + {
  51 + parent::__construct();
  52 + }
  53 +
  54 + /**
  55 + * '表' => [
  56 + * '指令key' => '表字段'
  57 + * ]
  58 + * @return array
  59 + * @author zbj
  60 + * @date 2023/11/3
  61 + */
  62 + protected $maps = [
  63 + 'gl_web_custom_template' => 'keywords',
  64 + 'gl_product' => 'seo_mate.keyword',
  65 + 'gl_product_category' => 'seo_keywords',
  66 + 'gl_blog' => 'seo_keywords',
  67 + 'gl_blog_category' => 'seo_keywords',
  68 + 'gl_news' => 'seo_keywords',
  69 + 'gl_news_category' => 'seo_keywords',
  70 + 'gl_product_keyword' => 'seo_keywords',
  71 + ];
  72 +
  73 + /**
  74 + * @return bool
  75 + */
  76 + public function handle()
  77 + {
  78 + $project_ids = Project::where('type', Project::TYPE_TWO)->pluck('id')->toArray();
  79 + foreach ($project_ids as $project_id){
  80 + echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL;
  81 + ProjectServer::useProject($project_id);
  82 + foreach ($this->maps as $table=>$field){
  83 + $list = DB::connection('custom_mysql')->table($table)->get();
  84 + foreach ($list as $item){
  85 + $item = (array) $item;
  86 + $field_arr = explode('.', $field);
  87 + if ($field == 'seo_mate.keyword') {
  88 + $data = json_decode($item[$field_arr[0]], true);
  89 + $value = $data['keyword'] ?? '';
  90 + if(!$value){
  91 + continue;
  92 + }
  93 + $data['keyword'] = implode(', ', array_map('trim', explode(',', $value)));
  94 + DB::connection('custom_mysql')->table($table)->where('id', $item['id'])->update(['seo_mate' => json_encode($data)]);
  95 + } else {
  96 + $value = $item[$field];
  97 + if(!$value){
  98 + continue;
  99 + }
  100 + $value = implode(', ', array_map('trim', explode(',', $value)));
  101 + DB::connection('custom_mysql')->table($table)->where('id', $item['id'])->update([$field => $value]);
  102 + }
  103 + }
  104 + }
  105 + }
  106 + }
  107 +}