RelationEqInSpController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.request.AdmResponse;
  10. import com.persagy.proxy.adm.service.IAdmRelationService;
  11. import com.persagy.proxy.adm.utils.AdmContextUtil;
  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. * ADM 关系 EqInSp API
  21. * @author Charlie Yu
  22. * @date 2021-08-16
  23. */
  24. @RestController
  25. @RequestMapping("/rel/eq-in-sp-base")
  26. public class RelationEqInSpController {
  27. @Autowired
  28. private IAdmRelationService service;
  29. @Value("${middleware.group.code}")
  30. private String groupCode;
  31. /**
  32. * 创建设备所在业务空间关系
  33. * @param type
  34. * @param param
  35. * @return
  36. * @throws Exception
  37. */
  38. @PostMapping("/link-list")
  39. public AdmResponse create(@RequestParam(value = "type", required = false) String type,
  40. @RequestBody JSONObject param) throws Exception {
  41. JSONArray content = param == null ? null : param.getJSONArray("content");
  42. if(CollUtil.isEmpty(content)) {
  43. return AdmResponse.success();
  44. }
  45. // 创建关系对象
  46. List<RelationDTO> voList = new ArrayList<>();
  47. for(int i = 0;i < content.size();i++) {
  48. JSONObject para = content.getJSONObject(i);
  49. String equipId = para.getString("equipId");
  50. String spaceId = para.getString("spaceId");
  51. if(StrUtil.isBlank(equipId) || StrUtil.isBlank(spaceId)) {
  52. return AdmResponse.failure("必填项:EquipId(设备id)、SpaceId(业务空间id)");
  53. }
  54. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  55. voList.add(new RelationDTO(null, "MechInArch", "Eq2Sp", type, equipId, spaceId));
  56. }
  57. service.doSave(AdmContextUtil.toDmpContext(), voList);
  58. return AdmResponse.success();
  59. }
  60. /**
  61. * 创建设备所在业务空间关系
  62. * @param type
  63. * @param param
  64. * @return
  65. * @throws Exception
  66. */
  67. @PostMapping("/link-eq-sp")
  68. public AdmResponse createEq(@RequestParam(value = "type", required = false) String type,
  69. @RequestBody JSONObject param) throws Exception {
  70. String spaceId = param.getString("spaceId");
  71. JSONArray equipIdList = param.getJSONArray("equipIdList");
  72. if(StrUtil.isBlank(spaceId) || CollUtil.isEmpty(equipIdList)) {
  73. return AdmResponse.failure("必填项:EquipId(设备id)、SpaceId(业务空间id)");
  74. }
  75. // 创建关系对象
  76. List<RelationDTO> voList = new ArrayList<>();
  77. for(int i = 0;i < equipIdList.size();i++) {
  78. String equipId = equipIdList.getString(i);
  79. if(StrUtil.isBlank(equipId)) {
  80. continue;
  81. }
  82. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  83. voList.add(new RelationDTO(null, "MechInArch", "Eq2Sp", type, equipId, spaceId));
  84. }
  85. service.doSave(AdmContextUtil.toDmpContext(), voList);
  86. return AdmResponse.success();
  87. }
  88. /**
  89. * 根据对象删除设备所在业务空间关系
  90. * @param type
  91. * @param param
  92. * @return
  93. * @throws Exception
  94. */
  95. @PostMapping("/unlink-list")
  96. public AdmResponse delete(@RequestParam(value = "type", required = false) String type,
  97. @RequestBody JSONArray param) throws Exception {
  98. if(CollUtil.isEmpty(param)) {
  99. return AdmResponse.success();
  100. }
  101. // 组装上下文条件
  102. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  103. // 组装条件
  104. QueryCriteria criteria = new QueryCriteria();
  105. for(int i = 0;i < param.size();i++) {
  106. JSONObject para = param.getJSONObject(i);
  107. String equipId = para.getString("equipId");
  108. String spaceId = para.getString("spaceId");
  109. if(StrUtil.isBlank(equipId) || StrUtil.isBlank(spaceId)) {
  110. return AdmResponse.failure("必填项:EquipId(设备id)、SpaceId(业务空间id)");
  111. }
  112. ObjectNode node = JsonNodeFactory.instance.objectNode();
  113. node.put("graphCode", "MechInArch");
  114. node.put("relCode", "Eq2Sp");
  115. node.put("relValue", type);
  116. node.put("objFrom", equipId);
  117. node.put("objTo", spaceId);
  118. criteria.setCriteria(node);
  119. service.doDelete(context, criteria);
  120. }
  121. return AdmResponse.success();
  122. }
  123. }