|
@@ -0,0 +1,325 @@
|
|
|
+package com.persagy.dmp.rwd.digital.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+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.model.entity.BaseEntity;
|
|
|
+import com.persagy.dmp.common.model.response.CommonResult;
|
|
|
+import com.persagy.dmp.common.utils.ResultHelper;
|
|
|
+import com.persagy.dmp.digital.entity.ObjectDigital;
|
|
|
+import com.persagy.dmp.digital.entity.ObjectRelation;
|
|
|
+import com.persagy.dmp.rwd.basic.constant.BusinessErrorRwdCode;
|
|
|
+import com.persagy.dmp.rwd.digital.dao.ObjectDigitalMapper;
|
|
|
+import com.persagy.dmp.rwd.digital.dao.ObjectRelationMapper;
|
|
|
+import com.persagy.dmp.rwd.digital.domain.RelationCalSign;
|
|
|
+import com.persagy.dmp.rwd.digital.service.IObjectRelationService;
|
|
|
+import com.persagy.dmp.rwd.digital.service.RelationCaclService;
|
|
|
+import com.persagy.dmp.rwd.digital.service.RelationCalSignService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.hibernate.validator.constraints.Length;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class RelationCaclServiceImpl implements RelationCaclService {
|
|
|
+
|
|
|
+ private final ObjectDigitalMapper objectDigitalMapper;
|
|
|
+ private final IObjectRelationService objectRelationService;
|
|
|
+ private final RelationCalSignService relationCalSignService;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 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
|
|
|
+ public CommonResult<List<ObjectRelation>> createObjRelationsByKeywordAndObjType(@RequestBody RequestData requestData) {
|
|
|
+
|
|
|
+ requestData.setProjectId(AppContext.getContext().getProjectId());
|
|
|
+ requestData.setGroupCode(AppContext.getContext().getGroupCode());
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ List<ObjectDigital> resultList =
|
|
|
+ objectDigitalMapper.queryObjectListByIdOrNameOrCadId(requestData);
|
|
|
+ if (CollUtil.isEmpty(resultList)){
|
|
|
+ return ResultHelper.multi(new ArrayList<>(),0L);
|
|
|
+ }
|
|
|
+
|
|
|
+ checkMasterObjValid(resultList,requestData);
|
|
|
+
|
|
|
+ checkSlaveObjValid(resultList,requestData);
|
|
|
+
|
|
|
+
|
|
|
+ ObjectDigital masterObjectDigital = resultList
|
|
|
+ .stream()
|
|
|
+ .filter(ObjectDigital::getMainFlag)
|
|
|
+ .collect(Collectors.toList())
|
|
|
+ .get(0);
|
|
|
+
|
|
|
+ checkSensor(masterObjectDigital,requestData);
|
|
|
+
|
|
|
+ checkClassCode(masterObjectDigital,requestData);
|
|
|
+
|
|
|
+ List<ObjectDigital> slaveObjectDigitals = resultList
|
|
|
+ .stream()
|
|
|
+ .filter(objectDigital -> !objectDigital.getMainFlag())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ Map<String,ObjectRelation> relationMap =
|
|
|
+ queryObjectRelationMapByFromIdAndToIds(masterObjectDigital.getId(),slaveObjectDigitals
|
|
|
+ .stream().map(BaseEntity::getId).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ List<ObjectRelation> addRelations = new ArrayList<>();
|
|
|
+ List<ObjectRelation> updateRelations = new ArrayList<>();
|
|
|
+ List<RelationCalSign> relationCalSigns = new ArrayList<>();
|
|
|
+ boolean flag = StrUtil.isNotBlank(requestData.getSign()) && NumberUtil.isInteger(requestData.getSign());
|
|
|
+ for (ObjectDigital slaveObjectDigital : slaveObjectDigitals) {
|
|
|
+
|
|
|
+ String uniqueKey = StrUtil
|
|
|
+ .format(CommonConstant.RELATION_UNIQUE_KEY_FORMAT,
|
|
|
+ requestData.getGraphCode(),
|
|
|
+ requestData.getRelCode(),
|
|
|
+ masterObjectDigital.getId(),
|
|
|
+ slaveObjectDigital.getId());
|
|
|
+ if (relationMap.containsKey(uniqueKey)){
|
|
|
+ ObjectRelation objectRelation = relationMap.get(uniqueKey);
|
|
|
+ objectRelation.setUpdateApp(AppContext.getContext().getAppId());
|
|
|
+ objectRelation.setModifier(AppContext.getContext().getAccountId());
|
|
|
+ if (requestData.getHasRelValue()){
|
|
|
+ objectRelation.setRelValue(masterObjectDigital.getClassCode());
|
|
|
+ }
|
|
|
+ updateRelations.add(objectRelation);
|
|
|
+ if (flag){
|
|
|
+ relationCalSigns.add(RelationCalSign
|
|
|
+ .builder()
|
|
|
+ .id(objectRelation.getId())
|
|
|
+ .sign(NumberUtil.parseInt(requestData.getSign()))
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectRelation objectRelation = new ObjectRelation();
|
|
|
+ objectRelation.setId(IdWorker.getIdStr());
|
|
|
+ objectRelation.setGraphCode(requestData.getGraphCode());
|
|
|
+ objectRelation.setGroupCode(AppContext.getContext().getGroupCode());
|
|
|
+ objectRelation.setProjectId(AppContext.getContext().getProjectId());
|
|
|
+ objectRelation.setCreateApp(AppContext.getContext().getAppId());
|
|
|
+ objectRelation.setUpdateApp(AppContext.getContext().getAppId());
|
|
|
+ objectRelation.setGraphId(getDefaultGraphIdByGraphCode(requestData.getGraphCode()));
|
|
|
+ objectRelation.setObjFrom(masterObjectDigital.getId());
|
|
|
+ objectRelation.setObjTo(slaveObjectDigital.getId());
|
|
|
+ objectRelation.setRelCode(requestData.getRelCode());
|
|
|
+ if (requestData.getHasRelValue()){
|
|
|
+ objectRelation.setRelValue(masterObjectDigital.getClassCode());
|
|
|
+ }
|
|
|
+ objectRelation.setCreator(AppContext.getContext().getAccountId());
|
|
|
+ objectRelation.setModifier(AppContext.getContext().getAccountId());
|
|
|
+ addRelations.add(objectRelation);
|
|
|
+ if (flag){
|
|
|
+ relationCalSigns.add(RelationCalSign
|
|
|
+ .builder()
|
|
|
+ .id(objectRelation.getId())
|
|
|
+ .sign(NumberUtil.parseInt(requestData.getSign()))
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (CollUtil.isNotEmpty(updateRelations)){
|
|
|
+ objectRelationService.updateBatchById(updateRelations);
|
|
|
+ }
|
|
|
+ if (CollUtil.isNotEmpty(addRelations)){
|
|
|
+ objectRelationService.saveBatch(addRelations);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollUtil.isNotEmpty(relationCalSigns)){
|
|
|
+ relationCalSignService.saveOrUpdateBatch(relationCalSigns);
|
|
|
+ }
|
|
|
+ addRelations.addAll(updateRelations);
|
|
|
+ return ResultHelper.multi(addRelations,addRelations.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ * 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<>();
|
|
|
+ }
|
|
|
+ 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<>();
|
|
|
+ }
|
|
|
+ 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: 获取默认的图实例id
|
|
|
+ * @param graphCode : 图类型编码
|
|
|
+ * @return : java.lang.String
|
|
|
+ * @author : lijie
|
|
|
+ * @date :2021/9/3 16:07
|
|
|
+ * Update By lijie 2021/9/3 16:07
|
|
|
+ */
|
|
|
+ private String getDefaultGraphIdByGraphCode(String graphCode) {
|
|
|
+ return StrUtil.format(CommonConstant.DEFAULT_GRAPH_ID_FORMAT,graphCode);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 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.getFromContent()+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.getFromContent()+BusinessErrorRwdCode.A7301.getDesc());
|
|
|
+ }
|
|
|
+ if (count>1){
|
|
|
+ throw new BusinessException(BusinessErrorRwdCode.A7302.getCode(), requestData.getFromContent()+BusinessErrorRwdCode.A7302.getDesc());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|