refactor: 产品分类添加path层级路径逻辑

This commit is contained in:
2025-04-24 14:21:33 +08:00
parent e1502daed7
commit 3be2b03452
4 changed files with 22 additions and 17 deletions

View File

@@ -98,6 +98,9 @@ class ProductCategory
if ($data['pid'] > 0) { if ($data['pid'] > 0) {
$parent = $category->bypk($data['pid'])->find(); $parent = $category->bypk($data['pid'])->find();
if (!empty($parent)) { if (!empty($parent)) {
if (!empty($parent->path)) {
$data['path'] = $parent->path . ',' . $parent->id;
}
$data['level'] = $parent->level + 1; $data['level'] = $parent->level + 1;
} }
} }
@@ -145,6 +148,10 @@ class ProductCategory
if ($data['pid'] != $category->pid) { if ($data['pid'] != $category->pid) {
$parent = ProductCategoryModel::bypk($data['pid'])->find(); $parent = ProductCategoryModel::bypk($data['pid'])->find();
if (!empty($parent)) { if (!empty($parent)) {
$data['path'] = $parent->id;
if (!empty($parent->path)) {
$data['path'] = $parent->path . ',' . $data['path'];
}
$data['level'] = $parent->level + 1; $data['level'] = $parent->level + 1;
$updated_level = true; $updated_level = true;
} }
@@ -156,21 +163,24 @@ class ProductCategory
// 处理子分类层级 // 处理子分类层级
if ($updated_level) { if ($updated_level) {
$this->handle_children($category->id, $data['level']); $this->handle_children($category->id, $data['path'], $data['level']);
} }
return success('操作成功'); return success('操作成功');
} }
private function handle_children($pid, $level) private function handle_children($pid, $path, $level)
{ {
$children = ProductCategoryModel::pid($pid)->select(); $children = ProductCategoryModel::pid($pid)->select();
if ($children->isEmpty()) { if ($children->isEmpty()) {
return; return;
} }
foreach ($children as $child) { foreach ($children as $child) {
if (!empty($path)) {
$child->path = $path. ','. $child->pid;
}
$child->level = $level + 1; $child->level = $level + 1;
if ($child->save()) { if ($child->save()) {
$this->handle_children($child->id, $child->level); $this->handle_children($child->id, $child->path, $child->level);
} }
} }
} }

View File

@@ -66,20 +66,13 @@ class ProductCategoryValidate extends Validate
if ($value == 0) { if ($value == 0) {
return true; return true;
} }
$table_name = (new ProductCategoryModel)->getTable(); // 如果pid和id相同返回false
$children = Db::query( if ($value == $data['id']) {
preg_replace( return false;
'/\s+/u', }
' ', // 如果pid是id的子分类返回false
"WITH RECURSIVE tree_by AS ( $children = ProductCategoryModel::whereRaw('FIND_IN_SET(:id, path)', ['id' => $data['id']])->column('id');
SELECT a.id, a.pid FROM $table_name a WHERE a.id = {$data['id']} if (!empty($children) && in_array($value, $children)) {
UNION ALL
SELECT k.id, k.pid FROM $table_name k INNER JOIN tree_by t ON t.id = k.pid
)
SELECT id FROM tree_by WHERE id <> {$data['id']};"
)
);
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
return false; return false;
} }

View File

@@ -21,6 +21,7 @@ class ProductCategoryBaseModel extends BaseModel
'language_id' => 'int', 'language_id' => 'int',
'unique_id' => 'string', 'unique_id' => 'string',
'pid' => 'int', 'pid' => 'int',
'path' => 'string',
'name' => 'string', 'name' => 'string',
'icon' => 'string', 'icon' => 'string',
'desc' => 'string', 'desc' => 'string',

View File

@@ -32,6 +32,7 @@ class CreateProductCategory extends Migrator
$table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID']) $table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID'])
->addColumn('unique_id', 'string', ['limit' => 64, 'null' => false, 'comment' => '唯一ID']) ->addColumn('unique_id', 'string', ['limit' => 64, 'null' => false, 'comment' => '唯一ID'])
->addColumn('pid', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'comment' => '父级ID']) ->addColumn('pid', 'integer', ['signed' => false, 'null' => false, 'default' => 0, 'comment' => '父级ID'])
->addColumn('path', 'string', ['limit' => 128, 'null' => true, 'default' => null, 'comment' => '层级路径'])
->addColumn('name', 'string', ['limit' => 64, 'null' => false, 'comment' => '分类名称']) ->addColumn('name', 'string', ['limit' => 64, 'null' => false, 'comment' => '分类名称'])
->addColumn('icon', 'string', ['limit' => 125, 'default' => null, 'comment' => '图标']) ->addColumn('icon', 'string', ['limit' => 125, 'default' => null, 'comment' => '图标'])
->addColumn('desc', 'string', ['limit' => 255, 'default' => null, 'comment' => '描述信息']) ->addColumn('desc', 'string', ['limit' => 255, 'default' => null, 'comment' => '描述信息'])