package com.persagy.proxy.calculation.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.digital.client.DigitalObjectClient;
import com.persagy.dmp.digital.client.DigitalRelationCaclClient;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.proxy.adm.constant.AdmCommonConstant;
import com.persagy.proxy.adm.constant.AdmObjectInfoConstant;
import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
import com.persagy.proxy.adm.constant.BusinessErrorRwdCode;
import com.persagy.proxy.adm.request.AdmCreateResponse;
import com.persagy.proxy.adm.request.AdmManualRelCalResponse;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.calculation.model.AdmManualRelCalRequest;
import com.persagy.proxy.calculation.model.AdmManualRelationCalcDel;
import com.persagy.proxy.calculation.model.AdmRelationAddRequest;
import com.persagy.proxy.calculation.service.AdmManualRelCalService;
import com.persagy.proxy.common.entity.InstanceUrlParam;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/***
 * Description: 手动计算逻辑处理类
 * @author : lijie
 * @date :2021/8/31 11:51
 * Update By lijie 2021/8/31 11:51
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AdmManualRelCalServiceImpl implements AdmManualRelCalService {

    private final DigitalRelationCaclClient digitalRelationCaclClient;
    private final DigitalObjectClient digitalObjectClient;



    /***
     * Description: 添加关系
     *
     * @param context
     * @param request : 请求参数
     * @return : com.persagy.proxy.adm.request.AdmCreateResponse
     * @author : lijie
     * @date :2021/8/31 12:14
     * Update By lijie 2021/8/31 12:14
     */
    @Override
    public AdmCreateResponse addRels(InstanceUrlParam context, AdmRelationAddRequest request) {
        // 获取项目id
        String projectId = AppContext.getContext().getProjectId();
        if (StrUtil.isBlank(request.getRelType())){
            return AdmCreateResponse.failure("关系类型不能为空");
        }
        if (StrUtil.isBlank(request.getType())){
            return AdmCreateResponse.failure("识别不能为空");
        }
        if (StrUtil.isBlank(request.getMainContent())){
            return AdmCreateResponse.failure("主对象不能为空","1");
        }
        if (CollUtil.isEmpty(request.getFromContent())){
            return AdmCreateResponse.failure("从对象不能为空","2");
        }
        // 2.1 pe2sp,pe2sh,eq2sh_for,eq2fl_for,eq2bd_for,sy2sp_for,sy2sh_for,sy2fl_for,sy2bd_for
        // sp2sp_ConvectionNetwork_1,sp2sp_ConvectionNetwork_2,sp2sp_ConvectionNetwork_3,sp2sp_SpaceNeighborhood_5
        // sp2sp_SpaceNeighborhood_2,sp2sp_SpaceNeighborhood_4,sp2sp_TrafficNetwork_3,sp2sp_TrafficNetwork_2,
        // sp2sp_RadiationNetwork_1,sp2sp_RadiationNetwork_2,sp2sp_SpaceNeighborhood_1,sp2sp_TrafficNetwork_1,
        // sp2sp_SpaceNeighborhood_3,eq2sy_ControlRelation_2,eq2sy_ValveRelationship_vv2sy,
        // eq2sy_SensorRelationship_ss2sy,eq2sp_SensorRelationship_ss2sp,eq2sp_x,bd2bd_ArchForArch,bd2fl_ArchForArch,
        // bd2sh_ArchForArch,bd2sp_ArchForArch,fl2bd_ArchForArch,fl2fl_ArchForArch,fl2sh_ArchForArch,fl2sp_ArchForArch,
        // sh2bd_ArchForArch,sh2fl_ArchForArch,sh2sh_ArchForArch,sh2sp_ArchForArch,sp2bd_ArchForArch,sp2fl_ArchForArch,
        // sp2sh_ArchForArch,sp2sp_ArchForArch
        // 校验关系类型是否存在
        Map<String, AdmRelationTypeEnum> relationTypeMap = AdmCommonConstant.RELATION_TYPE_MAP;
        AdmRelationTypeEnum relationTypeEnum = relationTypeMap.get(request.getRelType());
        if (relationTypeEnum == null){
            return AdmCreateResponse.failure("不存在的关系类型");
        }
        // type为cadId直接提示从对象不存在CADID编码并且errorType=2
        if (AdmObjectInfoConstant.CADID.equals(request.getType()) && relationTypeEnum.getHasCheckCadId()){
            return AdmCreateResponse.failure("从对象不存在CADID编码","2");
        }
        // relType为下面几种的从对象不能重复,主对象和从对象不能重复
        if (AdmObjectInfoConstant.CHECK_SPECIAL_TYPES.contains(request.getRelType())){
            Set<String> fromContent = request.getFromContent();
            if (fromContent.contains(request.getMainContent())){
                return AdmCreateResponse.failure(request.getMainContent()+":从对象不能重复","2");
            }
        }
        // 判断主内容重复
        CommonResult<List<ObjectNode>> mainResult = queryMainContent(request.getType(),request.getMainContent(),relationTypeEnum.getMasterObjType());
        if (!CommonConstant.QUERY_SUCCESS.equals(mainResult.getResult())){
            return AdmCreateResponse.failure(mainResult.getMessage());
        }
        List<ObjectNode> mainResultData = mainResult.getData();
        if (CollUtil.isEmpty(mainResultData)){
            return AdmCreateResponse.failure(request.getMainContent()+":不存在","1");
        }
        if (mainResultData.size()>1){
            return AdmCreateResponse.failure(request.getMainContent()+":存在多个实例","1");
        }
        // 判断从内容重复
        CommonResult<List<ObjectNode>> fromResult = queryFromContent(request.getType(),request.getFromContent(),relationTypeEnum.getSlaveObjType());
        if (!CommonConstant.QUERY_SUCCESS.equals(fromResult.getResult())){
            return AdmCreateResponse.failure(fromResult.getMessage());
        }
        List<ObjectNode> fromResultData = fromResult.getData();
        if (CollUtil.isEmpty(fromResultData)){
            return AdmCreateResponse.failure(String.join(",", request.getFromContent()) +":不存在","2");
        }
        Map<String,List<ObjectNode>> fromMap = new HashMap<>();
        for (ObjectNode node : fromResultData) {
            List<ObjectNode> subList = fromMap.getOrDefault(node
                    .get(AdmObjectInfoConstant.CADID.equals(request.getType())?AdmObjectInfoConstant.CADID_INFO:request.getType()).asText(), new ArrayList<>());
            subList.add(node);
            fromMap.put(node
                    .get(AdmObjectInfoConstant.CADID.equals(request.getType())?AdmObjectInfoConstant.CADID_INFO:request.getType()).asText(),subList);
        }
        for (String fromContent : request.getFromContent()) {
            if (!fromMap.containsKey(fromContent) || CollUtil.isEmpty(fromMap.get(fromContent))){
                return AdmCreateResponse.failure(fromContent +":不存在","2");
            }
            if (fromMap.get(fromContent).size()>1){
                return AdmCreateResponse.failure(fromContent +":存在多个实例","2");
            }

        }
        RequestData requestData = RequestData.builder()
                .graphCode(relationTypeEnum.getGraphCode())
                .relCode(relationTypeEnum.getRelCode())
                .masterObjType(relationTypeEnum.getMasterObjType())
                .slaveObjType(relationTypeEnum.getSlaveObjType())
                .type(request.getType())
                .hasRelValue(relationTypeEnum.getHasRelValue())
                .hasFilterMasterId(relationTypeEnum.getHasFilterMasterId())
                .hasFilterClassCode(relationTypeEnum.getHasFilterClassCode())
                .hasCheckSensor(relationTypeEnum.getHasCheckSensor())
                .hasCheckClassCode(relationTypeEnum.getHasCheckClassCode())
                .checkClassCodes(relationTypeEnum.getCheckClassCodes())
                .checkSensorCodes(relationTypeEnum.getCheckSensorCodes())
                .sign("1")
                .mainContent(request.getMainContent())
                .slaveContent(request.getFromContent())
                .build();
        CommonResult<List<ObjectRelation>> commonResult = digitalRelationCaclClient
                .createObjRelationsByKeywordAndObjType(context.getGroupCode(), context.getProjectId(),
                        context.getAppId(), "0", requestData);
        if (BusinessErrorRwdCode.A7301.getCode().equals(commonResult.getResult())
                || BusinessErrorRwdCode.A7302.getCode().equals(commonResult.getResult())
                || BusinessErrorRwdCode.A7305.getCode().equals(commonResult.getResult())
                || BusinessErrorRwdCode.A7306.getCode().equals(commonResult.getResult())){
            return AdmCreateResponse.failure(commonResult.getMessage(),"1");
        }
        if (BusinessErrorRwdCode.A7303.getCode().equals(commonResult.getResult())
                || BusinessErrorRwdCode.A7304.getCode().equals(commonResult.getResult())){
            return AdmCreateResponse.failure(commonResult.getMessage(),"2");
        }
        return AdmCreateResponse.success("添加成功");
        // 原数据中心逻辑
        // 2.2 特殊类型的判断
        // *******
        // sp2sp_RadiationNetwork_2,sp2sp_RadiationNetwork_1 && classCode!=LightingZone errorType=1,不是照明分区类型
        // sp2sp_ConvectionNetwork_2,sp2sp_ConvectionNetwork_3,sp2sp_ConvectionNetwork_1 && classCode!=AirConditioningZone errorType=1,不是空调分区类型
        // sp2sp_TrafficNetwork_3,sp2sp_TrafficNetwork_2,sp2sp_TrafficNetwork_1 && classCode!=GeneralZone errorType=1,不是物业分区类型
        // relType为上面几种的规定从对象不能重复,主对象和从对象不能重复
        // *******
        // eq2eq_EquipPower_1,eq2eq_EquipPower_2,eq2eq_EquipPower_3,eq2eq_EquipPower_4
        // 主对象的class_code必须等于SETDLS,SETDHS,其他的报不符合主对象条件限制错误
        // *******
        // 2.3 根据对象信息点和对象内容(主/副)查询对象,然后添加关系及计算标记
        // 1.obj_type,project_id,type(设备名称(name),设备ID(id),本地编码(localId),本地名称(localName))查询主对象
        // 2.obj_type,project_id,type(设备名称(name),设备ID(id),本地编码(localId),本地名称(localName)),是否排除主对象id,是否主对象的class_code查询从对象
        // 3.不存在/多实例,errorType为1,2(1-主实例,2-从实例),结束
        // 4.插入关系from_id,to_id,图类型编码,边类型编码,项目id,如果rel_value有值则加上,结束
        // 5.处理sign:计算标记 1 为手动 2为自动
        // pe2sp:from_id为资产id,to_id为空间id,主对象obj_type为property,从对象obj_type为space,图类型为PropertyInArch,边类型为Pe2Sp,rel_value为查询的空间对应的class_code
        // pe2sh:from_id为资产id,to_id为竖井id,主对象obj_type为property,从对象obj_type为shaft,图类型为PropertyInArch,边类型为Pe2Sh
        // eq2sh_for:from_id为设备id,to_id为竖井id,主对象obj_type为equipment,从对象obj_type为shaft,图类型为MechForArch,边类型为Eq2Sh
        // eq2fl_for:from_id为设备id,to_id为楼层id,主对象obj_type为equipment,从对象obj_type为floor,图类型为MechForArch,边类型为Eq2Fl
        // eq2bd_for:from_id为设备id,to_id为建筑id,主对象obj_type为equipment,从对象obj_type为building,图类型为MechForArch,边类型为Eq2Bd
        // sy2sp_for:from_id为系统id,to_id为空间id,主对象obj_type为system,从对象obj_type为space,图类型为MechForArch,边类型为Sy2Sp
        // sy2sh_for:from_id为系统id,to_id为竖井id,主对象obj_type为system,从对象obj_type为shaft,图类型为MechForArch,边类型为Sy2Sh
        // sy2fl_for:from_id为系统id,to_id为楼层id,主对象obj_type为system,从对象obj_type为floor,图类型为MechForArch,边类型为Sy2Fl
        // sy2bd_for:from_id为系统id,to_id为建筑id,主对象obj_type为system,从对象obj_type为building,图类型为MechForArch,边类型为Sy2Bd
        // sy2bd_for:from_id为系统id,to_id为建筑id,主对象obj_type为system,从对象obj_type为floor,图类型为MechForArch,边类型为Sy2Bd
        // *******
        // 从对象查询排除主对象id,主对象的class_code查询从对象
        // sp2sp_ConvectionNetwork_1,sp2sp_ConvectionNetwork_2,sp2sp_ConvectionNetwork_3,sp2sp_SpaceNeighborhood_5
        // sp2sp_SpaceNeighborhood_2,sp2sp_SpaceNeighborhood_4,sp2sp_TrafficNetwork_3,sp2sp_TrafficNetwork_2,
        // sp2sp_RadiationNetwork_1,sp2sp_RadiationNetwork_2,sp2sp_SpaceNeighborhood_1,sp2sp_TrafficNetwork_1,
        // sp2sp_SpaceNeighborhood_3
        // from_id为空间id,to_id为空间id,主对象obj_type为space,从对象obj_type为space,rel_value为查询的主对象空间对应的class_code
        // sp2sp_ConvectionNetwork_1:图类型为ConvectionNetwork,边类型为Natural
        // sp2sp_ConvectionNetwork_2:图类型为ConvectionNetwork,边类型为MixMech
        // sp2sp_ConvectionNetwork_3:图类型为ConvectionNetwork,边类型为OnewayMech
        // sp2sp_SpaceNeighborhood_1:图类型为SpaceNeighborhood,边类型为None
        // sp2sp_SpaceNeighborhood_2:图类型为SpaceNeighborhood,边类型为Door
        // sp2sp_SpaceNeighborhood_3:图类型为SpaceNeighborhood,边类型为Window
        // sp2sp_SpaceNeighborhood_4:图类型为SpaceNeighborhood,边类型为DoorWindow
        // sp2sp_SpaceNeighborhood_5:图类型为SpaceNeighborhood,边类型为Connect
        // sp2sp_TrafficNetwork_1:图类型为TrafficNetwork,边类型为Normal
        // sp2sp_TrafficNetwork_2:图类型为TrafficNetwork,边类型为FFOpen
        // sp2sp_TrafficNetwork_3:图类型为TrafficNetwork,边类型为FFClose
        // sp2sp_RadiationNetwork_1:图类型为RadiationNetwork,边类型为Connect
        // sp2sp_RadiationNetwork_2:图类型为RadiationNetwork,边类型为Transparent
        // *******
        // 从对象查询排除主对象id
        // eq2eq_LUDistribution_1,eq2eq_LUDistribution_2,eq2eq_HUDistribution_1,eq2eq_HUDistribution_2,
        // eq2eq_EquipPower_1,eq2eq_EquipPower_2,eq2eq_EquipPower_3,eq2eq_EquipPower_4
        // from_id为设备id,to_id为设备id,主对象obj_type为equipment,从对象obj_type为equipment,
        // eq2eq_LUDistribution_1:图类型为LUDistribution,边类型为Normal
        // eq2eq_LUDistribution_2:图类型为LUDistribution,边类型为Backup
        // eq2eq_HUDistribution_1:图类型为LUDistribution,边类型为Normal
        // eq2eq_HUDistribution_2:图类型为LUDistribution,边类型为Backup
        // eq2eq_EquipPower_1:图类型为EquipPower,边类型为EqNormal
        // eq2eq_EquipPower_2:图类型为EquipPower,边类型为EqBackup
        // eq2eq_EquipPower_3:图类型为EquipPower,边类型为SyNormal
        // eq2eq_EquipPower_4:图类型为EquipPower,边类型为SyBackup
        // ******
        // 从对象查询排除主对象id
        // eq2eq_ACAirNetwork_1,eq2eq_ACAirNetwork_2,eq2eq_ACAirNetwork_3
        // eq2eq_ChillWaterLoop_1,eq2eq_ChillWaterLoop_2,eq2eq_ChillWaterLoop_3,eq2eq_ChillWaterLoop_4
        // eq2eq_CoolingWaterLoop_1,eq2eq_CoolingWaterLoop_2,eq2eq_CoolingWaterLoop_3,eq2eq_CoolingWaterLoop_4
        // eq2eq_CondWaterNetwork_1
        // eq2eqFreshAirNetwork_1
        // eq2eq_HeatWaterLoop_1,eq2eq_HeatWaterLoop_2,eq2eq_HeatWaterLoop_3,eq2eq_HeatWaterLoop_4
        // eq2eq_VentNetwork_1,eq2eq_VentNetwork_2,eq2eq_VentNetwork_3
        // eq2eq_VRFNetwork_1
        // from_id为设备id,to_id为设备id,主对象obj_type为equipment,从对象obj_type为equipment,
        // eq2eq_ACAirNetwork_1:图类型为ACAirNetwork,边类型为Supply
        // eq2eq_ACAirNetwork_2:图类型为ACAirNetwork,边类型为Return
        // eq2eq_ACAirNetwork_3:图类型为ACAirNetwork,边类型为Discharge
        // eq2eq_ChillWaterLoop_1:图类型为ChillWaterLoop,边类型为Supply
        // eq2eq_ChillWaterLoop_2:图类型为ChillWaterLoop,边类型为Return
        // eq2eq_ChillWaterLoop_3:图类型为ChillWaterLoop,边类型为Bypass
        // eq2eq_ChillWaterLoop_4:图类型为ChillWaterLoop,边类型为Fill
        // eq2eq_CoolingWaterLoop_1:图类型为CoolingWaterLoop,边类型为Supply
        // eq2eq_CoolingWaterLoop_2:图类型为CoolingWaterLoop,边类型为Return
        // eq2eq_CoolingWaterLoop_3:图类型为CoolingWaterLoop,边类型为Bypass
        // eq2eq_CoolingWaterLoop_4:图类型为CoolingWaterLoop,边类型为Fill
        // eq2eq_CondWaterNetwork_1:图类型为CondWaterNetwork,边类型为Discharge
        // eq2eqFreshAirNetwork_1:图类型为FreshAirNetwork,边类型为Fresh
        // eq2eq_HeatWaterLoop_1:图类型为HeatWaterLoop,边类型为Supply
        // eq2eq_HeatWaterLoop_2:图类型为HeatWaterLoop,边类型为Return
        // eq2eq_HeatWaterLoop_3:图类型为HeatWaterLoop,边类型为Bypass
        // eq2eq_HeatWaterLoop_4:图类型为HeatWaterLoop,边类型为Fill
        // eq2eq_VentNetwork_1:图类型为VentNetwork,边类型为Supply
        // eq2eq_VentNetwork_2:图类型为VentNetwork,边类型为Discharge
        // eq2eq_VentNetwork_3:图类型为VentNetwork,边类型为Uncertain
        // eq2eq_VRFNetwork_1:图类型为VRFNetwork,边类型为SupplyReturn
        // ******
        // 从对象查询排除主对象id
        // eq2eq_SupplyWaterNetwork_1,eq2eq_SupplyWaterNetwork_2
        // eq2eq_DrainingWaterNetwork_1,eq2eq_DrainingWaterNetwork_2
        // eq2eq_DrinkingWaterNetwork_1,eq2eq_DrinkingWaterNetwork_2
        // eq2eq_DHWNetwork_1,eq2eq_DHWNetwork_2
        // eq2eq_GasNetwork_1,eq2eq_GasNetwork_2
        // eq2eq_FireWaterNetwork_1,eq2eq_FireWaterNetwork_2
        // eq2eq_FireVentNetwork_1
        // eq2eq_RecycleWaterNetwork_1,eq2eq_RecycleWaterNetwork_2
        // from_id为设备id,to_id为设备id,主对象obj_type为equipment,从对象obj_type为equipment,
        // eq2eq_SupplyWaterNetwork_1:图类型为SupplyWaterNetwork,边类型为Supply
        // eq2eq_SupplyWaterNetwork_2:图类型为SupplyWaterNetwork,边类型为Bypass
        // eq2eq_DrainingWaterNetwork_1:图类型为DrainingWaterNetwork,边类型为Discharge
        // eq2eq_DrainingWaterNetwork_2:图类型为DrainingWaterNetwork,边类型为Bypass
        // eq2eq_DrinkingWaterNetwork_1:图类型为DrinkingWaterNetwork,边类型为Supply
        // eq2eq_DrinkingWaterNetwork_2:图类型为DrinkingWaterNetwork,边类型为Bypass
        // eq2eq_DHWNetwork_1:图类型为DHWNetwork,边类型为Supply
        // eq2eq_DHWNetwork_2:图类型为DHWNetwork,边类型为Bypass
        // eq2eq_GasNetwork_1:图类型为GasNetwork,边类型为Supply
        // eq2eq_GasNetwork_2:图类型为GasNetwork,边类型为Bypass
        // eq2eq_FireWaterNetwork_1:图类型为FireWaterNetwork,边类型为Supply
        // eq2eq_FireWaterNetwork_2:图类型为FireWaterNetwork,边类型为Bypass
        // eq2eq_FireVentNetwork_1:图类型为FireVentNetwork,边类型为Discharge
        // eq2eq_RecycleWaterNetwork_1:图类型为RecycleWaterNetwork,边类型为Supply
        // eq2eq_RecycleWaterNetwork_2:图类型为RecycleWaterNetwork,边类型为Bypass
        // ******
        // eq2eq_SensorNetwork_1,eq2eq_ControlRelation_1,eq2eq_ValveRelationship_vv2eq,eq2eq_ControlEquipNetwork
        // 从对象查询排除主对象id
        // from_id为设备id,to_id为设备id,主对象obj_type为equipment,从对象obj_type为equipment,
        // eq2eq_SensorNetwork_1:图类型为SensorNetwork,边类型为Normal
        // eq2eq_ControlRelation_1:图类型为ControlRelation,边类型为MechCtrl
        // eq2eq_ValveRelationship_vv2eq:图类型为ValveRelationship,边类型为Vv2Eq
        // eq2eq_ControlEquipNetwork:图类型为ControlEquipNetwork,边类型为Normal
        // ******
        // eq2eq_SensorRelationship_ss2eq
        // 从对象查询排除主对象id
        // 传感器类型检查
        // eq2eq_SensorRelationship_ss2eq:图类型为SensorRelationship,边类型为Ss2Eq
        // *****
        // eq2sy_ControlRelation_2,eq2sy_ValveRelationship_vv2sy
        // eq2sy_ControlRelation_2:图类型为ControlRelation,边类型为SyCtrl
        // eq2sy_ValveRelationship_vv2sy:图类型为ValveRelationship,边类型为Vv2Sy
        // *****
        // eq2sy_SensorRelationship_ss2sy
        // 传感器类型检查
        // eq2sy_SensorRelationship_ss2sy:图类型为SensorRelationship,边类型为Ss2Sy
        // *****
        // eq2sp_SensorRelationship_ss2sp
        // 传感器类型检查
        // eq2sp_SensorRelationship_ss2sp:图类型为SensorRelationship,边类型为Ss2Sp
        // *****
        // eq2sp_x,已经不存在的类型
        // bd2bd_ArchForArch,bd2fl_ArchForArch,bd2sh_ArchForArch,bd2sp_ArchForArch,fl2bd_ArchForArch,fl2fl_ArchForArch
        // fl2sh_ArchForArch,fl2sp_ArchForArch,sh2bd_ArchForArch,sh2fl_ArchForArch,sh2sh_ArchForArch,sh2sp_ArchForArch
        // sp2bd_ArchForArch,sp2fl_ArchForArch,sp2sh_ArchForArch,sp2sp_ArchForArch

    }
    /***
     * Description: 查询从内容
     * @param type : 字段类型
     * @param fromContent : 从内容
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.persagy.dmp.digital.entity.ObjectDigital>
     * @author : lijie
     * @date :2021/11/26 15:02
     * Update By lijie 2021/11/26 15:02
     */
    private CommonResult<List<ObjectNode>> queryFromContent(String type, Set<String> fromContent,String objType) {
        QueryCriteria queryCriteria = new QueryCriteria();
        ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put(ObjectDigital.PROP_VALID,Boolean.TRUE);
        objectNode.putPOJO(AdmObjectInfoConstant.CADID.equals(type)?AdmObjectInfoConstant.CADID_INFO:type,fromContent);
        objectNode.put(ObjectDigital.OBJ_TYPE_HUM,objType);
        queryCriteria.setCriteria(objectNode);
        return DigitalObjectFacade.queryPrototype(AppContext.getContext().getGroupCode(),
                AppContext.getContext().getProjectId(),AppContext.getContext().getAppId(),
                AppContext.getContext().getAccountId(),queryCriteria);
    }

    /***
     * Description: 查询主内容
     * @param type : 字段类型
     * @param mainContent : 主内容
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.persagy.dmp.digital.entity.ObjectDigital>
     * @author : lijie
     * @date :2021/11/26 15:02
     * Update By lijie 2021/11/26 15:02
     */
    private CommonResult<List<ObjectNode>> queryMainContent(String type, String mainContent,String objType) {
        QueryCriteria queryCriteria = new QueryCriteria();
        ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put(ObjectDigital.PROP_VALID,Boolean.TRUE);
        objectNode.put(AdmObjectInfoConstant.CADID.equals(type)?AdmObjectInfoConstant.CADID_INFO:type,mainContent);
        objectNode.put(ObjectDigital.OBJ_TYPE_HUM,objType);
        queryCriteria.setCriteria(objectNode);
        return DigitalObjectFacade.queryPrototype(AppContext.getContext().getGroupCode(),
                AppContext.getContext().getProjectId(),AppContext.getContext().getAppId(),
                AppContext.getContext().getAccountId(),queryCriteria);
    }

    /***
     * Description: 删除关系
     * @param admManualRelationCalcDel : 请求参数
     * @return : com.persagy.proxy.adm.request.AdmCreateResponse
     * @author : lijie
     * @date :2021/8/31 12:10
     * Update By lijie 2021/8/31 12:10
     */
    @Override
    public AdmCreateResponse delRels(InstanceUrlParam context, AdmManualRelationCalcDel admManualRelationCalcDel) {
        ObjectRelation objectRelation = new ObjectRelation();
        AdmRelationTypeEnum relationTypeEnum = AdmCommonConstant.RELATION_TYPE_MAP.get(admManualRelationCalcDel.getRelType());
        if (relationTypeEnum == null){
            return AdmCreateResponse.failure("不存在的关系类型");
        }
        objectRelation.setGraphCode(relationTypeEnum.getGraphCode());
        objectRelation.setRelCode(relationTypeEnum.getRelCode());
        objectRelation.setObjFrom(admManualRelationCalcDel.getFromId());
        objectRelation.setObjTo(admManualRelationCalcDel.getToId());
        CommonResult<List<ObjectRelation>> commonResult = digitalRelationCaclClient.deleteObjRelationsByRelId(context.getGroupCode(),
                context.getProjectId(), context.getAppId(), "0", objectRelation);
        if (!"success".equals(commonResult.getResult())){
            return AdmCreateResponse.failure(commonResult.getMessage());
        }
        return AdmCreateResponse.success("删除关系成功");
    }

    /***
     * Description: 查询关系数据
     * @param admManualRelCalRequest : 请求参数
     * @return : com.persagy.proxy.adm.request.AdmCreateResponse
     * @author : lijie
     * @date :2021/8/31 12:10
     * Update By lijie 2021/8/31 12:10
     */
    @Override
    public AdmResponse query(InstanceUrlParam context, AdmManualRelCalRequest admManualRelCalRequest) {
        // 1.获取关系枚举类型映射
        Map<String, AdmRelationTypeEnum> relationTypeMap = AdmRelationTypeEnum.getRelationTypeMap();
        if (!relationTypeMap.containsKey(admManualRelCalRequest.getRelType())){
            return AdmManualRelCalResponse.success(new ArrayList<>());
        }
        // 2.构建查询参数
        AdmRelationTypeEnum relationTypeEnum = relationTypeMap.get(admManualRelCalRequest.getRelType());
        RequestData requestData = RequestData.builder()
                .graphCode(relationTypeEnum.getGraphCode())
                .relCode(relationTypeEnum.getRelCode())
                .relValue(admManualRelCalRequest.getCategory())
                .mainContent(admManualRelCalRequest.getVague())
                .build();
        if (null!=admManualRelCalRequest.getPageNumber()){
            requestData.setPage(Long.valueOf(admManualRelCalRequest.getPageNumber()));
        }
        if (null!=admManualRelCalRequest.getPageSize()){
            requestData.setSize(Long.valueOf(admManualRelCalRequest.getPageSize()));
        }
        if (StrUtil.isNotBlank(admManualRelCalRequest.getVagueTo())){
            requestData.setSlaveContent(CollUtil.newHashSet(admManualRelCalRequest.getVagueTo()));
        }
        // 3.查询参数
        CommonResult<List<ObjectDigital>> commonResult = digitalObjectClient
                .queryObjectListByGraphCodeAndRelCode(context.getGroupCode(), context.getProjectId(),
                        context.getAppId(), "0", requestData);
        // 4.处理参数
        List<ObjectNode> result = new ArrayList<>();
        ObjectMapper objectMapper = SpringUtil.getBean(ObjectMapper.class);
        if (ResponseCode.A00000.getDesc().equals(commonResult.getResult())
                && CollUtil.isNotEmpty(commonResult.getData())){
            List<ObjectDigital> objectDigitals = commonResult.getData();
            for (ObjectDigital objectDigital : objectDigitals) {
                ObjectNode temp = objectMapper.createObjectNode();
                createCommonResultObj(objectDigital,temp);
                ArrayNode objectInfos = objectMapper.createArrayNode();
                List<ObjectDigital> relObjs = objectDigital.getRelObjs();
                if (CollUtil.isNotEmpty(relObjs)){
                    for (ObjectDigital relObj : relObjs) {
                        ObjectNode objectNode = objectMapper.createObjectNode();
                        createCommonResultObj(relObj,objectNode);
                        objectInfos.add(objectNode);
                    }
                }
                temp.set("objectInfo",objectInfos);
                result.add(temp);
            }
        }
        return new AdmManualRelCalResponse(commonResult.getResult(),commonResult.getMessage(),commonResult.getCount(),
                admManualRelCalRequest.getPageNumber(),admManualRelCalRequest.getPageSize(),
                result,"Left");
    }
    /***
     * Description: 通用的创建结果对象
     * @param objectDigital : 物理世界对象
     * @param temp : 存储对象信息
     * @return : void
     * @author : lijie
     * @date :2021/10/30 14:57
     * Update By lijie 2021/10/30 14:57
     */
    private void createCommonResultObj(ObjectDigital objectDigital,ObjectNode temp) {
        ObjectNode infos = objectDigital.getInfos();
        if (null==infos){
            infos = SpringHelper.getBean(ObjectMapper.class).createObjectNode();
        }
        temp.set("bimId",infos.get("bimId"));
        temp.set("buildingId",infos.get("buildingId"));
        temp.set("floorId",infos.get("floorId"));
        temp.set("cadId",infos.get("cADID"));
        temp.put("id",objectDigital.getId());
        if (null!=objectDigital.getCreationTime()){
            temp.put("createTime", DateUtil.format(objectDigital.getCreationTime().getDate(),"yyyy-MM-dd HH:mm:ss"));
        }
        if (null!=objectDigital.getModifiedTime()){
            temp.put("lastUpdate", DateUtil.format(objectDigital.getModifiedTime().getDate(),"yyyy-MM-dd HH:mm:ss"));
        }
        temp.put("localId", objectDigital.getLocalId());
        temp.put("localName", objectDigital.getLocalName());
        temp.put("name", objectDigital.getName());
        temp.put("objectType", objectDigital.getObjType());
        temp.put("objectType", objectDigital.getObjType());
        temp.put("projectId", objectDigital.getProjectId());
        temp.putObject("statistics");
    }
}
