RelationSpInSiController.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.persagy.proxy.adm.controller;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  4. import com.fasterxml.jackson.databind.node.ObjectNode;
  5. import com.persagy.dmp.basic.model.QueryCriteria;
  6. import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
  7. import com.persagy.proxy.adm.model.relation.SiInSpBase;
  8. import com.persagy.proxy.adm.model.relation.SiInSpBaseList;
  9. import com.persagy.proxy.adm.request.AdmCreateRequest;
  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 lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.util.CollectionUtils;
  19. import org.springframework.web.bind.annotation.*;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. /**
  23. * @author zhangwencheng
  24. * 业务空间内的元空间
  25. */
  26. @Slf4j
  27. @RestController
  28. @RequestMapping("/rel/si-in-sp-base")
  29. public class RelationSpInSiController {
  30. @Autowired
  31. private IAdmRelationService service;
  32. @Value("${middleware.group.code}")
  33. private String groupCode;
  34. /**
  35. * 创建业务空间内的元空间关系
  36. *
  37. * @param siInSpBaseRequest 元空间内的业务空间关系对象
  38. * @return 创建的结果信息
  39. */
  40. @PostMapping("/link-list")
  41. public AdmResponse create(@RequestParam(value = "type",required = false) String type,
  42. @RequestBody AdmCreateRequest<SiInSpBase> siInSpBaseRequest) {
  43. List<SiInSpBase> content = siInSpBaseRequest.getContent();
  44. if (CollectionUtils.isEmpty(content)) {
  45. return AdmResponse.failure("content不能为空");
  46. }
  47. List<RelationDTO> voList = new ArrayList<>();
  48. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SP2SI;
  49. String graphCode = typeEnum.getGraphCode();
  50. String relCode = typeEnum.getRelCode();
  51. for (SiInSpBase siInSpBase : content) {
  52. String ispaceId = siInSpBase.getIspaceId();
  53. String spaceId = siInSpBase.getSpaceId();
  54. String objectType = siInSpBase.getObjectType();
  55. if (StrUtil.isEmpty(spaceId) || StrUtil.isBlank(ispaceId)) {
  56. return AdmResponse.failure("必填项:ispaceId(元空间id)、spaceId(空间id)");
  57. }
  58. RelationDTO relation = new RelationDTO(null, graphCode, relCode, objectType, spaceId, ispaceId);
  59. voList.add(relation);
  60. }
  61. service.doSave(AdmContextUtil.toDmpContext(), voList);
  62. return AdmResponse.success();
  63. }
  64. /**
  65. * 创建业务空间内的元空间关系-此方法会覆盖以前的记录
  66. *
  67. * @param siInSpBaseList 业务空间和元空间的关系对象
  68. * @return 创建的结果信息
  69. */
  70. @PostMapping("/link-sp")
  71. public AdmResponse createSp(@RequestParam(value = "type", required = false) String type,
  72. @RequestBody SiInSpBaseList siInSpBaseList) {
  73. List<String> ispaceIdList = siInSpBaseList.getIspaceIdList();
  74. String spaceId = siInSpBaseList.getSpaceId();
  75. if (StrUtil.isEmpty(spaceId) || CollectionUtils.isEmpty(ispaceIdList)) {
  76. return AdmResponse.failure("必填项:ispaceIdList(元空间id)、spaceId(空间id)");
  77. }
  78. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  79. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SP2SI;
  80. String graphCode = typeEnum.getGraphCode();
  81. String relCode = typeEnum.getRelCode();
  82. deleteBySpaceId(spaceId, context, typeEnum);
  83. List<RelationDTO> voList = new ArrayList<>();
  84. for (String ispaceId : ispaceIdList) {
  85. RelationDTO relation = new RelationDTO(null, graphCode, relCode, siInSpBaseList.getZoneType(), spaceId, ispaceId);
  86. voList.add(relation);
  87. }
  88. service.doSave(context, voList);
  89. return AdmResponse.success();
  90. }
  91. /**
  92. * 删除信息
  93. *
  94. * @param spaceId
  95. * @param context
  96. * @param typeEnum
  97. */
  98. private void deleteBySpaceId(String spaceId, InstanceUrlParam context, AdmRelationTypeEnum typeEnum) {
  99. QueryCriteria criteria = new QueryCriteria();
  100. ObjectNode node = JsonNodeFactory.instance.objectNode();
  101. node.put("graphCode", typeEnum.getGraphCode());
  102. node.put("relCode", typeEnum.getRelCode());
  103. node.put("objFrom", spaceId);
  104. criteria.setCriteria(node);
  105. service.doDelete(context, criteria);
  106. }
  107. /**
  108. * 业务空间内的元空间,业务空间一对多,累计添加不会覆盖以前记录
  109. *
  110. * @param siInSpBaseList 业务空间和元空间的关系对象
  111. * @return 创建的结果信息
  112. */
  113. @PostMapping("/link-si-sp")
  114. public AdmResponse createSpSi(@RequestParam(value = "type",required = false) String type,
  115. @RequestBody SiInSpBaseList siInSpBaseList){
  116. List<String> ispaceIdList = siInSpBaseList.getIspaceIdList();
  117. String spaceId = siInSpBaseList.getSpaceId();
  118. if (StrUtil.isEmpty(spaceId) || CollectionUtils.isEmpty(ispaceIdList)) {
  119. return AdmResponse.failure("必填项:ispaceIdList(元空间id)、spaceId(空间id)");
  120. }
  121. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  122. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SP2SI;
  123. String graphCode = typeEnum.getGraphCode();
  124. String relCode = typeEnum.getRelCode();
  125. List<RelationDTO> voList = new ArrayList<>();
  126. for (String ispaceId : ispaceIdList) {
  127. RelationDTO relation = new RelationDTO(null, graphCode, relCode, siInSpBaseList.getZoneType(), spaceId, ispaceId);
  128. voList.add(relation);
  129. }
  130. service.doSave(context, voList);
  131. return AdmResponse.success();
  132. }
  133. /**
  134. * 根据对象删除业务空间内的元空间关系
  135. *
  136. * @param siInSpBases 对象
  137. * @return 删除的结果信息
  138. */
  139. @PostMapping("/unlink")
  140. public AdmResponse delete(@RequestParam(value = "type",required = false) String type,
  141. @RequestBody List<SiInSpBase> siInSpBases) {
  142. if(CollectionUtils.isEmpty(siInSpBases)){
  143. return AdmResponse.failure("必填项:siInSpBases不能为空");
  144. }
  145. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.SP2SI;
  146. String graphCode = typeEnum.getGraphCode();
  147. String relCode = typeEnum.getRelCode();
  148. List<QueryCriteria> queryCriteriaList = new ArrayList<>();
  149. for (SiInSpBase siInSpBase : siInSpBases) {
  150. String spaceId = siInSpBase.getSpaceId();
  151. String ispaceId = siInSpBase.getIspaceId();
  152. String objectType = siInSpBase.getObjectType();
  153. if (StrUtil.isBlank(spaceId) || StrUtil.isEmpty(ispaceId)) {
  154. return AdmResponse.failure("必填项:ispaceId(元空间)、spaceId(空间id)");
  155. }
  156. QueryCriteria criteria = new QueryCriteria();
  157. ObjectNode node = JsonNodeFactory.instance.objectNode();
  158. node.put("graphCode", graphCode);
  159. node.put("relCode", relCode);
  160. node.put("relValue", objectType);
  161. node.put("objFrom", spaceId);
  162. node.put("objTo", ispaceId);
  163. criteria.setCriteria(node);
  164. queryCriteriaList.add(criteria);
  165. }
  166. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  167. for (QueryCriteria queryCriteria : queryCriteriaList) {
  168. service.doDelete(context, queryCriteria);
  169. }
  170. return AdmResponse.success();
  171. }
  172. }