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

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

View File

@@ -98,6 +98,9 @@ class ProductCategory
if ($data['pid'] > 0) {
$parent = $category->bypk($data['pid'])->find();
if (!empty($parent)) {
if (!empty($parent->path)) {
$data['path'] = $parent->path . ',' . $parent->id;
}
$data['level'] = $parent->level + 1;
}
}
@@ -145,6 +148,10 @@ class ProductCategory
if ($data['pid'] != $category->pid) {
$parent = ProductCategoryModel::bypk($data['pid'])->find();
if (!empty($parent)) {
$data['path'] = $parent->id;
if (!empty($parent->path)) {
$data['path'] = $parent->path . ',' . $data['path'];
}
$data['level'] = $parent->level + 1;
$updated_level = true;
}
@@ -156,21 +163,24 @@ class ProductCategory
// 处理子分类层级
if ($updated_level) {
$this->handle_children($category->id, $data['level']);
$this->handle_children($category->id, $data['path'], $data['level']);
}
return success('操作成功');
}
private function handle_children($pid, $level)
private function handle_children($pid, $path, $level)
{
$children = ProductCategoryModel::pid($pid)->select();
if ($children->isEmpty()) {
return;
}
foreach ($children as $child) {
if (!empty($path)) {
$child->path = $path. ','. $child->pid;
}
$child->level = $level + 1;
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) {
return true;
}
$table_name = (new ProductCategoryModel)->getTable();
$children = Db::query(
preg_replace(
'/\s+/u',
' ',
"WITH RECURSIVE tree_by AS (
SELECT a.id, a.pid FROM $table_name a WHERE a.id = {$data['id']}
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'))) {
// 如果pid和id相同返回false
if ($value == $data['id']) {
return false;
}
// 如果pid是id的子分类返回false
$children = ProductCategoryModel::whereRaw('FIND_IN_SET(:id, path)', ['id' => $data['id']])->column('id');
if (!empty($children) && in_array($value, $children)) {
return false;
}

View File

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

View File

@@ -32,6 +32,7 @@ class CreateProductCategory extends Migrator
$table->addColumn('language_id', 'integer', ['signed' => false, '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('path', 'string', ['limit' => 128, 'null' => true, 'default' => null, 'comment' => '层级路径'])
->addColumn('name', 'string', ['limit' => 64, 'null' => false, 'comment' => '分类名称'])
->addColumn('icon', 'string', ['limit' => 125, 'default' => null, 'comment' => '图标'])
->addColumn('desc', 'string', ['limit' => 255, 'default' => null, 'comment' => '描述信息'])