瀏覽代碼

fixed productcategory

zero 1 年之前
父節點
當前提交
52ffe9b5b3
共有 1 個文件被更改,包括 55 次插入22 次删除
  1. 55 22
      src/main/java/com/example/xiaoshiweixinback/service/ProductCategoryService.java

+ 55 - 22
src/main/java/com/example/xiaoshiweixinback/service/ProductCategoryService.java

@@ -78,11 +78,11 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
             queryWrapper.like(ProductCategory::getName, name);
         }
 
-        if (parentId == 0) {
-            queryWrapper.isNull(ProductCategory::getParentId);
-        } else if (parentId != 0) {
-            queryWrapper.eq(ProductCategory::getParentId, parentId);
-        }
+//        if (parentId == 0) {
+//            queryWrapper.isNull(ProductCategory::getParentId);
+//        } else if (parentId != 0){
+//        }
+        queryWrapper.eq(ProductCategory::getParentId, parentId);
 
         if (ifConcern != null && ifConcern) {
             //获取关注的
@@ -164,7 +164,7 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
                 }
             }
             //装载父类产品类别
-            if (productCategory.getParentId() != null) {
+            if (productCategory.getParentId() != 0) {
                 ProductCategory parentCategory = this.getById(productCategory.getParentId());
                 SelectCategoryLevelVO levelVO = new SelectCategoryLevelVO();
                 levelVO.setId(parentCategory.getId());
@@ -206,7 +206,7 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
     public Integer addCategory(AddCategoryDTO categoryDTO) {
         ProductCategory category = new ProductCategory();
-        if (categoryDTO.getParentId() != null) {
+        if (categoryDTO.getParentId() != 0) {
             category.setParentId(categoryDTO.getParentId());
             ProductCategory parentCategory = this.getById(categoryDTO.getParentId());
             Integer level = parentCategory.getLevel();
@@ -216,12 +216,13 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
                 level = parentCategory.getLevel();
             }
             category.setLevel(level + 1);
-            if (parentCategory.getParentId() != null) {
+            if (parentCategory.getParentId() != 0) {
                 category.setPath(this.getPath(parentCategory.getParentId()) + "/" + parentCategory.getId() + "/");
             } else {
                 category.setPath(parentCategory.getId() + "/");
             }
         } else {
+            category.setParentId(0);
             category.setLevel(1);
         }
         Long count = productCategoryMapper.selectCount(new LambdaQueryWrapper<ProductCategory>()
@@ -265,7 +266,7 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
         if (StringUtils.isNotEmpty(categoryDTO.getSearchCondition())) {
             category.setSearchCondition(categoryDTO.getSearchCondition());
         }
-        if (categoryDTO.getParentId() != null) {
+        if (categoryDTO.getParentId() != 0) {
             category.setParentId(categoryDTO.getParentId());
             ProductCategory parentCategory = this.getById(categoryDTO.getParentId());
             Integer level = parentCategory.getLevel();
@@ -275,11 +276,19 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
                 level = parentCategory.getLevel();
             }
             category.setLevel(level + 1);
-            if (parentCategory.getParentId() != null) {
+            if (parentCategory.getParentId() != 0) {
                 category.setPath(this.getPath(parentCategory.getParentId()) + "/" + parentCategory.getId() + "/");
             } else {
                 category.setPath(parentCategory.getId() + "/");
             }
+            this.getParentPath(category.getId(), category.getPath(),category.getLevel());
+        } else {
+            //todo
+//            category.setParentId(0);
+//            category.setLevel(category.getLevel() - 1);
+//            category.setPath(null);
+
+
         }
         Long count = productCategoryMapper.selectCount(new LambdaQueryWrapper<ProductCategory>()
                 .ne(ProductCategory::getId, categoryDTO.getId())
@@ -321,7 +330,7 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
         BeanUtil.copy(category, categoryVO);
         //获取父类产品类别
         Integer parentId = category.getParentId();
-        if (parentId != null) {
+        if (parentId != 0) {
             ProductCategory parentCategory = this.getById(parentId);
             SelectCategoryLevelVO levelVO = new SelectCategoryLevelVO();
             levelVO.setId(parentCategory.getId());
@@ -366,7 +375,6 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
                 }
             }
         }
-//        categoryVO.setSystemFileList(systemFiles);
         //判断是否有子集
         Long count = productCategoryMapper.selectCount(new LambdaQueryWrapper<ProductCategory>()
                 .eq(ProductCategory::getParentId, categoryId));
@@ -424,7 +432,7 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
         String s = "";
         ProductCategory parentCategory = this.getById(parentId);
         if (ToolUtil.isNotEmpty(parentCategory)) {
-            if (parentCategory.getParentId() != null) {
+            if (parentCategory.getParentId() != 0) {
                 String pathId = this.getPath(parentCategory.getParentId());
                 s = s + "/" + pathId;
             } else {
@@ -434,19 +442,44 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
         return s;
     }
 
-    public String getChildPath(Integer id) {
-        String s = "";
+    public void getParentPath(Integer id, String path, Integer level) {
         List<ProductCategory> categories = productCategoryMapper.selectList(new LambdaQueryWrapper<ProductCategory>()
                 .eq(ProductCategory::getParentId, id));
         if (ToolUtil.isNotEmpty(categories)) {
-//            if (parentCategory.getParentId() != null) {
-//                String pathId = this.getPath(parentCategory.getParentId());
-//                s = s + "/" + pathId;
-//            } else {
-//                s = parentCategory.getId().toString();
-//            }
+            for (ProductCategory category : categories) {
+                ProductCategory productCategory = productCategoryMapper.selectById(category.getId());
+                String productCategoryPath = productCategory.getPath();
+                if (StringUtils.isNotEmpty(productCategoryPath)) {
+                    productCategory.setPath(path + productCategoryPath);
+                } else {
+                    productCategory.setPath(path);
+                }
+                productCategory.setLevel(level + 1);
+                productCategory.updateById();
+                //循环
+                this.getParentPath(productCategory.getId(), productCategory.getPath(), productCategory.getLevel());
+            }
+        }
+    }
+
+    public void getChildPath(Integer id, String path, Integer level) {
+        List<ProductCategory> categories = productCategoryMapper.selectList(new LambdaQueryWrapper<ProductCategory>()
+                .eq(ProductCategory::getParentId, id));
+        if (ToolUtil.isNotEmpty(categories)) {
+            for (ProductCategory category : categories) {
+                ProductCategory productCategory = productCategoryMapper.selectById(category.getId());
+                String productCategoryPath = productCategory.getPath();
+                if (StringUtils.isNotEmpty(productCategoryPath)) {
+                    productCategory.setPath(path + productCategoryPath);
+                } else {
+                    productCategory.setPath(path);
+                }
+                productCategory.setLevel(level + 1);
+                productCategory.updateById();
+                //循环
+                this.getChildPath(productCategory.getId(), productCategory.getPath(), productCategory.getLevel());
+            }
         }
-        return s;
     }
 
     /**