RelationEqForFlController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.EqForFl;
  8. import com.persagy.proxy.adm.model.relation.EqForFlList;
  9. import com.persagy.proxy.adm.model.relation.FlForEqList;
  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.PostMapping;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * 037 -设备服务于楼层
  27. *
  28. * @author zhangwencheng
  29. */
  30. @Slf4j
  31. @RestController
  32. @RequestMapping("/rel/eq-for-fl")
  33. public class RelationEqForFlController {
  34. @Autowired
  35. private IAdmRelationService service;
  36. @Value("${middleware.group.code}")
  37. private String groupCode;
  38. /**
  39. * 创建设备服务于楼层关系
  40. * 必填项:EquipId(设备id)、FloorId(楼层id)
  41. *
  42. * @param eqForFl 设备和楼层的关系对象
  43. * @return 创建的结果信息
  44. */
  45. @PostMapping("/link")
  46. public AdmResponse create(@RequestBody EqForFl eqForFl) {
  47. String equipId = eqForFl.getEquipId();
  48. String floorId = eqForFl.getFloorId();
  49. //检验输入参数
  50. if (StrUtil.isEmpty(equipId) || StrUtil.isEmpty(floorId)) {
  51. return AdmResponse.failure("必填项:equipId(设备id)、buildingId(建筑id)");
  52. }
  53. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
  54. String graphCode = typeEnum.getGraphCode();
  55. String relCode = typeEnum.getRelCode();
  56. List<RelationDTO> voList = new ArrayList<>();
  57. RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, floorId);
  58. voList.add(relation);
  59. service.doSave(AdmContextUtil.toDmpContext(), voList);
  60. return AdmResponse.success();
  61. }
  62. /**
  63. * 创建服务于楼层的设备关系
  64. *
  65. * @param flForEqList 设备和楼层关系对象
  66. * @return 创建的结果信息
  67. */
  68. @PostMapping("link-eq")
  69. public AdmResponse createEq(@RequestBody FlForEqList flForEqList) {
  70. List<String> equipIdList = flForEqList.getEquipIdList();
  71. String floorId = flForEqList.getFloorId();
  72. //检验输入参数
  73. if (CollectionUtils.isEmpty(equipIdList) || StrUtil.isEmpty(floorId)) {
  74. return AdmResponse.failure("必填项:equipId(设备id)、equipIdList(设备Id列表)");
  75. }
  76. List<RelationDTO> voList = new ArrayList<>();
  77. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  78. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
  79. String graphCode = typeEnum.getGraphCode();
  80. String relCode = typeEnum.getRelCode();
  81. //根据floorId删除信息
  82. deleteByObjId(null, floorId, typeEnum, context);
  83. for (String equipId : equipIdList) {
  84. RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, floorId);
  85. voList.add(relation);
  86. }
  87. service.doSave(AdmContextUtil.toDmpContext(), voList);
  88. return AdmResponse.success();
  89. }
  90. /**
  91. * 设备服务于楼层,设备一对多
  92. * 必填项:FloorIdList(楼层id列表)、EquipId(设备id),测方法会覆盖以前的记录
  93. *
  94. * @return 创建的结果信息
  95. */
  96. @PostMapping("link-fl")
  97. public AdmResponse createFl(@RequestBody EqForFlList eqForFlList) {
  98. String equipId = eqForFlList.getEquipId();
  99. List<String> floorIdList = eqForFlList.getFloorIdList();
  100. //检验输入参数
  101. if (CollectionUtils.isEmpty(floorIdList) || StrUtil.isEmpty(equipId)) {
  102. return AdmResponse.failure("必填项:equipId(设备id)、floorIdList(楼层Id列表)");
  103. }
  104. List<RelationDTO> voList = new ArrayList<>();
  105. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  106. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
  107. String graphCode = typeEnum.getGraphCode();
  108. String relCode = typeEnum.getRelCode();
  109. deleteByObjId(equipId, null, typeEnum, context);
  110. for (String floorId : floorIdList) {
  111. RelationDTO relation = new RelationDTO(null, graphCode, relCode, "", equipId, floorId);
  112. voList.add(relation);
  113. }
  114. service.doSave(context, voList);
  115. return AdmResponse.success();
  116. }
  117. /**
  118. * 根据对象删除设备服务于楼层的关系,只针对一个对象
  119. * 必填项:EquipId(设备id)、FloorId(楼层id)
  120. * @param eqForFl 对象
  121. * @return 删除的结果信息
  122. */
  123. @PostMapping(value = "/unlink")
  124. public AdmResponse delete(@RequestBody EqForFl eqForFl){
  125. String floorId = eqForFl.getFloorId();
  126. String equipId = eqForFl.getEquipId();
  127. if (StrUtil.isEmpty(floorId) || StrUtil.isEmpty(equipId)) {
  128. return AdmResponse.failure("必填项:equipId(设备id)、floorId(楼层floorId)");
  129. }
  130. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  131. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2FL_FOR;
  132. deleteByObjId(equipId, floorId, typeEnum, context);
  133. return AdmResponse.success();
  134. }
  135. /**
  136. * 根据楼层Id删除
  137. *
  138. * @param typeEnum
  139. * @param context
  140. */
  141. private void deleteByObjId(String equipId, String floorId, AdmRelationTypeEnum typeEnum, InstanceUrlParam context) {
  142. QueryCriteria criteria = new QueryCriteria();
  143. ObjectNode node = JsonNodeFactory.instance.objectNode();
  144. node.put("graphCode", typeEnum.getGraphCode());
  145. node.put("relCode", typeEnum.getRelCode());
  146. node.put("objFrom", equipId);
  147. node.put("objTo", floorId);
  148. criteria.setCriteria(node);
  149. service.doDelete(context, criteria);
  150. }
  151. }