RelationSyEqController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.persagy.proxy.adm.controller;
  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.databind.node.JsonNodeFactory;
  7. import com.fasterxml.jackson.databind.node.ObjectNode;
  8. import com.persagy.dmp.basic.model.QueryCriteria;
  9. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  10. import com.persagy.proxy.adm.request.AdmResponse;
  11. import com.persagy.proxy.adm.service.IAdmRelationService;
  12. import com.persagy.proxy.adm.utils.AdmContextUtil;
  13. import com.persagy.proxy.common.entity.InstanceUrlParam;
  14. import com.persagy.proxy.common.entity.RelationDTO;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * @description:系统下的设备设施
  23. * @author:lgy
  24. * @data:2021/9/3 15:25
  25. */
  26. @RestController
  27. @RequestMapping("/rel/sy-in-eq")
  28. public class RelationSyEqController {
  29. @Autowired
  30. private IAdmRelationService service;
  31. @Value("${middleware.group.code}")
  32. private String groupCode;
  33. /**
  34. * 创建系统下的设备关系,数据叠加,不会覆盖以前的数据
  35. * 必填项:EquipID(设备id)、SysID(系统id)
  36. * @param param
  37. * @return
  38. * @throws Exception
  39. */
  40. @PostMapping(value = {"/link-sy-eq","/link"})
  41. public AdmResponse create(@RequestBody JSONObject param) throws Exception {
  42. //String equipId = param.getString("equipId");
  43. JSONArray equipIdList = param.getJSONArray("equipIdList");
  44. String sysID = param.getString("sysId");
  45. if(StringUtils.isEmpty(sysID) || CollUtil.isEmpty(equipIdList)) {
  46. return AdmResponse.failure("必填项:EquipIdList(设备id列表)、SysID(系统id)");
  47. }
  48. // 创建关系对象
  49. List<RelationDTO> voList = new ArrayList<>();
  50. equipIdList.forEach(item->{
  51. voList.add(new RelationDTO(null, "MechSubset", "Sy2Eq", null, sysID , item.toString()));
  52. });
  53. // 组装上下文条件
  54. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  55. service.doSave(context, voList);
  56. return AdmResponse.success();
  57. }
  58. /**
  59. * 根据对象删除系统和设备的关系,只针对一个对象
  60. *必填项:EquipID(设备id)、SysID(系统id)
  61. * @param param 对象
  62. * @return 删除的结果信息
  63. */
  64. @PostMapping("/unlink")
  65. public AdmResponse delete(@RequestBody JSONObject param) throws Exception {
  66. if(CollUtil.isEmpty(param)) {
  67. return AdmResponse.success();
  68. }
  69. // 组装上下文条件
  70. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  71. // 组装条件
  72. QueryCriteria criteria = new QueryCriteria();
  73. String equipId = param.getString("equipId");
  74. String sysID = param.getString("sysId");
  75. if(!StrUtil.isAllNotEmpty(equipId,sysID)) {
  76. return AdmResponse.failure("必填项:EquipID(设备id)、SysID(系统id)");
  77. }
  78. ObjectNode node = JsonNodeFactory.instance.objectNode();
  79. node.put("graphCode", "MechSubset");
  80. node.put("relCode", "Sy2Eq");
  81. node.put("objFrom", sysID);
  82. node.put("objTo", equipId);
  83. criteria.setCriteria(node);
  84. service.doDelete(context, criteria);
  85. return AdmResponse.success();
  86. }
  87. /**
  88. * 创建系统下的设备关系
  89. *
  90. * @param syEqList 系统和设备的关系对象
  91. * @return 创建结果信息
  92. */
  93. @PostMapping("/link-eq")
  94. public AdmResponse createBatchWithDeleteOldsysIdRelation(@RequestBody JSONObject syEqList) throws Exception {
  95. JSONArray equipIdList = syEqList.getJSONArray("equipIdList");
  96. String sysId = syEqList.getString("sysId");
  97. if(StrUtil.isBlank(sysId) || CollUtil.isEmpty(equipIdList)) {
  98. return AdmResponse.failure("必填项:EquipIdList(设备id列表)、SysID(系统id)");
  99. }
  100. InstanceUrlParam context =AdmContextUtil.toDmpContext();
  101. QueryCriteria queryRequest = new QueryCriteria();
  102. ObjectNode node = JsonNodeFactory.instance.objectNode();
  103. node.put("graphCode", "MechSubset");
  104. node.put("relCode", "Sy2Eq");
  105. node.put("objFrom", sysId);
  106. queryRequest.setCriteria(node);
  107. service.doDelete(context,queryRequest);
  108. // 创建关系对象
  109. List<RelationDTO> voList = new ArrayList<>();
  110. for (int i = 0; i < equipIdList.size(); i++) {
  111. voList.add(new RelationDTO(null, "MechSubset", "Sy2Eq", null, sysId , equipIdList.getString(i)));
  112. }
  113. // 组装上下文条件
  114. service.doSave(context, voList);
  115. return AdmResponse.success();
  116. }
  117. /**
  118. * 创建系统下的设备关系
  119. *
  120. * @param param 系统和设备的关系对象
  121. * @return 创建结果信息
  122. */
  123. @PostMapping("/link-sy")
  124. public AdmResponse createDistinctEq(@RequestBody JSONObject param) throws Exception {
  125. String equipId = param.getString("equipId");
  126. JSONArray sysIdList = param.getJSONArray("sysIdList");
  127. if(StringUtils.isEmpty(equipId) || CollUtil.isEmpty(sysIdList)) {
  128. return AdmResponse.failure("必填项:EquipId(设备id)、sysIdList(系统id列表)");
  129. }
  130. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  131. QueryCriteria queryRequest = new QueryCriteria();
  132. ObjectNode node = JsonNodeFactory.instance.objectNode();
  133. node.put("graphCode", "MechSubset");
  134. node.put("relCode", "Sy2Eq");
  135. node.put("objTo", equipId);
  136. queryRequest.setCriteria(node);
  137. service.doDelete(context,queryRequest);
  138. // 创建关系对象
  139. List<RelationDTO> voList = new ArrayList<>();
  140. sysIdList.forEach(item->{
  141. voList.add(new RelationDTO(null, "MechSubset", "Sy2Eq", null, item.toString() , equipId));
  142. });
  143. // 组装上下文条件
  144. service.doSave(context, voList);
  145. return AdmResponse.success();
  146. }
  147. /**
  148. * 根据对象删除系统和设备的关系
  149. *
  150. * @param entytyList 根据对象删除系统和设备的关系
  151. * @return 创建结果信息
  152. */
  153. public AdmResponse deleteBatch(@RequestParam String projectId,
  154. @RequestBody JSONArray entytyList) throws Exception {
  155. if(CollUtil.isEmpty(entytyList)) {
  156. return AdmResponse.success();
  157. }
  158. // 组装上下文条件
  159. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  160. for (int i = 0; i < entytyList.size(); i++) {
  161. JSONObject entity = entytyList.getJSONObject(i);
  162. // 组装条件
  163. QueryCriteria criteria = new QueryCriteria();
  164. String equipId = entity.getString("equipId");
  165. String sysID = entity.getString("sysId");
  166. if(!StrUtil.isAllNotEmpty(equipId,sysID)) {
  167. continue;
  168. }
  169. ObjectNode node = JsonNodeFactory.instance.objectNode();
  170. node.put("graphCode", "MechSubset");
  171. node.put("relCode", "Sy2Eq");
  172. node.put("objFrom", sysID);
  173. node.put("objTo", equipId);
  174. criteria.setCriteria(node);
  175. service.doDelete(context, criteria);
  176. }
  177. return AdmResponse.success();
  178. }
  179. }