后端:物料bom完善,递归树创建(之前只能加载到第3层)
master
xs 1 month ago
parent 7ba5490698
commit 92f08c8a85

@ -407,16 +407,17 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
*/ */
private List<TreeSelect> buildMaterialBomTree(List<ProdMaterialBomVo> bomList) { private List<TreeSelect> buildMaterialBomTree(List<ProdMaterialBomVo> bomList) {
// 1. 建立索引 // 1. 建立索引
Map<String, List<ProdMaterialBomVo>> parentVersionGroup = new HashMap<>(); Map<String, List<ProdMaterialBomVo>> parentVersionGroup = new HashMap<>();//根据parentid_materialbomversion分组
Map<Long, Set<String>> materialVersions = new HashMap<>(); Map<Long, Set<String>> materialVersions = new HashMap<>();//根据materialid分组存储materialbomversion
Map<Long, String> materialNameMap = new HashMap<>(); Map<Long, String> materialNameMap = new HashMap<>();//根据materialid存储materialname
Map<Long, Long> materialTypeIdMap = new HashMap<>(); Map<Long, Long> materialTypeIdMap = new HashMap<>();//根据materialid存储materialtypeid
Map<Long, String> materialTypeNameMap = new HashMap<>(); Map<Long, String> materialTypeNameMap = new HashMap<>();//根据materialid存储materialtypename
Map<String, String> versionActiveFlagMap = new HashMap<>(); // 新增:存储版本状态 Map<String, String> versionActiveFlagMap = new HashMap<>(); // 根据materialid和version存储版本状态
for (ProdMaterialBomVo item : bomList) { for (ProdMaterialBomVo item : bomList) {
// 按parentId+version分组 // 按parentId+version分组
String parentKey = item.getParentId() + "_" + item.getMaterialBomVersion(); String parentKey = item.getParentId() + "_" + item.getMaterialBomVersion();
//如果不存在则计算添加
parentVersionGroup.computeIfAbsent(parentKey, k -> new ArrayList<>()).add(item); parentVersionGroup.computeIfAbsent(parentKey, k -> new ArrayList<>()).add(item);
// 记录每个material的所有版本 // 记录每个material的所有版本
@ -443,7 +444,7 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
// 记录版本状态(新增) // 记录版本状态(新增)
String versionKey = item.getParentId() + "_" + item.getMaterialBomVersion(); String versionKey = item.getParentId() + "_" + item.getMaterialBomVersion();
versionActiveFlagMap.putIfAbsent(versionKey, item.getActiveFlag()); // 假设activeFlag是boolean类型 versionActiveFlagMap.putIfAbsent(versionKey, item.getActiveFlag());
} }
@ -464,41 +465,17 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
for (String version : parentVersions) { for (String version : parentVersions) {
String versionKey = topParentId + "_" + version;
String activeFlag = versionActiveFlagMap.get(versionKey);
TreeSelect versionNode = new TreeSelect();
versionNode.setTreeKey(versionKey);
versionNode.setLabel(buildLabel(topParentId, version, materialNameMap,materialTypeNameMap));
versionNode.setMaterialBomVersion(version);
versionNode.setMaterialId(topParentId);
versionNode.setMaterialTypeId(materialTypeIdMap.getOrDefault(topParentId, 0L));
versionNode.setMaterialTypeName(materialTypeNameMap.getOrDefault(topParentId, ""));
versionNode.setActiveFlag(activeFlag);
versionNode.setChildren(new ArrayList<>());
String parentKey = topParentId + "_" + version; String parentKey = topParentId + "_" + version;
String activeFlag = versionActiveFlagMap.get(parentKey);
TreeSelect versionNode = createParentNode(topParentId, version, activeFlag, materialNameMap, materialTypeIdMap, materialTypeNameMap, parentKey);
if (parentVersionGroup.containsKey(parentKey)) { if (parentVersionGroup.containsKey(parentKey)) {
// 获取该父节点下的所有materialId // 获取该父节点下的所有materialId
Set<Long> childMaterialIds = parentVersionGroup.get(parentKey).stream() Set<Long> childMaterialIds = parentVersionGroup.get(parentKey).stream()
.map(ProdMaterialBomVo::getMaterialId) .map(ProdMaterialBomVo::getMaterialId)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
buildTree(versionNode,childMaterialIds,materialVersions,parentVersionGroup,materialNameMap,materialTypeIdMap,materialTypeNameMap,versionActiveFlagMap);
for (Long materialId : childMaterialIds) {
// 添加无版本号的基础节点
versionNode.getChildren().add(createBasicNode(materialId,materialNameMap,materialTypeIdMap,materialTypeNameMap));
// 动态添加该material的有子节点的版本
if (materialVersions.containsKey(materialId)) {
for (String childVersion : materialVersions.get(materialId)) {
TreeSelect versionedNode = createVersionNodeIfHasChildren(
materialId, childVersion, activeFlag,parentVersionGroup,materialNameMap,materialTypeIdMap,materialTypeNameMap);
if (versionedNode != null) {
versionNode.getChildren().add(versionedNode);
}
}
}
}
} }
// 只有当有子节点时才添加父节点 // 只有当有子节点时才添加父节点
@ -513,6 +490,7 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
// return rootNodes.stream().map(TreeSelect::new).collect(Collectors.toList()); // return rootNodes.stream().map(TreeSelect::new).collect(Collectors.toList());
} }
private static String buildLabel(Long materialId, String version, Map<Long, String> materialNameMap, Map<Long, String> materialTypeNameMap) { private static String buildLabel(Long materialId, String version, Map<Long, String> materialNameMap, Map<Long, String> materialTypeNameMap) {
String materialName = materialNameMap.getOrDefault(materialId, ""); String materialName = materialNameMap.getOrDefault(materialId, "");
String materialTypeName = materialTypeNameMap.getOrDefault(materialId, ""); String materialTypeName = materialTypeNameMap.getOrDefault(materialId, "");
@ -523,7 +501,29 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
} }
private static TreeSelect createBasicNode(Long materialId, private static void buildTree(TreeSelect versionNode,
Set<Long> childMaterialIds,Map<Long, Set<String>> materialVersions,
Map<String, List<ProdMaterialBomVo>> parentVersionGroup,
Map<Long, String> materialNameMap, Map<Long, Long> materialTypeIdMap,
Map<Long, String> materialTypeNameMap,Map<String, String> versionActiveFlagMap){
for (Long materialId : childMaterialIds) {
// 添加无版本号的基础节点
versionNode.getChildren().add(createChildNode(materialId, materialNameMap, materialTypeIdMap, materialTypeNameMap));
// 动态添加该material的有子节点的版本
if (materialVersions.containsKey(materialId)) {
for (String childVersion : materialVersions.get(materialId)) {
TreeSelect versionedNode = createVersionNodeIfHasChildren(
materialId, childVersion, materialVersions,parentVersionGroup, materialNameMap, materialTypeIdMap, materialTypeNameMap, versionActiveFlagMap);
if (versionedNode != null) {
versionNode.getChildren().add(versionedNode);
}
}
}
}
}
private static TreeSelect createChildNode(Long materialId,
Map<Long, String> materialNameMap, Map<Long, Long> materialTypeIdMap, Map<Long, String> materialTypeNameMap) { Map<Long, String> materialNameMap, Map<Long, Long> materialTypeIdMap, Map<Long, String> materialTypeNameMap) {
// this.parentId = prodMaterialBomVo.getParentId(); // this.parentId = prodMaterialBomVo.getParentId();
@ -543,17 +543,40 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
node.setActiveFlag(null); node.setActiveFlag(null);
return node; return node;
} }
private static TreeSelect createVersionNodeIfHasChildren(Long materialId, String version,String activeFlag,
private static TreeSelect createVersionNodeIfHasChildren(Long materialId, String version,
Map<Long,Set<String>> materialVersions,
Map<String, List<ProdMaterialBomVo>> parentVersionGroup, Map<String, List<ProdMaterialBomVo>> parentVersionGroup,
Map<Long, String> materialNameMap,Map<Long, Long> materialTypeIdMap,Map<Long, String> materialTypeNameMap) { Map<Long, String> materialNameMap, Map<Long, Long> materialTypeIdMap,
Map<Long, String> materialTypeNameMap, Map<String, String> versionActiveFlagMap) {
String key = materialId+"_"+version; String key = materialId+"_"+version;
String activeFlag = versionActiveFlagMap.getOrDefault(key, "");
// 只有当该版本有子节点时才创建 // 只有当该版本有子节点时才创建
if (parentVersionGroup.containsKey(key)) { if (parentVersionGroup.containsKey(key)) {
TreeSelect node = createParentNode(materialId, version, activeFlag, materialNameMap, materialTypeIdMap, materialTypeNameMap, key);
// 添加子节点
Set<Long> childIds = parentVersionGroup.get(key).stream()
.map(ProdMaterialBomVo::getMaterialId)
.collect(Collectors.toSet());
buildTree(node,childIds,materialVersions,parentVersionGroup,materialNameMap,materialTypeIdMap,materialTypeNameMap,versionActiveFlagMap);
// for (Long childId : childIds) {
// node.getChildren().add(createChildNode(childId, materialNameMap, materialTypeIdMap, materialTypeNameMap));
// }
return node;
}
return null;
}
private static TreeSelect createParentNode(Long materialId, String version, String activeFlag, Map<Long, String> materialNameMap, Map<Long, Long> materialTypeIdMap, Map<Long, String> materialTypeNameMap, String key) {
TreeSelect node = new TreeSelect(); TreeSelect node = new TreeSelect();
node.setTreeKey(key); node.setTreeKey(key);
node.setLabel(buildLabel(materialId, version, materialNameMap, materialTypeNameMap)); node.setLabel(buildLabel(materialId, version, materialNameMap, materialTypeNameMap));
node.setMaterialBomVersion(version); node.setMaterialBomVersion(version);
node.setMaterialBomVersion(version);
node.setMaterialId(materialId); node.setMaterialId(materialId);
node.setMaterialTypeId(materialTypeIdMap.getOrDefault(materialId, 0L)); node.setMaterialTypeId(materialTypeIdMap.getOrDefault(materialId, 0L));
node.setMaterialTypeName(materialTypeNameMap.getOrDefault(materialId, "")); node.setMaterialTypeName(materialTypeNameMap.getOrDefault(materialId, ""));
@ -561,19 +584,8 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
node.setChildren(new ArrayList<>()); node.setChildren(new ArrayList<>());
// 添加子节点
Set<Long> childIds = parentVersionGroup.get(key).stream()
.map(ProdMaterialBomVo::getMaterialId)
.collect(Collectors.toSet());
for (Long childId : childIds) {
node.getChildren().add(createBasicNode(childId,materialNameMap,materialTypeIdMap,materialTypeNameMap));
}
return node; return node;
} }
return null;
}
private static List<TreeSelect> buildChildren(Long currentParentId, List<ProdMaterialBomVo> items, private static List<TreeSelect> buildChildren(Long currentParentId, List<ProdMaterialBomVo> items,
@ -629,8 +641,6 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
} }
// private static void printTree(List<ProdMaterialBomVo> nodes, int indent) { // private static void printTree(List<ProdMaterialBomVo> nodes, int indent) {
// for (ProdMaterialBomVo node : nodes) { // for (ProdMaterialBomVo node : nodes) {
// System.out.println(" ".repeat(indent) + node.getParentMaterialName()+"["+node.getMaterialBomVersion()+"]"+node.getMaterialName()); // System.out.println(" ".repeat(indent) + node.getParentMaterialName()+"["+node.getMaterialBomVersion()+"]"+node.getMaterialName());
@ -747,7 +757,6 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
} }
/** /**
* *
*/ */

Loading…
Cancel
Save