Browse Source

fix bug:系统台账-删除系统关联的建筑楼层报错的问题

lijie 3 years ago
parent
commit
daf8aa3230

+ 64 - 19
src/main/java/com/persagy/proxy/relation/controller/RelationSyInBdController.java

@@ -1,9 +1,15 @@
 package com.persagy.proxy.relation.controller;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.persagy.dmp.basic.model.QueryCriteria;
+import com.persagy.dmp.common.constant.CommonConstant;
+import com.persagy.dmp.digital.client.DigitalRelationFacade;
+import com.persagy.dmp.digital.entity.ObjectRelation;
 import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
 import com.persagy.proxy.adm.request.AdmResponse;
 import com.persagy.proxy.adm.service.IAdmRelationService;
@@ -22,6 +28,8 @@ import org.springframework.web.bind.annotation.*;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * @author zhangwencheng
@@ -103,41 +111,56 @@ public class RelationSyInBdController {
 
     /**
      * 创建系统所在建筑关系
-     * @param syInFlList
-     * @param projectId
+     * @param syInFlList: 请求参数
      * @return
      * @throws Exception
      */
     @PostMapping("/link-bd")
     public AdmResponse createBd(@RequestBody SyInBdList syInFlList) throws Exception {
-        log.info("创建系统所在建筑关系createBD方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
+        log.debug("创建系统所在建筑关系createBD方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
+        // 原来的逻辑:2021年11月2日14:59:13 lijie修改
+        // 1.删除图类型MechInArch,边类型为Sy2Bd,系统id为sysID的关系
+        // 2.如果建筑id列表不为空,则创建对应的关系进行保存
         //系统Id
         String sysID = syInFlList.getSysId();
         //楼层
-        List<String> builds = syInFlList.getBuildingIdList();
-        if (StringUtils.isBlank(sysID) || CollectionUtils.isEmpty(builds)) {
-            return AdmResponse.failure("必填项:SysId(系统id)、buildingIdList(建筑Id)");
+        List<String> buildIds = syInFlList.getBuildingIdList();
+        if (StringUtils.isBlank(sysID)) {
+            return AdmResponse.success();
+        }
+        // 1.1 根据图类型MechInArch,边类型为Sy2Bd,系统id为sysID查询有效关系列表
+        InstanceUrlParam context = AdmContextUtil.toDmpContext();
+        List<ObjectRelation> objectRelations =
+                queryObjectRelationsByGraphCodeAndRelCodeAndSystemId(AdmRelationTypeEnum.SY2BD_MECHINARCH.getGraphCode(),
+                    AdmRelationTypeEnum.SY2BD_MECHINARCH.getRelCode(), sysID);
+        if (CollUtil.isNotEmpty(objectRelations)){
+            // 1.2 收集关系的主键列表
+            List<String> relIds = objectRelations.stream()
+                    .filter(objectRelation -> StrUtil.isNotBlank(objectRelation.getId()))
+                    .map(ObjectRelation::getId).collect(Collectors.toList());
+            // 1.3 删除关系
+            DigitalRelationFacade.delete(context.getGroupCode(),context.getProjectId(),
+                    context.getAppId(),context.getUserId(),relIds);
+        }
+        if (CollUtil.isEmpty(buildIds)){
+            return AdmResponse.success(new ArrayList<>());
         }
         List<RelationDTO> voList = new ArrayList<>();
-        AdmRelationTypeEnum sy2bdMechinarch = AdmRelationTypeEnum.SY2BD_MECHINARCH;
-        String graphCode = sy2bdMechinarch.getGraphCode();
-        String relCode = sy2bdMechinarch.getRelCode();
-        for (String buildId : builds) {
-            RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysID, buildId);
+        for (String buildId : buildIds) {
+            RelationDTO relation = new RelationDTO(StrUtil
+                    .format(CommonConstant.DEFAULT_GRAPH_ID_FORMAT,AdmRelationTypeEnum.SY2BD_MECHINARCH.getGraphCode()),
+                    AdmRelationTypeEnum.SY2BD_MECHINARCH.getGraphCode(),
+                    AdmRelationTypeEnum.SY2BD_MECHINARCH.getRelCode(), null, sysID, buildId);
             voList.add(relation);
         }
-        // 组装上下文条件
-        InstanceUrlParam context = AdmContextUtil.toDmpContext();
         service.doSave(context, voList);
-        log.info("创建系统所在建筑关系createBD方法结束");
+        log.debug("创建系统所在建筑关系createBD方法结束");
         return AdmResponse.success();
     }
 
-
     /**
      * 创建系统所在建筑关系 同上
-     * @param syInFlList
-     * @param projectId
+     * @param syInFlList:请求参数
      * @return
      * @throws Exception
      */
@@ -166,7 +189,6 @@ public class RelationSyInBdController {
         return AdmResponse.success();
     }
 
-
     /**
      * 创建所在建筑的系统关系
      * @param bdInSyList
@@ -238,5 +260,28 @@ public class RelationSyInBdController {
         return AdmResponse.success();
     }
 
-
+    /***
+     * Description: 根据图类型,边类型,系统id查询有效关系
+     * @param graphCode : 图类型
+     * @param relCode : 边类型
+     * @param systemId : 系统id
+     * @return : void
+     * @author : lijie
+     * @date :2021/11/2 15:07
+     * Update By lijie 2021/11/2 15:07
+     */
+    private List<ObjectRelation> queryObjectRelationsByGraphCodeAndRelCodeAndSystemId(String graphCode, String relCode, String systemId) {
+        InstanceUrlParam context = AdmContextUtil.toDmpContext();
+        QueryCriteria queryCriteria = new QueryCriteria();
+        ObjectMapper mapper = SpringUtil.getBean(ObjectMapper.class);
+        ObjectNode criteria = mapper.createObjectNode();
+        criteria.put("graphCode",graphCode);
+        criteria.put("relCode",relCode);
+        criteria.put("objFrom",systemId);
+        criteria.put("valid",Boolean.TRUE);
+        queryCriteria.setCriteria(criteria);
+        // 根据systemId,图类型,边类型查询有效的数据
+        return DigitalRelationFacade.query(context.getGroupCode(), context.getProjectId(),
+                context.getAppId(), context.getUserId(), queryCriteria);
+    }
 }

+ 45 - 34
src/main/java/com/persagy/proxy/relation/controller/RelationSyInFlController.java

@@ -2,11 +2,16 @@ package com.persagy.proxy.relation.controller;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.excel.util.CollectionUtils;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.persagy.dmp.basic.model.QueryCriteria;
+import com.persagy.dmp.common.constant.ResponseCode;
+import com.persagy.dmp.common.model.response.CommonResult;
+import com.persagy.dmp.digital.client.DigitalRelationFacade;
 import com.persagy.dmp.digital.entity.ObjectRelation;
 import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
 import com.persagy.proxy.adm.request.AdmResponse;
@@ -87,53 +92,59 @@ public class RelationSyInFlController {
         String sysId = syInFlList.getSysId();
         //楼层Id
         List<String> floorIdList = syInFlList.getFloorIdList();
-        if (StringUtils.isBlank(sysId) || CollectionUtils.isEmpty(floorIdList)) {
+        if (StringUtils.isBlank(sysId)) {
             //return AdmResponse.failure("必填项:sysId(系统id)、floorIdList(楼层Id)");
             //兼容adm历史逻辑,若楼层id为空时,返回success
             return AdmResponse.success();
         }
-
+        // 按原有逻辑接口处理:2021年11月2日16:52:13 by lijie
+        // 1.根据系统id,图类型编码为MechInArch,边类型为Sy2Fl删除关系
+        // 1.1 组装删除参数
+        QueryCriteria deleteQueryCriteria = new QueryCriteria();
+        ObjectMapper mapper = SpringUtil.getBean(ObjectMapper.class);
+        ObjectNode criteria = mapper.createObjectNode();
+        criteria.put("graphCode",AdmRelationTypeEnum.SY2FL.getGraphCode());
+        criteria.put("relCode",AdmRelationTypeEnum.SY2FL.getRelCode());
+        criteria.put("objFrom",sysId);
+        criteria.put("valid",Boolean.TRUE);
+        deleteQueryCriteria.setCriteria(criteria);
+        // 1.2 根据参数删除关系
+        InstanceUrlParam context = AdmContextUtil.toDmpContext();
+        CommonResult<List<ObjectRelation>> commonResult = DigitalRelationFacade.deleteByCriteria(context.getGroupCode(),
+                context.getProjectId(), context.getAppId(), context.getUserId(), deleteQueryCriteria);
+        if (!ResponseCode.A00000.getDesc().equals(commonResult.getResult())){
+            return AdmResponse.failure(commonResult.getMessage());
+        }
+        if (CollUtil.isEmpty(floorIdList)){
+            return AdmResponse.success(new ArrayList<>());
+        }
+        // 2.创建系统所属楼层关系
         List<RelationDTO> voList = new ArrayList<>();
         AdmRelationTypeEnum sy2fl = AdmRelationTypeEnum.SY2FL;
         String graphCode = sy2fl.getGraphCode();
         String relCode = sy2fl.getRelCode();
-
-        //需要解绑关系
-        QueryCriteria queryCriteria = new QueryCriteria();
-        ObjectNode criteriaDel = JsonNodeFactory.instance.objectNode();
-        criteriaDel.put("graphCode", graphCode);
-        criteriaDel.put("relCode", relCode);
-        criteriaDel.put("objFrom", sysId);
-        queryCriteria.setCriteria(criteriaDel);
-        service.doDelete(AdmContextUtil.toDmpContext(), queryCriteria);
-
-        criteriaDel.put("relCode", "Sy2Bd");
-        queryCriteria.setCriteria(criteriaDel);
-        service.doDelete(AdmContextUtil.toDmpContext(), queryCriteria);
-
         for (String floorId : floorIdList) {
             RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysId, floorId);
             voList.add(relation);
         }
-        //查询 floor所属的建筑 增加关系
-        QueryCriteria criteriaQuery = new QueryCriteria();
-        ObjectNode criteria = JsonNodeFactory.instance.objectNode();
-        criteria.put(ObjectRelation.REL_CODE_HUM, "Bd2Fl");
-        criteria.put(ObjectRelation.GRAPH_CODE_HUM, "ArchSubset");
-        ArrayNode arrayNode = criteria.putObject("objTo").putArray("$in");
-        floorIdList.stream().forEach(floorId -> arrayNode.add(floorId));
-        criteriaQuery.setCriteria(criteria);
-        List<ObjectRelation> relationList = service.queryByCondition(AdmContextUtil.toDmpContext(), criteriaQuery);
-        if(CollUtil.isNotEmpty(relationList)){
-            Map<String,ObjectRelation> objectRelationMap = CollUtil.fieldValueMap(relationList, "objFrom");
-            for (String buildingId : objectRelationMap.keySet()) {
-                RelationDTO relation = new RelationDTO(null, graphCode, "Sy2Bd", null, sysId, buildingId);
-                voList.add(relation);
-            }
-        }
+        //查询 floor所属的建筑 增加关系:页面交互的/rel/sy-in-bd/link-bd接口会创建系统所属建筑的关系,该处省略.防止重复创建
+//        QueryCriteria criteriaQuery = new QueryCriteria();
+//        ObjectNode buildCriteria = JsonNodeFactory.instance.objectNode();
+//        buildCriteria.put(ObjectRelation.REL_CODE_HUM, "Bd2Fl");
+//        buildCriteria.put(ObjectRelation.GRAPH_CODE_HUM, "ArchSubset");
+//        ArrayNode arrayNode = buildCriteria.putObject("objTo").putArray("$in");
+//        floorIdList.stream().forEach(floorId -> arrayNode.add(floorId));
+//        criteriaQuery.setCriteria(buildCriteria);
+//        List<ObjectRelation> relationList = service.queryByCondition(AdmContextUtil.toDmpContext(), criteriaQuery);
+//        if(CollUtil.isNotEmpty(relationList)){
+//            Map<String,ObjectRelation> objectRelationMap = CollUtil.fieldValueMap(relationList, "objFrom");
+//            for (String buildingId : objectRelationMap.keySet()) {
+//                RelationDTO relation = new RelationDTO(null, graphCode, "Sy2Bd", null, sysId, buildingId);
+//                voList.add(relation);
+//            }
+//        }
         // 组装上下文条件
-        InstanceUrlParam context = AdmContextUtil.toDmpContext();
-        service.doSave(context, voList);
+        service.doSave(AdmContextUtil.toDmpContext(), voList);
         log.info("创建系统所在楼层关系link-fl方法结束");
         return AdmResponse.success();
     }