AdmEquipmentServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.persagy.proxy.adm.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fasterxml.jackson.core.JsonProcessingException;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.fasterxml.jackson.databind.node.ArrayNode;
  9. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  10. import com.fasterxml.jackson.databind.node.ObjectNode;
  11. import com.persagy.dmp.basic.model.QueryCriteria;
  12. import com.persagy.dmp.common.model.entity.BaseEntity;
  13. import com.persagy.dmp.define.entity.ObjectTypeDefine;
  14. import com.persagy.dmp.digital.client.DigitalRelationFacade;
  15. import com.persagy.dmp.digital.entity.ObjectRelation;
  16. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  17. import com.persagy.proxy.adm.model.AdmEquipment;
  18. import com.persagy.proxy.adm.model.AdmProperty;
  19. import com.persagy.proxy.adm.service.IAdmComAndEquipService;
  20. import com.persagy.proxy.adm.service.IAdmEquipmentService;
  21. import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
  22. import com.persagy.proxy.adm.utils.ObjectNameUtil;
  23. import com.persagy.proxy.common.client.DmpRwdClient;
  24. import com.persagy.proxy.common.entity.DmpResult;
  25. import com.persagy.proxy.common.entity.InstanceUrlParam;
  26. import com.persagy.proxy.common.entity.ObjRelationDTO;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.stereotype.Service;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.Set;
  34. import java.util.stream.Collectors;
  35. /**
  36. * ADM 设备 实现
  37. * @author lvxy
  38. * @date 2021/8/18
  39. */
  40. @Slf4j
  41. @Service
  42. public class AdmEquipmentServiceImpl extends AbstractAdmBaseServiceImpl<AdmEquipment> implements IAdmEquipmentService, IAdmComAndEquipService {
  43. @Autowired
  44. private DmpRwdClient rwdClient;
  45. @Autowired
  46. private ObjectMapper objectMapper;
  47. @Override
  48. public List<AdmEquipment> doInsertEuip(InstanceUrlParam context, Class<AdmEquipment> clazz, List<AdmEquipment> voList) {
  49. if(CollUtil.isEmpty(voList)) {
  50. return null;
  51. }
  52. Set<String> classCodes = voList.stream().map(AdmEquipment::getClassCode).collect(Collectors.toSet());
  53. //查询定义类型
  54. QueryCriteria criteriaObject = getDmpCriteria(context, classCodes, "equipment");
  55. JSONObject paraPe = null;
  56. try {
  57. String paraStr = objectMapper.writeValueAsString(criteriaObject);
  58. paraPe = JSONObject.parseObject(paraStr);
  59. } catch (JsonProcessingException e) {
  60. throw new RuntimeException("转换查询条件出错:" + e.getMessage());
  61. }
  62. DmpResult<JSONArray> resultDef = rwdClient.queryDefClass(context, paraPe);
  63. Map<String, ObjectTypeDefine> finalDefClassMap = getDefineMap(resultDef);
  64. // 设备名字处理
  65. voList.forEach(equip -> {
  66. if (StrUtil.isNotEmpty(equip.getName())) {
  67. if(CollUtil.isNotEmpty(finalDefClassMap) && finalDefClassMap.containsKey(equip.getClassCode())){
  68. ObjectTypeDefine defclass = finalDefClassMap.get(equip.getClassCode());
  69. equip.setName(ObjectNameUtil.objectName(defclass.getName()+"-"));
  70. }else{
  71. equip.setName(ObjectNameUtil.objectName("设备-"));
  72. }
  73. }
  74. });
  75. return doInsert(context, clazz, voList);
  76. }
  77. @Override
  78. public List<AdmEquipment> doUpdateEuip(InstanceUrlParam context, Class<AdmEquipment> clazz, List<AdmEquipment> voList) {
  79. //TODO 处理关系标记
  80. /*try {
  81. *//** 处理关系标记 *//*
  82. val listtype = ArrayList<String>()
  83. listtype.add("pe2bd")
  84. listtype.add("pe2fl")
  85. listtype.add("pe2sh")
  86. listtype.add("pe2sp")
  87. for (type in listtype){
  88. val entity = relationTypeProjectservice.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.eq("relationType", type)).entity()
  89. if (entity!=null){
  90. entity.computationalState = 2
  91. relationTypeProjectservice.update(entity)
  92. }
  93. }
  94. } catch (e: Exception) {
  95. e.printStackTrace()
  96. }*/
  97. Map<String, AdmEquipment> voMap = CollUtil.fieldValueMap(voList, BaseEntity.PROP_ID);
  98. //查询设备对应的资产 关系 Pe2Eq (1:N,一个资产关联多个设备,设备是最小单位 by:lvxianyun)
  99. DmpResult<List<ObjRelationDTO>> relationRes = queryRelation(context, voMap);
  100. boolean isDealDataSucc = false;
  101. //不是绑定关系,清理绑定关系,重新绑定
  102. if(CollUtil.isNotEmpty(relationRes.getData())){
  103. isDealDataSucc = updateRelation2Pe(context, relationRes, voMap);
  104. }else {
  105. //不存在关系,需要新绑定关系,更新per,插入关系
  106. isDealDataSucc = insertRelation2Pe(context, voList, voMap);
  107. }
  108. return isDealDataSucc ? doUpdate(context, clazz, voList) : null;
  109. }
  110. @Override
  111. public void doDeleteEuip(InstanceUrlParam context, List<AdmEquipment> voList) {
  112. Map<String, AdmEquipment> voMap = CollUtil.fieldValueMap(voList, BaseEntity.PROP_ID);
  113. DmpResult<List<ObjRelationDTO>> relationRes = queryRelation(context, voMap);
  114. //清理绑定关系
  115. if(CollUtil.isNotEmpty(relationRes.getData())){
  116. updateRelation2Pe(context, relationRes, voMap);
  117. }
  118. doDelete(context,voList);
  119. }
  120. @Override
  121. public List<AdmEquipment> doInsertExt(InstanceUrlParam context, Class<AdmEquipment> clazz, List<AdmEquipment> voList) {
  122. //创建设备
  123. List<AdmEquipment> vos = doInsert(context, AdmEquipment.class, voList);
  124. //TODO 创建资产
  125. //TODO 创建设备和系统关系 systemList
  126. return null;
  127. }
  128. private DmpResult<List<ObjRelationDTO>> queryRelation(InstanceUrlParam context, Map<String, AdmEquipment> voMap){
  129. QueryCriteria dmpRequest = new QueryCriteria();
  130. ObjectNode criteria = JsonNodeFactory.instance.objectNode();
  131. ArrayNode array = criteria.putObject("objTo").putArray("$in");
  132. voMap.keySet().forEach(equipId -> array.add(equipId));
  133. criteria.put("relCode", "Pe2Eq");
  134. criteria.put("graphCode", "Property2Mech");
  135. dmpRequest.setCriteria(criteria);
  136. dmpRequest.setSize(voMap.size() * 1L);
  137. dmpRequest.setPage(1L);
  138. dmpRequest.setWithoutCount(true);
  139. // 转换参数
  140. JSONObject paraRel = null;
  141. try {
  142. String paraStr = objectMapper.writeValueAsString(dmpRequest);
  143. paraRel = JSONObject.parseObject(paraStr);
  144. } catch (JsonProcessingException e) {
  145. log.error(e.getMessage(), e);
  146. return null;
  147. }
  148. return rwdClient.queryRelation(context, dmpRequest);
  149. }
  150. private DmpResult<JSONArray> queryObjectById(InstanceUrlParam context, Map<String, AdmEquipment> peMap){
  151. QueryCriteria criteriaObject = new QueryCriteria();
  152. ObjectNode criteriaPe = JsonNodeFactory.instance.objectNode();
  153. ArrayNode array = criteriaPe.putObject("id").putArray("$in");
  154. peMap.keySet().forEach(equipId -> array.add(equipId));
  155. criteriaPe.put("classCode", "property");
  156. criteriaPe.put("objType", "property");
  157. criteriaObject.setCriteria(criteriaPe);
  158. criteriaObject.setSize(peMap.size() * 1L);
  159. criteriaObject.setPage(1L);
  160. criteriaObject.setWithoutCount(true);
  161. // 转换参数
  162. JSONObject paraPe = null;
  163. try {
  164. String paraStr = objectMapper.writeValueAsString(criteriaObject);
  165. paraPe = JSONObject.parseObject(paraStr);
  166. } catch (JsonProcessingException e) {
  167. log.error(e.getMessage(), e);
  168. return null;
  169. }
  170. return rwdClient.queryObject(context, paraPe);
  171. }
  172. private AdmProperty createPropertyObj(String id, String equipId, Boolean isBimIdNull){
  173. AdmProperty admProperty = new AdmProperty();
  174. admProperty.setId(id);
  175. admProperty.setEquipId(equipId);
  176. if(isBimIdNull){
  177. admProperty.setBimId(null);
  178. }
  179. return admProperty;
  180. }
  181. private ObjectNode creatRelationObj(String objTo,String objFrom){
  182. ObjectNode relationNew = JsonNodeFactory.instance.objectNode();
  183. relationNew.put("relCode", "Pe2Eq");
  184. relationNew.put("graphCode", "Property2Mech");
  185. relationNew.put("graphCode", "Property2Mech001");
  186. relationNew.put("objTo", objTo);
  187. relationNew.put("objFrom", objFrom);
  188. return relationNew;
  189. }
  190. /**
  191. * 更新设备关联的资产(资产中的equipId 信息点) 以及设备资产关系
  192. * @return
  193. */
  194. private Boolean updateRelation2Pe(InstanceUrlParam context, DmpResult<List<ObjRelationDTO>> relationRes, Map<String, AdmEquipment> voMap){
  195. boolean isDealDataSucc = false;
  196. List<AdmProperty> updatePropertyList = new ArrayList<AdmProperty>();
  197. List<String> del_relationIds = new ArrayList<>(relationRes.getData().size());
  198. Map<String, ObjRelationDTO> relationMap = CollUtil.fieldValueMap(relationRes.getData(), "objTo");
  199. relationMap.keySet().forEach(euipId -> {
  200. //存在绑定的资产
  201. if(voMap.keySet().contains(euipId)){
  202. AdmEquipment admEquipment = voMap.get(euipId);
  203. ObjRelationDTO relation = relationMap.get(euipId);
  204. String propertyId = relation.getObjFrom();
  205. if(StrUtil.isNotEmpty(admEquipment.getPropertyId()) && StrUtil.isNotEmpty(propertyId)
  206. && !admEquipment.getPropertyId().equals(propertyId)){
  207. //1 与设备提供的资产绑定
  208. AdmProperty propertyFrom = createPropertyObj(admEquipment.getPropertyId(), euipId, false);
  209. propertyFrom.setId(admEquipment.getPropertyId());
  210. propertyFrom.setEquipId(euipId);
  211. updatePropertyList.add(propertyFrom);
  212. //2 从关系中查询出的资产与当前设备解绑
  213. //更新资产中的信息点
  214. AdmProperty property = createPropertyObj(propertyId, null, true);
  215. updatePropertyList.add(property);
  216. //更新关系
  217. del_relationIds.add(relation.getId());
  218. }
  219. }
  220. });
  221. DigitalRelationFacade.delete(context.getGroupCode(),context.getProjectId(), AdmCommonConstant.APP_ID,null,del_relationIds);
  222. DmpResult propertyResult = rwdClient.updateObject(context, JSONArray.parseArray(updatePropertyList.toString()));
  223. if(propertyResult.getResult().equals(DmpResult.SUCCESS)){
  224. isDealDataSucc = true;
  225. }
  226. return isDealDataSucc;
  227. }
  228. /**
  229. * 不存在关系,需要新绑定关系,更新per,插入关系
  230. * @param context
  231. * @param voList
  232. * @param voMap
  233. * @return
  234. */
  235. private Boolean insertRelation2Pe(InstanceUrlParam context, List<AdmEquipment> voList, Map<String, AdmEquipment> voMap){
  236. boolean isDealDataSucc = false;
  237. Map<String, AdmEquipment> peMap = CollUtil.fieldValueMap(voList, "propertyId");
  238. //查询资产
  239. DmpResult<JSONArray> resultPe = queryObjectById(context, peMap);
  240. if (CollUtil.isNotEmpty(resultPe.getData())) {
  241. //更新资产 创建资产设备关系
  242. List<AdmProperty> propertyList = AdmEntityTransferUtil.toAdmMultiEntity(resultPe.getData(), null, AdmProperty.class);
  243. List<ObjectNode> objRelationDTONews = new ArrayList<>();
  244. propertyList.forEach(property -> {
  245. //存在绑定的资产 更新
  246. AdmEquipment admEquipment = peMap.get(property.getId());
  247. property.setEquipId(admEquipment.getId());
  248. //创建新的设备资产关系
  249. ObjectNode relationNew = creatRelationObj( admEquipment.getId(), property.getId());
  250. objRelationDTONews.add(relationNew);
  251. });
  252. List<ObjectRelation> relationList = DigitalRelationFacade.create(context.getGroupCode(),context.getProjectId(), AdmCommonConstant.APP_ID,null,objRelationDTONews);
  253. if(CollUtil.isNotEmpty(relationList)){
  254. DmpResult<JSONArray> propertyRes = rwdClient.updateObject(context, JSONArray.parseArray(propertyList.toString()));
  255. if(propertyRes.getResult().equals(DmpResult.SUCCESS)){
  256. isDealDataSucc = true;
  257. }
  258. }
  259. }
  260. return isDealDataSucc;
  261. }
  262. }