package com.persagy.dmp.rwd.digital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.persagy.dmp.basic.dto.RelationCalDTO;
import com.persagy.dmp.basic.dto.RelationCalRuleDTO;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.common.model.entity.BaseEntity;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.define.entity.RelationDefine;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.dmp.digital.entity.ObjectRelationProjectCal;
import com.persagy.dmp.rwd.basic.constant.BusinessErrorRwdCode;
import com.persagy.dmp.rwd.basic.constant.CalRuleTypeEnum;
import com.persagy.dmp.rwd.basic.constant.ComputationalStateEnum;
import com.persagy.dmp.rwd.define.service.IRelationDefineService;
import com.persagy.dmp.rwd.digital.dao.ObjectDigitalMapper;
import com.persagy.dmp.rwd.digital.domain.CalculatingDTO;
import com.persagy.dmp.rwd.digital.service.CalculateService;
import com.persagy.dmp.rwd.digital.service.IObjectRelationService;
import com.persagy.dmp.rwd.digital.service.RelationCalService;
import com.persagy.dmp.rwd.digital.service.RelationProjectCalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

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

@Service
@Slf4j
@RequiredArgsConstructor
public class RelationCalServiceImpl implements RelationCalService {

    private final ObjectDigitalMapper objectDigitalMapper;
    private final IObjectRelationService objectRelationService;
    private final RelationProjectCalService relationProjectCalService;
    private final IRelationDefineService relationDefineService;


    /***
     * Description: 根据id,localId,name,localName,CADID及关系类型信息,对象分类信息创建关系
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.digital.entity.ObjectRelation>>
     * @author : lijie
     * @date :2021/9/2 21:24
     * Update By lijie 2021/9/2 21:24
     */
    @Override
    @Transactional
    public CommonResult<List<ObjectRelation>> createObjRelationsByKeywordAndObjType(@RequestBody RequestData requestData) {
        // 0.更新计算时间,根据项目id和图类型编码及关系类型编码
        updateComputeTimeByProjectAndGraphCodeAndRelCode(requestData.getGraphCode(),requestData.getRelCode(),null);
        // 根据对象信息点和对象内容(主/副)查询对象,然后添加关系及计算标记
        requestData.setProjectId(AppContext.getContext().getProjectId());
        requestData.setGroupCode(AppContext.getContext().getGroupCode());
        // 1. 查询主对象及从对象
        // 1.1 查询主对象:obj_type,project_id,type(设备名称(name),设备ID(id),本地编码(localId),本地名称(localName))查询主对象
        // 1.2 查询从对象:obj_type,project_id,type(设备名称(name),设备ID(id),本地编码(localId),本地名称(localName)),排除主对象id,是否主对象的class_code查询从对象
        List<ObjectDigital> resultList =
                objectDigitalMapper.queryObjectListByIdOrNameOrCadId(requestData);
        if (CollUtil.isEmpty(resultList)){
            return ResultHelper.multi(new ArrayList<>(),0L);
        }
        // 1.3 按mainFlag对主对象分组并进行校验
        checkMasterObjValid(resultList,requestData);
        // 1.4 对从对象按mainFlag,fromContent分组
        checkSlaveObjValid(resultList,requestData);
        // 4.插入关系from_id,to_id,图类型编码,边类型编码,项目id,如果rel_value有值则加上,结束
        // 4.1 获得主对象
        ObjectDigital masterObjectDigital = resultList.stream().filter(ObjectDigital::getMainFlag).collect(Collectors.toList()).get(0);
        // 校验传感器类型
        checkSensor(masterObjectDigital,requestData);
        // 校验对象类型
        checkClassCode(masterObjectDigital,requestData);
        // 4.2 获得从对象
        List<ObjectDigital> slaveObjectDigitals = resultList.stream().filter(objectDigital -> !objectDigital.getMainFlag()).collect(Collectors.toList());
        // 4.3 根据主对象id和从对象id数组查询已经存在的关系
        Map<String, ObjectRelation> relationMap =
                queryObjectRelationMapByFromIdAndToIds(masterObjectDigital.getId(),slaveObjectDigitals
                .stream().map(BaseEntity::getId).collect(Collectors.toList()));
        // 4.3 创建关系
        List<ObjectRelation> relations = new ArrayList<>();
        //List<RelationCalSign> relationCalSigns = new ArrayList<>();
        boolean flag = StrUtil.isNotBlank(requestData.getSign()) && NumberUtil.isInteger(requestData.getSign());
        // 生成插入关系
        for (ObjectDigital slaveObjectDigital : slaveObjectDigitals) {
            ObjectRelation objectRelation = new ObjectRelation();
            objectRelation.setGraphCode(requestData.getGraphCode());
            objectRelation.setObjFrom(masterObjectDigital.getId());
            objectRelation.setObjTo(slaveObjectDigital.getId());
            objectRelation.setRelCode(requestData.getRelCode());
            if (requestData.getHasRelValue()){
                objectRelation.setRelValue(masterObjectDigital.getClassCode());
            }
            relations.add(objectRelation);
        }
        if (CollUtil.isNotEmpty(relations)){
            objectRelationService.save(relations);
        }
        // 5.处理sign:计算标记 1 为手动 2为自动
//        if (CollUtil.isNotEmpty(relationCalSigns)){
//            relationCalSignService.saveOrUpdateBatch(relationCalSigns);
//        }
        return ResultHelper.multi(relations, relations.size());
    }

