123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- 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<AdmEquipment> implements IAdmEquipmentService, IAdmComAndEquipService {
- @Autowired
- private DmpRwdClient rwdClient;
- @Autowired
- private ObjectMapper objectMapper;
- @Override
- public List<AdmEquipment> doInsertEuip(InstanceUrlParam context, Class<AdmEquipment> clazz, List<AdmEquipment> voList) {
- if(CollUtil.isEmpty(voList)) {
- return null;
- }
- Set<String> 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<JSONArray> resultDef = rwdClient.queryDefClass(context, paraPe);
- Map<String, ObjectTypeDefine> 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<AdmEquipment> doUpdateEuip(InstanceUrlParam context, Class<AdmEquipment> clazz, List<AdmEquipment> voList) {
- //TODO 处理关系标记
- /*try {
- *//** 处理关系标记 *//*
- val listtype = ArrayList<String>()
- 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<String, AdmEquipment> voMap = CollUtil.fieldValueMap(voList, BaseEntity.PROP_ID);
- //查询设备对应的资产 关系 Pe2Eq (1:N,一个资产关联多个设备,设备是最小单位 by:lvxianyun)
- DmpResult<List<ObjRelationDTO>> 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<AdmEquipment> voList) {
- Map<String, AdmEquipment> voMap = CollUtil.fieldValueMap(voList, BaseEntity.PROP_ID);
- DmpResult<List<ObjRelationDTO>> relationRes = queryRelation(context, voMap);
- //清理绑定关系
- if(CollUtil.isNotEmpty(relationRes.getData())){
- updateRelation2Pe(context, relationRes, voMap);
- }
- doDelete(context,voList);
- }
- @Override
- public List<AdmEquipment> doInsertExt(InstanceUrlParam context, Class<AdmEquipment> clazz, List<AdmEquipment> voList) {
- //创建设备
- List<AdmEquipment> vos = doInsert(context, AdmEquipment.class, voList);
- //TODO 创建资产
- //TODO 创建设备和系统关系 systemList
- return null;
- }
- private DmpResult<List<ObjRelationDTO>> queryRelation(InstanceUrlParam context, Map<String, AdmEquipment> 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<JSONArray> queryObjectById(InstanceUrlParam context, Map<String, AdmEquipment> 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<List<ObjRelationDTO>> relationRes, Map<String, AdmEquipment> voMap){
- boolean isDealDataSucc = false;
- List<AdmProperty> updatePropertyList = new ArrayList<AdmProperty>();
- List<String> del_relationIds = new ArrayList<>(relationRes.getData().size());
- Map<String, ObjRelationDTO> 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<AdmEquipment> voList, Map<String, AdmEquipment> voMap){
- boolean isDealDataSucc = false;
- Map<String, AdmEquipment> peMap = CollUtil.fieldValueMap(voList, "propertyId");
- //查询资产
- DmpResult<JSONArray> resultPe = queryObjectById(context, peMap);
- if (CollUtil.isNotEmpty(resultPe.getData())) {
- //更新资产 创建资产设备关系
- List<AdmProperty> propertyList = AdmEntityTransferUtil.toAdmMultiEntity(resultPe.getData(), null, AdmProperty.class);
- List<ObjectNode> 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<ObjectRelation> relationList = DigitalRelationFacade.create(context.getGroupCode(),context.getProjectId(), AdmCommonConstant.APP_ID,null,objRelationDTONews);
- if(CollUtil.isNotEmpty(relationList)){
- DmpResult<JSONArray> propertyRes = rwdClient.updateObject(context, JSONArray.parseArray(propertyList.toString()));
- if(propertyRes.getResult().equals(DmpResult.SUCCESS)){
- isDealDataSucc = true;
- }
- }
- }
- return isDealDataSucc;
- }
- }
|