ProjectUpdateTemp.php 7.8 KB
<?php

namespace App\Console\Commands\Update;

use App\Helper\Arr;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Models\Collect\CollectSource;
use App\Models\Collect\CollectTask;
use App\Models\Com\UpdateLog;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Services\CosService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

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


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

    protected function start_update()
    {
        $list = UpdateLog::where('api_type', 'post')->get();

        foreach ($list as $task) {


            $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;

            //设置数据库
            $project = ProjectServer::useProject($project_id);
            if ($project) {
                if ($api_type == 'category') {
                    //产品分类
                    $url = $api_url . '?' . http_build_query(['w' => 'category']);
                    $data = curl_c($url);
                    if (isset($data['code']) && $data['code'] == 200) {
                        $items = $data['data'] ?? [];
                        $this->category_insert($project_id, $items, 0);
                    } else {
                        return true;
                    }
                } elseif ($api_type == 'post') {
                    //产品
                    $url = $api_url . '?' . http_build_query(['w' => 'post', 'page' => 1, 'pagesize' => 0]);
                    $data = curl_c($url);
                    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 = curl_c($url_page);
                            if (isset($data_page['code']) && $data_page['code'] == 200) {
                                $items = $data_page['data']['data'] ?? [];

                                $model = new Product();
                                $category_model = new Category();
                                $logic = new CategoryLogic();

                                foreach ($items as $item) {
                                    $route = $this->get_url_route($item['url'] ?? '');
                                    if ($route) {
                                        $product = $model->read(['route' => $route], 'id');
                                        if ($product) {
                                            $category_id = '';
                                            if ($item['category'] ?? []) {
                                                $category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);
                                                $category_id = $logic->getLastCategory(array_column($category_arr, 'id'));
                                            }

                                            $model->edit(['category_id' => $category_id, 'product_type' => ''], ['id' => $product['id']]);
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        return true;
                    }
                }
            }
            //关闭数据库
            DB::disconnect('custom_mysql');

            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_url_route($url)
    {
        $arr = parse_url(urldecode($url));
        if (empty($arr['path'])) {
            return '';
        }
        $path = $arr['path'];

        if (strpos($path, '.') !== false) {
            $path = substr($path, 0, strpos($path, '.'));
        }

        $path_arr = explode('/', $path);

        return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
    }

    //产品多级分类入库
    protected function category_insert($project_id, $items, $pid = 0)
    {
        $model = new Category();
        foreach ($items as $item) {
            $route = $this->get_url_route($item['url'] ?? '');
            if ($route) {
                $parent = $model->read(['pid' => $pid, 'route' => $route], 'id');
                if (!$parent) {
                    try {
                        $item['name'] = $this->special2str($item['name'] ?? '');
                        $parent_id = $model->addReturnId([
                            'project_id' => $project_id,
                            'title' => $item['name'],
                            'pid' => $pid,
                            'keywords' => $item['keywords'] ?? '',
                            'describe' => $item['description'] ?? '',
                            'original_id' => $item['id'],
                            'route' => $route
                        ]);
                        $this->set_map($route, RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
                    } catch (\Exception $e) {
                        echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;
                        continue;
                    }
                } else {
                    $parent_id = $parent['id'];
                }

                if (!empty($item['children'])) {
                    $this->category_insert($project_id, $item['children'], $parent_id);
                }
            }
        }
    }

    //特殊字符转换
    protected function special2str($str)
    {
        if (strpos($str, ';') === false) {
            return $str;
        }

        $list = [
            '&lt;' => '<',
            '&gt;' => '>',
            '&amp;' => '&',
            '&acute;' => '´',
            '&quot;' => '“',
            '&nbsp;' => ' '
        ];

        foreach ($list as $k => $v) {
            $str = str_replace($k, $v, $str);
        }

        return $str;
    }

    //路由入库
    protected function set_map($route, $source, $source_id, $project_id)
    {
        if ($route) {
            $route_map = RouteMap::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->first();
            if (!$route_map) {
                $route_map = new RouteMap();
                $route_map->project_id = $project_id;
                $route_map->source = $source;
                $route_map->source_id = $source_id;
                $route_map->route = $route;

                if ($source == RouteMap::SOURCE_NEWS) {
                    $route_map->path = RouteMap::SOURCE_NEWS;
                } elseif ($source == RouteMap::SOURCE_BLOG) {
                    $route_map->path = RouteMap::SOURCE_BLOG;
                }

                $route_map->save();
            }
        }
    }
}