zero пре 1 година
родитељ
комит
42f7b4fd7a

+ 85 - 58
src/main/java/com/example/xiaoshiweixinback/service/ProductCategoryService.java

@@ -2,7 +2,9 @@ package com.example.xiaoshiweixinback.service;
 
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.example.xiaoshiweixinback.business.common.base.Records;
@@ -10,8 +12,6 @@ import com.example.xiaoshiweixinback.business.common.base.SystemFile;
 import com.example.xiaoshiweixinback.business.exception.BusinessException;
 import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
 import com.example.xiaoshiweixinback.business.utils.BeanUtil;
-import com.example.xiaoshiweixinback.business.utils.CacheUtil;
-import com.example.xiaoshiweixinback.business.utils.LoginUtils;
 import com.example.xiaoshiweixinback.business.utils.ToolUtil;
 import com.example.xiaoshiweixinback.domain.*;
 import com.example.xiaoshiweixinback.entity.dto.ProductCategoryDTO;
@@ -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() != 0) {
+        if (categoryDTO.getParentId() != null && categoryDTO.getParentId() != 0) {
             category.setParentId(categoryDTO.getParentId());
             ProductCategory parentCategory = this.getById(categoryDTO.getParentId());
             Integer level = parentCategory.getLevel();
@@ -257,16 +257,10 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
     public Integer editCategory(EditCategoryDTO categoryDTO) {
         Integer categoryId = categoryDTO.getId();
         ProductCategory category = this.getById(categoryId);
-        if (StringUtils.isNotEmpty(categoryDTO.getName())) {
-            category.setName(categoryDTO.getName());
-        }
-        if (StringUtils.isNotEmpty(categoryDTO.getDescription())) {
-            category.setDescription(categoryDTO.getDescription());
-        }
-        if (StringUtils.isNotEmpty(categoryDTO.getSearchCondition())) {
-            category.setSearchCondition(categoryDTO.getSearchCondition());
-        }
-        if (categoryDTO.getParentId() != 0) {
+        category.setName(categoryDTO.getName());
+        category.setDescription(categoryDTO.getDescription());
+        category.setSearchCondition(categoryDTO.getSearchCondition());
+        if (categoryDTO.getParentId() != null && categoryDTO.getParentId() != 0) {
             category.setParentId(categoryDTO.getParentId());
             ProductCategory parentCategory = this.getById(categoryDTO.getParentId());
             Integer level = parentCategory.getLevel();
@@ -283,12 +277,12 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
             }
             this.getParentPath(category.getId(), category.getPath(),category.getLevel());
         } else {
-            //todo
-//            category.setParentId(0);
-//            category.setLevel(category.getLevel() - 1);
-//            category.setPath(null);
-
-
+            Integer parentId = category.getParentId();
+            Integer level = category.getLevel();
+            category.setParentId(0);
+            category.setLevel(level - 1);
+            category.setPath(null);
+            this.getChildPath(category.getId(), parentId, level);
         }
         Long count = productCategoryMapper.selectCount(new LambdaQueryWrapper<ProductCategory>()
                 .ne(ProductCategory::getId, categoryDTO.getId())
@@ -422,29 +416,40 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
         return list;
     }
 
+//    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)) {
+//            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());
+//            }
+//        }
+//    }
+
     /**
-     * 获取回显路径
-     *
-     * @param parentId
-     * @return
+     * 修改产品类别高层级变低层级
+     * @param id
+     * @param path
+     * @param level
      */
-    public String getPath(Integer parentId) {
-        String s = "";
-        ProductCategory parentCategory = this.getById(parentId);
-        if (ToolUtil.isNotEmpty(parentCategory)) {
-            if (parentCategory.getParentId() != 0) {
-                String pathId = this.getPath(parentCategory.getParentId());
-                s = s + "/" + pathId;
-            } else {
-                s = parentCategory.getId().toString();
-            }
-        }
-        return s;
-    }
-
     public void getParentPath(Integer id, String path, Integer level) {
-        List<ProductCategory> categories = productCategoryMapper.selectList(new LambdaQueryWrapper<ProductCategory>()
-                .eq(ProductCategory::getParentId, id));
+        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
+        if (level > 2) {
+            wrapper.like(ProductCategory::getPath, "/" + id + "/");
+        } else {
+            wrapper.likeRight(ToolUtil.isNotEmpty(id), ProductCategory::getPath, id + "/");
+        }
+        List<ProductCategory> categories = productCategoryMapper.selectList(wrapper);
         if (ToolUtil.isNotEmpty(categories)) {
             for (ProductCategory category : categories) {
                 ProductCategory productCategory = productCategoryMapper.selectById(category.getId());
@@ -454,32 +459,54 @@ public class ProductCategoryService extends ServiceImpl<ProductCategoryMapper, P
                 } else {
                     productCategory.setPath(path);
                 }
-                productCategory.setLevel(level + 1);
+                productCategory.setLevel(productCategory.getLevel() + 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());
+    /**
+     * 修改产品类别低层级变高层级
+     * @param id
+     * @param parentId
+     * @param level
+     */
+    public void getChildPath(Integer id, Integer parentId, Integer level) {
+        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
+        if (level >= 2) {
+            wrapper.like(ProductCategory::getPath, "/" + id + "/");
+        } else {
+            wrapper.likeRight(ToolUtil.isNotEmpty(id), ProductCategory::getPath, id + "/");
+        }
+        List<ProductCategory> categories = productCategoryMapper.selectList(wrapper);
+        for (ProductCategory category : categories) {
+            ProductCategory productCategory = productCategoryMapper.selectById(category.getId());
+            String categoryPath = productCategory.getPath();
+            Integer categoryLevel = productCategory.getLevel();
+            productCategory.setPath(categoryPath.replace(parentId + "/", ""));
+            productCategory.setLevel(categoryLevel - 1);
+            productCategory.updateById();
+        }
+    }
+
+    /**
+     * 获取路径
+     *
+     * @param parentId
+     * @return
+     */
+    public String getPath(Integer parentId) {
+        String s = "";
+        ProductCategory parentCategory = this.getById(parentId);
+        if (ToolUtil.isNotEmpty(parentCategory)) {
+            if (parentCategory.getParentId() != 0) {
+                String pathId = this.getPath(parentCategory.getParentId());
+                s = s + "/" + pathId;
+            } else {
+                s = parentCategory.getId().toString();
             }
         }
+        return s;
     }
 
     /**

+ 4 - 4
src/main/resources/application-dev.yml

@@ -1,12 +1,12 @@
 spring:
   rabbitmq:
-    host: 192.168.1.24
+    host: 192.168.2.24
     port: 5672
     username: admin
     password: 123456
   data:
    redis:
-     host: 192.168.1.24
+     host: 192.168.2.24
      port: 6379
      database: 3
      password: Xx0GWxdWQJxx6Swe
@@ -18,7 +18,7 @@ spring:
          max-wait: -1ms
      timeout: 2000ms
   datasource:
-    url: jdbc:mysql://192.168.1.24:3306/ecs?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
+    url: jdbc:mysql://192.168.2.24:3306/ecs?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
     userName: root
     password: rrzTwWAYX8Gxh5JH
     driver-class-name: com.mysql.cj.jdbc.Driver
@@ -51,7 +51,7 @@ spring:
             threadsInheritContextClassLoaderOfInitializingThread: true
           dataSource:
             default:
-              URL: jdbc:mysql://192.168.1.24:3306/ecs?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
+              URL: jdbc:mysql://192.168.2.24:3306/ecs?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
               user: root
               password: rrzTwWAYX8Gxh5JH
               driver: com.mysql.jdbc.Driver