UpdateProductCategory.php
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?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]]],'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(['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]);
}
}
}
}
}
}