From 88209e11fa21aa47499bb8116e63e8a4848b1684 Mon Sep 17 00:00:00 2001 From: zhaochencheng Date: Fri, 30 Dec 2022 21:23:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=86=E7=B1=BB=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E6=94=B9=E7=94=B1=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=EF=BC=8C=E6=96=B9=E4=BE=BF=E5=A4=9A=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProductCategoryController.java | 14 +---- .../cyl/pms/pojo/vo/ProductCategoryVO.java | 11 ++-- .../pms/service/ProductCategoryService.java | 53 ++++++++++++++++++- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/ruoyi-mall/src/main/java/com/cyl/pms/controller/ProductCategoryController.java b/ruoyi-mall/src/main/java/com/cyl/pms/controller/ProductCategoryController.java index 4ebcfe6..47272bf 100644 --- a/ruoyi-mall/src/main/java/com/cyl/pms/controller/ProductCategoryController.java +++ b/ruoyi-mall/src/main/java/com/cyl/pms/controller/ProductCategoryController.java @@ -45,21 +45,11 @@ public class ProductCategoryController extends BaseController { @ApiOperation("查询商品分类列表") @PreAuthorize("@ss.hasPermi('pms:productCategory:list')") @PostMapping("/list") - public ResponseEntity> list(@RequestBody ProductCategoryQuery query) { - List list = service.selectList(query, null); + public ResponseEntity> list(@RequestBody ProductCategoryQuery query) { + List list = service.selectList(query, null); return ResponseEntity.ok(list); } - @ApiOperation("导出商品分类列表") - @PreAuthorize("@ss.hasPermi('pms:productCategory:export')") - @Log(title = "商品分类", businessType = BusinessType.EXPORT) - @GetMapping("/export") - public ResponseEntity export(ProductCategoryQuery query) { - List list = service.selectList(query, null); - ExcelUtil util = new ExcelUtil<>(ProductCategoryVO.class); - return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "商品分类数据")); - } - @ApiOperation("获取商品分类详细信息") @PreAuthorize("@ss.hasPermi('pms:productCategory:query')") @GetMapping(value = "/{id}") diff --git a/ruoyi-mall/src/main/java/com/cyl/pms/pojo/vo/ProductCategoryVO.java b/ruoyi-mall/src/main/java/com/cyl/pms/pojo/vo/ProductCategoryVO.java index 232cdc5..b9ab95c 100644 --- a/ruoyi-mall/src/main/java/com/cyl/pms/pojo/vo/ProductCategoryVO.java +++ b/ruoyi-mall/src/main/java/com/cyl/pms/pojo/vo/ProductCategoryVO.java @@ -1,9 +1,13 @@ package com.cyl.pms.pojo.vo; +import com.cyl.pms.domain.ProductCategory; import com.ruoyi.common.annotation.Excel; import com.fasterxml.jackson.annotation.JsonFormat; import com.ruoyi.common.core.domain.BaseAudit; import lombok.Data; + +import java.util.List; + /** * 商品分类 数据视图对象 * @@ -14,21 +18,16 @@ public class ProductCategoryVO extends BaseAudit { /** ID */ private Long id; /** 上级分类的编号:0表示一级分类 */ - @Excel(name = "上级分类的编号:0表示一级分类") private Long parentId; /** NAME */ - @Excel(name = "NAME") private String name; /** 分类级别:0->1级;1->2级 */ - @Excel(name = "分类级别:0->1级;1->2级") private Integer level; /** 显示状态:0->不显示;1->显示 */ - @Excel(name = "显示状态:0->不显示;1->显示") private Integer showStatus; /** SORT */ - @Excel(name = "SORT") private Integer sort; /** 图标 */ - @Excel(name = "图标") private String icon; + private List children; } diff --git a/ruoyi-mall/src/main/java/com/cyl/pms/service/ProductCategoryService.java b/ruoyi-mall/src/main/java/com/cyl/pms/service/ProductCategoryService.java index 8544f00..d2f31a9 100644 --- a/ruoyi-mall/src/main/java/com/cyl/pms/service/ProductCategoryService.java +++ b/ruoyi-mall/src/main/java/com/cyl/pms/service/ProductCategoryService.java @@ -1,9 +1,16 @@ package com.cyl.pms.service; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.time.LocalDateTime; + +import cn.hutool.core.lang.tree.Tree; +import cn.hutool.core.lang.tree.TreeNodeConfig; +import cn.hutool.core.lang.tree.TreeUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.cyl.pms.convert.ProductCategoryConvert; +import com.cyl.pms.pojo.vo.ProductCategoryVO; import com.github.pagehelper.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; @@ -23,6 +30,8 @@ import com.cyl.pms.pojo.query.ProductCategoryQuery; public class ProductCategoryService { @Autowired private ProductCategoryMapper productCategoryMapper; + @Autowired + private ProductCategoryConvert convert; /** * 查询商品分类 @@ -41,7 +50,7 @@ public class ProductCategoryService { * @param page 分页条件 * @return 商品分类 */ - public List selectList(ProductCategoryQuery query, Pageable page) { + public List selectList(ProductCategoryQuery query, Pageable page) { if (page != null) { PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize()); } @@ -70,9 +79,49 @@ public class ProductCategoryService { if (!StringUtils.isEmpty(icon)) { qw.eq("icon", icon); } - return productCategoryMapper.selectList(qw); + qw.orderByAsc("sort"); + + List productCategories = productCategoryMapper.selectList(qw); + List productCategoryVOS = convert.dos2vos(productCategories); + return formatTree(productCategoryVOS); + + } + + private List formatTree(List nodes) { + List tree = new ArrayList<>(); + List children = new ArrayList<>(); + // 1)先获取到所有根节点 + for (ProductCategoryVO node : nodes) { + if (node.getParentId() == null || node.getParentId() == 0 ) { + tree.add(node); + } else { + children.add(node); + } + } + // 2)把所有除根结点外的节点作为子节点,然后遍历每一个根节点 + for (ProductCategoryVO node : tree) { + // 3)递归构建此根的子节点 + recur(node, children); + } + return tree; + } + + private void recur(ProductCategoryVO rootNode, List children) { + // 1)遍历剩余子节点,找出当前根的子节点 + for (ProductCategoryVO node : children) { + // 2)如果子节点的父id等于根节点的id,那么就将这个节点加到根节点的children列表中 + if (rootNode.getId() == node.getParentId()) { + if (rootNode.getChildren() == null) { + rootNode.setChildren(new ArrayList<>()); + } + rootNode.getChildren().add(node); + // 3)以当前节点作为根节点进行递归,检查是否还有子节点。 + recur(node, children); + } + } } + /** * 新增商品分类 *