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

@ -236,11 +236,11 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
*/
private void validEntityUsed(@NotNull ProdMaterialBomBo entity) {
if(entity.getActiveFlag().equals(HwMomMesConstants.MES_MATERIAL_BOM_ACTIVE_FLAG_ACTIVE)){
if (entity.getActiveFlag().equals(HwMomMesConstants.MES_MATERIAL_BOM_ACTIVE_FLAG_ACTIVE)) {
throw new ServiceException("此物料bom已经发布不能修改");
}
if(entity.getActiveFlag().equals(HwMomMesConstants.MES_MATERIAL_BOM_ACTIVE_FLAG_HISTORY)){
if (entity.getActiveFlag().equals(HwMomMesConstants.MES_MATERIAL_BOM_ACTIVE_FLAG_HISTORY)) {
throw new ServiceException("此物料bom为历史版本不能修改");
}
@ -407,16 +407,17 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
*/
private List<TreeSelect> buildMaterialBomTree(List<ProdMaterialBomVo> bomList) {
// 1. 建立索引
Map<String, List<ProdMaterialBomVo>> parentVersionGroup = new HashMap<>();
Map<Long, Set<String>> materialVersions = new HashMap<>();
Map<Long, String> materialNameMap = new HashMap<>();
Map<Long, Long> materialTypeIdMap = new HashMap<>();
Map<Long, String> materialTypeNameMap = new HashMap<>();
Map<String, String> versionActiveFlagMap = new HashMap<>(); // 新增:存储版本状态
Map<String, List<ProdMaterialBomVo>> parentVersionGroup = new HashMap<>();//根据parentid_materialbomversion分组
Map<Long, Set<String>> materialVersions = new HashMap<>();//根据materialid分组存储materialbomversion
Map<Long, String> materialNameMap = new HashMap<>();//根据materialid存储materialname
Map<Long, Long> materialTypeIdMap = new HashMap<>();//根据materialid存储materialtypeid
Map<Long, String> materialTypeNameMap = new HashMap<>();//根据materialid存储materialtypename
Map<String, String> versionActiveFlagMap = new HashMap<>(); // 根据materialid和version存储版本状态
for (ProdMaterialBomVo item : bomList) {
// 按parentId+version分组
String parentKey = item.getParentId() + "_" + item.getMaterialBomVersion();
//如果不存在则计算添加
parentVersionGroup.computeIfAbsent(parentKey, k -> new ArrayList<>()).add(item);
// 记录每个material的所有版本
@ -443,7 +444,7 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
// 记录版本状态(新增)
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());
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 activeFlag = versionActiveFlagMap.get(parentKey);
TreeSelect versionNode = createParentNode(topParentId, version, activeFlag, materialNameMap, materialTypeIdMap, materialTypeNameMap, parentKey);
if (parentVersionGroup.containsKey(parentKey)) {
// 获取该父节点下的所有materialId
Set<Long> childMaterialIds = parentVersionGroup.get(parentKey).stream()
.map(ProdMaterialBomVo::getMaterialId)
.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());
}
private static String buildLabel(Long materialId, String version, Map<Long, String> materialNameMap, Map<Long, String> materialTypeNameMap) {
String materialName = materialNameMap.getOrDefault(materialId, "");
String materialTypeName = materialTypeNameMap.getOrDefault(materialId, "");
@ -523,8 +501,30 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
}
private static TreeSelect createBasicNode(Long materialId,
Map<Long, String> materialNameMap,Map<Long, Long> materialTypeIdMap,Map<Long, String> materialTypeNameMap) {
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) {
// this.parentId = prodMaterialBomVo.getParentId();
TreeSelect node = new TreeSelect();
@ -543,40 +543,52 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
node.setActiveFlag(null);
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<Long, String> materialNameMap,Map<Long, Long> materialTypeIdMap,Map<Long, String> materialTypeNameMap) {
String key = materialId + "_" + version;
Map<Long, String> materialNameMap, Map<Long, Long> materialTypeIdMap,
Map<Long, String> materialTypeNameMap, Map<String, String> versionActiveFlagMap) {
String key = materialId+"_"+version;
String activeFlag = versionActiveFlagMap.getOrDefault(key, "");
// 只有当该版本有子节点时才创建
if (parentVersionGroup.containsKey(key)) {
TreeSelect node = new TreeSelect();
node.setTreeKey(key);
node.setLabel(buildLabel(materialId, version, materialNameMap,materialTypeNameMap));
node.setMaterialBomVersion(version);
node.setMaterialBomVersion(version);
node.setMaterialId(materialId);
node.setMaterialTypeId(materialTypeIdMap.getOrDefault(materialId, 0L));
node.setMaterialTypeName(materialTypeNameMap.getOrDefault(materialId, ""));
node.setActiveFlag(activeFlag);
node.setChildren(new ArrayList<>());
TreeSelect node = createParentNode(materialId, version, activeFlag, materialNameMap, materialTypeIdMap, materialTypeNameMap, key);
// 添加子节点
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));
}
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();
node.setTreeKey(key);
node.setLabel(buildLabel(materialId, version, materialNameMap, materialTypeNameMap));
node.setMaterialBomVersion(version);
node.setMaterialId(materialId);
node.setMaterialTypeId(materialTypeIdMap.getOrDefault(materialId, 0L));
node.setMaterialTypeName(materialTypeNameMap.getOrDefault(materialId, ""));
node.setActiveFlag(activeFlag);
private static List<TreeSelect> buildChildren(Long currentParentId,List<ProdMaterialBomVo> items,
node.setChildren(new ArrayList<>());
return node;
}
private static List<TreeSelect> buildChildren(Long currentParentId, List<ProdMaterialBomVo> items,
Map<Long, Map<String, List<ProdMaterialBomVo>>> parentVersionMap) {
List<TreeSelect> children = new ArrayList<>();
@ -629,8 +641,6 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
}
// private static void printTree(List<ProdMaterialBomVo> nodes, int indent) {
// for (ProdMaterialBomVo node : nodes) {
// System.out.println(" ".repeat(indent) + node.getParentMaterialName()+"["+node.getMaterialBomVersion()+"]"+node.getMaterialName());
@ -747,18 +757,17 @@ public class ProdMaterialBomServiceImpl implements IProdMaterialBomService {
}
/**
*
*/
private void recursionFn(List<ProdMaterialBomVo> list, ProdMaterialBomVo parent) {
// 获取子节点
List<ProdMaterialBomVo> childList = getChildList(list, parent);
if(childList == null || childList.isEmpty()){
System.out.println(parent.getMaterialBomId()+"----");
if (childList == null || childList.isEmpty()) {
System.out.println(parent.getMaterialBomId() + "----");
// parent.setMaterialBomVersion("");
}else{
System.out.println(parent.getMaterialBomId()+"****"+childList.get(0).getMaterialBomVersion());
} else {
System.out.println(parent.getMaterialBomId() + "****" + childList.get(0).getMaterialBomVersion());
// parent.setMaterialBomVersion(childList.get(0).getMaterialBomVersion());
}
parent.setChildren(childList);

Loading…
Cancel
Save