AdmSpaceController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package com.persagy.proxy.object.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.map.MapUtil;
  4. import cn.hutool.core.util.ReflectUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.fasterxml.jackson.databind.node.ArrayNode;
  9. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  10. import com.fasterxml.jackson.databind.node.ObjectNode;
  11. import com.persagy.dmp.basic.model.QueryCriteria;
  12. import com.persagy.dmp.basic.utils.JsonNodeUtils;
  13. import com.persagy.dmp.common.helper.SpringHelper;
  14. import com.persagy.dmp.digital.entity.ObjectDigital;
  15. import com.persagy.proxy.adm.constant.AdmDictConstant;
  16. import com.persagy.proxy.adm.request.*;
  17. import com.persagy.proxy.adm.service.IAdmRelationService;
  18. import com.persagy.proxy.adm.utils.AdmContextUtil;
  19. import com.persagy.proxy.adm.utils.ObjectNameUtil;
  20. import com.persagy.proxy.common.entity.RelationDTO;
  21. import com.persagy.proxy.object.model.AdmBuilding;
  22. import com.persagy.proxy.object.model.AdmFloor;
  23. import com.persagy.proxy.object.model.AdmSpace;
  24. import com.persagy.proxy.object.service.IAdmBuildingService;
  25. import com.persagy.proxy.object.service.IAdmFloorService;
  26. import com.persagy.proxy.object.service.IAdmSpaceService;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.web.bind.annotation.PostMapping;
  29. import org.springframework.web.bind.annotation.RequestBody;
  30. import org.springframework.web.bind.annotation.RequestMapping;
  31. import org.springframework.web.bind.annotation.RestController;
  32. import javax.ws.rs.QueryParam;
  33. import java.lang.reflect.Field;
  34. import java.util.*;
  35. /**
  36. * Adm 空间 API
  37. * @author lvxy
  38. * @date 2021/8/16
  39. */
  40. @RestController
  41. @RequestMapping("/object/zone")
  42. public class AdmSpaceController {
  43. @Autowired
  44. private IAdmSpaceService service;
  45. @Autowired
  46. private IAdmBuildingService buildingService;
  47. @Autowired
  48. private IAdmRelationService relationService;
  49. @Autowired
  50. private IAdmFloorService floorService;
  51. /**
  52. * 统计
  53. *
  54. * @param request
  55. * @return
  56. * @throws Exception
  57. */
  58. @PostMapping("/count")
  59. public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception {
  60. request.setName(AdmSpace.OBJ_TYPE);
  61. request.setOnlyCount(true);
  62. //return service.doQuery( AdmContextUtil.toDmpContext(), request, AdmSpace.class);
  63. return service.query(request);
  64. }
  65. /**
  66. * 创建
  67. * @param createRequest
  68. * @return
  69. * @throws Exception
  70. */
  71. @PostMapping("/create")
  72. public AdmCreateResponse create(@RequestBody AdmCreateRequest<AdmSpace> createRequest) throws Exception {
  73. List<AdmSpace> vos = CollUtil.newArrayList(createRequest.getContent());
  74. String msg = null;
  75. for (AdmSpace admSpace : vos) {
  76. if(StrUtil.isEmpty(admSpace.getClassCode())){
  77. msg = "classCode不可以为空!";
  78. break;
  79. }
  80. if (StrUtil.isEmpty(admSpace.getName())) {
  81. admSpace.setName(ObjectNameUtil.objectName("空间-"));
  82. }
  83. }
  84. if(StrUtil.isEmpty(msg)){
  85. vos = service.doInsertExt(AdmContextUtil.toDmpContext(), AdmSpace.class, vos);
  86. AdmCreateResponse admResponseObj = AdmCreateResponse.success(vos);
  87. return admResponseObj;
  88. }else{
  89. return AdmCreateResponse.failure(msg);
  90. }
  91. }
  92. /**
  93. * 根据ID删除
  94. * @param vos
  95. * @return
  96. * @throws Exception
  97. */
  98. @PostMapping("/delete")
  99. public AdmResponse delete( @RequestBody List<AdmSpace> vos) throws Exception {
  100. service.doDelete( AdmContextUtil.toDmpContext(), vos);
  101. QueryCriteria queryCriteria = new QueryCriteria();
  102. vos.forEach(vo -> {
  103. /** 清除空间和建筑楼层的的数据 */
  104. dealRelation(vo.getId(), queryCriteria,"Bd2Sp", false);
  105. dealRelation(vo.getId(), queryCriteria,"Fl2Sp", false);
  106. dealRelation(vo.getId(), queryCriteria,"Sp2Si", true);
  107. });
  108. return AdmResponse.success();
  109. }
  110. /**
  111. * 更新分区对象信息
  112. * @param createRequest
  113. * @return
  114. * @throws Exception
  115. */
  116. @PostMapping("/update")
  117. public AdmResponse update(@RequestBody AdmCreateRequest<AdmSpace> createRequest, @QueryParam("projectId") String projectId) throws Exception {
  118. List<AdmSpace> vos = CollUtil.newArrayList(createRequest.getContent());
  119. // 特殊处理一下AdmSpace的infos
  120. if (CollUtil.isEmpty(vos)){
  121. return AdmResponse.success(new ArrayList<>());
  122. }
  123. Map<String, Field> fieldMap = JsonNodeUtils.getFieldMap(AdmSpace.class);
  124. Map<String, Field> objFieldMap = JsonNodeUtils.getFieldMap(ObjectDigital.class);
  125. Set<String> fields = fieldMap.keySet();
  126. ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
  127. for (AdmSpace space : vos) {
  128. Map<String, Object> infos = space.getInfos();
  129. if (MapUtil.isEmpty(infos)){
  130. continue;
  131. }
  132. Set<String> mapKeys = infos.keySet();
  133. for (String mapKey : mapKeys) {
  134. if ((AdmDictConstant.TABLE_INFOS.contains(mapKey)
  135. && !AdmDictConstant.HEIGHT.equals(mapKey)
  136. && objFieldMap.containsKey(mapKey))
  137. || AdmDictConstant.PIC.equals(mapKey)){
  138. // 这些信息就算在infos也不更新,
  139. // 和com.persagy.proxy.dictionary.service.impl.AbstractAdmDictBaseServiceImpl.handleObjectInfoPath
  140. // 的判断逻辑保持一致
  141. continue;
  142. }
  143. try{
  144. if (fields.contains(mapKey) && infos.get(mapKey) instanceof ArrayList){
  145. ReflectUtil.setFieldValue(space,fieldMap.get(mapKey),objectMapper.readValue(objectMapper.writeValueAsString(infos.get(mapKey)), ArrayNode.class));
  146. continue;
  147. }
  148. if (fields.contains(mapKey)){
  149. // 如果属性值存在
  150. ReflectUtil.setFieldValue(space,fieldMap.get(mapKey),infos.get(mapKey));
  151. }
  152. } catch (Exception e){
  153. // 信息点传值有问题,直接移除
  154. infos.remove(mapKey);
  155. }
  156. }
  157. }
  158. return service.doUpdate(vos);
  159. }
  160. /**
  161. * 查询
  162. *
  163. * @param request
  164. * @return
  165. * @throws Exception
  166. */
  167. @PostMapping("/query")
  168. public ObjectNode query(@RequestBody AdmDCSQueryRequest request) throws Exception {
  169. AdmResponse response = service.doQuerySpaces(AdmContextUtil.toDmpContext(), request);
  170. ObjectNode objectNode = JsonNodeUtils.toObjectNode(response, null, null);
  171. if(StrUtil.isNotEmpty(request.getZoneType())){
  172. objectNode.put("zoneType", request.getZoneType());
  173. }
  174. return objectNode;
  175. /*ObjectNode criteria = AdmQueryCriteriaHelper.toCriteria(null, request.getFilters());
  176. List<OrderItem> orderItem = AdmQueryCriteriaHelper.toOrderItem(request.getOrders());
  177. return this.spaceRelationInfoHandler.querySpaceInfo(request, criteria, orderItem);*/
  178. }
  179. /**
  180. * 查询业务空间的建筑楼层,包含受影响的空间数量
  181. *
  182. * @param request
  183. * @return
  184. * @throws Exception
  185. */
  186. @PostMapping("/building-floor")
  187. public AdmResponse queryBuildingFloor(@RequestBody AdmQueryCriteria request) throws Exception {
  188. request.setName(AdmBuilding.OBJ_TYPE);
  189. AdmQueryCriteria criteriaFloor = new AdmQueryCriteria();
  190. criteriaFloor.setName(AdmFloor.OBJ_TYPE);
  191. AdmQueryCriteria criteriaSpace = new AdmQueryCriteria();
  192. criteriaSpace.setName("spaceList");
  193. criteriaSpace.setFilters("state = 1");
  194. criteriaFloor.setCascade(CollUtil.newArrayList(criteriaSpace));
  195. criteriaFloor.setOrders("floorSequenceID desc");
  196. request.setCascade(CollUtil.newArrayList(criteriaFloor));
  197. AdmResponse response = buildingService.doQueryBuildingFloor(AdmContextUtil.toDmpContext(), request);
  198. List<AdmBuilding> buildings = (List<AdmBuilding>) response.getContent();
  199. buildings.forEach(building -> {
  200. if(CollUtil.isNotEmpty(building.getFloor())){
  201. List<AdmFloor> floors = building.getFloor();
  202. floors.forEach(floor -> {
  203. if(CollUtil.isNotEmpty(floor.getSpaceList())){
  204. floor.setCount(floor.getSpaceList().size());
  205. floor.setSpaceList(null);
  206. }else{
  207. floor.setCount(0);
  208. }
  209. });
  210. }
  211. });
  212. return response;
  213. }
  214. /**
  215. * 修改业务空间对象和建筑楼层的关系
  216. * spaceId 业务空间id,id 楼层或建筑id,type 业务空间的类型
  217. * @param voList
  218. * @return
  219. * @throws Exception
  220. */
  221. @PostMapping("/update-fl-bd")
  222. public AdmResponse updateFlBd(@RequestBody List<AdmUpDataFloorAndBuildingRequests> voList, @QueryParam("projectId") String projectId) throws Exception {
  223. if(CollUtil.isEmpty(voList)){
  224. return AdmResponse.failure("不可以传入空数据");
  225. }
  226. //TODO 循环删除 需要改为 批量删除
  227. List<RelationDTO> RSpInBd_up = new ArrayList<>();
  228. List<RelationDTO> RSpInFl_up = new ArrayList<>();
  229. List<AdmSpace> space_up = new ArrayList<>();
  230. QueryCriteria queryCriteria = new QueryCriteria();
  231. voList.stream().forEach(requestData -> {
  232. String spaceId = requestData.getSpaceId();
  233. String id = requestData.getId();
  234. String type = requestData.getType();//classCode
  235. //更新实体
  236. AdmSpace space = new AdmSpace();
  237. space.setId(spaceId);
  238. space.setObjectType(AdmSpace.OBJ_TYPE);
  239. space.setClassCode(type);
  240. /** 清除空间和建筑楼层的的数据 */
  241. dealRelation(spaceId, queryCriteria,"Bd2Sp", false);
  242. dealRelation(spaceId, queryCriteria,"Fl2Sp", false);
  243. if(StrUtil.isNotEmpty(id)){
  244. if(id.startsWith("Fl") && StrUtil.isNotEmpty(spaceId)){
  245. AdmQueryCriteria admQueryCriteria = new AdmQueryCriteria();
  246. admQueryCriteria.setName(AdmFloor.OBJ_TYPE);
  247. admQueryCriteria.setFilters("id = '"+id+"'");
  248. AdmResponse admResponseFl = floorService.query(admQueryCriteria);
  249. if(CollUtil.isNotEmpty(admResponseFl.getContent())){
  250. AdmFloor floor = (AdmFloor) admResponseFl.getContent().get(0);
  251. if (floor!=null){
  252. //更新关系
  253. RelationDTO rBd_up = new RelationDTO();
  254. rBd_up.setObjTo(spaceId);
  255. rBd_up.setRelCode("Bd2Sp");
  256. rBd_up.setGraphCode("ArchSubset");
  257. rBd_up.setObjFrom(floor.getBuildingId());
  258. RSpInBd_up.add(rBd_up);
  259. RelationDTO rFl_up = new RelationDTO();
  260. rFl_up.setObjTo(spaceId);
  261. rFl_up.setRelCode("Fl2Sp");
  262. rFl_up.setGraphCode("ArchSubset");
  263. rFl_up.setObjFrom(floor.getId());
  264. RSpInFl_up.add(rFl_up);
  265. //楼层 建筑信息点维护
  266. space.setBuildingId(floor.getBuildingId());
  267. space.setFloorId(floor.getId());
  268. space_up.add(space);
  269. }
  270. }
  271. }else if(id.startsWith("Bd")){
  272. //更新关系
  273. QueryCriteria criteria = new QueryCriteria();
  274. ObjectNode node = JsonNodeFactory.instance.objectNode();
  275. //删除关系 然后重建
  276. node.put("relCode", "Bd2Sp");
  277. node.put("objTo", "spaceId");
  278. node.put("graphCode", "ArchSubset");
  279. criteria.setCriteria(node);
  280. relationService.doDelete(AdmContextUtil.toDmpContext(), criteria);
  281. RelationDTO rBd_up = new RelationDTO();
  282. rBd_up.setObjTo(spaceId);
  283. rBd_up.setRelCode("Bd2Sp");
  284. rBd_up.setGraphCode("ArchSubset");
  285. rBd_up.setObjFrom(id);
  286. RSpInBd_up.add(rBd_up);
  287. //楼层 建筑信息点维护
  288. space.setBuildingId(id);
  289. Map<String, Object> info = new HashMap<>(1);
  290. info.put("$remove",Arrays.asList("floorId"));
  291. space.setInfos(info);
  292. space_up.add(space);
  293. }
  294. }else{
  295. //楼层 建筑信息点维护 清除
  296. Map<String, Object> info = new HashMap<>(1);
  297. info.put("$remove",Arrays.asList("floorId","buildingId"));
  298. space.setInfos(info);
  299. space_up.add(space);
  300. }
  301. });
  302. service.doUpdate( AdmContextUtil.toDmpContext(), AdmSpace.class, space_up);
  303. relationService.doSave( AdmContextUtil.toDmpContext(), RSpInBd_up);
  304. relationService.doSave( AdmContextUtil.toDmpContext(), RSpInFl_up);
  305. return AdmResponse.success(space_up);
  306. }
  307. /**
  308. * 更新分区对象信息-Outline
  309. * @param createRequest
  310. * @return
  311. * @throws Exception
  312. */
  313. @PostMapping("/update-outline")
  314. public AdmResponse updateOutLine(@RequestBody AdmCreateRequest<AdmSpace> createRequest) throws Exception {
  315. List<AdmSpace> vos = CollUtil.newArrayList(createRequest.getContent());
  316. /*vos = service.doUpdate( AdmContextUtil.toDmpContext(), AdmSpace.class, vos);
  317. //更新计算关系ZoneSpaceBaseService.upDateOutline
  318. service.processRelationType(vos);
  319. return AdmResponse.success(vos);*/
  320. return service.updateOutLine(vos);
  321. }
  322. /**
  323. * 查询空间信息-outline 级联查询:建筑(building),楼层(floor),空间类型(objectType)
  324. *
  325. * @param request
  326. * @return
  327. * @throws Exception
  328. */
  329. @PostMapping("/query-outline")
  330. public ObjectNode queryOutline(@RequestBody AdmDCSQueryRequest request) throws Exception {
  331. request.setName(AdmSpace.OBJ_TYPE);
  332. request.setProjection(null);
  333. List<String> list = new ArrayList<>();
  334. list.add("id");
  335. list.add("name");
  336. list.add("localId");
  337. list.add("localName");
  338. list.add("outline");
  339. list.add("classCode");
  340. list.add("buildingId");
  341. list.add("floorId");
  342. list.add("state");
  343. list.add("floorSequenceId");
  344. list.add("modelId");
  345. list.add("infos");
  346. list.add("properties");
  347. request.setProjection(list);
  348. AdmResponse response = service.doQuerySpaces(AdmContextUtil.toDmpContext(), request);
  349. ObjectNode objectNode = JsonNodeUtils.toObjectNode(response, null, null);
  350. if(StrUtil.isNotEmpty(request.getZoneType())){
  351. objectNode.put("zoneType", request.getZoneType());
  352. }
  353. if(StrUtil.isNotEmpty(request.getFloorId())){
  354. objectNode.put("floorId", request.getFloorId());
  355. }
  356. if(StrUtil.isNotEmpty(request.getBuildingId())){
  357. objectNode.put("buildingId", request.getBuildingId());
  358. }
  359. return objectNode;
  360. }
  361. /**
  362. * 获取项目下已有的空间类型
  363. *
  364. * @return
  365. * @throws Exception
  366. */
  367. @PostMapping("/space-type")
  368. public List<JSONObject> querySpaceType() throws Exception {
  369. return service.doQuerySpaceTypes( AdmContextUtil.toDmpContext());
  370. }
  371. /**
  372. * 查询业务空间所有类型
  373. *
  374. * @param request
  375. * @return
  376. * @throws Exception
  377. */
  378. @PostMapping("/type")
  379. public ObjectNode queryType(@RequestBody AdmDCSQueryRequest request) throws Exception {
  380. AdmResponse response = service.doQueryType( AdmContextUtil.toDmpContext(), request);
  381. ObjectNode objectNode = JsonNodeUtils.toObjectNode(response, null, null);
  382. if(StrUtil.isNotEmpty(request.getZoneType())){
  383. objectNode.put("zoneType", request.getZoneType());
  384. }
  385. return objectNode;
  386. }
  387. /**
  388. * 上传EXCEL 识别镜头信息点
  389. *
  390. * @param
  391. * @return
  392. * @throws Exception
  393. */
  394. @PostMapping("/import")
  395. public AdmResponse importExcel() throws Exception {
  396. return null;
  397. }
  398. private void dealRelation(String spaceId, QueryCriteria queryCriteria, String relCode, Boolean isObjFrom){
  399. ObjectNode rBd = JsonNodeFactory.instance.objectNode();
  400. if(isObjFrom){
  401. rBd.put("objFrom",spaceId);
  402. }else{
  403. rBd.put("objTo",spaceId);
  404. }
  405. rBd.put("relCode",relCode);
  406. queryCriteria.setCriteria(rBd);
  407. relationService.doDelete( AdmContextUtil.toDmpContext(), queryCriteria);
  408. }
  409. }