RelationEqForSpConreoller.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.common.entity.InstanceUrlParam;
  13. import com.persagy.proxy.common.entity.RelationDTO;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.web.bind.annotation.*;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. /**
  20. * @description:设备服务于业务空间
  21. * 018、关系-设备服务于业务空间
  22. * @author:lgy
  23. * @data:2021/9/1 10:23
  24. */
  25. @RestController
  26. @RequestMapping("/rel/eq-for-sp-base")
  27. public class RelationEqForSpConreoller {
  28. @Autowired
  29. private IAdmRelationService service;
  30. @Value("${middleware.group.code}")
  31. private String groupCode;
  32. /**
  33. * create 创建设备服务于业务空间关系
  34. * @param projectId projectId
  35. * @param type type
  36. * @param param param
  37. * @return {@link com.persagy.proxy.adm.request.AdmResponse}
  38. * @author lgy
  39. * @date 2021/9/1 10:28
  40. */
  41. @PostMapping("/link-list")
  42. public AdmResponse create(@RequestParam("projectId") String projectId,
  43. @RequestParam(value = "type", required = false) String type,
  44. @RequestBody JSONObject param) throws Exception {
  45. JSONArray content = param == null ? null : param.getJSONArray("content");
  46. if(CollUtil.isEmpty(content)) {
  47. return AdmResponse.success();
  48. }
  49. // 创建关系对象
  50. List<RelationDTO> voList = new ArrayList<>();
  51. for(int i = 0;i < content.size();i++) {
  52. JSONObject para = content.getJSONObject(i);
  53. String equipId = para.getString("equipId");
  54. String spaceId = para.getString("spaceId");
  55. if(StrUtil.isBlank(equipId) || StrUtil.isBlank(spaceId)) {
  56. return AdmResponse.failure("必填项:EquipId(设备id)、SpaceId(业务空间id)");
  57. }
  58. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  59. voList.add(new RelationDTO(null, "MechForArch", "Eq2Sp", type, equipId, spaceId));
  60. }
  61. // 组装上下文条件
  62. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  63. service.doSave(context, voList);
  64. return AdmResponse.success();
  65. }
  66. /**
  67. * 创建服务于业务空间的设备关系
  68. * 服务于业务空间的设备,业务空间一对多,累计添加不会覆盖以前记(必填项:EquipIdList(设备id列表)、SpaceId(业务空间id))
  69. * @param type
  70. * @param param
  71. * @return
  72. * @throws Exception
  73. */
  74. @PostMapping("/link-eq-sp")
  75. public AdmResponse createEq(@RequestParam("projectId") String projectId,
  76. @RequestParam(value = "type", required = false) String type,
  77. @RequestBody JSONObject param) throws Exception {
  78. String spaceId = param.getString("spaceId");
  79. JSONArray equipIdList = param.getJSONArray("equipIdList");
  80. if(StrUtil.isBlank(spaceId) || CollUtil.isEmpty(equipIdList)) {
  81. return AdmResponse.failure("必填项:EquipId(设备id)、SpaceId(业务空间id)");
  82. }
  83. // 创建关系对象
  84. List<RelationDTO> voList = new ArrayList<>();
  85. for(int i = 0;i < equipIdList.size();i++) {
  86. String equipId = equipIdList.getString(i);
  87. if(StrUtil.isBlank(equipId)) {
  88. continue;
  89. }
  90. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  91. voList.add(new RelationDTO(null, "MechForArch", "Eq2Sp", type, equipId, spaceId));
  92. }
  93. // 组装上下文条件
  94. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  95. service.doSave(context, voList);
  96. return AdmResponse.success();
  97. }
  98. /**
  99. * 根据对象删除关系
  100. * 根据对象删除设备服务于业务空间关系(必填项:EquipId(设备id)、SpaceId(业务空间id))
  101. * @param type
  102. * @param param
  103. * @return
  104. * @throws Exception
  105. */
  106. @PostMapping("/unlink-list")
  107. public AdmResponse delete(@RequestParam String projectId,
  108. @RequestParam(value = "type", required = false) String type,
  109. @RequestBody JSONArray param) throws Exception {
  110. if(CollUtil.isEmpty(param)) {
  111. return AdmResponse.success();
  112. }
  113. // 组装上下文条件
  114. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  115. // 组装条件
  116. QueryCriteria criteria = new QueryCriteria();
  117. for(int i = 0;i < param.size();i++) {
  118. JSONObject para = param.getJSONObject(i);
  119. String equipId = para.getString("equipId");
  120. String spaceId = para.getString("spaceId");
  121. if(StrUtil.isBlank(equipId) || StrUtil.isBlank(spaceId)) {
  122. return AdmResponse.failure("必填项:EquipId(设备id)、SpaceId(业务空间id)");
  123. }
  124. ObjectNode node = JsonNodeFactory.instance.objectNode();
  125. node.put("graphCode", "MechForArch");
  126. node.put("relCode", "Eq2Sp");
  127. node.put("relValue", type);
  128. node.put("objFrom", equipId);
  129. node.put("objTo", spaceId);
  130. criteria.setCriteria(node);
  131. service.doDelete(context, criteria);
  132. }
  133. return AdmResponse.success();
  134. }
  135. }