package com.persagy.proxy.adm.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException; 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.model.entity.BaseEntity; import com.persagy.dmp.define.entity.ObjectTypeDefine; import com.persagy.dmp.digital.client.DigitalRelationFacade; import com.persagy.dmp.digital.entity.ObjectRelation; import com.persagy.proxy.adm.constant.AdmCommonConstant; import com.persagy.proxy.adm.model.AdmEquipment; import com.persagy.proxy.adm.model.AdmProperty; import com.persagy.proxy.adm.service.IAdmComAndEquipService; import com.persagy.proxy.adm.service.IAdmEquipmentService; import com.persagy.proxy.adm.utils.AdmEntityTransferUtil; import com.persagy.proxy.adm.utils.ObjectNameUtil; import com.persagy.proxy.common.client.DmpRwdClient; import com.persagy.proxy.common.entity.DmpResult; import com.persagy.proxy.common.entity.InstanceUrlParam; import com.persagy.proxy.common.entity.ObjRelationDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; /** * ADM 设备 实现 * @author lvxy * @date 2021/8/18 */ @Slf4j @Service public class AdmEquipmentServiceImpl extends AbstractAdmBaseServiceImpl implements IAdmEquipmentService, IAdmComAndEquipService { @Autowired private DmpRwdClient rwdClient; @Autowired private ObjectMapper objectMapper; @Override public List doInsertEuip(InstanceUrlParam context, Class clazz, List voList) { if(CollUtil.isEmpty(voList)) { return null; } Set classCodes = voList.stream().map(AdmEquipment::getClassCode).collect(Collectors.toSet()); //查询定义类型 QueryCriteria criteriaObject = getDmpCriteria(context, classCodes, "equipment"); JSONObject paraPe = null; try { String paraStr = objectMapper.writeValueAsString(criteriaObject); paraPe = JSONObject.parseObject(paraStr); } catch (JsonProcessingException e) { throw new RuntimeException("转换查询条件出错:" + e.getMessage()); } DmpResult resultDef = rwdClient.queryDefClass(context, paraPe); Map finalDefClassMap = getDefineMap(resultDef); // 设备名字处理 voList.forEach(equip -> { if (StrUtil.isNotEmpty(equip.getName())) { if(CollUtil.isNotEmpty(finalDefClassMap) && finalDefClassMap.containsKey(equip.getClassCode())){ ObjectTypeDefine defclass = finalDefClassMap.get(equip.getClassCode()); equip.setName(ObjectNameUtil.objectName(defclass.getName()+"-")); }else{ equip.setName(ObjectNameUtil.objectName("设备-")); } } }); return doInsert(context, clazz, voList); } @Override public List doUpdateEuip(InstanceUrlParam context, Class clazz, List voList) { //TODO 处理关系标记 /*try { *//** 处理关系标记 *//* val listtype = ArrayList() listtype.add("pe2bd") listtype.add("pe2fl") listtype.add("pe2sh") listtype.add("pe2sp") for (type in listtype){ val entity = relationTypeProjectservice.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.eq("relationType", type)).entity() if (entity!=null){ entity.computationalState = 2 relationTypeProjectservice.update(entity) } } } catch (e: Exception) { e.printStackTrace() }*/ Map voMap = CollUtil.fieldValueMap(voList, BaseEntity.PROP_ID); //查询设备对应的资产 关系 Pe2Eq (1:N,一个资产关联多个设备,设备是最小单位 by:lvxianyun) DmpResult> relationRes = queryRelation(context, voMap); boolean isDealDataSucc = false; //不是绑定关系,清理绑定关系,重新绑定 if(CollUtil.isNotEmpty(relationRes.getData())){ isDealDataSucc = updateRelation2Pe(context, relationRes, voMap); }else { //不存在关系,需要新绑定关系,更新per,插入关系 isDealDataSucc = insertRelation2Pe(context, voList, voMap); } return isDealDataSucc ? doUpdate(context, clazz, voList) : null; } @Override public void doDeleteEuip(InstanceUrlParam context, List voList) { Map voMap = CollUtil.fieldValueMap(voList, BaseEntity.PROP_ID); DmpResult> relationRes = queryRelation(context, voMap); //清理绑定关系 if(CollUtil.isNotEmpty(relationRes.getData())){ updateRelation2Pe(context, relationRes, voMap); } doDelete(context,voList); } @Override public List doInsertExt(InstanceUrlParam context, Class clazz, List voList) { //创建设备 List vos = doInsert(context, AdmEquipment.class, voList); //创建资产 //创建设备和系统关系 systemList return null; } private DmpResult> queryRelation(InstanceUrlParam context, Map voMap){ QueryCriteria dmpRequest = new QueryCriteria(); ObjectNode criteria = JsonNodeFactory.instance.objectNode(); ArrayNode array = criteria.putObject("objTo").putArray("$in"); voMap.keySet().forEach(equipId -> array.add(equipId)); criteria.put("relCode", "Pe2Eq"); criteria.put("graphCode", "Property2Mech"); dmpRequest.setCriteria(criteria); dmpRequest.setSize(voMap.size() * 1L); dmpRequest.setPage(1L); dmpRequest.setWithoutCount(true); // 转换参数 JSONObject paraRel = null; try { String paraStr = objectMapper.writeValueAsString(dmpRequest); paraRel = JSONObject.parseObject(paraStr); } catch (JsonProcessingException e) { log.error(e.getMessage(), e); return null; } return rwdClient.queryRelation(context, dmpRequest); } private DmpResult queryObjectById(InstanceUrlParam context, Map peMap){ QueryCriteria criteriaObject = new QueryCriteria(); ObjectNode criteriaPe = JsonNodeFactory.instance.objectNode(); ArrayNode array = criteriaPe.putObject("id").putArray("$in"); peMap.keySet().forEach(equipId -> array.add(equipId)); criteriaPe.put("classCode", "property"); criteriaPe.put("objType", "property"); criteriaObject.setCriteria(criteriaPe); criteriaObject.setSize(peMap.size() * 1L); criteriaObject.setPage(1L); criteriaObject.setWithoutCount(true); // 转换参数 JSONObject paraPe = null; try { String paraStr = objectMapper.writeValueAsString(criteriaObject); paraPe = JSONObject.parseObject(paraStr); } catch (JsonProcessingException e) { log.error(e.getMessage(), e); return null; } return rwdClient.queryObject(context, paraPe); } private AdmProperty createPropertyObj(String id, String equipId, Boolean isBimIdNull){ AdmProperty admProperty = new AdmProperty(); admProperty.setId(id); admProperty.setEquipId(equipId); if(isBimIdNull){ admProperty.setBimId(null); } return admProperty; } private ObjectNode creatRelationObj(String objTo,String objFrom){ ObjectNode relationNew = JsonNodeFactory.instance.objectNode(); relationNew.put("relCode", "Pe2Eq"); relationNew.put("graphCode", "Property2Mech"); relationNew.put("graphCode", "Property2Mech001"); relationNew.put("objTo", objTo); relationNew.put("objFrom", objFrom); return relationNew; } /** * 更新设备关联的资产(资产中的equipId 信息点) 以及设备资产关系 * @return */ private Boolean updateRelation2Pe(InstanceUrlParam context, DmpResult> relationRes, Map voMap){ boolean isDealDataSucc = false; List updatePropertyList = new ArrayList(); List del_relationIds = new ArrayList<>(relationRes.getData().size()); Map relationMap = CollUtil.fieldValueMap(relationRes.getData(), "objTo"); relationMap.keySet().forEach(euipId -> { //存在绑定的资产 if(voMap.keySet().contains(euipId)){ AdmEquipment admEquipment = voMap.get(euipId); ObjRelationDTO relation = relationMap.get(euipId); String propertyId = relation.getObjFrom(); if(StrUtil.isNotEmpty(admEquipment.getPropertyId()) && StrUtil.isNotEmpty(propertyId) && !admEquipment.getPropertyId().equals(propertyId)){ //1 与设备提供的资产绑定 AdmProperty propertyFrom = createPropertyObj(admEquipment.getPropertyId(), euipId, false); propertyFrom.setId(admEquipment.getPropertyId()); propertyFrom.setEquipId(euipId); updatePropertyList.add(propertyFrom); //2 从关系中查询出的资产与当前设备解绑 //更新资产中的信息点 AdmProperty property = createPropertyObj(propertyId, null, true); updatePropertyList.add(property); //更新关系 del_relationIds.add(relation.getId()); } } }); DigitalRelationFacade.delete(context.getGroupCode(),context.getProjectId(), AdmCommonConstant.APP_ID,null,del_relationIds); DmpResult propertyResult = rwdClient.updateObject(context, JSONArray.parseArray(updatePropertyList.toString())); if(propertyResult.getResult().equals(DmpResult.SUCCESS)){ isDealDataSucc = true; } return isDealDataSucc; } /** * 不存在关系,需要新绑定关系,更新per,插入关系 * @param context * @param voList * @param voMap * @return */ private Boolean insertRelation2Pe(InstanceUrlParam context, List voList, Map voMap){ boolean isDealDataSucc = false; Map peMap = CollUtil.fieldValueMap(voList, "propertyId"); //查询资产 DmpResult resultPe = queryObjectById(context, peMap); if (CollUtil.isNotEmpty(resultPe.getData())) { //更新资产 创建资产设备关系 List propertyList = AdmEntityTransferUtil.toAdmMultiEntity(resultPe.getData(), null, AdmProperty.class); List objRelationDTONews = new ArrayList<>(); propertyList.forEach(property -> { //存在绑定的资产 更新 AdmEquipment admEquipment = peMap.get(property.getId()); property.setEquipId(admEquipment.getId()); //创建新的设备资产关系 ObjectNode relationNew = creatRelationObj( admEquipment.getId(), property.getId()); objRelationDTONews.add(relationNew); }); List relationList = DigitalRelationFacade.create(context.getGroupCode(),context.getProjectId(), AdmCommonConstant.APP_ID,null,objRelationDTONews); if(CollUtil.isNotEmpty(relationList)){ DmpResult propertyRes = rwdClient.updateObject(context, JSONArray.parseArray(propertyList.toString())); if(propertyRes.getResult().equals(DmpResult.SUCCESS)){ isDealDataSucc = true; } } } return isDealDataSucc; } }