RelationSyInBdController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.persagy.proxy.relation.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.extra.spring.SpringUtil;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  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.dmp.common.constant.CommonConstant;
  10. import com.persagy.dmp.digital.client.DigitalRelationFacade;
  11. import com.persagy.dmp.digital.entity.ObjectRelation;
  12. import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
  13. import com.persagy.proxy.adm.request.AdmResponse;
  14. import com.persagy.proxy.adm.service.IAdmRelationService;
  15. import com.persagy.proxy.adm.utils.AdmContextUtil;
  16. import com.persagy.proxy.common.entity.InstanceUrlParam;
  17. import com.persagy.proxy.common.entity.RelationDTO;
  18. import com.persagy.proxy.relation.model.BdInSyList;
  19. import com.persagy.proxy.relation.model.SyInBd;
  20. import com.persagy.proxy.relation.model.SyInBdList;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.util.CollectionUtils;
  26. import org.springframework.web.bind.annotation.*;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Set;
  30. import java.util.stream.Collectors;
  31. /**
  32. * @author zhangwencheng
  33. * 系统所在建筑
  34. */
  35. @Slf4j
  36. @RestController
  37. @RequestMapping("/rel/sy-in-bd")
  38. public class RelationSyInBdController {
  39. @Autowired
  40. private IAdmRelationService service;
  41. @Value("${middleware.group.code}")
  42. private String groupCode;
  43. /**
  44. * 系统所在建筑
  45. *
  46. * @param syInBd
  47. * @param projectId
  48. * @return
  49. * @throws Exception
  50. */
  51. @PostMapping("/link")
  52. public AdmResponse create(@RequestBody SyInBd syInBd) throws Exception {
  53. log.info("创建系统所在建筑关系create方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
  54. //系统Id
  55. String sysID = syInBd.getSysId();
  56. //建筑Id
  57. String buildingId = syInBd.getBuildingId();
  58. if (StrUtil.isBlank(sysID) || StrUtil.isBlank(buildingId)) {
  59. return AdmResponse.failure("必填项:SysId(系统id)、BuildingId(建筑id)");
  60. }
  61. List<RelationDTO> voList = new ArrayList<>();
  62. AdmRelationTypeEnum sy2bdMechinarch = AdmRelationTypeEnum.SY2BD_MECHINARCH;
  63. String graphCode = sy2bdMechinarch.getGraphCode();
  64. String relCode = sy2bdMechinarch.getRelCode();
  65. RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysID, buildingId);
  66. voList.add(relation);
  67. // 组装上下文条件
  68. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  69. service.doSave(context, voList);
  70. log.info("创建系统所在建筑create方法结束");
  71. return AdmResponse.success();
  72. }
  73. // /**
  74. // * 系统和楼层的关系对象
  75. // *
  76. // * @param syInFlList
  77. // * @param projectId
  78. // * @return
  79. // * @throws Exception
  80. // */
  81. // @PostMapping("/link-fl")
  82. // public AdmResponse createSy(@RequestBody SyInFlList syInFlList, @RequestParam("projectId") String projectId) throws Exception {
  83. // log.info("创建系统所在建筑关系create方法开始参数projectId为{}", projectId);
  84. // //系统Id
  85. // String sysID = syInFlList.getSysId();
  86. // //楼层
  87. // List<String> floors = syInFlList.getFloorIdList();
  88. // if (StringUtils.isBlank(sysID) || CollectionUtils.isEmpty(floors)) {
  89. // return AdmResponse.failure("必填项:SysId(系统id)、floorIdList(楼层)");
  90. // }
  91. // List<RelationDTO> voList = new ArrayList<>();
  92. // for (String floorId : floors) {
  93. // //todo graphCode
  94. // RelationDTO relation = new RelationDTO(null, "MechInArch", "Eq2Sp", null, sysID, floorId);
  95. // voList.add(relation);
  96. // }
  97. // // 组装上下文条件
  98. // InstanceUrlParam context = new InstanceUrlParam(groupCode, null, projectId, AdmCommonConstant.APP_ID);
  99. // service.doSave(context, voList);
  100. // log.info("创建系统服务于业务空间关系create方法结束");
  101. // return AdmResponse.success();
  102. // }
  103. /**
  104. * 创建系统所在建筑关系
  105. * @param syInFlList: 请求参数
  106. * @return
  107. * @throws Exception
  108. */
  109. @PostMapping("/link-bd")
  110. public AdmResponse createBd(@RequestBody SyInBdList syInFlList) throws Exception {
  111. log.debug("创建系统所在建筑关系createBD方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
  112. // 原来的逻辑:2021年11月2日14:59:13 lijie修改
  113. // 1.删除图类型MechInArch,边类型为Sy2Bd,系统id为sysID的关系
  114. // 2.如果建筑id列表不为空,则创建对应的关系进行保存
  115. //系统Id
  116. String sysID = syInFlList.getSysId();
  117. //楼层
  118. List<String> buildIds = syInFlList.getBuildingIdList();
  119. if (StringUtils.isBlank(sysID)) {
  120. return AdmResponse.success();
  121. }
  122. // 1.1 根据图类型MechInArch,边类型为Sy2Bd,系统id为sysID查询有效关系列表
  123. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  124. List<ObjectRelation> objectRelations =
  125. queryObjectRelationsByGraphCodeAndRelCodeAndSystemId(AdmRelationTypeEnum.SY2BD_MECHINARCH.getGraphCode(),
  126. AdmRelationTypeEnum.SY2BD_MECHINARCH.getRelCode(), sysID);
  127. if (CollUtil.isNotEmpty(objectRelations)){
  128. // 1.2 收集关系的主键列表
  129. List<String> relIds = objectRelations.stream()
  130. .filter(objectRelation -> StrUtil.isNotBlank(objectRelation.getId()))
  131. .map(ObjectRelation::getId).collect(Collectors.toList());
  132. // 1.3 删除关系
  133. DigitalRelationFacade.delete(context.getGroupCode(),context.getProjectId(),
  134. context.getAppId(),context.getUserId(),relIds);
  135. }
  136. if (CollUtil.isEmpty(buildIds)){
  137. return AdmResponse.success(new ArrayList<>());
  138. }
  139. List<RelationDTO> voList = new ArrayList<>();
  140. for (String buildId : buildIds) {
  141. RelationDTO relation = new RelationDTO(StrUtil
  142. .format(CommonConstant.DEFAULT_GRAPH_ID_FORMAT,AdmRelationTypeEnum.SY2BD_MECHINARCH.getGraphCode()),
  143. AdmRelationTypeEnum.SY2BD_MECHINARCH.getGraphCode(),
  144. AdmRelationTypeEnum.SY2BD_MECHINARCH.getRelCode(), null, sysID, buildId);
  145. voList.add(relation);
  146. }
  147. service.doSave(context, voList);
  148. log.debug("创建系统所在建筑关系createBD方法结束");
  149. return AdmResponse.success();
  150. }
  151. /**
  152. * 创建系统所在建筑关系 同上
  153. * @param syInFlList:请求参数
  154. * @return
  155. * @throws Exception
  156. */
  157. @PostMapping("/link-bd-sy")
  158. public AdmResponse createSyBd(@RequestBody SyInBdList syInFlList) throws Exception {
  159. log.info("创建系统所在建筑关系createSyBd方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
  160. //系统Id
  161. String sysID = syInFlList.getSysId();
  162. //楼层
  163. List<String> builds = syInFlList.getBuildingIdList();
  164. if (StringUtils.isBlank(sysID) || CollectionUtils.isEmpty(builds)) {
  165. return AdmResponse.failure("必填项:SysId(系统id)、buildingIdList(建筑Id)");
  166. }
  167. List<RelationDTO> voList = new ArrayList<>();
  168. AdmRelationTypeEnum sy2bdMechinarch = AdmRelationTypeEnum.SY2BD_MECHINARCH;
  169. String graphCode = sy2bdMechinarch.getGraphCode();
  170. String relCode = sy2bdMechinarch.getRelCode();
  171. for (String buildId : builds) {
  172. RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysID, buildId);
  173. voList.add(relation);
  174. }
  175. // 组装上下文条件
  176. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  177. service.doSave(context, voList);
  178. log.info("创建系统所在建筑关系createSyBd方法结束");
  179. return AdmResponse.success();
  180. }
  181. /**
  182. * 创建所在建筑的系统关系
  183. * @param bdInSyList
  184. * @param projectId
  185. * @return
  186. * @throws Exception
  187. */
  188. @PostMapping("/link-sy")
  189. public AdmResponse createSyBd(@RequestBody BdInSyList bdInSyList) throws Exception {
  190. log.info("创建系统所在建筑关系createSyBd方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
  191. //建筑Id
  192. String buildingId = bdInSyList.getBuildingId();
  193. //系统
  194. List<String> sysIdList = bdInSyList.getSysIdList();
  195. if (StringUtils.isBlank(buildingId) || CollectionUtils.isEmpty(sysIdList)) {
  196. return AdmResponse.failure("必填项:buildingId(建筑id)、sysIdList(系统Id)");
  197. }
  198. List<RelationDTO> voList = new ArrayList<>();
  199. AdmRelationTypeEnum sy2bdMechinarch = AdmRelationTypeEnum.SY2BD_MECHINARCH;
  200. String graphCode = sy2bdMechinarch.getGraphCode();
  201. String relCode = sy2bdMechinarch.getRelCode();
  202. for (String sysId : sysIdList) {
  203. RelationDTO relation = new RelationDTO(null, graphCode, relCode, null, sysId, buildingId);
  204. voList.add(relation);
  205. }
  206. // 组装上下文条件
  207. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  208. service.doSave(context, voList);
  209. log.info("创建系统所在建筑关系createSyBd方法结束");
  210. return AdmResponse.success();
  211. }
  212. /**
  213. * 系统所在建筑
  214. *
  215. * @param syInBd
  216. * @param projectId
  217. * @return
  218. * @throws Exception
  219. */
  220. @PostMapping("/unlink")
  221. public AdmResponse delete(@RequestBody SyInBd syInBd) throws Exception {
  222. log.info("创建系统所在建筑关系delete方法开始参数projectId为{}", AdmContextUtil.toDmpContext().getProjectId());
  223. //系统Id
  224. String sysID = syInBd.getSysId();
  225. //建筑Id
  226. String buildingId = syInBd.getBuildingId();
  227. if (StrUtil.isBlank(sysID) || StrUtil.isBlank(buildingId)) {
  228. return AdmResponse.failure("必填项:SysId(系统id)、BuildingId(建筑id)");
  229. }
  230. QueryCriteria criteria = new QueryCriteria();
  231. // RelationDTO relation = new RelationDTO(null, "MechInArch", "Eq2Sp", null, sysID, buildingId);
  232. AdmRelationTypeEnum sy2bdMechinarch = AdmRelationTypeEnum.SY2BD_MECHINARCH;
  233. String graphCode = sy2bdMechinarch.getGraphCode();
  234. String relCode = sy2bdMechinarch.getRelCode();
  235. // 组装上下文条件
  236. ObjectNode node = JsonNodeFactory.instance.objectNode();
  237. node.put("graphCode", graphCode);
  238. node.put("relCode", relCode);
  239. // node.put("relValue", );
  240. node.put("objFrom", sysID);
  241. node.put("objTo", buildingId);
  242. criteria.setCriteria(node);
  243. // 组装上下文条件
  244. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  245. service.doDelete(context,criteria);
  246. log.info("创建系统服务于业务空间关系delete方法结束");
  247. return AdmResponse.success();
  248. }
  249. /***
  250. * Description: 根据图类型,边类型,系统id查询有效关系
  251. * @param graphCode : 图类型
  252. * @param relCode : 边类型
  253. * @param systemId : 系统id
  254. * @return : void
  255. * @author : lijie
  256. * @date :2021/11/2 15:07
  257. * Update By lijie 2021/11/2 15:07
  258. */
  259. private List<ObjectRelation> queryObjectRelationsByGraphCodeAndRelCodeAndSystemId(String graphCode, String relCode, String systemId) {
  260. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  261. QueryCriteria queryCriteria = new QueryCriteria();
  262. ObjectMapper mapper = SpringUtil.getBean(ObjectMapper.class);
  263. ObjectNode criteria = mapper.createObjectNode();
  264. criteria.put("graphCode",graphCode);
  265. criteria.put("relCode",relCode);
  266. criteria.put("objFrom",systemId);
  267. criteria.put("valid",Boolean.TRUE);
  268. queryCriteria.setCriteria(criteria);
  269. // 根据systemId,图类型,边类型查询有效的数据
  270. return DigitalRelationFacade.query(context.getGroupCode(), context.getProjectId(),
  271. context.getAppId(), context.getUserId(), queryCriteria);
  272. }
  273. }