RelationShContainSpBaseController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.persagy.proxy.relation.controller;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. import cn.hutool.core.comparator.CompareUtil;
  5. import cn.hutool.extra.spring.SpringUtil;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.persagy.dmp.digital.client.DigitalRelationFacade;
  8. import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
  9. import org.checkerframework.checker.units.qual.C;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  20. import com.fasterxml.jackson.databind.node.ObjectNode;
  21. import com.google.common.collect.Sets;
  22. import com.persagy.dmp.basic.dto.RequestData;
  23. import com.persagy.dmp.basic.model.QueryCriteria;
  24. import com.persagy.dmp.common.context.AppContext;
  25. import com.persagy.dmp.define.entity.ObjectTypeDefine;
  26. import com.persagy.dmp.digital.client.DigitalObjectFacade;
  27. import com.persagy.dmp.digital.entity.ObjectDigital;
  28. import com.persagy.dmp.digital.entity.ObjectRelation;
  29. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  30. import com.persagy.proxy.adm.constant.AdmDictCategoryEnum;
  31. import com.persagy.proxy.adm.constant.AdmObjectType;
  32. import com.persagy.proxy.adm.request.AdmResponse;
  33. import com.persagy.proxy.adm.service.IAdmRelationService;
  34. import com.persagy.proxy.adm.utils.AdmContextUtil;
  35. import com.persagy.proxy.common.client.DmpRwdClient;
  36. import com.persagy.proxy.common.entity.InstanceUrlParam;
  37. import com.persagy.proxy.common.entity.RelationDTO;
  38. import com.persagy.proxy.report.service.IRelationReportService;
  39. import cn.hutool.core.collection.CollUtil;
  40. import cn.hutool.core.collection.CollectionUtil;
  41. import cn.hutool.core.util.ObjectUtil;
  42. import cn.hutool.core.util.StrUtil;
  43. /**
  44. * @description:023、关系-竖井下的业务空间
  45. * @author:lgy
  46. * @data:2021/9/13 14:49
  47. */
  48. @RestController
  49. @RequestMapping("/rel/sh-contain-sp-base")
  50. public class RelationShContainSpBaseController {
  51. @Autowired
  52. private IAdmRelationService service;
  53. @Autowired
  54. DmpRwdClient rwdClient;
  55. @Value("${middleware.group.code}")
  56. private String groupCode;
  57. @Autowired
  58. private IRelationReportService relationReportService;
  59. /**
  60. * 默认图类型
  61. */
  62. public static String DEFAULT_GRAPH_CODE = "ArchSubset";
  63. /**
  64. * 默认边类型
  65. */
  66. public static String DEFAULT_REL_CODE = "Sh2Sp";
  67. /**
  68. * 创建竖井下的业务空间关系
  69. * @param param 竖井和业务空间的关系对象
  70. * @return
  71. * @throws Exception
  72. */
  73. @PostMapping(value = {"/link"})
  74. public AdmResponse create(@RequestBody JSONObject param) throws Exception {
  75. String shaftId = param.getString("shaftId");
  76. String spaceId = param.getString("spaceId");
  77. String type = param.getString("objectType");
  78. if(!StrUtil.isAllNotEmpty(shaftId,spaceId)) {
  79. return AdmResponse.failure("必填项:ShaftID(竖井id)、SpaceID(业务空间id)");
  80. }
  81. // 创建关系对象
  82. List<RelationDTO> voList = new ArrayList<>();
  83. voList.add(new RelationDTO(null, DEFAULT_GRAPH_CODE, DEFAULT_REL_CODE, type, shaftId , spaceId));
  84. service.doSave(AdmContextUtil.toDmpContext(), voList);
  85. return AdmResponse.success();
  86. }
  87. /**
  88. * 根据对象删除关系
  89. *根据对象删除竖井下的业务空间关系,只针对一个对象
  90. * @param param 对象
  91. * @param type 空间类型
  92. * @return 删除的结果信息
  93. */
  94. @PostMapping("/unlink")
  95. public AdmResponse delete(@RequestParam(value = "type", required = false) String type,
  96. @RequestBody JSONObject param) throws Exception {
  97. if(CollUtil.isEmpty(param)) {
  98. return AdmResponse.success();
  99. }
  100. // 组装上下文条件
  101. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  102. // 组装条件
  103. QueryCriteria criteria = new QueryCriteria();
  104. String shaftId = param.getString("shaftId");
  105. String spaceId = param.getString("spaceId");
  106. if(!StrUtil.isAllNotEmpty(shaftId,spaceId)) {
  107. return AdmResponse.failure("必填项:ShaftID(竖井id)、SpaceID(业务空间id)");
  108. }
  109. ObjectNode node = JsonNodeFactory.instance.objectNode();
  110. node.put("graphCode", DEFAULT_GRAPH_CODE);
  111. node.put("relCode", DEFAULT_REL_CODE);
  112. node.put("relValue", type);
  113. node.put("objFrom", shaftId);
  114. node.put("objTo",spaceId);
  115. criteria.setCriteria(node);
  116. service.doDelete(context, criteria);
  117. return AdmResponse.success();
  118. }
  119. /**
  120. * 创建竖井下的业务空间关系
  121. *
  122. * @param param 竖井和业务空间的关系对象
  123. * @param type 空间类型
  124. * @return 创建的结果信息
  125. */
  126. @PostMapping("/link-shsps")
  127. public AdmResponse createShsps(@RequestParam(value = "type", required = false) String type,
  128. @RequestBody JSONObject param) {
  129. String shaftId = param.getString("shaftId");
  130. JSONArray spaceIdList = param.getJSONArray("spaceIdList");
  131. if(StrUtil.isBlank(shaftId) || CollUtil.isEmpty(spaceIdList)) {
  132. return AdmResponse.success("空间类型和空间列表不能为空");
  133. }
  134. // 创建关系对象.先删除,后添加
  135. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  136. List<RelationDTO> voList = new ArrayList<>();
  137. for(int i = 0;i < spaceIdList.size();i++) {
  138. String spaceId = spaceIdList.getString(i);
  139. if(StrUtil.isBlank(spaceId)) {
  140. continue;
  141. }
  142. QueryCriteria queryRequest = new QueryCriteria();
  143. ObjectNode node = JsonNodeFactory.instance.objectNode();
  144. node.put("graphCode", DEFAULT_GRAPH_CODE);
  145. node.put("relCode", DEFAULT_REL_CODE);
  146. node.put("relValue", type);
  147. node.put("objFrom", shaftId);
  148. node.put("objTo", spaceId);
  149. queryRequest.setCriteria(node);
  150. service.doDelete(context,queryRequest);
  151. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  152. voList.add(new RelationDTO(null, DEFAULT_GRAPH_CODE, DEFAULT_REL_CODE, type, shaftId, spaceId));
  153. }
  154. // 组装上下文条件
  155. service.doSave(context, voList);
  156. return AdmResponse.success();
  157. }
  158. /**
  159. * 竖井下的业务空间,空间一对多
  160. * @param param 竖井和业务空间的关系对象
  161. * @param type 空间类型
  162. * @return 创建的结果信息
  163. */
  164. @PostMapping("/link-spshs")
  165. public AdmResponse createSpshs(@RequestParam(value = "type", required = false) String type,
  166. @RequestBody JSONObject param) {
  167. String spaceId = param.getString("spaceId");
  168. JSONArray shaftIdList = param.getJSONArray("shaftIdList");
  169. if(StrUtil.isBlank(spaceId) || CollUtil.isEmpty(shaftIdList)) {
  170. return AdmResponse.failure("必填项:SpaceId(业务空间id)、ShaftIDList(竖井id列表)");
  171. }
  172. // 创建关系对象.先删除,后添加
  173. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  174. List<RelationDTO> voList = new ArrayList<>();
  175. for(int i = 0;i < shaftIdList.size();i++) {
  176. String shaftId = shaftIdList.getString(i);
  177. if(StrUtil.isBlank(shaftId)) {
  178. continue;
  179. }
  180. QueryCriteria queryRequest = new QueryCriteria();
  181. ObjectNode node = JsonNodeFactory.instance.objectNode();
  182. node.put("graphCode", DEFAULT_GRAPH_CODE);
  183. node.put("relCode", DEFAULT_REL_CODE);
  184. node.put("relValue", type);
  185. node.put("objFrom", shaftId);
  186. node.put("objTo", spaceId);
  187. queryRequest.setCriteria(node);
  188. service.doDelete(context,queryRequest);
  189. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  190. voList.add(new RelationDTO(null, DEFAULT_GRAPH_CODE, DEFAULT_REL_CODE, type, shaftId, spaceId));
  191. }
  192. // 组装上下文条件
  193. service.doSave(context, voList);
  194. return AdmResponse.success();
  195. }
  196. /**
  197. * 创建竖井下的业务空间,竖井一对多,此方法会覆盖以前的记录
  198. *
  199. * @param param 竖井和业务空间的关系对象
  200. * @param type 空间类型
  201. * @return 创建的结果信息
  202. */
  203. @PostMapping("/link-shsps-replace")
  204. public AdmResponse createShSpsReplace(@RequestParam(value = "Type", required = false) String type,
  205. @RequestParam(value = "BuildingId", required = false) String buildingId,
  206. @RequestParam(value = "FloorId", required = false) String floorId,
  207. @RequestBody JSONObject param) throws Exception {
  208. String shaftId = param.getString("shaftId");
  209. JSONArray spaceIdList = param.getJSONArray("spaceIdList");
  210. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  211. // 查出和当前竖井关联并为当前空间类型的的业务空间id
  212. QueryCriteria queryRequestTmp = new QueryCriteria();
  213. ObjectNode nodeTmp = JsonNodeFactory.instance.objectNode();
  214. nodeTmp.put("graphCode", DEFAULT_GRAPH_CODE);
  215. nodeTmp.put("relCode", DEFAULT_REL_CODE);
  216. nodeTmp.put("relValue", type);
  217. nodeTmp.put("objFrom", shaftId);
  218. queryRequestTmp.setCriteria(nodeTmp);
  219. Set<String> oldSpaceIds = service.queryByCondition(context, queryRequestTmp).stream().map(ObjectRelation::getObjTo).collect(Collectors.toSet());
  220. // 楼层、建筑、对象类查询的空间id
  221. RequestData requestData = RequestData.builder().buildingId(buildingId).classCode(type)
  222. .floorId(floorId).projectId(context.getProjectId()).objType(AdmDictCategoryEnum.SPACE.getValue()).build();
  223. List<ObjectDigital> objectDigitals = DigitalObjectFacade.queryObjectListSuperiorId(context.getGroupCode(),
  224. context.getProjectId(), AdmCommonConstant.APP_ID, context.getUserId(), requestData);
  225. Set<String> spaceIdSet = objectDigitals.stream().map(ObjectDigital::getId).collect(Collectors.toSet());
  226. //空间id取交集
  227. if(ObjectUtil.isNotEmpty(oldSpaceIds)){
  228. spaceIdSet.retainAll(oldSpaceIds);
  229. }
  230. //删除空间关系
  231. for (String oldSpaceId : spaceIdSet) {
  232. QueryCriteria queryRequest = new QueryCriteria();
  233. ObjectNode node = JsonNodeFactory.instance.objectNode();
  234. node.put("graphCode", DEFAULT_GRAPH_CODE);
  235. node.put("relCode", DEFAULT_REL_CODE);
  236. node.put("relValue", type);
  237. node.put("objFrom", shaftId);
  238. node.put("objTo", oldSpaceId);
  239. queryRequest.setCriteria(node);
  240. service.doDelete(context,queryRequest);
  241. }
  242. //替换空间关系
  243. if (CollectionUtil.isNotEmpty(spaceIdList)) {
  244. List<RelationDTO> voList = new ArrayList<>();
  245. for(int i = 0;i < spaceIdList.size();i++) {
  246. String spaceId = spaceIdList.getString(i);
  247. if(StrUtil.isBlank(spaceId)) {
  248. continue;
  249. }
  250. QueryCriteria queryRequest = new QueryCriteria();
  251. ObjectNode node = JsonNodeFactory.instance.objectNode();
  252. node.put("graphCode", DEFAULT_GRAPH_CODE);
  253. node.put("relCode", DEFAULT_REL_CODE);
  254. node.put("relValue", type);
  255. node.put("objFrom", shaftId);
  256. node.put("objTo", spaceId);
  257. queryRequest.setCriteria(node);
  258. service.doDelete(context,queryRequest);
  259. // 创建关系对象 - 参考(datasyn中DataCenterSync.kt)
  260. voList.add(new RelationDTO(null, DEFAULT_GRAPH_CODE, DEFAULT_REL_CODE, type, shaftId, spaceId));
  261. }
  262. // 组装上下文条件
  263. service.doSave(context, voList);
  264. }
  265. return AdmResponse.success();
  266. }
  267. /**
  268. * 查询空间类型和名称
  269. * @param shaftId shaftId 竖井id
  270. * @param param param
  271. * @return {@link AdmResponse}
  272. * @author lgy
  273. * @date 2021/9/23 18:37
  274. */
  275. @PostMapping("/space-type-query")
  276. public AdmResponse spaceTypeQuery(@RequestParam(value = "shaftId", required = false) String shaftId,
  277. @RequestBody JSONObject param) throws Exception {
  278. // 组装上下文条件
  279. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  280. if (StrUtil.isBlank(shaftId)) {
  281. shaftId = param.getString("shaftId");
  282. }
  283. String groupCode = AppContext.getContext().getGroupCode();
  284. String projectId = AppContext.getContext().getProjectId();
  285. List<ObjectTypeDefine> classList = this.relationReportService.queryClassList(groupCode, projectId,
  286. null, Sets.newHashSet(AdmObjectType.SPACE.getIndex()), null);
  287. if (CollectionUtil.isEmpty(classList)) {
  288. return AdmResponse.success();
  289. }
  290. QueryCriteria queryCriteria = new QueryCriteria();
  291. ObjectMapper objectMapper = SpringUtil.getBean(ObjectMapper.class);
  292. ObjectNode objectNode = objectMapper.createObjectNode();
  293. objectNode.put("graphCode", AdmRelationTypeEnum.SH2SP.getGraphCode());
  294. objectNode.put("relCode", AdmRelationTypeEnum.SH2SP.getRelCode());
  295. objectNode.put("objFrom", shaftId);
  296. objectNode.put("valid", Boolean.TRUE);
  297. queryCriteria.setCriteria(objectNode);
  298. List<ObjectRelation> objectRelations = DigitalRelationFacade.query(context.getGroupCode(),
  299. context.getProjectId(), context.getAppId(), context.getUserId(), queryCriteria);
  300. if (CollUtil.isEmpty(objectRelations)){
  301. return AdmResponse.success(new ArrayList<>());
  302. }
  303. Set<String> filterSpaceTypes = objectRelations.stream()
  304. .filter(objectRelation -> StrUtil.isNotBlank(objectRelation.getRelValue()))
  305. .map(ObjectRelation::getRelValue).collect(Collectors.toSet());
  306. if (CollUtil.isEmpty(filterSpaceTypes)){
  307. return AdmResponse.success(new ArrayList<>());
  308. }
  309. List<JSONObject> result = new ArrayList<>();
  310. for (ObjectTypeDefine objectTypeDefine : classList) {
  311. if (!filterSpaceTypes.contains(objectTypeDefine.getCode())){
  312. continue;
  313. }
  314. JSONObject temp = new JSONObject();
  315. temp.put("objectType", objectTypeDefine.getCode());
  316. temp.put("objectTypeName", objectTypeDefine.getName());
  317. result.add(temp);
  318. }
  319. // 根据分区名称排序
  320. result = result.stream().sorted(CompareUtil.comparingPinyin(obj -> obj.getString("objectTypeName"))).collect(Collectors.toList());
  321. return AdmResponse.success(result);
  322. }
  323. }