|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @remark :
|
|
|
|
* @name :UpdateProductCategory.php
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2023/12/6 16:07
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
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(['type'=>['in',[1,2,3,4],'project_id'=>1]]);
|
|
|
|
try {
|
|
|
|
foreach ($list as $v) {
|
|
|
|
echo date('Y-m-d H:i:s') . ' start: ' . $v['id'] . PHP_EOL;
|
|
|
|
ProjectServer::useProject($v['id']);
|
|
|
|
$this->getUpdateProductCategory($v['id']);
|
|
|
|
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,$category){
|
|
|
|
$category = trim($category,',');
|
|
|
|
$cate_arr = explode(',',$category);
|
|
|
|
if(!empty($cate_arr) && is_array($cate_arr)){
|
|
|
|
foreach ($cate_arr as $v){
|
|
|
|
$categoryModel = new Category();
|
|
|
|
$info = $categoryModel->read(['pid'=>$v],['id']);
|
|
|
|
if($info === false){
|
|
|
|
//有下级时,跳过
|
|
|
|
continue;
|
|
|
|
}else{
|
|
|
|
//更新关联表
|
|
|
|
$cateRelatedModel = new CategoryRelated();
|
|
|
|
$relateInfo = $cateRelatedModel->read(['product_id'=>$id,'cate_id'=>$v]);
|
|
|
|
if($relateInfo === false){
|
|
|
|
$cateRelatedModel->add(['product_id'=>$id, 'cate_id'=>$v]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|