UpdateProductCategory.php 3.1 KB
<?php
/**
 * @remark :
 * @name   :UpdateProductCategory.php
 * @author :lyh
 * @method :post
 * @time   :2023/12/6 16:07
 */

namespace App\Console\Commands\Test;

use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * @remark :更新分类
 * @name   :UpdateProductCategory
 * @author :lyh
 * @method :post
 * @time   :2023/12/6 16:07
 */
class UpdateProductCategory extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_product_cate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新产品分类';

    /**
     * @remark :执行方法
     * @name   :handle
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 16:09
     */
    public function handle(){
        //获取所有项目
        $projectModel = new Project();
        $list = $projectModel->list(['id'=>['in',[1515]]],'id',['id']);
        echo date('Y-m-d H:i:s') . ' start: ' . json_encode($list) . PHP_EOL;
        try {
            foreach ($list as $v) {
                echo date('Y-m-d H:i:s') . ' start: ' . $v['id'] . PHP_EOL;
                ProjectServer::useProject($v['id']);
                $this->getUpdateProductCategory();
                DB::disconnect('custom_mysql');
            }
        }catch (\Exception $e){
            echo date('Y-m-d H:i:s') . ' error:  ->' . $e->getMessage() . PHP_EOL;
        }
        echo date('Y-m-d H:i:s') . ' end: ' . PHP_EOL;
    }

    /**
     * @remark :更新
     * @name   :getUpdateProductCategory
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 16:12
     */
    public function getUpdateProductCategory(){
        $productModel = new Product();
        $lists = $productModel->list(['status'=>1],'id',['id','category_id']);
        foreach ($lists as $k => $v){
            if(!empty($v['category_id'])){
                $this->handleCategory($v['id'],$v['category_id']);
            }
        }
        return true;
    }

    /**
     * @remark :处理分类
     * @name   :handleCategory
     * @author :lyh
     * @method :post
     * @time   :2023/12/6 16:20
     */
    public function handleCategory($id,$cate_arr){
        if(!empty($cate_arr) && is_array($cate_arr)){
            foreach ($cate_arr as $v){
                $categoryModel = new Category();
                $info = $categoryModel->read(['id'=>$v],['id']);
                if($info === false){
                    continue;
                }else{
                    //更新关联表
                    $cateRelatedModel = new CategoryRelated();
                    $cateRelatedModel->del(['cate_id'=>$v]);
                    $relateInfo = $cateRelatedModel->read(['product_id'=>$id,'cate_id'=>$v]);
                    if($relateInfo === false){
                        $cateRelatedModel->add(['product_id'=>$id, 'cate_id'=>$v]);
                    }
                }
            }
        }
    }
}