RelationEqForBdController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.AdmCommonConstant;
  7. import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
  8. import com.persagy.proxy.adm.model.relation.BdForEqList;
  9. import com.persagy.proxy.adm.model.relation.EqForBd;
  10. import com.persagy.proxy.adm.model.relation.EqForBdList;
  11. import com.persagy.proxy.adm.request.AdmResponse;
  12. import com.persagy.proxy.adm.service.IAdmRelationService;
  13. import com.persagy.proxy.common.entity.InstanceUrlParam;
  14. import com.persagy.proxy.common.entity.RelationDTO;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.util.CollectionUtils;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * 设备服务于建筑
  23. *
  24. * @author zhangwencheng
  25. */
  26. @RestController
  27. @RequestMapping("/rel/eq-for-bd")
  28. public class RelationEqForBdController {
  29. @Autowired
  30. private IAdmRelationService service;
  31. @Value("${middleware.group.code}")
  32. private String groupCode;
  33. /**
  34. * 创建设备服务于建筑关系
  35. * 必填项:EquipId(设备id)、BuildingId(建筑id)
  36. *
  37. * @param eq2Bd 设备和建筑的关系对象
  38. * @return 创建的结果信息
  39. */
  40. @PostMapping("/link")
  41. public AdmResponse create(@RequestBody EqForBd eq2Bd, @RequestParam("projectId") String projectId) {
  42. String buildingId = eq2Bd.getBuildingId();
  43. String equipId = eq2Bd.getEquipId();
  44. //检验输入参数
  45. if (StrUtil.isEmpty(buildingId) || StrUtil.isEmpty(equipId)) {
  46. return AdmResponse.failure("必填项:equipId(设备id)、buildingId(建筑id列表)");
  47. }
  48. List<RelationDTO> voList = new ArrayList<>();
  49. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2BD;
  50. String graphCode = typeEnum.getGraphCode();
  51. String relCode = typeEnum.getRelCode();
  52. RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, buildingId);
  53. voList.add(relation);
  54. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  55. service.doSave(context, voList);
  56. return AdmResponse.success();
  57. }
  58. /**
  59. * 设备服务于建筑,设备一对多
  60. * 必填项:BuildingIdList(建筑id列表)、EquipId(设备id),测方法会覆盖以前的记录
  61. *
  62. * @param eqForBdList
  63. * @return 创建的结果信息
  64. */
  65. @PostMapping("link-bd")
  66. public AdmResponse createBd(@RequestBody EqForBdList eqForBdList, @RequestParam("projectId") String projectId) {
  67. List<String> buildingIdList = eqForBdList.getBuildingIdList();
  68. String equipId = eqForBdList.getEquipId();
  69. //检验输入参数
  70. if (CollectionUtils.isEmpty(buildingIdList) || StrUtil.isEmpty(equipId)) {
  71. return AdmResponse.failure("必填项:equipId(设备id)、buildingIdList(建筑id列表)");
  72. }
  73. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2BD;
  74. String graphCode = typeEnum.getGraphCode();
  75. String relCode = typeEnum.getRelCode();
  76. List<RelationDTO> voList = new ArrayList<>();
  77. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  78. //根据设备删除以前信息
  79. deleteByEquipId(equipId, typeEnum, context);
  80. for (String buildingId : buildingIdList) {
  81. RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, equipId, buildingId);
  82. voList.add(relation);
  83. }
  84. service.doSave(context, voList);
  85. return AdmResponse.success();
  86. }
  87. /**
  88. * 根据设备Id删除信息
  89. *
  90. * @param equipId
  91. * @param typeEnum
  92. * @param context
  93. */
  94. private void deleteByEquipId(String equipId, AdmRelationTypeEnum typeEnum, InstanceUrlParam context) {
  95. QueryCriteria criteria = new QueryCriteria();
  96. ObjectNode node = JsonNodeFactory.instance.objectNode();
  97. node.put("graphCode", typeEnum.getGraphCode());
  98. node.put("relCode", typeEnum.getRelCode());
  99. node.put("objFrom", equipId);
  100. criteria.setCriteria(node);
  101. service.doDelete(context, criteria);
  102. }
  103. /**
  104. * 服务于建筑的设备,建筑一对多
  105. * 必填项:EquipIdList(设备id列表)、BuildingId(建筑id)
  106. *
  107. * @param bdForEqList 设备和建筑关系对象
  108. * @return 创建的结果信息
  109. */
  110. @PostMapping("link-sy")
  111. public AdmResponse createSy(@RequestBody BdForEqList bdForEqList, @RequestParam("projectId") String projectId) {
  112. String buildingId = bdForEqList.getBuildingId();
  113. List<String> equipIdList = bdForEqList.getEquipIdList();
  114. //检验输入参数
  115. if (CollectionUtils.isEmpty(equipIdList) || StrUtil.isEmpty(buildingId)) {
  116. return AdmResponse.failure("必填项:equipIdList(设备id列表)、buildingId(建筑id)");
  117. }
  118. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2BD;
  119. String graphCode = typeEnum.getGraphCode();
  120. String relCode = typeEnum.getRelCode();
  121. List<RelationDTO> voList = new ArrayList<>();
  122. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  123. deleteByBuildId(buildingId, typeEnum, context);
  124. for (String equipId : equipIdList) {
  125. RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, equipId, buildingId);
  126. voList.add(relation);
  127. }
  128. service.doSave(context, voList);
  129. return AdmResponse.success();
  130. }
  131. /**
  132. * 根据建筑Id删除设备和建筑关系
  133. *
  134. * @param buildingId
  135. * @param typeEnum
  136. * @param context
  137. */
  138. private void deleteByBuildId(String buildingId, AdmRelationTypeEnum typeEnum, InstanceUrlParam context) {
  139. QueryCriteria criteria = new QueryCriteria();
  140. ObjectNode node = JsonNodeFactory.instance.objectNode();
  141. node.put("graphCode", typeEnum.getGraphCode());
  142. node.put("relCode", typeEnum.getRelCode());
  143. node.put("objTo", buildingId);
  144. criteria.setCriteria(node);
  145. service.doDelete(context, criteria);
  146. }
  147. /**
  148. * 根据对象删除设备服务于建筑的关系,只针对一个对象
  149. * 必填项:EquipId(设备id)、BuildingId(建筑id)
  150. *
  151. * @param eqForBd 对象
  152. * @return 删除的结果信息
  153. */
  154. @PostMapping("/unlink")
  155. public AdmResponse delete(@RequestBody EqForBd eqForBd, @RequestParam("projectId") String projectId) {
  156. String equipId = eqForBd.getEquipId();
  157. String buildingId = eqForBd.getBuildingId();
  158. //检验输入参数
  159. if (StrUtil.isEmpty(equipId) || StrUtil.isEmpty(buildingId)) {
  160. return AdmResponse.failure("必填项:equipId(设备id)、buildingId(建筑id)");
  161. }
  162. InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  163. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2BD;
  164. String graphCode = typeEnum.getGraphCode();
  165. String relCode = typeEnum.getRelCode();
  166. QueryCriteria criteria = new QueryCriteria();
  167. ObjectNode node = JsonNodeFactory.instance.objectNode();
  168. node.put("graphCode", graphCode);
  169. node.put("relCode", relCode);
  170. node.put("objTo", buildingId);
  171. node.put("objFrom", equipId);
  172. criteria.setCriteria(node);
  173. service.doDelete(context, criteria);
  174. return AdmResponse.success();
  175. }
  176. }