商品模块代码提交

This commit is contained in:
Tankaikai
2025-02-13 18:52:22 +08:00
parent 5cc5e87d7f
commit c1b4f06670
16 changed files with 764 additions and 54 deletions

View File

@@ -0,0 +1,18 @@
package com.czg.constant;
/**
* 常量
*
* @author admin admin@cashier.com
* @since 1.0.0
*/
public interface GlobalConstant {
/**
* 树形数据根节点标识
*/
Long TREE_ROOT = 0L;
}

View File

@@ -0,0 +1,35 @@
package com.czg.utils;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 树节点,所有需要实现树节点的,都需要继承该类
*
* @author admin admin@cashier.com
* @since 1.0.0
*/
@Data
public class TreeNode<T> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 主键
*/
private Long id;
/**
* 上级ID
*/
private Long pid;
/**
* 子节点列表
*/
private List<T> children = new ArrayList<>();
}

View File

@@ -0,0 +1,72 @@
package com.czg.utils;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 树形结构工具类,如:菜单、部门等
*
* @author admin admin@cashier.com
* @since 1.0.0
*/
public class TreeUtils {
/**
* 根据pid构建树节点
*/
public static <T extends TreeNode> List<T> build(List<T> treeNodes, Long pid) {
//pid不能为空
AssertUtil.isNull(pid, "pid不能为空");
List<T> treeList = new ArrayList<>();
for(T treeNode : treeNodes) {
if (pid.equals(treeNode.getPid())) {
treeList.add(findChildren(treeNodes, treeNode));
}
}
return treeList;
}
/**
* 查找子节点
*/
private static <T extends TreeNode> T findChildren(List<T> treeNodes, T rootNode) {
for(T treeNode : treeNodes) {
if(rootNode.getId().equals(treeNode.getPid())) {
rootNode.getChildren().add(findChildren(treeNodes, treeNode));
}
}
return rootNode;
}
/**
* 构建树节点
*/
public static <T extends TreeNode> List<T> build(List<T> treeNodes) {
List<T> result = new ArrayList<>();
//list转map
Map<Long, T> nodeMap = new LinkedHashMap<>(treeNodes.size());
for(T treeNode : treeNodes){
nodeMap.put(treeNode.getId(), treeNode);
}
for(T node : nodeMap.values()) {
T parent = nodeMap.get(node.getPid());
if(parent != null && !(node.getId().equals(parent.getId()))){
parent.getChildren().add(node);
continue;
}
result.add(node);
}
return result;
}
}