From 7735861b880a4b8586fc360df6339eb2768180bc Mon Sep 17 00:00:00 2001 From: linbangquan <1437892690@qq.com> Date: Tue, 23 Jan 2024 14:57:08 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BF=AE=E5=A4=8D]=20cmdb=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=BC=82=E5=B8=B8-=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E2=80=9Cxxx=E2=80=9D=E5=AD=98=E5=9C=A8=E6=8B=A5=E6=9C=89?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E5=94=AF=E4=B8=80=E8=A7=84=E5=88=99=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E7=9A=84=E9=85=8D=E7=BD=AE=E9=A1=B9=E2=80=9Cyyy?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1075292183953408]cmdb节点同步异常-模型“xxx”存在拥有相同唯一规则属性的配置项“yyy” http://192.168.0.96:8090/demo/rdm.html#/bug-detail/939050947543040/939050947543057/1075292183953408 --- .../stephandler/CmdbSyncProcessComponent.java | 113 +++++++++++++++++- 1 file changed, 108 insertions(+), 5 deletions(-) diff --git a/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java b/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java index 1fefb6b4..cf7d1408 100644 --- a/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java +++ b/src/main/java/neatlogic/module/cmdb/process/stephandler/CmdbSyncProcessComponent.java @@ -244,18 +244,18 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { return 0; } /* 遍历起始模型配置信息列表,生成CiEntityTransactionVo列表 */ - List ciEntityTransactionList = new ArrayList<>(); + List allCiEntityTransactionList = new ArrayList<>(); for (CiEntitySyncConfigVo mainConfigObj : startConfigList) { List list = createCiEntityTransactionVo(ciEntityTransactionMap, mainConfigObj, dependencyConfigMap); for (CiEntityTransactionVo ciEntityTransactionVo : list) { - if (!ciEntityTransactionList.contains(ciEntityTransactionVo)) { - ciEntityTransactionList.add(ciEntityTransactionVo); + if (!allCiEntityTransactionList.contains(ciEntityTransactionVo)) { + allCiEntityTransactionList.add(ciEntityTransactionVo); } } } - if (CollectionUtils.isNotEmpty(ciEntityTransactionList)) { + if (CollectionUtils.isNotEmpty(allCiEntityTransactionList)) { // 遍历ciEntityTransactionList,根据唯一规则属性值删除重复配置信息 - removeDuplicatesByUniqueAttrValue(ciEntityTransactionList); + List ciEntityTransactionList = removeDuplicatesByUniqueAttrValue(allCiEntityTransactionList); EscapeTransactionJob.State s = new EscapeTransactionJob(() -> { InputFromContext.init(InputFrom.ITSM); Long transactionGroupId = ciEntityService.saveCiEntity(ciEntityTransactionList); @@ -382,9 +382,11 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { Map uniqueAttrValueListJoinStr2UuidMap = new HashMap<>(); // 保存将要删除uuid与等价替换uuid的映射关系 Map toDeleteUuid2EquivalentUuidMap = new HashMap<>(); + Map ciEntityTransactionMap = new HashMap<>(); // 反向遍历,如果唯一规则属性值相同,后面数据优先级高 for (int i = ciEntityTransactionList.size() - 1; i >= 0; i--) { CiEntityTransactionVo configObj = ciEntityTransactionList.get(i); + ciEntityTransactionMap.put(configObj.getCiEntityUuid(), configObj); String uniqueAttrValueListJoinStr = ""; if (Objects.equals(configObj.getAction(), TransactionActionType.UPDATE.getValue())) { uniqueAttrValueListJoinStr = configObj.getCiEntityId().toString(); @@ -438,6 +440,7 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { String equivalentUuid = toDeleteUuid2EquivalentUuidMap.get(ciEntityUuid); if (StringUtils.isNotBlank(equivalentUuid)) { valueObj.put("ciEntityUuid", equivalentUuid); + merge(ciEntityTransactionMap.get(equivalentUuid), ciEntityTransactionMap.get(ciEntityUuid)); } } } @@ -446,6 +449,106 @@ public class CmdbSyncProcessComponent extends ProcessStepHandlerBase { return newConfigList; } + private void merge(CiEntityTransactionVo ciEntityTransactionA, CiEntityTransactionVo ciEntityTransactionB) { + { + JSONObject attrEntityDataA = ciEntityTransactionA.getAttrEntityData(); + JSONObject attrEntityDataB = ciEntityTransactionB.getAttrEntityData(); + if (MapUtils.isNotEmpty(attrEntityDataB)) { + if (MapUtils.isNotEmpty(attrEntityDataA)) { + for (Map.Entry entryB : attrEntityDataB.entrySet()) { + JSONObject attrEntityB = (JSONObject) entryB.getValue(); + JSONArray valueListB = attrEntityB.getJSONArray("valueList"); + if (CollectionUtils.isNotEmpty(valueListB)) { + boolean keyExists = false; + for (Map.Entry entryA : attrEntityDataA.entrySet()) { + if (Objects.equals(entryA.getKey(), entryB.getKey())) { + keyExists = true; + JSONObject attrEntityA = (JSONObject) entryA.getValue(); + JSONArray valueListA = attrEntityA.getJSONArray("valueList"); + if (CollectionUtils.isEmpty(valueListA)) { + attrEntityDataA.put(entryB.getKey(), attrEntityB); + } + } + if (keyExists) { + break; + } + } + if (!keyExists) { + attrEntityDataA.put(entryB.getKey(), attrEntityB); + } + } + } + } else { + ciEntityTransactionA.setAttrEntityData(attrEntityDataB); + } + } + } + { + JSONObject globalAttrEntityDataA = ciEntityTransactionA.getGlobalAttrEntityData(); + JSONObject globalAttrEntityDataB = ciEntityTransactionB.getGlobalAttrEntityData(); + if (MapUtils.isNotEmpty(globalAttrEntityDataB)) { + if (MapUtils.isNotEmpty(globalAttrEntityDataA)) { + for (Map.Entry entryB : globalAttrEntityDataB.entrySet()) { + JSONObject globalAttrEntityB = (JSONObject) entryB.getValue(); + JSONArray valueListB = globalAttrEntityB.getJSONArray("valueList"); + if (CollectionUtils.isNotEmpty(valueListB)) { + boolean keyExists = false; + for (Map.Entry entryA : globalAttrEntityDataA.entrySet()) { + if (Objects.equals(entryA.getKey(), entryB.getKey())) { + keyExists = true; + JSONObject globalAttrEntityA = (JSONObject) entryA.getValue(); + JSONArray valueListA = globalAttrEntityA.getJSONArray("valueList"); + if (CollectionUtils.isEmpty(valueListA)) { + globalAttrEntityDataA.put(entryB.getKey(), globalAttrEntityB); + } + } + if (keyExists) { + break; + } + } + if (!keyExists) { + globalAttrEntityDataA.put(entryB.getKey(), globalAttrEntityB); + } + } + } + } else { + ciEntityTransactionA.setGlobalAttrEntityData(globalAttrEntityDataB); + } + } + } + { + JSONObject relEntityDataA = ciEntityTransactionA.getRelEntityData(); + JSONObject relEntityDataB = ciEntityTransactionB.getRelEntityData(); + if (MapUtils.isNotEmpty(relEntityDataB)) { + if (MapUtils.isNotEmpty(relEntityDataA)) { + for (Map.Entry entryB : relEntityDataB.entrySet()) { + JSONObject attrEntityB = (JSONObject) entryB.getValue(); + JSONArray valueListB = attrEntityB.getJSONArray("valueList"); + if (CollectionUtils.isNotEmpty(valueListB)) { + boolean keyExists = false; + for (Map.Entry entryA : relEntityDataA.entrySet()) { + if (Objects.equals(entryA.getKey(), entryB.getKey())) { + keyExists = true; + JSONObject attrEntityA = (JSONObject) entryA.getValue(); + JSONArray valueListA = attrEntityA.getJSONArray("valueList"); + valueListA.addAll(valueListB); + } + if (keyExists) { + break; + } + } + if (!keyExists) { + relEntityDataA.put(entryB.getKey(), attrEntityB); + } + } + } + } else { + ciEntityTransactionA.setAttrEntityData(relEntityDataB); + } + } + } + } + /** * 重新构建configList配置信息 * @param originalConfigList 原始配置信息列表 -- Gitee