Browse Source

fix bug:/rel/sh-in-bd/link-bd接口新建关系未删除已存在但本次去掉的关系数据.

lijie 3 years ago
parent
commit
52f21e0d61

+ 27 - 5
src/main/java/com/persagy/proxy/relation/controller/RelationShInBdController.java

@@ -1,6 +1,7 @@
 package com.persagy.proxy.relation.controller;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.map.MapUtil;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.persagy.dmp.basic.model.QueryCriteria;
@@ -23,9 +24,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -61,7 +60,7 @@ public class RelationShInBdController {
         QueryCriteria criteria = new QueryCriteria();
         ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
         ObjectNode queryObj = objectMapper.createObjectNode();
-        queryObj.set("objFrom",objectMapper.readTree(objectMapper.writeValueAsString(buildingIdList)));
+        //queryObj.set("objFrom",objectMapper.readTree(objectMapper.writeValueAsString(buildingIdList)));
         queryObj.put("objTo",shaftId);
         queryObj.put("graphCode",graphCode);
         queryObj.put("relCode",relCode);
@@ -70,7 +69,7 @@ public class RelationShInBdController {
         List<ObjectRelation> relations = service.queryByCondition(AdmContextUtil.toDmpContext(), criteria);
         Set<String> havedBuildIds = CollUtil.newHashSet(CollUtil.isNotEmpty(relations),
                 relations.stream().map(ObjectRelation::getObjFrom).collect(Collectors.toSet()));
-        // 组装关系
+        // 3.新增新的关系
         for (String buildingId : buildingIdList) {
             if (havedBuildIds.contains(buildingId)){
                 continue;
@@ -80,6 +79,29 @@ public class RelationShInBdController {
             voList.add(relation);
         }
         service.doSave(AdmContextUtil.toDmpContext(), voList);
+        // 4.删除旧的关系
+        Set<String> deleteRelationIds = new HashSet<>();
+        if (CollUtil.isNotEmpty(relations)){
+            Map<String, List<ObjectRelation>> buildMap = relations.stream()
+                    .collect(Collectors.groupingBy(ObjectRelation::getObjFrom));
+            Set<Map.Entry<String, List<ObjectRelation>>> entries = buildMap.entrySet();
+            for (Map.Entry<String, List<ObjectRelation>> entry : entries) {
+                if (buildingIdList.contains(entry.getKey())
+                        || CollUtil.isEmpty(entry.getValue())){
+                    continue;
+                }
+                // 需删除的关系
+                deleteRelationIds
+                        .addAll(entry.getValue().stream().map(ObjectRelation::getId).collect(Collectors.toSet()));
+            }
+        }
+        if (CollUtil.isNotEmpty(deleteRelationIds)){
+            QueryCriteria deleteParam = new QueryCriteria();
+            ObjectNode deleteObj = objectMapper.createObjectNode();
+            deleteObj.set("id",objectMapper.readTree(objectMapper.writeValueAsString(deleteRelationIds)));
+            deleteParam.setCriteria(deleteObj);
+            service.doDelete(AdmContextUtil.toDmpContext(),deleteParam);
+        }
         return AdmResponse.success();
     }