LyhImportTest.php 14.9 KB
<?php
/**
 * @remark :
 * @name   :LyhImportTest.php
 * @author :lyh
 * @method :post
 * @time   :2025/2/24 14:52
 */

namespace App\Console\Commands\LyhTest;

use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\CustomModule\CustomModuleExtentContent;
use App\Models\News\News;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Column;
use App\Models\Product\Detail;
use App\Models\Product\ExtendInfo;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Models\Template\Template;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class LyhImportTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'lyh_import_test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入数据';


    /**
     * @remark :统一更新路由
     * @name   :handle
     * @author :lyh
     * @method :post
     * @time   :2023/11/20 15:13
     */
    public function handle()
    {
        ProjectServer::useProject(3531);
        echo date('Y-m-d H:i:s') . 'start->3531' . PHP_EOL;
//        $this->importProductCategory('https://ecdn6-nc.globalso.com/upload/p/3654/file/2025-06/products-1.csv',3654);
        $this->import3531CustomModule(3531);
        DB::disconnect('custom_mysql');
        echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
    }

    /**
     * @remark :3951项目导入产品
     * @name   :import3951Product
     * @author :lyh
     * @method :post
     * @time   :2025/5/24 11:32
     */
    public function import3951Product($url, $project_id)
    {
        $line_of_text = [];
        $opts = [
            'http' => [
                'method' => 'GET',
                'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false
            ]
        ];
        $file_handle = fopen($url, 'r', null, stream_context_create($opts));
        while (!feof($file_handle)) {
            $line_of_text[] = fgetcsv($file_handle, 0, ',');
        }
        fclose($file_handle);
        $saveData = [];
        foreach ($line_of_text as $k => $val) {
            if ($k < 1) {
                continue;
            }
            if (empty($val[0])) {
                echo '跳过的名称:' . $val[0];
                continue;
            }
            $saveData[] = [
                'title' => $val[0],
                'thumb' => json_encode(['alt' => '主图', 'url' => '/upload/p/3951/image/', $val[2]], true),
                'gallery' => json_encode([['alt' => '主图', 'url' => '/upload/p/3951/image/', $val[2]]], true)
            ];
        }
    }

    /**
     * @remark :导入产品分类
     * @name   :productCategory
     * @author :lyh
     * @method :post
     * @time   :2025/3/13 9:58
     */
    public function importProductCategory($url, $project_id)
    {
        $line_of_text = [];
        $opts = [
            'http' => [
                'method' => 'GET',
                'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false
            ]
        ];
        $file_handle = fopen($url, 'r', null, stream_context_create($opts));
        while (!feof($file_handle)) {
            $line_of_text[] = fgetcsv($file_handle, 0, ',');
        }
        fclose($file_handle);
        $categoryModel = new Category();
        foreach ($line_of_text as $k => $val) {
            if ($k < 1) {
                continue;
            }
            if (empty($val[0])) {
                echo '跳过的名称:' . $val[0];
                continue;
            }
            try {
                $cateArr = explode('/', $val[0]);
                $pid = 0;
                $two_pid = 0;
                foreach ($cateArr as $key => $item) {
                    if ($key == 0) {
                        //查看一级分类是否存在
                        $info = $categoryModel->read(['title' => $item, 'pid' => 0], ['id']);
                        if ($info === false) {
                            $pid = $categoryModel->addReturnId(['project_id' => $project_id, 'title' => $item]);
                            //设置路由
                            $route = RouteMap::setRoute($item, RouteMap::SOURCE_PRODUCT_CATE, $pid, $project_id);
                            $categoryModel->edit(['route' => $route], ['id' => $pid]);
                        } else {
                            $pid = $info['id'];
                        }
                    } elseif ($key == 1) {
                        //查看当前下面的子级别是否存在
                        $two_info = $categoryModel->read(['title' => $item, 'pid' => $pid], ['id']);
                        if ($two_info === false) {
                            $two_pid = $categoryModel->addReturnId(['project_id' => $project_id, 'title' => $item, 'pid' => $pid]);
                            //设置路由
                            $route = RouteMap::setRoute($item, RouteMap::SOURCE_PRODUCT_CATE, $two_pid, $project_id);
                            $categoryModel->edit(['route' => $route], ['id' => $two_pid]);
                        } else {
                            $two_pid = $two_info['id'];
                        }
                    } else {
                        $id = $categoryModel->addReturnId(['project_id' => $project_id, 'title' => $item, 'pid' => $two_pid]);
                        $route = RouteMap::setRoute($item, RouteMap::SOURCE_PRODUCT_CATE, $id, $project_id);
                        $categoryModel->edit(['route' => $route], ['id' => $id]);
                    }
                }
                echo date('Y-m-d H:i:s') . '产品分类id:' . PHP_EOL;
            } catch (\Exception $e) {
                echo date('Y-m-d H:i:s') . '跳过的名称:' . $val[1];
                continue;
            }
        }
        return true;
    }

    /**
     * @remark :导入分类
     * @name   :importProductCategory
     * @author :lyh
     * @method :post
     * @time   :2025/3/3 15:59
     */
    public function importProduct($url, $project_id)
    {
        $line_of_text = [];
        $opts = [
            'http' => [
                'method' => 'GET',
                'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false
            ]
        ];
        $file_handle = fopen($url, 'r', null, stream_context_create($opts));
        while (!feof($file_handle)) {
            $line_of_text[] = fgetcsv($file_handle, 0, ',');
        }
        fclose($file_handle);
        $cateModel = new Category();
        $productModel = new Product();
        $detailModel = new Detail();
        $extentInfoModel = new ExtendInfo();
        foreach ($line_of_text as $k => $val) {
            if ($k < 1) {
                continue;
            }
            $saveData = [];
            if (!empty($val[11])) {
                $saveData['title'] = $val[11];
            } else {
                continue;
            }
            if (!empty($val[1])) {
                $cateInfo = $cateModel->read(['seo_title' => trim($val[1])]);
                if ($cateInfo !== false) {
                    $saveData['category_id'] = ',' . $cateInfo['id'] . ',';
                }
            }
            $saveData['project_id'] = $project_id;
            $saveData['status'] = 1;
            $saveData['intro'] = $val[6] ?? '';
            $seo = ['title' => $val[11]];
            $saveData['seo_mate'] = json_encode($seo, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $thumb = ['alt' => '主图', 'url' => str_replace('/public', '/upload/p/3283', $val[12])];
            $gallery = [['alt' => '主图', 'url' => str_replace('/public', '/upload/p/3283', $val[12])]];
            $saveData['thumb'] = json_encode($thumb, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $saveData['gallery'] = json_encode($gallery, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            try {
                $id = $productModel->addReturnId($saveData);
            } catch (\Exception $e) {
                @file_put_contents(storage_path('logs/lyh_error.log'), var_export('未导入成功--标题:' . $val[0], true) . PHP_EOL, FILE_APPEND);
                continue;
            }
            //设置关联关系
            if ($cateInfo !== false) {
                CategoryRelated::saveRelated($id, [$cateInfo['id']]);
            }
            //设置路由
            $route = RouteMap::setRoute($val[11], RouteMap::SOURCE_PRODUCT, $id, $project_id);
            $productModel->edit(['route' => $route], ['id' => $id]);
            echo date('Y-m-d H:i:s') . '新增产品id:' . $id . PHP_EOL;
            if (!empty($val[2])) {
                $extent = [
                    'key' => 'pd_extended_field_4',
                    'product_id' => $id,
                    'project_id' => $project_id,
                    'values' => $val[2],
                ];
                try {
                    $extentInfoModel->addReturnId($extent);
                } catch (\Exception $e) {
                    echo '错误:' . $val[2];
                }

            }
            if (!empty($val[3])) {
                $extent = [
                    'key' => 'pd_extended_field_1',
                    'product_id' => $id,
                    'project_id' => $project_id,
                    'values' => $val[3],
                ];
                try {
                    $extentInfoModel->addReturnId($extent);
                } catch (\Exception $e) {
                    echo '错误:' . $val[3];
                }
            }
            if (!empty($val[4])) {
                $extent = [
                    'key' => 'pd_extended_field_2',
                    'product_id' => $id,
                    'project_id' => $project_id,
                    'values' => $val[4],
                ];
                try {
                    $extentInfoModel->addReturnId($extent);
                } catch (\Exception $e) {
                    echo '错误:' . $val[4];
                }
            }
            if (!empty($val[5])) {
                $extent = [
                    'key' => 'pd_extended_field_3',
                    'product_id' => $id,
                    'project_id' => $project_id,
                    'values' => $val[5],
                ];
                try {
                    $extentInfoModel->addReturnId($extent);
                } catch (\Exception $e) {
                    echo '错误:' . $val[5];
                }
            }
            //产品描述
            if (!empty($val[7])) {
                //设置产品描述
                $detail = [
                    'title' => 'SEODescription',
                    'product_id' => $id,
                    'column_id' => 1,
                    'text_type' => 1,
                    'content' => json_encode(['content' => $val[7]], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
                ];
                $detailModel->addReturnId($detail);
            }
            //产品描述
            if (!empty($val[8])) {
                //设置产品描述
                $detail = [
                    'title' => 'SEOSpecification',
                    'product_id' => $id,
                    'column_id' => 1,
                    'text_type' => 1,
                    'content' => json_encode(['content' => $val[8]], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
                ];
                $detailModel->addReturnId($detail);
            }
            if (!empty($val[9])) {
                try {
                    $faqsDetail = json_decode($val[9], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    if (!empty($faqsDetail) && is_array($faqsDetail)) {
                        $faqContent = '<div>';
                        foreach ($faqsDetail as $faq_Val) {
                            $faqContent .= "<span>question:" . $faq_Val['question'] . "</span><br /><span>" . "answer:" . $faq_Val['answer'] . "</span><br />";
                        }
                        $faqContent .= '</div>';
                        $detailFaqInfo = [
                            'title' => 'SEOQandA',
                            'product_id' => $id,
                            'column_id' => 1,
                            'text_type' => 1,
                            'content' => json_encode(['content' => $faqContent], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
                        ];
                        $detailModel->addReturnId($detailFaqInfo);
                    }
                } catch (\Exception $e) {
                    echo 'fqs';
                }
            }
            if (!empty($val[10])) {
                try {
                    $faqsDetail = json_decode($val[10], true);
                    if (!empty($faqsDetail) && is_array($faqsDetail)) {
                        $faqContent = '<div>';
                        foreach ($faqsDetail as $faq_Val) {
                            $faqContent .= "<span>question:" . $faq_Val['question'] . "</span><br /><span>" . "answer:" . $faq_Val['answer'] . "</span><br />";
                        }
                        $faqContent .= '</div>';
                        $detailFaqInfo = [
                            'title' => 'SEOQandA_Product',
                            'product_id' => $id,
                            'column_id' => 1,
                            'text_type' => 1,
                            'content' => json_encode(['content' => $faqContent])
                        ];
                        $detailModel->addReturnId($detailFaqInfo);
                    }
                } catch (\Exception $e) {
                    continue;
                }
            }
        }
        return true;
    }

    public function handleCatePid()
    {
        $cateModel = new Category();
        $list = $cateModel->list([], 'id', ['*'], 'asc');
        foreach ($list as $k => $v) {
            if ($v['seo_des'] == 0) {
                $pid = 0;
            } else {
                $info = $cateModel->read(['seo_title' => $v['seo_des']], ['id']);
                if ($info !== false) {
                    $pid = $info['id'];
                } else {
                    $pid = 0;
                }
            }
            //更新pid
            $cateModel->edit(['pid' => $pid], ['id' => $v['id']]);
        }
        return true;
    }
}