From 5a99772af6ef714f56ff87fa3b7d9a9d1a89423b Mon Sep 17 00:00:00 2001 From: zcy9999 <1328476842@qq.com> Date: Fri, 10 Jun 2022 14:16:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=93=81=E7=B1=BB=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../controller/v1/CategoryController.java | 68 ++++++++++++++ .../latticy/dto/category/CategoryDTO.java | 53 +++++++++++ .../latticy/dto/category/CategoryPageDTO.java | 21 +++++ .../dto/category/UpdateCategoryDTO.java | 27 ++++++ .../latticy/mapper/CategoryMapper.java | 14 +++ .../talelin/latticy/model/CategoryDO.java | 60 ++++++++++++ .../latticy/service/CategoryService.java | 62 +++++++++++++ .../service/impl/CategoryServiceImpl.java | 91 +++++++++++++++++++ src/main/resources/mapper/CategoryMapper.xml | 14 +++ 10 files changed, 416 insertions(+) create mode 100644 src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java create mode 100644 src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java create mode 100644 src/main/java/io/github/talelin/latticy/dto/category/CategoryPageDTO.java create mode 100644 src/main/java/io/github/talelin/latticy/dto/category/UpdateCategoryDTO.java create mode 100644 src/main/java/io/github/talelin/latticy/mapper/CategoryMapper.java create mode 100644 src/main/java/io/github/talelin/latticy/model/CategoryDO.java create mode 100644 src/main/java/io/github/talelin/latticy/service/CategoryService.java create mode 100644 src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java create mode 100644 src/main/resources/mapper/CategoryMapper.xml diff --git a/pom.xml b/pom.xml index 48078af..bb11797 100644 --- a/pom.xml +++ b/pom.xml @@ -213,6 +213,12 @@ 2.2.1.RELEASE + + cn.hutool + hutool-all + 5.7.6 + + diff --git a/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java new file mode 100644 index 0000000..9875a6b --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java @@ -0,0 +1,68 @@ +package io.github.talelin.latticy.controller.v1; + +import io.github.talelin.core.annotation.PermissionMeta; +import io.github.talelin.core.annotation.PermissionModule; +import io.github.talelin.latticy.common.util.PageUtil; +import io.github.talelin.latticy.dto.category.CategoryDTO; +import io.github.talelin.latticy.dto.category.CategoryPageDTO; +import io.github.talelin.latticy.dto.category.UpdateCategoryDTO; +import io.github.talelin.latticy.model.CategoryDO; +import io.github.talelin.latticy.service.CategoryService; +import io.github.talelin.latticy.vo.CreatedVO; +import io.github.talelin.latticy.vo.DeletedVO; +import io.github.talelin.latticy.vo.PageResponseVO; +import io.github.talelin.latticy.vo.UpdatedVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * ClassName CategoryController + * Description + * Create by zcy + * Date 2022-06-09 + */ +@RestController +@RequestMapping("/v1/category") +@PermissionModule(value = "品类管理") +public class CategoryController { + + + @Autowired + private CategoryService categoryService; + + + @GetMapping("/page") + @PermissionMeta(value = "品类列表查询", mount = false) + private PageResponseVO getCategoryPage(CategoryPageDTO categoryPageDTO) { + return PageUtil.build(categoryService.getCategoryPage(categoryPageDTO)); + } + + @PostMapping("") + @PermissionMeta(value = "新增品类", mount = false) + private CreatedVO createCategory(@RequestBody CategoryDTO categoryDTO) { + categoryService.createCategory(categoryDTO); + return new CreatedVO(); + } + + @PutMapping("/{id}") + @PermissionMeta(value = "修改品类", mount = false) + private UpdatedVO updateCategory(@PathVariable Integer id, @RequestBody UpdateCategoryDTO categoryDTO) { + categoryService.updateCategory(id, categoryDTO); + return new UpdatedVO(); + } + + @DeleteMapping("/{id}") + @PermissionMeta(value = "删除品类", mount = false) + private DeletedVO deleteCategory(@PathVariable Integer id) { + categoryService.deleteCategory(id); + return new DeletedVO(); + } + + @GetMapping("/getCategoryTree") + @PermissionMeta(value = "查询树形结构", mount = false) + private List getCategoryTree() { + return categoryService.getCategoryTree(); + } +} diff --git a/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java new file mode 100644 index 0000000..eb2f764 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java @@ -0,0 +1,53 @@ +package io.github.talelin.latticy.dto.category; + +import io.github.talelin.latticy.model.CategoryDO; +import lombok.Data; +import org.springframework.beans.BeanUtils; + +import java.io.Serializable; +import java.util.List; + +/** + * ClassName CategoryDTO + * Description + * Create by zcy + * Date 2022-06-09 + */ +@Data +public class CategoryDTO implements Serializable { + private static final long serialVersionUID = -4817961500875249016L; + + /** + * id + */ + private Integer id; + /** + * 父ID + */ + private Integer parentId; + + /** + * 品类名称 + */ + private String categoryName; + + /** + * 品类英文名称 + */ + private String categoryEnglishName; + + /** + * 等级 + */ + private Integer level; + + + private List children; + + public CategoryDTO(CategoryDO categoryDO) { + BeanUtils.copyProperties(categoryDO, this); + } + + public CategoryDTO() { + } +} diff --git a/src/main/java/io/github/talelin/latticy/dto/category/CategoryPageDTO.java b/src/main/java/io/github/talelin/latticy/dto/category/CategoryPageDTO.java new file mode 100644 index 0000000..8427f35 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/dto/category/CategoryPageDTO.java @@ -0,0 +1,21 @@ +package io.github.talelin.latticy.dto.category; + +import io.github.talelin.latticy.dto.query.BasePageDTO; +import lombok.Data; + +import java.io.Serializable; + +/** + * ClassName CategoryPage + * Description + * Create by zcy + * Date 2022-06-09 + */ +@Data +public class CategoryPageDTO extends BasePageDTO implements Serializable { + private static final long serialVersionUID = -1695031667529655768L; + + + private String search; + +} diff --git a/src/main/java/io/github/talelin/latticy/dto/category/UpdateCategoryDTO.java b/src/main/java/io/github/talelin/latticy/dto/category/UpdateCategoryDTO.java new file mode 100644 index 0000000..fdb1c23 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/dto/category/UpdateCategoryDTO.java @@ -0,0 +1,27 @@ +package io.github.talelin.latticy.dto.category; + +import lombok.Data; + +import java.io.Serializable; + +/** + * ClassName UpdateCategoryDTO + * Description + * Create by zcy + * Date 2022-06-09 + */ +@Data +public class UpdateCategoryDTO implements Serializable { + private static final long serialVersionUID = 632304028394556350L; + + /** + * 品类名称 + */ + private String categoryName; + + /** + * 品类英文名称 + */ + private String categoryEnglishName; + +} diff --git a/src/main/java/io/github/talelin/latticy/mapper/CategoryMapper.java b/src/main/java/io/github/talelin/latticy/mapper/CategoryMapper.java new file mode 100644 index 0000000..2f20957 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/mapper/CategoryMapper.java @@ -0,0 +1,14 @@ +package io.github.talelin.latticy.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import io.github.talelin.latticy.model.CategoryDO; + +/** + * ClassName CategoryMapper + * Description + * Create by zcy + * Date 2022-06-09 + */ +public interface CategoryMapper extends BaseMapper { + +} diff --git a/src/main/java/io/github/talelin/latticy/model/CategoryDO.java b/src/main/java/io/github/talelin/latticy/model/CategoryDO.java new file mode 100644 index 0000000..23d825c --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/model/CategoryDO.java @@ -0,0 +1,60 @@ +package io.github.talelin.latticy.model; + +import com.baomidou.mybatisplus.annotation.TableName; +import io.github.talelin.latticy.dto.category.CategoryDTO; +import io.github.talelin.latticy.dto.category.UpdateCategoryDTO; +import io.github.talelin.latticy.dto.warehouse.WarehouseDTO; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.beans.BeanUtils; + +import java.io.Serializable; +import java.util.Date; + +/** + * 品类 + * ClassName CategoryDO + * Description + * Create by zcy + * Date 2022-06-09 + */ +@Data +@TableName("wms_category") +@EqualsAndHashCode(callSuper = true) +public class CategoryDO extends BaseModel implements Serializable { + + + private static final long serialVersionUID = 4886045618174102463L; + + /** + * 父ID + */ + private Integer parentId; + + /** + * 品类名称 + */ + private String categoryName; + + /** + * 品类英文名称 + */ + private String categoryEnglishName; + + /** + * 等级 + */ + private Integer level; + + public CategoryDO(CategoryDTO dto) { + BeanUtils.copyProperties(dto, this); + } + + public CategoryDO(Integer id, UpdateCategoryDTO dto) { + BeanUtils.copyProperties(dto, this); + this.setId(id); + } + + public CategoryDO() { + } +} diff --git a/src/main/java/io/github/talelin/latticy/service/CategoryService.java b/src/main/java/io/github/talelin/latticy/service/CategoryService.java new file mode 100644 index 0000000..987c320 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/service/CategoryService.java @@ -0,0 +1,62 @@ +package io.github.talelin.latticy.service; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.IService; +import io.github.talelin.latticy.dto.category.CategoryDTO; +import io.github.talelin.latticy.dto.category.CategoryPageDTO; +import io.github.talelin.latticy.dto.category.UpdateCategoryDTO; +import io.github.talelin.latticy.model.CategoryDO; + +import java.util.List; + +/** + * 品类管理 + * + * ClassName CategoryService + * Description + * Create by zcy + * Date 2022-06-09 + */ +public interface CategoryService extends IService { + + + /** + * 新增品类 + * + * @param categoryDTO + */ + void createCategory(CategoryDTO categoryDTO); + + + /** + * 修改品类 + * + * @param id + * @param updateCategoryDTO + */ + void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO); + + + /** + * 删除品类 + * + * @param id + */ + void deleteCategory(Integer id); + + /** + * 分页查询 + * + * @param categoryPageDTO + * @return + */ + IPage getCategoryPage(CategoryPageDTO categoryPageDTO); + + + /** + * 查询树形结构 + * + * @return + */ + List getCategoryTree(); +} diff --git a/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java new file mode 100644 index 0000000..b610622 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java @@ -0,0 +1,91 @@ +package io.github.talelin.latticy.service.impl; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import io.github.talelin.latticy.common.mybatis.Page; +import io.github.talelin.latticy.dto.category.CategoryDTO; +import io.github.talelin.latticy.dto.category.CategoryPageDTO; +import io.github.talelin.latticy.dto.category.UpdateCategoryDTO; +import io.github.talelin.latticy.mapper.CategoryMapper; +import io.github.talelin.latticy.model.CategoryDO; +import io.github.talelin.latticy.service.CategoryService; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * ClassName CategoryServiceImpl + * Description + * Create by zcy + * Date 2022-06-09 + */ +@Service +public class CategoryServiceImpl extends ServiceImpl implements CategoryService { + + @Override + public void createCategory(CategoryDTO categoryDTO) { + CategoryDO categoryDO = new CategoryDO(categoryDTO); + Assert.isTrue(this.baseMapper.insert(categoryDO) > 0, "新增品类失败!"); + } + + @Override + public void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO) { + CategoryDO categoryDO = new CategoryDO(id, updateCategoryDTO); + Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "修改品类失败!"); + } + + @Override + public void deleteCategory(Integer id) { + Assert.isTrue(this.baseMapper.deleteById(id) > 0, "删除品类失败!"); + } + + @Override + public IPage getCategoryPage(CategoryPageDTO categoryPageDTO) { + return this.baseMapper.selectPage(new Page<>(categoryPageDTO.getPage(), categoryPageDTO.getCount()), new LambdaQueryWrapper().like(StrUtil.isNotEmpty(categoryPageDTO.getSearch()), CategoryDO::getCategoryName, categoryPageDTO.getSearch()).like(StrUtil.isNotEmpty(categoryPageDTO.getSearch()), CategoryDO::getCategoryEnglishName, categoryPageDTO.getSearch())); + } + + @Override + public List getCategoryTree() { + List categoryDOList = this.baseMapper.selectList(new LambdaQueryWrapper<>()); + //判断是否有数据 + if (CollUtil.isEmpty(categoryDOList)) { + return new ArrayList<>(); + } + // 过滤所有的一级目录,父id为0 + List firstColumn = categoryDOList.stream().filter(c -> c.getParentId() == 0).map(CategoryDTO::new).collect(Collectors.toList()); + //查出非一级目录,按照其父id分类 + Map> categoryMapList = categoryDOList.stream().filter(c -> (c.getParentId() != 0)).map(CategoryDTO::new).collect(Collectors.groupingBy(CategoryDTO::getParentId)); + //遍历一级目录 + firstColumn.forEach(a -> { + //如果一级目录的id中是否包含非一级目录的父id + if (categoryMapList.get(a.getId()) == null) { + //没有,说明不是该一级目录没有下一级目录,则将其子目录设为空 + a.setChildren(new ArrayList<>()); + } else { + //有,则说明有二级目录,将二级目录的的List集合,赋给子目录 + a.setChildren(categoryMapList.get(a.getId())); + + //构建三级树 + //遍历二级目录,判断是否含有三级目录 + categoryMapList.get(a.getId()).forEach(b -> { + if (categoryMapList.get(b.getId()) == null) { + //没有,将二级目录下的三级目录设为空 + b.setChildren(new ArrayList<>()); + } else { + //有,将三级目录的List集合,赋给三级目录 + b.setChildren(categoryMapList.get(b.getId())); + } + }); + } + }); + //将firstColumn返回,前端可以通过遍历获取各级目录的数据从而形成三级树的效果 + return firstColumn; + } +} diff --git a/src/main/resources/mapper/CategoryMapper.xml b/src/main/resources/mapper/CategoryMapper.xml new file mode 100644 index 0000000..db39de0 --- /dev/null +++ b/src/main/resources/mapper/CategoryMapper.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + -- Gitee From 2e3584945fefaae95f8770eafca9bc0d20de69ba Mon Sep 17 00:00:00 2001 From: zcy9999 <1328476842@qq.com> Date: Sat, 11 Jun 2022 01:33:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=93=81=E7=B1=BB=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/RestExceptionHandler.java | 17 ++++++ .../controller/v1/CategoryController.java | 10 +++- .../latticy/dto/category/CategoryDTO.java | 5 ++ .../service/impl/CategoryServiceImpl.java | 55 +++++++++---------- .../resources/ValidationMessages.properties | 8 ++- src/main/resources/code-message.properties | 3 + 6 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java b/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java index 5b563dd..7f94a64 100644 --- a/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java +++ b/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java @@ -279,6 +279,23 @@ public class RestExceptionHandler { return result; } + /** + * IllegalArgumentException + */ + @ExceptionHandler({IllegalArgumentException.class}) + public UnifyResponseVO processException(IllegalArgumentException exception, + HttpServletRequest request, + HttpServletResponse response) { + log.error("", exception); + UnifyResponseVO result = new UnifyResponseVO<>(); + result.setRequest(getSimpleRequest(request)); + result.setMessage(CodeMessageConfiguration.getMessage(Integer.valueOf(exception.getMessage()))); + result.setCode(Code.FAIL.getCode()); + response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); + return result; + } + + private UnifyResponseVO> getMapUnifyResponseVO(HttpServletRequest request, HttpServletResponse response, Map msg) { diff --git a/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java index 9875a6b..91b8971 100644 --- a/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java +++ b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java @@ -1,5 +1,6 @@ package io.github.talelin.latticy.controller.v1; +import io.github.talelin.core.annotation.GroupRequired; import io.github.talelin.core.annotation.PermissionMeta; import io.github.talelin.core.annotation.PermissionModule; import io.github.talelin.latticy.common.util.PageUtil; @@ -15,6 +16,7 @@ import io.github.talelin.latticy.vo.UpdatedVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.validation.constraints.Positive; import java.util.List; /** @@ -35,11 +37,13 @@ public class CategoryController { @GetMapping("/page") @PermissionMeta(value = "品类列表查询", mount = false) + @GroupRequired private PageResponseVO getCategoryPage(CategoryPageDTO categoryPageDTO) { return PageUtil.build(categoryService.getCategoryPage(categoryPageDTO)); } @PostMapping("") + @GroupRequired @PermissionMeta(value = "新增品类", mount = false) private CreatedVO createCategory(@RequestBody CategoryDTO categoryDTO) { categoryService.createCategory(categoryDTO); @@ -47,15 +51,17 @@ public class CategoryController { } @PutMapping("/{id}") + @GroupRequired @PermissionMeta(value = "修改品类", mount = false) - private UpdatedVO updateCategory(@PathVariable Integer id, @RequestBody UpdateCategoryDTO categoryDTO) { + private UpdatedVO updateCategory(@PathVariable @Positive(message = "{category.id}") Integer id, @RequestBody UpdateCategoryDTO categoryDTO) { categoryService.updateCategory(id, categoryDTO); return new UpdatedVO(); } @DeleteMapping("/{id}") + @GroupRequired @PermissionMeta(value = "删除品类", mount = false) - private DeletedVO deleteCategory(@PathVariable Integer id) { + private DeletedVO deleteCategory(@PathVariable @Positive(message = "{category.id}") Integer id) { categoryService.deleteCategory(id); return new DeletedVO(); } diff --git a/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java index eb2f764..68ba527 100644 --- a/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java +++ b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java @@ -4,6 +4,7 @@ import io.github.talelin.latticy.model.CategoryDO; import lombok.Data; import org.springframework.beans.BeanUtils; +import javax.validation.constraints.NotBlank; import java.io.Serializable; import java.util.List; @@ -24,21 +25,25 @@ public class CategoryDTO implements Serializable { /** * 父ID */ + @NotBlank(message = "{category.parent-id}") private Integer parentId; /** * 品类名称 */ + @NotBlank(message = "{category.category-name}") private String categoryName; /** * 品类英文名称 */ + @NotBlank(message = "{category.category-english-name}") private String categoryEnglishName; /** * 等级 */ + @NotBlank(message = "{category.level}") private Integer level; diff --git a/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java index b610622..106b98c 100644 --- a/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java +++ b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java @@ -32,18 +32,18 @@ public class CategoryServiceImpl extends ServiceImpl @Override public void createCategory(CategoryDTO categoryDTO) { CategoryDO categoryDO = new CategoryDO(categoryDTO); - Assert.isTrue(this.baseMapper.insert(categoryDO) > 0, "新增品类失败!"); + Assert.isTrue(this.baseMapper.insert(categoryDO) > 0, "18503"); } @Override public void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO) { CategoryDO categoryDO = new CategoryDO(id, updateCategoryDTO); - Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "修改品类失败!"); + Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "18504"); } @Override public void deleteCategory(Integer id) { - Assert.isTrue(this.baseMapper.deleteById(id) > 0, "删除品类失败!"); + Assert.isTrue(this.baseMapper.deleteById(id) > 0, "18505"); } @Override @@ -58,34 +58,29 @@ public class CategoryServiceImpl extends ServiceImpl if (CollUtil.isEmpty(categoryDOList)) { return new ArrayList<>(); } - // 过滤所有的一级目录,父id为0 List firstColumn = categoryDOList.stream().filter(c -> c.getParentId() == 0).map(CategoryDTO::new).collect(Collectors.toList()); - //查出非一级目录,按照其父id分类 - Map> categoryMapList = categoryDOList.stream().filter(c -> (c.getParentId() != 0)).map(CategoryDTO::new).collect(Collectors.groupingBy(CategoryDTO::getParentId)); - //遍历一级目录 - firstColumn.forEach(a -> { - //如果一级目录的id中是否包含非一级目录的父id - if (categoryMapList.get(a.getId()) == null) { - //没有,说明不是该一级目录没有下一级目录,则将其子目录设为空 - a.setChildren(new ArrayList<>()); - } else { - //有,则说明有二级目录,将二级目录的的List集合,赋给子目录 - a.setChildren(categoryMapList.get(a.getId())); + List lastColumn = categoryDOList.stream().filter(c -> c.getParentId() != 0).map(CategoryDTO::new).collect(Collectors.toList()); - //构建三级树 - //遍历二级目录,判断是否含有三级目录 - categoryMapList.get(a.getId()).forEach(b -> { - if (categoryMapList.get(b.getId()) == null) { - //没有,将二级目录下的三级目录设为空 - b.setChildren(new ArrayList<>()); - } else { - //有,将三级目录的List集合,赋给三级目录 - b.setChildren(categoryMapList.get(b.getId())); - } - }); - } - }); - //将firstColumn返回,前端可以通过遍历获取各级目录的数据从而形成三级树的效果 - return firstColumn; + return firstColumn.stream().filter((category) -> category.getParentId() == 0).map((category) -> { + category.setChildren(getChildren(category, lastColumn)); + return category; + }).collect(Collectors.toList()); + } + + /** + * 递归查找所有菜单的子菜单 + * + * @param root + * @param all + * @return + */ + private List getChildren(CategoryDTO root, List all) { + return all.stream().filter(categoryEntity -> { + return categoryEntity.getParentId().equals(root.getId()); + }).map(category -> { + //查询子品类 + category.setChildren(getChildren(category, all)); + return category; + }).collect(Collectors.toList()); } } diff --git a/src/main/resources/ValidationMessages.properties b/src/main/resources/ValidationMessages.properties index 9f5a75a..39b73cb 100644 --- a/src/main/resources/ValidationMessages.properties +++ b/src/main/resources/ValidationMessages.properties @@ -67,4 +67,10 @@ warearea.length=\u5206\u533A\u540D\u957F\u5EA6\u5FC5\u987B\u57282~10\u4E4B\u95F4 # \u5E93\u4F4D\u5F02\u5E38\u4FE1\u606F location.not-blank=\u5E93\u4F4D\u540D\u4E0D\u53EF\u4E3A\u7A7A -location.length=\u5E93\u4F4D\u540D\u957F\u5EA6\u5FC5\u987B\u57282~10\u4E4B\u95F4 \ No newline at end of file +location.length=\u5E93\u4F4D\u540D\u957F\u5EA6\u5FC5\u987B\u57282~10\u4E4B\u95F4 + +category.category-name=品类名称不可为空 +category.category-english-name=品类英文名称不可为空 +category.parent-id=上级ID不可为空 +category.level=等级不可为空 +category.id=品类ID不可为空 \ No newline at end of file diff --git a/src/main/resources/code-message.properties b/src/main/resources/code-message.properties index c427613..fb223bb 100644 --- a/src/main/resources/code-message.properties +++ b/src/main/resources/code-message.properties @@ -78,3 +78,6 @@ code-message[18401]=\u5206\u533A\u4E0D\u5B58\u5728 code-message[18402]=\u5206\u533A\u5DF2\u4F7F\u7528\uFF0C\u4E0D\u80FD\u88AB\u5220\u9664 code-message[18501]=\u5E93\u4F4D\u540D\u79F0\u5DF2\u88AB\u5360\u7528 code-message[18502]=\u5E93\u4F4D\u4E0D\u5B58\u5728 +code-message[18503]=新增品类失败 +code-message[18504]=修改品类失败 +code-message[18505]=删除品类失败 \ No newline at end of file -- Gitee
+ * ClassName CategoryService + * Description + * Create by zcy + * Date 2022-06-09 + */ +public interface CategoryService extends IService { + + + /** + * 新增品类 + * + * @param categoryDTO + */ + void createCategory(CategoryDTO categoryDTO); + + + /** + * 修改品类 + * + * @param id + * @param updateCategoryDTO + */ + void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO); + + + /** + * 删除品类 + * + * @param id + */ + void deleteCategory(Integer id); + + /** + * 分页查询 + * + * @param categoryPageDTO + * @return + */ + IPage getCategoryPage(CategoryPageDTO categoryPageDTO); + + + /** + * 查询树形结构 + * + * @return + */ + List getCategoryTree(); +} diff --git a/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java new file mode 100644 index 0000000..b610622 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java @@ -0,0 +1,91 @@ +package io.github.talelin.latticy.service.impl; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import io.github.talelin.latticy.common.mybatis.Page; +import io.github.talelin.latticy.dto.category.CategoryDTO; +import io.github.talelin.latticy.dto.category.CategoryPageDTO; +import io.github.talelin.latticy.dto.category.UpdateCategoryDTO; +import io.github.talelin.latticy.mapper.CategoryMapper; +import io.github.talelin.latticy.model.CategoryDO; +import io.github.talelin.latticy.service.CategoryService; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * ClassName CategoryServiceImpl + * Description + * Create by zcy + * Date 2022-06-09 + */ +@Service +public class CategoryServiceImpl extends ServiceImpl implements CategoryService { + + @Override + public void createCategory(CategoryDTO categoryDTO) { + CategoryDO categoryDO = new CategoryDO(categoryDTO); + Assert.isTrue(this.baseMapper.insert(categoryDO) > 0, "新增品类失败!"); + } + + @Override + public void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO) { + CategoryDO categoryDO = new CategoryDO(id, updateCategoryDTO); + Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "修改品类失败!"); + } + + @Override + public void deleteCategory(Integer id) { + Assert.isTrue(this.baseMapper.deleteById(id) > 0, "删除品类失败!"); + } + + @Override + public IPage getCategoryPage(CategoryPageDTO categoryPageDTO) { + return this.baseMapper.selectPage(new Page<>(categoryPageDTO.getPage(), categoryPageDTO.getCount()), new LambdaQueryWrapper().like(StrUtil.isNotEmpty(categoryPageDTO.getSearch()), CategoryDO::getCategoryName, categoryPageDTO.getSearch()).like(StrUtil.isNotEmpty(categoryPageDTO.getSearch()), CategoryDO::getCategoryEnglishName, categoryPageDTO.getSearch())); + } + + @Override + public List getCategoryTree() { + List categoryDOList = this.baseMapper.selectList(new LambdaQueryWrapper<>()); + //判断是否有数据 + if (CollUtil.isEmpty(categoryDOList)) { + return new ArrayList<>(); + } + // 过滤所有的一级目录,父id为0 + List firstColumn = categoryDOList.stream().filter(c -> c.getParentId() == 0).map(CategoryDTO::new).collect(Collectors.toList()); + //查出非一级目录,按照其父id分类 + Map> categoryMapList = categoryDOList.stream().filter(c -> (c.getParentId() != 0)).map(CategoryDTO::new).collect(Collectors.groupingBy(CategoryDTO::getParentId)); + //遍历一级目录 + firstColumn.forEach(a -> { + //如果一级目录的id中是否包含非一级目录的父id + if (categoryMapList.get(a.getId()) == null) { + //没有,说明不是该一级目录没有下一级目录,则将其子目录设为空 + a.setChildren(new ArrayList<>()); + } else { + //有,则说明有二级目录,将二级目录的的List集合,赋给子目录 + a.setChildren(categoryMapList.get(a.getId())); + + //构建三级树 + //遍历二级目录,判断是否含有三级目录 + categoryMapList.get(a.getId()).forEach(b -> { + if (categoryMapList.get(b.getId()) == null) { + //没有,将二级目录下的三级目录设为空 + b.setChildren(new ArrayList<>()); + } else { + //有,将三级目录的List集合,赋给三级目录 + b.setChildren(categoryMapList.get(b.getId())); + } + }); + } + }); + //将firstColumn返回,前端可以通过遍历获取各级目录的数据从而形成三级树的效果 + return firstColumn; + } +} diff --git a/src/main/resources/mapper/CategoryMapper.xml b/src/main/resources/mapper/CategoryMapper.xml new file mode 100644 index 0000000..db39de0 --- /dev/null +++ b/src/main/resources/mapper/CategoryMapper.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + -- Gitee From 2e3584945fefaae95f8770eafca9bc0d20de69ba Mon Sep 17 00:00:00 2001 From: zcy9999 <1328476842@qq.com> Date: Sat, 11 Jun 2022 01:33:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=93=81=E7=B1=BB=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/RestExceptionHandler.java | 17 ++++++ .../controller/v1/CategoryController.java | 10 +++- .../latticy/dto/category/CategoryDTO.java | 5 ++ .../service/impl/CategoryServiceImpl.java | 55 +++++++++---------- .../resources/ValidationMessages.properties | 8 ++- src/main/resources/code-message.properties | 3 + 6 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java b/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java index 5b563dd..7f94a64 100644 --- a/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java +++ b/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java @@ -279,6 +279,23 @@ public class RestExceptionHandler { return result; } + /** + * IllegalArgumentException + */ + @ExceptionHandler({IllegalArgumentException.class}) + public UnifyResponseVO processException(IllegalArgumentException exception, + HttpServletRequest request, + HttpServletResponse response) { + log.error("", exception); + UnifyResponseVO result = new UnifyResponseVO<>(); + result.setRequest(getSimpleRequest(request)); + result.setMessage(CodeMessageConfiguration.getMessage(Integer.valueOf(exception.getMessage()))); + result.setCode(Code.FAIL.getCode()); + response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); + return result; + } + + private UnifyResponseVO> getMapUnifyResponseVO(HttpServletRequest request, HttpServletResponse response, Map msg) { diff --git a/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java index 9875a6b..91b8971 100644 --- a/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java +++ b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java @@ -1,5 +1,6 @@ package io.github.talelin.latticy.controller.v1; +import io.github.talelin.core.annotation.GroupRequired; import io.github.talelin.core.annotation.PermissionMeta; import io.github.talelin.core.annotation.PermissionModule; import io.github.talelin.latticy.common.util.PageUtil; @@ -15,6 +16,7 @@ import io.github.talelin.latticy.vo.UpdatedVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.validation.constraints.Positive; import java.util.List; /** @@ -35,11 +37,13 @@ public class CategoryController { @GetMapping("/page") @PermissionMeta(value = "品类列表查询", mount = false) + @GroupRequired private PageResponseVO getCategoryPage(CategoryPageDTO categoryPageDTO) { return PageUtil.build(categoryService.getCategoryPage(categoryPageDTO)); } @PostMapping("") + @GroupRequired @PermissionMeta(value = "新增品类", mount = false) private CreatedVO createCategory(@RequestBody CategoryDTO categoryDTO) { categoryService.createCategory(categoryDTO); @@ -47,15 +51,17 @@ public class CategoryController { } @PutMapping("/{id}") + @GroupRequired @PermissionMeta(value = "修改品类", mount = false) - private UpdatedVO updateCategory(@PathVariable Integer id, @RequestBody UpdateCategoryDTO categoryDTO) { + private UpdatedVO updateCategory(@PathVariable @Positive(message = "{category.id}") Integer id, @RequestBody UpdateCategoryDTO categoryDTO) { categoryService.updateCategory(id, categoryDTO); return new UpdatedVO(); } @DeleteMapping("/{id}") + @GroupRequired @PermissionMeta(value = "删除品类", mount = false) - private DeletedVO deleteCategory(@PathVariable Integer id) { + private DeletedVO deleteCategory(@PathVariable @Positive(message = "{category.id}") Integer id) { categoryService.deleteCategory(id); return new DeletedVO(); } diff --git a/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java index eb2f764..68ba527 100644 --- a/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java +++ b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java @@ -4,6 +4,7 @@ import io.github.talelin.latticy.model.CategoryDO; import lombok.Data; import org.springframework.beans.BeanUtils; +import javax.validation.constraints.NotBlank; import java.io.Serializable; import java.util.List; @@ -24,21 +25,25 @@ public class CategoryDTO implements Serializable { /** * 父ID */ + @NotBlank(message = "{category.parent-id}") private Integer parentId; /** * 品类名称 */ + @NotBlank(message = "{category.category-name}") private String categoryName; /** * 品类英文名称 */ + @NotBlank(message = "{category.category-english-name}") private String categoryEnglishName; /** * 等级 */ + @NotBlank(message = "{category.level}") private Integer level; diff --git a/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java index b610622..106b98c 100644 --- a/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java +++ b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java @@ -32,18 +32,18 @@ public class CategoryServiceImpl extends ServiceImpl @Override public void createCategory(CategoryDTO categoryDTO) { CategoryDO categoryDO = new CategoryDO(categoryDTO); - Assert.isTrue(this.baseMapper.insert(categoryDO) > 0, "新增品类失败!"); + Assert.isTrue(this.baseMapper.insert(categoryDO) > 0, "18503"); } @Override public void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO) { CategoryDO categoryDO = new CategoryDO(id, updateCategoryDTO); - Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "修改品类失败!"); + Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "18504"); } @Override public void deleteCategory(Integer id) { - Assert.isTrue(this.baseMapper.deleteById(id) > 0, "删除品类失败!"); + Assert.isTrue(this.baseMapper.deleteById(id) > 0, "18505"); } @Override @@ -58,34 +58,29 @@ public class CategoryServiceImpl extends ServiceImpl if (CollUtil.isEmpty(categoryDOList)) { return new ArrayList<>(); } - // 过滤所有的一级目录,父id为0 List firstColumn = categoryDOList.stream().filter(c -> c.getParentId() == 0).map(CategoryDTO::new).collect(Collectors.toList()); - //查出非一级目录,按照其父id分类 - Map> categoryMapList = categoryDOList.stream().filter(c -> (c.getParentId() != 0)).map(CategoryDTO::new).collect(Collectors.groupingBy(CategoryDTO::getParentId)); - //遍历一级目录 - firstColumn.forEach(a -> { - //如果一级目录的id中是否包含非一级目录的父id - if (categoryMapList.get(a.getId()) == null) { - //没有,说明不是该一级目录没有下一级目录,则将其子目录设为空 - a.setChildren(new ArrayList<>()); - } else { - //有,则说明有二级目录,将二级目录的的List集合,赋给子目录 - a.setChildren(categoryMapList.get(a.getId())); + List lastColumn = categoryDOList.stream().filter(c -> c.getParentId() != 0).map(CategoryDTO::new).collect(Collectors.toList()); - //构建三级树 - //遍历二级目录,判断是否含有三级目录 - categoryMapList.get(a.getId()).forEach(b -> { - if (categoryMapList.get(b.getId()) == null) { - //没有,将二级目录下的三级目录设为空 - b.setChildren(new ArrayList<>()); - } else { - //有,将三级目录的List集合,赋给三级目录 - b.setChildren(categoryMapList.get(b.getId())); - } - }); - } - }); - //将firstColumn返回,前端可以通过遍历获取各级目录的数据从而形成三级树的效果 - return firstColumn; + return firstColumn.stream().filter((category) -> category.getParentId() == 0).map((category) -> { + category.setChildren(getChildren(category, lastColumn)); + return category; + }).collect(Collectors.toList()); + } + + /** + * 递归查找所有菜单的子菜单 + * + * @param root + * @param all + * @return + */ + private List getChildren(CategoryDTO root, List all) { + return all.stream().filter(categoryEntity -> { + return categoryEntity.getParentId().equals(root.getId()); + }).map(category -> { + //查询子品类 + category.setChildren(getChildren(category, all)); + return category; + }).collect(Collectors.toList()); } } diff --git a/src/main/resources/ValidationMessages.properties b/src/main/resources/ValidationMessages.properties index 9f5a75a..39b73cb 100644 --- a/src/main/resources/ValidationMessages.properties +++ b/src/main/resources/ValidationMessages.properties @@ -67,4 +67,10 @@ warearea.length=\u5206\u533A\u540D\u957F\u5EA6\u5FC5\u987B\u57282~10\u4E4B\u95F4 # \u5E93\u4F4D\u5F02\u5E38\u4FE1\u606F location.not-blank=\u5E93\u4F4D\u540D\u4E0D\u53EF\u4E3A\u7A7A -location.length=\u5E93\u4F4D\u540D\u957F\u5EA6\u5FC5\u987B\u57282~10\u4E4B\u95F4 \ No newline at end of file +location.length=\u5E93\u4F4D\u540D\u957F\u5EA6\u5FC5\u987B\u57282~10\u4E4B\u95F4 + +category.category-name=品类名称不可为空 +category.category-english-name=品类英文名称不可为空 +category.parent-id=上级ID不可为空 +category.level=等级不可为空 +category.id=品类ID不可为空 \ No newline at end of file diff --git a/src/main/resources/code-message.properties b/src/main/resources/code-message.properties index c427613..fb223bb 100644 --- a/src/main/resources/code-message.properties +++ b/src/main/resources/code-message.properties @@ -78,3 +78,6 @@ code-message[18401]=\u5206\u533A\u4E0D\u5B58\u5728 code-message[18402]=\u5206\u533A\u5DF2\u4F7F\u7528\uFF0C\u4E0D\u80FD\u88AB\u5220\u9664 code-message[18501]=\u5E93\u4F4D\u540D\u79F0\u5DF2\u88AB\u5360\u7528 code-message[18502]=\u5E93\u4F4D\u4E0D\u5B58\u5728 +code-message[18503]=新增品类失败 +code-message[18504]=修改品类失败 +code-message[18505]=删除品类失败 \ No newline at end of file -- Gitee