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] =?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