商品分类数据结构改由后端实现,方便多前端使用

pull/1/head
zhaochencheng 3 years ago
parent fcb63620b0
commit 88209e11fa

@ -45,21 +45,11 @@ public class ProductCategoryController extends BaseController {
@ApiOperation("查询商品分类列表")
@PreAuthorize("@ss.hasPermi('pms:productCategory:list')")
@PostMapping("/list")
public ResponseEntity<List<ProductCategory>> list(@RequestBody ProductCategoryQuery query) {
List<ProductCategory> list = service.selectList(query, null);
public ResponseEntity<List<ProductCategoryVO>> list(@RequestBody ProductCategoryQuery query) {
List<ProductCategoryVO> 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<String> export(ProductCategoryQuery query) {
List<ProductCategory> list = service.selectList(query, null);
ExcelUtil<ProductCategoryVO> util = new ExcelUtil<>(ProductCategoryVO.class);
return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "商品分类数据"));
}
@ApiOperation("获取商品分类详细信息")
@PreAuthorize("@ss.hasPermi('pms:productCategory:query')")
@GetMapping(value = "/{id}")

@ -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<ProductCategoryVO> children;
}

@ -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<ProductCategory> selectList(ProductCategoryQuery query, Pageable page) {
public List<ProductCategoryVO> 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<ProductCategory> productCategories = productCategoryMapper.selectList(qw);
List<ProductCategoryVO> productCategoryVOS = convert.dos2vos(productCategories);
return formatTree(productCategoryVOS);
}
private List<ProductCategoryVO> formatTree(List<ProductCategoryVO> nodes) {
List<ProductCategoryVO> tree = new ArrayList<>();
List<ProductCategoryVO> 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<ProductCategoryVO> 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);
}
}
}
/**
*
*

Loading…
Cancel
Save