ProjectUpdate.php 19.1 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\Category;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use App\Models\WebSetting\WebSettingReceiving;
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(60);
            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 == 'category') {
                //分类
                $url = $api_url . '?' . http_build_query(['w' => 'category']);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $items = $data['data'] ?? [];
                    $model = new Category();
                    foreach ($items as $item) {
                        $parent = $model->read(['pid' => 0, 'title' => $item['name']], 'id');
                        if (!$parent) {
                            try {
                                $parent_id = $model->addReturnId([
                                    'project_id' => $project_id,
                                    'title' => $item['name'],
                                    'pid' => 0,
                                    'keywords' => $item['keywords'],
                                    'describe' => $item['description']
                                ]);
                                $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
                                $model->edit(['route' => $route], ['id' => $parent_id]);
                            } catch (\Exception $e) {
                                echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
                                continue;
                            }
                        }else{
                            $parent_id = $parent['id'];
                        }

                        foreach ($item['children'] as $child) {
                            $child_info = $model->read(['pid' => $parent_id, 'title' => $child['name']]);
                            if (!$child_info) {
                                try {
                                    $child_id = $model->addReturnId([
                                        'project_id' => $project_id,
                                        'title' => $child['name'],
                                        'pid' => $parent_id,
                                        'keywords' => $child['keywords'],
                                        'describe' => $child['description']
                                    ]);
                                    $route = RouteMap::setRoute($child['url'] ? $this->get_url_route($child['url']) : $child['name'], RouteMap::SOURCE_PRODUCT_CATE, $child_id, $project_id);
                                    $model->edit(['route' => $route], ['id' => $child_id]);
                                } catch (\Exception $e) {
                                    echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
                                    continue;
                                }
                            }
                        }
                    }
                }
            } elseif ($api_type == 'tag') {
                //关键词
                $url = $api_url . '?' . http_build_query(['w' => 'tag', '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' => 'tag', '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 Keyword();
                            foreach ($items as $item) {
                                $keyword = $model->read(['title' => $item['text_title']], 'id');
                                if (!$keyword) {
                                    try {
                                        $id = $model->addReturnId([
                                            'project_id' => $project_id,
                                            'title' => $item['text_title'],
                                            'seo_title' => $item['seo_title'],
                                            'seo_keywords' => $item['seo_keywords'],
                                            'seo_description' => $item['seo_description'],
                                        ]);
                                        $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['text_title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
                                        $model->edit(['route' => $route], ['id' => $id]);
                                    } catch (\Exception $e) {
                                        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            } elseif ($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) {
                    $phones = $data['data']['phones'] ?? '';
                    $emails = $data['data']['emails'] ?? '';

                    $model = new WebSettingReceiving();
                    if ($phones) {
                        $receiving_phones = $model->read(['type' => 1, 'values' => $phones]);
                        if (!$receiving_phones) {
                            $model->add([
                                'type' => 1,
                                'values' => $phones,
                                'project_id' => $project_id
                            ]);
                        }
                    }
                    if ($emails) {
                        $receiving_emails = $model->read(['type' => 2, 'values' => $emails]);
                        if (!$receiving_emails) {
                            $model->add([
                                'type' => 2,
                                'values' => $emails,
                                'project_id' => $project_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];
                                    }
                                    //分类
                                    $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));
                                        }
                                    }
                                    try {
                                        $id = $model->addReturnId([
                                            'project_id' => $project_id,
                                            'title' => $item['ttile'],
                                            'intro' => $item['description'],
                                            'content' => $item['content'],
                                            '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['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PRODUCT, $id, $project_id);
                                        $model->edit(['route' => $route], ['id' => $id]);
                                    } catch (\Exception $e) {
                                        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            } 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) {
                                    try {
                                        $id = $model->addReturnId([
                                            'project_id' => $project_id,
                                            'name' => $item['ttile'],
                                            'seo_title' => $item['ttile'],
                                            'seo_keywords' => $item['keywords'],
                                            'seo_description' => $item['description'],
                                            'text' => $item['content'],
                                            'image' => $item['images'][0] ?? '',
                                            'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE
                                        ]);
                                        $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
                                        $model->edit(['url' => $route], ['id' => $id]);
                                    } catch (\Exception $e) {
                                        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            } 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) {
                                    try {
                                        $id = $model->addReturnId([
                                            'project_id' => $project_id,
                                            'name' => $item['ttile'],
                                            'title' => $item['ttile'],
                                            'keywords' => $item['keywords'],
                                            'description' => $item['description'],
                                            'html' => $item['content'],
                                            'status' => 1
                                        ]);
                                        $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PAGE, $id, $project_id);
                                        $model->edit(['url' => $route], ['id' => $id]);
                                    } catch (\Exception $e) {
                                        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        //关闭数据库
        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)->orderBy('project_id', 'asc')->orderBy('sort', 'asc')->limit(7)->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;
    }

    //获取地址路由
    protected function get_url_route($url)
    {
        $arr = explode('/', $url);
        return $arr[count($arr) - 2];
    }
}