diff --git a/pom.xml b/pom.xml index cb1d6badf7ea22aaeb1ffc0046332ec1297ea039..ae4c3a9db69ce15403c51779f75b6b78731c13be 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/common/exception/RestExceptionHandler.java b/src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java index 5b563dd7873c21d8530e4a1ad3c177ebad41d161..7f94a643ed138b4e372b6626eff60e42b675d77d 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 new file mode 100644 index 0000000000000000000000000000000000000000..91b8971607c0af7a297cd20fc28c157a9899b00a --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/controller/v1/CategoryController.java @@ -0,0 +1,74 @@ +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; +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 javax.validation.constraints.Positive; +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) + @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); + return new CreatedVO(); + } + + @PutMapping("/{id}") + @GroupRequired + @PermissionMeta(value = "修改品类", mount = false) + 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 @Positive(message = "{category.id}") 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 0000000000000000000000000000000000000000..68ba527a21bd0a6d72ae2514366d3b6764ee39c8 --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/dto/category/CategoryDTO.java @@ -0,0 +1,58 @@ +package io.github.talelin.latticy.dto.category; + +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; + +/** + * 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 + */ + @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; + + + 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 0000000000000000000000000000000000000000..8427f359dac48d6fec982ac676b3150c7704dec5 --- /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 0000000000000000000000000000000000000000..fdb1c23a0b977bbf20ea368a5a8e512dddb91d21 --- /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 0000000000000000000000000000000000000000..2f20957d061a4d65ad33d032fb8663a7b4f5f1c6 --- /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 0000000000000000000000000000000000000000..23d825c9ccb88cb30cf49bf7a7226801ffddc9bf --- /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 0000000000000000000000000000000000000000..987c3203c74c5a705123cecc81203ea4fd431c11 --- /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 0000000000000000000000000000000000000000..106b98c8a0b8066d94510e7f847e5c7f8898d64b --- /dev/null +++ b/src/main/java/io/github/talelin/latticy/service/impl/CategoryServiceImpl.java @@ -0,0 +1,86 @@ +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, "18503"); + } + + @Override + public void updateCategory(Integer id, UpdateCategoryDTO updateCategoryDTO) { + CategoryDO categoryDO = new CategoryDO(id, updateCategoryDTO); + Assert.isTrue(this.baseMapper.updateById(categoryDO) > 0, "18504"); + } + + @Override + public void deleteCategory(Integer id) { + Assert.isTrue(this.baseMapper.deleteById(id) > 0, "18505"); + } + + @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<>(); + } + List firstColumn = categoryDOList.stream().filter(c -> c.getParentId() == 0).map(CategoryDTO::new).collect(Collectors.toList()); + List lastColumn = categoryDOList.stream().filter(c -> c.getParentId() != 0).map(CategoryDTO::new).collect(Collectors.toList()); + + 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 9f5a75a22509ebbf00f4de8dae80df19e1fd3e84..39b73cb7137d852ab3cd24770c1ab1eef5709729 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 c427613c27e19a945463ef505242bb8802b4582a..fb223bb0530c3fa2556cf9ddb33d9a70a1a37122 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 diff --git a/src/main/resources/mapper/CategoryMapper.xml b/src/main/resources/mapper/CategoryMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..db39de07ab9928440213dcfaa763fb4400970991 --- /dev/null +++ b/src/main/resources/mapper/CategoryMapper.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +