yangyb 1 месяц назад
Родитель
Сommit
5829098f26

+ 2 - 2
mall-api/src/main/java/com/txz/mall/vo/CategoryTreeVo.java

@@ -13,10 +13,10 @@ public class CategoryTreeVo implements Serializable {
 
     private static final long serialVersionUID = 1L;
 
-    private Integer id;
+    private Long id;
 
     @ApiModelProperty(value = "父级ID")
-    private Integer pid;
+    private Long pid;
 
     @ApiModelProperty(value = "路径")
     private String path;

+ 20 - 3
mall-service/src/main/java/com/txz/mall/controller/CategoryController.java

@@ -13,6 +13,7 @@ import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import tk.mybatis.mapper.entity.Condition;
 
@@ -63,9 +64,7 @@ public class CategoryController {
             return Result.fail(ResultCode.USERID_IS_NULL);
         }
         try {
-            Category category = new Category();
-            category.setIsDelete(1);
-            categoryService.update(category);
+            categoryService.delete(id);
         } catch (Exception e) {
             log.error("删除对象操作异常e:{}", e);
             return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);
@@ -150,4 +149,22 @@ public class CategoryController {
         List<CategoryTreeVo> listTree = categoryService.getListTree(type, status, name);
         return Result.success(listTree);
     }
+
+    @ApiOperation(value = "根据id集合获取分类列表")
+    @GetMapping(value = "/list/ids")
+    @ApiImplicitParam(name = "ids", value="分类id集合")
+    public Result<List<Category>> getByIds(@Validated @RequestParam(name = "ids") String ids) {
+        return Result.success(categoryService.findByIds(ids));
+    }
+
+    @ApiOperation(value = "更改分类状态")
+    @GetMapping(value = "/updateStatus/{id}")
+    @ApiImplicitParam(name = "id", value="分类id")
+    public Result getByIds(@Validated @PathVariable(name = "id") Long id) {
+        if (categoryService.updateStatus(id)) {
+            return Result.success("修改成功");
+        } else {
+            return Result.fail("修改失败");
+        }
+    }
 }

+ 1 - 0
mall-service/src/main/java/com/txz/mall/controller/StoreProductController.java

@@ -92,6 +92,7 @@ public class StoreProductController {
         }
         try {
             StoreProduct storeProduct = new StoreProduct();
+            storeProduct.setId(id);
             storeProduct.setIsDelete(1);
             storeProductService.update(storeProduct);
         } catch (Exception e) {

+ 16 - 1
mall-service/src/main/java/com/txz/mall/service/CategoryService.java

@@ -18,7 +18,22 @@ public interface CategoryService extends Service<Category> {
      * @param type   分类类型
      * @param status 状态
      * @param name   名称
-     * @return
+     * @return 树结构
      */
     List<CategoryTreeVo> getListTree(Integer type, Integer status, String name);
+
+    /**
+     * 更新状态
+     *
+     * @param id 分类id
+     * @return 状态
+     */
+    boolean updateStatus(Long id);
+
+    /**
+     * 删除
+     *
+     * @param id 分类id
+     */
+    void delete(Long id);
 }

+ 40 - 8
mall-service/src/main/java/com/txz/mall/service/impl/CategoryServiceImpl.java

@@ -1,9 +1,11 @@
 package com.txz.mall.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.txz.mall.core.AbstractService;
+import com.txz.mall.core.ServiceException;
 import com.txz.mall.dao.CategoryMapper;
 import com.txz.mall.model.Category;
 import com.txz.mall.service.CategoryService;
@@ -33,12 +35,43 @@ public class CategoryServiceImpl extends AbstractService<Category> implements Ca
         return getTree(type, status, name, null);
     }
 
-    /**
-     * 带结构的无线级分类
-     *
-     * @author Mr.Zhang
-     * @since 2020-04-16
-     */
+    @Override
+    public boolean updateStatus(Long id) {
+        Category category = findById(id);
+        if (category == null) {
+            return false;
+        }
+        if (category.getStatus().equals(0)) {
+            category.setStatus(1);
+        }
+        if (category.getStatus().equals(1)) {
+            category.setStatus(0);
+        }
+        this.update(category);
+        return true;
+    }
+
+    @Override
+    public void delete(Long id) {
+        //查看是否有子类, 物理删除
+        if (getChildCountByPid(id) > 0) {
+            throw new ServiceException("当前分类下有子类,请先删除子类!");
+        }
+
+        Category category = new Category();
+        category.setId(id);
+        category.setIsDelete(1);
+        this.update(category);
+    }
+
+    private int getChildCountByPid(Long pid) {
+        Condition condition = new Condition(Category.class);
+        Example.Criteria criteria = condition.createCriteria();
+        criteria.andEqualTo("pid", pid);
+        List<Category> list = this.findByCondition(condition);
+        return list.size();
+    }
+
     private List<CategoryTreeVo> getTree(Integer type, Integer status, String name, List<Integer> categoryIdList) {
         //循环数据,把数据对象变成带list结构的vo
         List<CategoryTreeVo> treeList = new ArrayList<>();
@@ -90,9 +123,8 @@ public class CategoryServiceImpl extends AbstractService<Category> implements Ca
             treeList.add(categoryTreeVo);
         }
 
-
         //返回
-        Map<Integer, CategoryTreeVo> map = new HashMap<>();
+        Map<Long, CategoryTreeVo> map = new HashMap<>();
         //ID 为 key 存储到map 中
         for (CategoryTreeVo categoryTreeVo1 : treeList) {
             map.put(categoryTreeVo1.getId(), categoryTreeVo1);

+ 2 - 1
mall-service/src/test/resources/generator/template/controller.ftl

@@ -67,7 +67,8 @@ public class ${modelNameUpperCamel}Controller {
     		return Result.fail(ResultCode.USERID_IS_NULL);
     	}
     	try {
-${modelNameUpperCamel} ${modelNameLowerCamel} = new ${modelNameUpperCamel}();
+            ${modelNameUpperCamel} ${modelNameLowerCamel} = new ${modelNameUpperCamel}();
+			${modelNameLowerCamel}.setId(id);
 			${modelNameLowerCamel}.setIsDelete(1);
 			${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
 		} catch (Exception e) {