    /***
     * Description: 根据fromId,toId,relType,graphCode删除关系
     * @param objectRelation : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.digital.entity.ObjectRelation>>
     * @author : lijie
     * @date :2021/9/2 20:20
     * Update By lijie 2021/9/2 20:20
     */
    @Override
    @Transactional
    public CommonResult<List<ObjectRelation>> deleteObjRelationsByRelId(ObjectRelation objectRelation) {
        // 1.先查询关系
        LambdaQueryWrapper<ObjectRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectRelation::getGraphCode,objectRelation.getGraphCode());
        queryWrapper.eq(ObjectRelation::getRelCode,objectRelation.getRelCode());
        queryWrapper.eq(ObjectRelation::getObjFrom,objectRelation.getObjFrom());
        queryWrapper.eq(ObjectRelation::getObjTo,objectRelation.getObjTo());
        List<ObjectRelation> objectRelations = objectRelationService.list(queryWrapper);
        if (CollUtil.isEmpty(objectRelations)){
            throw new BusinessException(BusinessErrorRwdCode.A7201.getCode(),BusinessErrorRwdCode.A7201.getDesc());
        }
        // 2.先更新计算时间
        updateComputeTimeByProjectAndGraphCodeAndRelCode(objectRelation.getGraphCode(),objectRelation.getRelCode(),null);
        // 3.删除关系
        objectRelationService.delete(queryWrapper);
        return ResultHelper.multi(objectRelations,objectRelations.size());
    }
    /***
     * Description: 关系计算(整体计算采用fast-fail机制处理,遇到失败立即中止,不进行计算)
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.digital.entity.ObjectRelation>>
     * @author : lijie
     * @date :2021/9/7 14:36
     * Update By lijie 2021/9/7 14:36
     */
    @Override
    @Transactional
    public CommonResult<List<ObjectRelation>> calculatingObjRelation(RequestData requestData) {
        // 拷贝参数到计算对象中
        CalculatingDTO calculatingDTO = copyRequestDataToDTO(requestData);
        // 1.应用fast-fail机制,先校验一下数据正确性,如果不正确拒绝计算
        // calculationRelationDatas(requestData);
        // 1.查询所有边类型数据
        // 因为边类型数据<200,所有数据大小在100KB以下,为了减少查询次数,所以一次查询,重复使用
        LambdaQueryWrapper<RelationDefine> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(RelationDefine::getGraphCode,requestData.getGraphCode());
        //queryWrapper.eq(RelationDefine::getCode,requestData.getRelCode());
        queryWrapper.eq(BaseEntity::getValid,true);
        List<RelationDefine> relationDefines = relationDefineService.queryListByCondition(queryWrapper);
        if (CollUtil.isEmpty(relationDefines)){
            throw new BusinessException(BusinessErrorRwdCode.A7309.getCode(),BusinessErrorRwdCode.A7309.getDesc());
        }
        // 2.获得当前关系定义并计算其前置关系,计算自己的关系
        Optional<RelationDefine> currentRelationDefineList = relationDefines.stream()
                .filter(relationDefine ->
                        relationDefine.getGraphCode().equals(calculatingDTO.getGraphCode())
                                && relationDefine.getCode().equals(calculatingDTO.getRelCode()))
                .findFirst();
        if (!currentRelationDefineList.isPresent()){
            throw new BusinessException(BusinessErrorRwdCode.A7309.getCode(),BusinessErrorRwdCode.A7309.getDesc());
        }
        RelationDefine currentRelationDefine = currentRelationDefineList.get();
        Map<String, RelationDefine> relationDefineMap = relationDefines.stream().collect(Collectors.toMap(relationDefine ->
                        relationDefine.getGraphCode() + StrUtil.UNDERLINE + relationDefine.getCode(),
                relationDefine -> relationDefine, (k1, k2) -> k1));
        calculatingDTO.setRelationDefineMap(relationDefineMap);
        if (null==currentRelationDefine.getCalRules()
                || StrUtil.isBlank(currentRelationDefine.getCalRules().getCalRuleType())){
            // 计算规则为空,则直接放弃计算
            throw new BusinessException(BusinessErrorRwdCode.A7308.getCode(), BusinessErrorRwdCode.A7308.getDesc());
        }
        // 2.1 前置关系是否存在,如果存在先处理前置关系
        handlePreRelation(currentRelationDefine,calculatingDTO);
        // 2.2 计算自身关系
        // 因为根据空间坐标计算关系需要知道关系两侧对应的对象分类,所以这里直接冗余存储一下关系定义对象
        calculatingDTO.setRelationDefine(currentRelationDefine);
        String calRuleType = currentRelationDefine.getCalRules().getCalRuleType();
        // {规则索引值:对应的算法类名称},用枚举记录计算策略实体类名称
        Map<String, String> calRuleTypeMap = CalRuleTypeEnum.getCalRuleTypeMap();
        if (!calRuleTypeMap.containsKey(calRuleType)) {
            throw new BusinessException(BusinessErrorRwdCode.A7308.getCode(), BusinessErrorRwdCode.A7308.getDesc());
        }
        // 获取策略类的bean
        CalculateService calculateService = SpringHelper
                .getBean(calRuleTypeMap.get(calRuleType), CalculateService.class);
        // 根据计算规则和参数计算关系
        calculateService.calculateRelation(currentRelationDefine.getCalRules(), calculatingDTO);
        return ResultHelper.multi(new ArrayList<>(),0L);
    }
    /***
     * Description: 拷贝请求参数到DTO中
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.rwd.digital.domain.CalculatingDTO
     * @author : lijie
     * @date :2021/9/8 14:56
     * Update By lijie 2021/9/8 14:56
     */
    private CalculatingDTO copyRequestDataToDTO(RequestData requestData) {
        return CalculatingDTO.builder()
                .graphCode(requestData.getGraphCode())
                .relCode(requestData.getRelCode())
                .mainContent(requestData.getMainContent())
                .slaveContent(requestData.getSlaveContent())
                .relValue(requestData.getRelValue())
                .calBeforeRelFlag(null==requestData.getCalBeforeRelFlag()?Boolean.FALSE:requestData.getCalBeforeRelFlag())
                .filterRelFlag(Boolean.TRUE)
                .groupCode(AppContext.getContext().getGroupCode())
                .projectId(AppContext.getContext().getProjectId())
                .appId(AppContext.getContext().getAppId())
                .accountId(AppContext.getContext().getAccountId())
                .build();
    }

    /***
     * Description: 计算前置关系
     * @param currentRelationDefine : 当前关系定义
     * @param calculatingDTO : 请求参数
     * @return : void
     * @author : lijie
     * @date :2021/9/8 11:03
     * Update By lijie 2021/9/8 11:03
     */
    private void handlePreRelation(RelationDefine currentRelationDefine, CalculatingDTO calculatingDTO) {
        if (CollUtil.isEmpty(currentRelationDefine.getBeforeCalRels())){
            return;
        }
        if (!calculatingDTO.getCalBeforeRelFlag()){
            return;
        }
        CalculatingDTO preCalculatingDTO = BeanUtil.toBean(calculatingDTO, CalculatingDTO.class);
        preCalculatingDTO.setFilterRelFlag(false);
        // 关系定义中的必须计算的前置关系
        List<RelationCalDTO> beforeCalRels = currentRelationDefine.getBeforeCalRels();
        // {图编码_关系编码:关系定义对象}
        Map<String, RelationDefine> relationDefineMap = preCalculatingDTO.getRelationDefineMap();
        // {规则索引值:对应的算法类名称},用枚举记录计算策略实体类名称
        Map<String, String> calRuleTypeMap = CalRuleTypeEnum.getCalRuleTypeMap();
        for (RelationCalDTO relationCalDTO : beforeCalRels) {
            // 图类型编码与关系编码为空时抛出异常
            if (StrUtil.isBlank(relationCalDTO.getGraphCode())
                    || StrUtil.isBlank(relationCalDTO.getRelCode())) {
                throw new BusinessException(BusinessErrorRwdCode.A7307.getCode(), BusinessErrorRwdCode.A7307.getDesc());
            }
            // 关系定义不存在抛出异常
            String tempKey = relationCalDTO.getGraphCode() + StrUtil.UNDERLINE + relationCalDTO.getRelCode();
            if (!relationDefineMap.containsKey(tempKey)) {
                throw new BusinessException(BusinessErrorRwdCode.A7309.getCode(), BusinessErrorRwdCode.A7309.getDesc());
            }
            // 计算关系策略类不存在抛出异常
            RelationDefine relationDefine = relationDefineMap.get(tempKey);
            RelationCalRuleDTO calRules = relationDefine.getCalRules();
            if (!calRuleTypeMap.containsKey(calRules.getCalRuleType())) {
                throw new BusinessException(BusinessErrorRwdCode.A7308.getCode(), BusinessErrorRwdCode.A7308.getDesc());
            }
            // 因为根据空间坐标计算关系需要知道关系两侧对应的对象分类,所以这里直接冗余存储一下关系定义对象
            preCalculatingDTO.setRelationDefine(relationDefine);
            // 获取策略类的bean
            CalculateService calculateService = SpringHelper
                    .getBean(calRuleTypeMap.get(calRules.getCalRuleType()), CalculateService.class);
            updateComputeTimeByProjectAndGraphCodeAndRelCode(relationCalDTO.getGraphCode(),relationCalDTO.getRelCode(),ComputationalStateEnum.COMPUTING.getState());
            // 根据计算规则和参数计算关系
            calculateService.calculateRelation(calRules, preCalculatingDTO);
            updateComputeTimeByProjectAndGraphCodeAndRelCode(relationCalDTO.getGraphCode(),relationCalDTO.getRelCode(),ComputationalStateEnum.INIT.getState());
        }
    }

    /***
     * Description: 根据项目id和图类型编码及关系类型编码更新计算时间
     * @param graphCode : 图类型编码
     * @param relCode : 关系类型编码
     * @return : void
     * @author : lijie
     * @date :2021/9/4 23:14
     * Update By lijie 2021/9/4 23:14
     */
    @Override
    public void updateComputeTimeByProjectAndGraphCodeAndRelCode(String graphCode,String relCode,Integer computationalState) {
        LambdaUpdateWrapper<ObjectRelationProjectCal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ObjectRelationProjectCal::getProjectId,AppContext.getContext().getProjectId());
        updateWrapper.eq(ObjectRelationProjectCal::getGraphCode,graphCode);
        updateWrapper.eq(ObjectRelationProjectCal::getRelCode,relCode);
        ObjectRelationProjectCal relationProjectCal = new ObjectRelationProjectCal();
        relationProjectCal.setComputingTime(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
        if (null!=computationalState){
            relationProjectCal.setComputationalState(computationalState);
        }
        relationProjectCalService.update(relationProjectCal,updateWrapper);
    }
    /***
     * Description: 查询当前计算状态
     * @param graphCode : 集团编码
     * @param relCode : 关系编码
     * @return : java.lang.Boolean
     * @author : lijie
     * @date :2021/12/7 12:01
     * Update By lijie 2021/12/7 12:01
     */
    @Override
    public Boolean hasComputingState(String graphCode, String relCode) {
        LambdaQueryWrapper<ObjectRelationProjectCal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectRelationProjectCal::getProjectId,AppContext.getContext().getProjectId());
        queryWrapper.eq(ObjectRelationProjectCal::getGroupCode,AppContext.getContext().getGroupCode());
        queryWrapper.eq(ObjectRelationProjectCal::getGraphCode,graphCode);
        queryWrapper.eq(ObjectRelationProjectCal::getRelCode,relCode);
        queryWrapper.eq(ObjectRelationProjectCal::getComputationalState,ComputationalStateEnum.COMPUTING.getState());
        return CollUtil.isNotEmpty(relationProjectCalService.list(queryWrapper));
    }

    /***
     * Description: 根据fromId和toIds查询关系
     * @param fromId : fromId
     * @param toIds : toIds
     * @return : java.util.Map<java.lang.String,com.persagy.dmp.digital.entity.ObjectRelation>
     *     {"图类型编码_关系类型编码_fromId_toId":关系对象}
     * @author : lijie
     * @date :2021/9/3 16:29
     * Update By lijie 2021/9/3 16:29
     */
    private Map<String, ObjectRelation> queryObjectRelationMapByFromIdAndToIds(String fromId, List<String> toIds) {
        if (StrUtil.isBlank(fromId) || CollUtil.isEmpty(toIds)){
            return new HashMap<>(16);
        }
        LambdaQueryWrapper<ObjectRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ObjectRelation::getObjTo,toIds);
        queryWrapper.eq(ObjectRelation::getObjFrom,fromId);
        queryWrapper.eq(ObjectRelation::getValid,true);
        List<ObjectRelation> objectRelations = objectRelationService.list(queryWrapper);
        if (CollUtil.isEmpty(objectRelations)){
            return new HashMap<>(16);
        }
        return objectRelations.stream()
                .collect(Collectors.toMap(objectRelation -> StrUtil.format(CommonConstant.RELATION_UNIQUE_KEY_FORMAT,
                        objectRelation.getGraphCode(),objectRelation.getRelCode(),
                        objectRelation.getObjFrom(),objectRelation.getObjTo()),
                        objectRelation -> objectRelation,(k1,k2)->k1));
    }

    /***
     * Description: 校验对象类型
     * @param masterObjectDigital : 主对象
     * @param requestData : 请求参数
     * @return : void
     * @author : lijie
     * @date :2021/9/3 15:50
     * Update By lijie 2021/9/3 15:50
     */
    private void checkClassCode(ObjectDigital masterObjectDigital, RequestData requestData) {
        if (null==requestData.getHasCheckClassCode()
                || !requestData.getHasCheckClassCode()
                || CollUtil.isEmpty(requestData.getCheckClassCodes())){
            return;
        }
        if (!requestData.getCheckClassCodes()
                .contains(masterObjectDigital.getClassCode())){
            throw new BusinessException(BusinessErrorRwdCode.A7306.getCode(), requestData.getMainContent()+BusinessErrorRwdCode.A7306.getDesc());
        }
    }

    /***
     * Description: 校验传感器类型
     * @param masterObjectDigital : 主对象
     * @param requestData : 请求参数
     * @return : void
     * @author : lijie
     * @date :2021/9/3 15:36
     * Update By lijie 2021/9/3 15:36
     */
    private void checkSensor(ObjectDigital masterObjectDigital, RequestData requestData) {
        if (null==requestData.getHasCheckSensor()
                || !requestData.getHasCheckSensor()
                || CollUtil.isEmpty(requestData.getCheckSensorCodes())){
            return;
        }
        if (!requestData.getCheckSensorCodes()
                .contains(masterObjectDigital.getClassCode())){
            throw new BusinessException(BusinessErrorRwdCode.A7305.getCode(), requestData.getMainContent()+BusinessErrorRwdCode.A7305.getDesc());
        }
    }

    /***
     * Description: 校验从对象的合法性
     * @param resultList : 结果集
     * @return : void
     * @author : lijie
     * @date :2021/9/3 14:52
     * Update By lijie 2021/9/3 14:52
     */
    private void checkSlaveObjValid(List<ObjectDigital> resultList,RequestData requestData) {
        long count = resultList.stream()
                .filter(objectDigital -> !objectDigital.getMainFlag()).count();
        if (count<=0){
            throw new BusinessException(BusinessErrorRwdCode.A7303.getCode(), requestData.getSlaveContent()+BusinessErrorRwdCode.A7303.getDesc());
        }
        if (ObjectDigital.NAME.equals(requestData.getType())){
            Map<String, List<ObjectDigital>> nameMap = resultList.stream()
                                .filter(objectDigital -> !objectDigital.getMainFlag())
                                .collect(Collectors.groupingBy(ObjectDigital::getName));
            commonCheckSlaveObjValid(nameMap);
        }
        if (ObjectDigital.PROP_ID.equals(requestData.getType())){
            Map<String, List<ObjectDigital>> idMap = resultList.stream()
                                .filter(objectDigital -> !objectDigital.getMainFlag())
                                .collect(Collectors.groupingBy(ObjectDigital::getId));
            commonCheckSlaveObjValid(idMap);
        }
        if (ObjectDigital.LOCAL_NAME.equals(requestData.getType())){
            Map<String, List<ObjectDigital>> localNameMap = resultList.stream()
                                .filter(objectDigital -> !objectDigital.getMainFlag())
                                .collect(Collectors.groupingBy(ObjectDigital::getLocalName));
            commonCheckSlaveObjValid(localNameMap);
        }
        if (ObjectDigital.LOCAL_ID.equals(requestData.getType())){
            Map<String, List<ObjectDigital>> localIdMap = resultList.stream()
                                .filter(objectDigital -> !objectDigital.getMainFlag())
                                .collect(Collectors.groupingBy(ObjectDigital::getLocalId));
            commonCheckSlaveObjValid(localIdMap);
        }
        if (RequestData.CAD_ID.equals(requestData.getType())){
            Map<String, List<ObjectDigital>> cadIdMap = resultList.stream()
                                .filter(objectDigital -> !objectDigital.getMainFlag())
                                .collect(Collectors.groupingBy(ObjectDigital::getCadId));
            commonCheckSlaveObjValid(cadIdMap);
        }
    }
    /***
     * Description: 通用校验从对象的合法性
     * @param dataMap : 映射
     * @return : void
     * @author : lijie
     * @date :2021/9/3 14:54
     * Update By lijie 2021/9/3 14:54
     */
    private void commonCheckSlaveObjValid(Map<String, List<ObjectDigital>> dataMap) {
        Set<Map.Entry<String, List<ObjectDigital>>> entries = dataMap.entrySet();
        for (Map.Entry<String, List<ObjectDigital>> entry : entries) {
            if (CollUtil.isEmpty(entry.getValue())){
                throw new BusinessException(BusinessErrorRwdCode.A7303.getCode(), entry.getKey()+BusinessErrorRwdCode.A7303.getDesc());
            }
            if (CollUtil.size(entry.getValue())>1){
                throw new BusinessException(BusinessErrorRwdCode.A7304.getCode(), entry.getKey()+BusinessErrorRwdCode.A7304.getDesc());
            }
        }
    }

    /***
     * Description: 校验主对象的合法性
     * @param resultList : 结果集
     * @return : void
     * @author : lijie
     * @date :2021/9/3 14:52
     * Update By lijie 2021/9/3 14:52
     */
    private void checkMasterObjValid(List<ObjectDigital> resultList,RequestData requestData) {
        long count = resultList.stream()
                .filter(ObjectDigital::getMainFlag).count();
        if (count<=0){
            throw new BusinessException(BusinessErrorRwdCode.A7301.getCode(), requestData.getMainContent()+BusinessErrorRwdCode.A7301.getDesc());
        }
        if (count>1){
            throw new BusinessException(BusinessErrorRwdCode.A7302.getCode(), requestData.getMainContent()+BusinessErrorRwdCode.A7302.getDesc());
        }
    }
}
