ProjectUpdate.php 12.0 KB
<?php

namespace App\Console\Commands\Update;

use App\Helper\Arr;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Http\Logic\Bside\Product\KeywordLogic;
use App\Models\Blog\Blog;
use App\Models\Com\UpdateLog;
use App\Models\News\News;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 4.0,5.0升级到6.0,内容同步
 * Class ProjectImport
 * @package App\Console\Commands
 * @author Akun
 * @date 2023/10/9 15:04
 */
class ProjectUpdate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project_update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '执行项目升级任务';


    public function handle()
    {
        while (true) {
            $this->start_update();
        }
    }

    protected function start_update()
    {
        $task_id = $this->get_task();
        if (!$task_id) {
            sleep(2);
            return true;
        }

        $task = UpdateLog::where('id', $task_id)->where('status', UpdateLog::STATUS_UN)->first();
        if (!$task) {
            sleep(2);
            return true;
        }

        $project_id = $task->project_id;
        $api_type = $task->api_type;
        $api_url_arr = explode('?', $task->api_url);
        $api_url = $api_url_arr[0];

        $page_size = 20;

        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;

        $task->status = UpdateLog::STATUS_ING;//同步中
        $task->save();

        //设置数据库
        $project = ProjectServer::useProject($task->project_id);
        if ($project) {
            if ($api_type == 'website_info') {
                //关键词
                $url = $api_url . '?' . http_build_query(['w' => 'website_info']);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $items = $data['data']['tags'] ?? [];

                    $model = new Keyword();
                    foreach ($items as $item) {
                        if (strlen($item) > 50) {
                            continue;
                        }
                        $keyword = $model->read(['title' => $item], 'id');
                        if (!$keyword) {
                            $id = $model->addReturnId([
                                'project_id' => $project_id,
                                'title' => $item
                            ]);
                            $route = RouteMap::setRoute($item, RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
                            $model->edit(['route' => $route], ['id' => $id]);
                        }
                    }
                }
            } elseif ($api_type == 'post') {
                //产品
                $url = $api_url . '?' . http_build_query(['w' => 'post', 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $count = $data['data']['count'] ?? 0;

                    $total_page = ceil($count / $page_size);
                    for ($page = 1; $page <= $total_page; $page++) {
                        $url_page = $api_url . '?' . http_build_query(['w' => 'post', 'page' => $page, 'pagesize' => $page_size]);
                        $data_page = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            $model = new Product();

                            foreach ($items as $item) {

                                $product = $model->read(['title' => $item['ttile']], 'id');
                                if (!$product) {
                                    //图片
                                    $gallery = [];
                                    foreach ($item['images'] as $k_img => $img) {
                                        $gallery[] = ['alt' => '这是一张产品图', 'url' => $img];
                                    }
                                    //关键词
                                    $keyword_id = '';
                                    if ($item['keywords']) {
                                        $keywordLogic = new KeywordLogic();
                                        $keyword_id = $keywordLogic->importProductKeyword($project_id, $item['keywords']);
                                    }
                                    //分类
                                    $category_id = '';
                                    if ($item['category']) {
                                        $category_arr = [];
                                        foreach ($item['category'] as $cate) {
                                            if ($cate['parent'] == 0) {
                                                array_unshift($category_arr, $cate['name']);
                                            } else {
                                                array_push($category_arr, $cate['name']);
                                            }
                                        }
                                        if ($category_arr) {
                                            $categoryLogic = new CategoryLogic();
                                            $category_id = $categoryLogic->importProductCategory($project_id, implode('/', $category_arr));
                                        }
                                    }
                                    $id = $model->addReturnId([
                                        'project_id' => $project_id,
                                        'title' => $item['ttile'],
                                        'intro' => $item['description'],
                                        'content' => $item['content'],
                                        'keyword_id' => $keyword_id,
                                        'category_id' => $category_id,
                                        'thumb' => isset($gallery[0]) ? Arr::a2s($gallery[0]) : '',
                                        'gallery' => Arr::a2s($gallery),
                                        'seo_mate' => Arr::a2s([
                                            'title' => $item['ttile'],
                                            'keyword' => $item['keywords'],
                                            'description' => $item['description']
                                        ]),
                                        'status' => Product::STATUS_ON
                                    ]);
                                    $route = RouteMap::setRoute($item['ttile'], RouteMap::SOURCE_PRODUCT, $id, $project_id);
                                    $model->edit(['route' => $route], ['id' => $id]);
                                }
                            }
                        }
                    }
                }
            } elseif ($api_type == 'news' || $api_type == 'blog') {
                //新闻或博客
                $url = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $count = $data['data']['count'] ?? 0;

                    $total_page = ceil($count / $page_size);
                    for ($page = 1; $page <= $total_page; $page++) {
                        $url_page = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => $page, 'pagesize' => $page_size]);
                        $data_page = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            if ($api_type == 'news') {
                                $model = new News();
                            } else {
                                $model = new Blog();
                            }

                            foreach ($items as $item) {

                                $news = $model->read(['name' => $item['ttile']], 'id');
                                if (!$news) {
                                    $id = $model->addReturnId([
                                        'project_id' => $project_id,
                                        'name' => $item['ttile'],
                                        'seo_title' => $item['ttile'],
                                        'text' => $item['content'],
                                        'image' => $item['images'][0] ?? '',
                                        'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE
                                    ]);
                                    $route = RouteMap::setRoute($item['ttile'], $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
                                    $model->edit(['url' => $route], ['id' => $id]);
                                }
                            }
                        }
                    }
                }
            } else {
                //单页
                $url = $api_url . '?' . http_build_query(['w' => 'page', 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $count = $data['data']['count'] ?? 0;

                    $total_page = ceil($count / $page_size);
                    for ($page = 1; $page <= $total_page; $page++) {
                        $url_page = $api_url . '?' . http_build_query(['w' => 'page', 'page' => $page, 'pagesize' => $page_size]);
                        $data_page = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            $model = new BCustomTemplate();

                            foreach ($items as $item) {

                                $custom = $model->read(['name' => $item['ttile']], 'id');
                                if (!$custom) {
                                    $id = $model->addReturnId([
                                        'project_id' => $project_id,
                                        'name' => $item['ttile'],
                                        'html' => $item['content']
                                    ]);
                                    $route = RouteMap::setRoute($item['ttile'], RouteMap::SOURCE_PAGE, $id, $project_id);
                                    $model->edit(['url' => $route], ['id' => $id]);
                                }
                            }
                        }
                    }
                }
            }
        }
        //关闭数据库
        DB::disconnect('custom_mysql');

        $task->status = UpdateLog::STATUS_COM;//同步完成
        $task->save();

        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;

        sleep(2);
    }

    //获取任务
    protected function get_task()
    {
        $key = 'console_update_task';
        $task_id = Redis::rpop($key);
        if ($task_id) {
            return $task_id;
        }

        $task_list = UpdateLog::where('status', UpdateLog::STATUS_UN)->limit(20)->get();
        if ($task_list->count() == 0) {
            return false;
        }

        foreach ($task_list as $value) {
            Redis::lpush($key, $value->id);
        }

        $task_id = Redis::rpop($key);
        return $task_id;
    }

}