AdmShaftController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package com.persagy.proxy.adm.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.json.JSONUtil;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.node.ArrayNode;
  8. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  9. import com.fasterxml.jackson.databind.node.ObjectNode;
  10. import com.persagy.dmp.basic.dto.RequestData;
  11. import com.persagy.dmp.basic.model.QueryCriteria;
  12. import com.persagy.dmp.basic.utils.JsonNodeUtils;
  13. import com.persagy.dmp.common.model.entity.BaseEntity;
  14. import com.persagy.dmp.define.client.DigitalObjectInfoFacade;
  15. import com.persagy.dmp.define.entity.ObjectInfoDefine;
  16. import com.persagy.dmp.digital.client.DigitalObjectFacade;
  17. import com.persagy.dmp.digital.client.DigitalRelationFacade;
  18. import com.persagy.dmp.simple.client.DigitalObjectSimpleFacade;
  19. import com.persagy.proxy.adm.model.*;
  20. import com.persagy.proxy.adm.request.AdmCreateRequest;
  21. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  22. import com.persagy.proxy.adm.request.AdmResponse;
  23. import com.persagy.proxy.adm.service.IAdmBuildingService;
  24. import com.persagy.proxy.adm.service.IAdmRelationService;
  25. import com.persagy.proxy.adm.service.IAdmShaftService;
  26. import com.persagy.proxy.adm.service.IAdmSpaceService;
  27. import com.persagy.proxy.adm.utils.AdmContextUtil;
  28. import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
  29. import com.persagy.proxy.adm.utils.ObjectNameUtil;
  30. import com.persagy.proxy.common.entity.RelationDTO;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.web.bind.annotation.PostMapping;
  33. import org.springframework.web.bind.annotation.RequestBody;
  34. import org.springframework.web.bind.annotation.RequestMapping;
  35. import org.springframework.web.bind.annotation.RestController;
  36. import javax.ws.rs.QueryParam;
  37. import java.util.*;
  38. import java.util.stream.Collectors;
  39. /**
  40. * Adm 竖井 API
  41. * @author lvxy
  42. * @date 2021/8/16
  43. */
  44. @RestController
  45. @RequestMapping("/object/shaft")
  46. public class AdmShaftController {
  47. @Autowired
  48. private IAdmShaftService service;
  49. @Autowired
  50. private IAdmRelationService relationService;
  51. @Autowired
  52. private IAdmBuildingService buildingService;
  53. @Autowired
  54. private IAdmSpaceService spaceService;
  55. @Autowired
  56. private ObjectMapper objectMapper;
  57. /**
  58. * 统计
  59. *
  60. * @param request
  61. * @return
  62. * @throws Exception
  63. */
  64. @PostMapping("/count")
  65. public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception {
  66. request.setName("shaft");
  67. request.setOnlyCount(true);
  68. return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmShaft.class);
  69. }
  70. /**
  71. * 创建
  72. * @param createRequest
  73. * @return
  74. * @throws Exception
  75. */
  76. @PostMapping("/create")
  77. public AdmResponse create(@RequestBody AdmCreateRequest<AdmShaft> createRequest) throws Exception {
  78. List<AdmShaft> vos = CollUtil.newArrayList(createRequest.getContent());
  79. List<AdmShaft> unRelationShaft = new ArrayList<>();
  80. List<AdmShaft> admShafts = new ArrayList<>(vos.size());
  81. List<RelationDTO> relationDTOS = new ArrayList<>(vos.size());
  82. vos.stream().forEach(admShaft -> {
  83. if (StrUtil.isEmpty(admShaft.getName())) {
  84. admShaft.setName(ObjectNameUtil.objectName("竖井-"));
  85. }
  86. if(CollUtil.isEmpty(admShaft.getBuildingList())){
  87. unRelationShaft.add(admShaft);
  88. }else{
  89. //插入对象 添加关系
  90. List<AdmShaft> shafts = service.doInsert(AdmContextUtil.toDmpContext(), AdmShaft.class, CollUtil.newArrayList(admShaft));
  91. admShaft.getBuildingList().stream().forEach(admBuilding ->{
  92. if(StrUtil.isNotEmpty(admBuilding.getId())){
  93. relationDTOS.add(new RelationDTO(null, "ArchSubset", "Bd2Sh", null , admBuilding.getId(), shafts.get(0).getId()));
  94. }
  95. });
  96. admShafts.addAll(shafts);
  97. }
  98. });
  99. if(CollUtil.isNotEmpty(relationDTOS)){
  100. relationService.doSave(AdmContextUtil.toDmpContext(), relationDTOS);
  101. }
  102. if(CollUtil.isNotEmpty(unRelationShaft)){
  103. List shaftList = service.doInsert(AdmContextUtil.toDmpContext(), AdmShaft.class, unRelationShaft);
  104. admShafts.addAll(shaftList);
  105. }
  106. return AdmResponse.success(admShafts);
  107. }
  108. /**
  109. * 根据ID删除
  110. * @param vos
  111. * @return
  112. * @throws Exception
  113. */
  114. @PostMapping("/delete")
  115. public AdmResponse delete( @RequestBody List<AdmShaft> vos) throws Exception {
  116. service.doDelete(AdmContextUtil.toDmpContext(), vos);
  117. return AdmResponse.success();
  118. }
  119. /**
  120. * 更新
  121. * @param createRequest
  122. * @return
  123. * @throws Exception
  124. */
  125. @PostMapping("/update")
  126. public AdmResponse update(@RequestBody AdmCreateRequest<AdmShaft> createRequest) throws Exception {
  127. List<AdmShaft> vos = CollUtil.newArrayList(createRequest.getContent());
  128. vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmShaft.class, vos);
  129. return AdmResponse.success(vos);
  130. }
  131. /**
  132. * 查询
  133. *
  134. * @param request
  135. * @return
  136. * @throws Exception
  137. */
  138. @PostMapping("/query")
  139. public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception {
  140. request.setName(AdmShaft.OBJ_TYPE);
  141. return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmShaft.class);
  142. }
  143. /**
  144. * 查询竖井信息-包含统计信息
  145. * 此方法可能会弃用
  146. *
  147. * 暂不实现
  148. *
  149. * @param request
  150. * @return
  151. * @throws Exception
  152. */
  153. @PostMapping("/query-with-count")
  154. public AdmResponse queryWithCount(@RequestBody AdmQueryCriteria request) throws Exception {
  155. request.setName("shaft");
  156. return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmShaft.class);
  157. }
  158. /**
  159. * 查询关联元空间
  160. * 可能移至关系
  161. *
  162. * @param request
  163. * @return
  164. * @throws Exception
  165. */
  166. @PostMapping("/relate-space")
  167. public AdmResponse queryRelateSp(@RequestBody JSONObject request) throws Exception {
  168. //Sh2Sp
  169. // 先取消后重新建立
  170. if(JSONUtil.isNull(request)){
  171. return AdmResponse.failure("参数为空");
  172. }
  173. String shaftId = request.getString("shaftId");
  174. String spaceId = request.getString("spaceId");
  175. // 组装条件
  176. QueryCriteria criteria = new QueryCriteria();
  177. if(!StrUtil.isAllNotEmpty(shaftId,spaceId)) {
  178. return AdmResponse.failure("必填项:shaftId(竖井id)、spaceId(空间id)");
  179. }
  180. ObjectNode node = JsonNodeFactory.instance.objectNode();
  181. node.put("graphCode", "ArchSubset");
  182. node.put("relCode", "Sh2Sp");
  183. node.put("objFrom", shaftId);
  184. criteria.setCriteria(node);
  185. relationService.doDelete(AdmContextUtil.toDmpContext(), criteria);
  186. List<String> spaceIds = Arrays.asList(spaceId.split(","));
  187. Set<String> existsIds = spaceExists(spaceIds);
  188. List<String> failureList = null;
  189. if(CollUtil.isNotEmpty(existsIds)){
  190. failureList = spaceIds.stream().filter(isId -> !existsIds.contains(isId)).collect(Collectors.toList());
  191. List<RelationDTO> relationDTOS = new ArrayList<>();
  192. existsIds.stream().forEach(spId -> {
  193. RelationDTO relationDTO = new RelationDTO();
  194. relationDTO.setGraphCode("ArchSubset");
  195. relationDTO.setObjFrom(shaftId);
  196. relationDTO.setRelCode("Sh2Sp");
  197. relationDTO.setObjTo(spId);
  198. relationDTOS.add(relationDTO);
  199. });
  200. List<ObjectNode> nodeList = JsonNodeUtils.toListNode(relationDTOS, null, null);
  201. DigitalRelationFacade.create(AdmContextUtil.toDmpContext().getGroupCode(), AdmContextUtil.toDmpContext().getProjectId(),AdmContextUtil.toDmpContext().getAppId(), null, nodeList);
  202. }else{
  203. failureList = spaceIds;
  204. }
  205. if(CollUtil.isNotEmpty(failureList))
  206. return AdmResponse.failure("元空间 " +failureList.stream().collect(Collectors.joining(","))+ " 不存在");
  207. else
  208. return AdmResponse.success();
  209. }
  210. /**
  211. * 查询项目下已有的竖井类型
  212. * 此方法可能会弃用
  213. * {
  214. * "id":"shaftFuncType",
  215. * "name":"竖井功能"
  216. * }
  217. *
  218. * @return
  219. * @throws Exception
  220. */
  221. @PostMapping("/query-shaft-functype")
  222. public AdmResponse queryShFuncType() throws Exception {
  223. String projectId = AdmContextUtil.toDmpContext().getProjectId();
  224. if(StrUtil.isNotEmpty(projectId)){
  225. AdmResponse admResponse = AdmResponse.success();
  226. //查询参数请求查询的类型是否存在
  227. AdmQueryCriteria admQueryCriteria = new AdmQueryCriteria();
  228. StringBuilder filters = new StringBuilder(" code = '");
  229. filters.append("shaftFuncType").append("';");
  230. filters.append(" classCode ='").append("shaft").append(" '");
  231. admQueryCriteria.setFilters(filters.toString());
  232. QueryCriteria dmpRequest = AdmQueryCriteriaHelper.toDmpCriteria(admQueryCriteria);
  233. List<ObjectInfoDefine> defineList = DigitalObjectInfoFacade.query(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(),AdmContextUtil.toDmpContext().getAppId(),null,dmpRequest);
  234. //是否有该类型的竖井信息
  235. if(CollUtil.isNotEmpty(defineList)){
  236. //查询中台项目实际使用该信息点的竖井
  237. RequestData requestData = new RequestData();
  238. requestData.setObjType(AdmShaft.OBJ_TYPE);
  239. requestData.setGroups(Arrays.asList("shaftFuncType"));
  240. List<Map<String, Object>> listInfos = DigitalObjectSimpleFacade.queryObjectCountByObjInfos(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getAppId(),null, requestData);
  241. if(CollUtil.isNotEmpty(listInfos)){
  242. ArrayNode datasource = defineList.get(0).getDataSource();
  243. List<HashMap> datasourceList = JSONUtil.toList(datasource.toString(), HashMap.class);
  244. Map<String, String> datasourceMap = new HashMap<>(datasourceList.size());
  245. for (HashMap map : datasourceList){
  246. datasourceMap.put(map.get("code").toString(),map.get("name").toString());
  247. }
  248. Map<String, Object> datasourceMapUsed = new HashMap<>(datasourceMap.size());
  249. listInfos.stream().forEach(map -> {
  250. if(!datasourceMapUsed.containsKey(map.get("shaftFuncType")) || datasourceMap.containsKey(map.get("shaftFuncType"))){
  251. JSONObject jsonObject = new JSONObject();
  252. jsonObject.put("id", map.get("shaftFuncType").toString());
  253. jsonObject.put("name", datasourceMap.get(map.get("shaftFuncType").toString()));
  254. datasourceMapUsed.put(map.get("shaftFuncType").toString(),jsonObject);
  255. }
  256. });
  257. if(CollUtil.isNotEmpty(datasourceMapUsed)){
  258. admResponse = AdmResponse.success(datasourceMapUsed.values().stream().collect(Collectors.toList()));
  259. }
  260. }
  261. }
  262. return admResponse;
  263. }else{
  264. return AdmResponse.failure("项目Id为空");
  265. }
  266. }
  267. /**
  268. * 查询和当前竖井有关联的业务空间
  269. * shaftId 为竖井id
  270. *
  271. * @param request
  272. * @return
  273. * @throws Exception
  274. */
  275. @PostMapping("/shaft-zoneSpaceBaseTable")
  276. public AdmResponse queryZoneSp(@QueryParam("ShaftId") String ShaftId, @RequestBody AdmQueryCriteria request) throws Exception {
  277. // 组装条件
  278. request.setName(AdmSpace.OBJ_TYPE);
  279. QueryCriteria queryCriteria = AdmQueryCriteriaHelper.toDmpCriteria(request);
  280. if(!StrUtil.isNotEmpty(ShaftId)) {
  281. return AdmResponse.failure("必填项:shaftId(竖井id)");
  282. }
  283. ObjectNode criteria = JsonNodeFactory.instance.objectNode();
  284. ObjectNode relationTO = JsonNodeFactory.instance.objectNode();
  285. relationTO.put("relCode", "Sh2Sp");
  286. relationTO.put("graphCode","ArchSubset");
  287. relationTO.put("objFrom", ShaftId);
  288. criteria.putPOJO("relationTo", relationTO);
  289. queryCriteria.setCriteria(criteria);
  290. AdmResponse admResponse = spaceService.doQuery(AdmContextUtil.toDmpContext(), queryCriteria, AdmSpace.class);
  291. return admResponse;
  292. }
  293. /**
  294. * 查询和当前竖井有关联的业务空间所在建筑及楼层信息
  295. * shaftId 为竖井id
  296. *
  297. * @return
  298. * @throws Exception
  299. */
  300. @PostMapping("/shaft-zoneSpaceBaseTable-building")
  301. public AdmResponse queryZoneSpBd(@QueryParam("shaftId") String shaftId,
  302. @QueryParam("objectType") String objectType) throws Exception {
  303. if(!StrUtil.isAllNotEmpty(shaftId,AdmContextUtil.toDmpContext().getProjectId())) {
  304. return AdmResponse.failure("必填项:shaftId(竖井id)、projectId(项目id)");
  305. }
  306. // 组装条件 查询竖井下的空间
  307. AdmQueryCriteria admQueryCriteria = new AdmQueryCriteria();
  308. admQueryCriteria.setName(AdmSpace.OBJ_TYPE);
  309. if(StrUtil.isNotEmpty(objectType)){
  310. admQueryCriteria.setFilters(" classCode = '"+objectType+"'");
  311. }
  312. //转化为中台的查询条件
  313. QueryCriteria queryCriteria = AdmQueryCriteriaHelper.toDmpCriteria(admQueryCriteria);
  314. ObjectNode criteria = JsonNodeFactory.instance.objectNode();
  315. ObjectNode relationTO = JsonNodeFactory.instance.objectNode();
  316. relationTO.put("relCode", "Sh2Sp");
  317. relationTO.put("graphCode","ArchSubset");
  318. relationTO.put("objFrom", shaftId);
  319. criteria.putPOJO("relationTo", relationTO);
  320. criteria.put("objType", AdmSpace.OBJ_TYPE);
  321. queryCriteria.setCriteria(criteria);
  322. List<ObjectNode> spaceList = DigitalObjectFacade.query(AdmContextUtil.toDmpContext().getGroupCode(), AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, queryCriteria);
  323. if(CollUtil.isNotEmpty(spaceList)){
  324. AdmQueryCriteria queryCriteriaBd = new AdmQueryCriteria();
  325. queryCriteriaBd.setName(AdmBuilding.OBJ_TYPE);
  326. queryCriteriaBd.setProjection(Arrays.asList("id","name","localName","floor"));
  327. //建筑级联楼层
  328. List<AdmQueryCriteria> cascade = new ArrayList<>(1);
  329. AdmQueryCriteria cascadeFloor = new AdmQueryCriteria();
  330. cascadeFloor.setName(AdmFloor.OBJ_TYPE);
  331. cascadeFloor.setProjection(Arrays.asList("id","name","localName"));
  332. cascade.add(cascadeFloor);
  333. cascadeFloor.setOrders("floorSequenceId desc");
  334. queryCriteriaBd.setCascade(cascade);
  335. List<AdmSpace> vos = JSONUtil.toList(spaceList.toString(), AdmSpace.class);
  336. Map<String, AdmSpace> admSpaceMap = CollUtil.fieldValueMap(vos, "buildingId");
  337. StringBuilder filter = new StringBuilder(" id in ['");
  338. admSpaceMap.keySet().stream().forEach(spaceId -> filter.append(spaceId).append("','"));
  339. String filters = StrUtil.sub(filter.toString(), 0,-2)+"]";
  340. queryCriteriaBd.setFilters(filters);
  341. AdmResponse response = buildingService.doQuery(AdmContextUtil.toDmpContext(), queryCriteriaBd, AdmBuilding.class);
  342. return response;
  343. }else{
  344. return AdmResponse.success();
  345. }
  346. }
  347. /**
  348. * 查询和当前竖井没有关联的业务空间
  349. * shaftId 为竖井id
  350. *
  351. * @param request
  352. * @return
  353. * @throws Exception
  354. */
  355. @PostMapping("/un-shaft-zoneSpaceBaseTable")
  356. public AdmResponse queryUnZoneSp(@QueryParam("shaftId") String shaftId,
  357. @RequestBody AdmQueryCriteria request) throws Exception {
  358. // 组装条件
  359. QueryCriteria queryCriteria = AdmQueryCriteriaHelper.toDmpCriteria(request);
  360. if(!StrUtil.isNotEmpty(shaftId)) {
  361. return AdmResponse.failure("必填项:shaftId(竖井id)");
  362. }
  363. ObjectNode criteria = JsonNodeFactory.instance.objectNode();
  364. ObjectNode relationTO = JsonNodeFactory.instance.objectNode();
  365. ArrayNode arrayNode = relationTO.putObject("objFrom").putArray("$notcontains");
  366. arrayNode.add(shaftId);
  367. relationTO.put("relCode", "Sh2Sp");
  368. relationTO.put("graphCode","ArchSubset");
  369. criteria.putPOJO("relationTo", relationTO);
  370. criteria.put("objType", "space");
  371. queryCriteria.setCriteria(relationTO);
  372. List<ObjectNode> spaceList = DigitalObjectFacade.query(AdmContextUtil.toDmpContext().getGroupCode(), AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, queryCriteria);
  373. if(CollUtil.isNotEmpty(spaceList)){
  374. List<AdmSpace> vos = JSONUtil.toList(spaceList.toString(), AdmSpace.class);
  375. return AdmResponse.success(spaceList);
  376. }else{
  377. return AdmResponse.success();
  378. }
  379. }
  380. /**
  381. * 元空间是否存在
  382. *
  383. * @param spaceIds failureList
  384. * @return 是否存在
  385. */
  386. private Set<String> spaceExists(List<String> spaceIds) {
  387. QueryCriteria queryCriteria = new QueryCriteria();
  388. ObjectNode node = JsonNodeFactory.instance.objectNode();
  389. ArrayNode arrayNode = node.putObject("id").putArray("$in");
  390. spaceIds.stream().forEach(spaceId -> arrayNode.add(spaceId));
  391. node.put("objType","ispace");
  392. queryCriteria.setCriteria(node);
  393. List<ObjectNode> ispaces = DigitalObjectFacade.query(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(),AdmContextUtil.toDmpContext().getAppId(),AdmContextUtil.toDmpContext().getAppId(),queryCriteria);
  394. if(CollUtil.isNotEmpty(ispaces)){
  395. List<AdmISpace> iSpaceList = JSONUtil.toList(JSONUtil.parseArray(ispaces.toString()), AdmISpace.class);
  396. Map<String, AdmISpace> admISpaceMap = CollUtil.fieldValueMap(iSpaceList, BaseEntity.PROP_ID);
  397. return admISpaceMap.keySet();
  398. }else{
  399. return null;
  400. }
  401. }
  402. }