AdmSpaceServiceImpl.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. package com.persagy.proxy.object.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.json.JSONUtil;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.metadata.OrderItem;
  9. import com.fasterxml.jackson.core.JsonProcessingException;
  10. import com.fasterxml.jackson.databind.ObjectMapper;
  11. import com.fasterxml.jackson.databind.node.ArrayNode;
  12. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  13. import com.fasterxml.jackson.databind.node.ObjectNode;
  14. import com.google.common.collect.Sets;
  15. import com.persagy.dmp.basic.model.QueryCriteria;
  16. import com.persagy.dmp.common.context.AppContext;
  17. import com.persagy.dmp.common.helper.SpringHelper;
  18. import com.persagy.dmp.common.model.response.CommonResult;
  19. import com.persagy.dmp.define.entity.ObjectTypeDefine;
  20. import com.persagy.dmp.digital.client.DigitalObjectFacade;
  21. import com.persagy.dmp.digital.entity.ObjectRelation;
  22. import com.persagy.dmp.simple.client.DigitalObjectSimpleFacade;
  23. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  24. import com.persagy.proxy.adm.constant.GraphCodeEnum;
  25. import com.persagy.proxy.adm.constant.RelCodeEnum;
  26. import com.persagy.proxy.adm.request.AdmDCSQueryRequest;
  27. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  28. import com.persagy.proxy.adm.request.AdmResponse;
  29. import com.persagy.proxy.adm.service.IAdmRelationService;
  30. import com.persagy.proxy.adm.service.impl.AbstractAdmBaseServiceImpl;
  31. import com.persagy.proxy.adm.utils.AdmContextUtil;
  32. import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
  33. import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
  34. import com.persagy.proxy.common.client.DmpRwdClient;
  35. import com.persagy.proxy.common.entity.DmpResult;
  36. import com.persagy.proxy.common.entity.InstanceUrlParam;
  37. import com.persagy.proxy.common.entity.RelationDTO;
  38. import com.persagy.proxy.object.model.AdmEquipment;
  39. import com.persagy.proxy.object.model.AdmSpace;
  40. import com.persagy.proxy.object.model.AdmSystem;
  41. import com.persagy.proxy.object.service.IAdmEquipmentService;
  42. import com.persagy.proxy.object.service.IAdmSpaceService;
  43. import com.persagy.proxy.object.service.IAdmSystemService;
  44. import com.persagy.proxy.report.service.IRelationReportService;
  45. import lombok.extern.slf4j.Slf4j;
  46. import org.apache.commons.lang.StringUtils;
  47. import org.springframework.beans.factory.annotation.Autowired;
  48. import org.springframework.stereotype.Service;
  49. import javax.servlet.http.HttpServletRequest;
  50. import java.util.*;
  51. import java.util.stream.Collectors;
  52. /**
  53. * ADM 空间 实现类
  54. * @author lvxy
  55. * @date 2021/8/18
  56. */
  57. @Slf4j
  58. @Service
  59. public class AdmSpaceServiceImpl extends AbstractAdmBaseServiceImpl<AdmSpace> implements IAdmSpaceService {
  60. @Autowired
  61. private DmpRwdClient rwdClient;
  62. @Autowired
  63. private ObjectMapper objectMapper;
  64. @Autowired
  65. private IAdmRelationService relationService;
  66. @Autowired
  67. private IAdmEquipmentService equipmentService;
  68. @Autowired
  69. private IAdmSystemService systemService;
  70. @Autowired
  71. private IRelationReportService relationReportService;
  72. @Override
  73. public AdmResponse doQuery(InstanceUrlParam context, AdmQueryCriteria request, Class<AdmSpace> clazz) {
  74. AdmResponse response = super.doQuery(context, request, clazz);
  75. // 系统需要补充分类名称属性
  76. ensureSpecialValues(context, (List<AdmSpace>) response.getContent());
  77. return response;
  78. }
  79. @Override
  80. public List<AdmSpace> doInsertExt(InstanceUrlParam context, Class<AdmSpace> clazz, List<AdmSpace> voList) {
  81. Map<String, List<AdmSpace>> spaceMapByFlId = voList.stream().filter(admSpace -> StringUtils.isNotEmpty(admSpace.getFloorId())).collect(Collectors.groupingBy(AdmSpace::getFloorId));
  82. //查询空间中floorId 对应的floor
  83. if(CollUtil.isNotEmpty(spaceMapByFlId)){
  84. List<ObjectNode> floors = queryFloorsByFloorIds(context, spaceMapByFlId.keySet());
  85. if(CollUtil.isNotEmpty(floors)){
  86. //ArrayList<AdmFloor> floorList = CollUtil.newArrayList(JSONUtil.toList(floors.getData().toString(),AdmFloor.class));
  87. //space.height = floor.infos.height
  88. voList = processHeight(spaceMapByFlId, floors);
  89. }
  90. }
  91. //插入空间
  92. List<AdmSpace> vosR = doInsert(context, clazz, voList);
  93. //添加绑定关系
  94. processRelations(vosR);
  95. //处理关系标记
  96. //TODO 处理关系标记
  97. //processRelationType();
  98. return vosR;
  99. }
  100. @Override
  101. public AdmResponse doQueryType(InstanceUrlParam context, AdmDCSQueryRequest request) {
  102. //查询定义类型
  103. ObjectNode criteriaEq = JsonNodeFactory.instance.objectNode();
  104. criteriaEq.put("parentCode", "space");
  105. DmpResult<JSONArray> resultDef = queryDefineType(context, criteriaEq);
  106. List<JSONObject> defineList = null;
  107. if(resultDef.getResult().equals(DmpResult.SUCCESS)){
  108. defineList = JSONUtil.toList(resultDef.getData().toString(), JSONObject.class);
  109. //获取实际空间对象的数量
  110. //根据classCode floorId 查询查询实际存在的空间
  111. assembleDefineTypes(context, defineList, request.getFloorId());
  112. // 设置返回值
  113. AdmResponse response = AdmResponse.success(defineList, request);
  114. Long total = resultDef.getCount() == null ? null : resultDef.getCount().longValue();
  115. response.setTotal(total);
  116. return response;
  117. }else{
  118. return null;
  119. }
  120. }
  121. @Override
  122. public AdmResponse doQuerySpaces(InstanceUrlParam context, AdmDCSQueryRequest request) {
  123. request.setName(AdmSpace.OBJ_TYPE);
  124. String classCodeFilter = "";
  125. if(StrUtil.isNotEmpty(request.getZoneType())){
  126. classCodeFilter = " classCode = '"+request.getZoneType()+"'";
  127. }else{
  128. classCodeFilter = " classCode in ['"+ String.join("', '",request.getZoneTypeList()) + "']";
  129. }
  130. String filters = request.getFilters();
  131. if(StrUtil.isBlank(filters)){
  132. request.setFilters(classCodeFilter);
  133. }else{
  134. request.setFilters(filters+"; "+classCodeFilter);
  135. }
  136. AdmResponse response = null;
  137. if(StrUtil.isNotEmpty(request.getBuildingId()) && StrUtil.isNotEmpty(request.getFloorId()) && !request.getFloorId().equals("isnull")){
  138. //明确建筑,明确楼层
  139. request.setFilters(request.getFilters()+" ; buildingId = '"+request.getBuildingId()+"' ; floorId='"+request.getFloorId()+"' ;");
  140. /*response = doQuery(context,request,AdmSpace.class);
  141. return response;*/
  142. }else if(StrUtil.isNotEmpty(request.getBuildingId()) && StrUtil.isNotEmpty(request.getFloorId()) && request.getFloorId().equals("isnull")){
  143. //查询此建筑下未关联楼层的空间
  144. request.setFilters(request.getFilters()+" ; buildingId = '"+request.getBuildingId()+"' ; floorId isnull");
  145. /*response = this.query(request);
  146. return response;*/
  147. }else if(StrUtil.isNotEmpty(request.getBuildingId()) && StrUtil.isEmpty(request.getFloorId()) && !request.getBuildingId().equals("isnull")){
  148. //查询建筑下的空间
  149. /*ObjectNode relationTo = JsonNodeFactory.instance.objectNode();
  150. relationTo.put("relCode", "Bd2Sp");
  151. relationTo.put("graphCode", "ArchSubset");
  152. relationTo.put("objFrom", request.getBuildingId());
  153. request.setRelationTo(relationTo);
  154. response = query(request);
  155. return response;*/
  156. request.setFilters(request.getFilters()+" ; buildingId = '"+request.getBuildingId()+"' ; ");
  157. }else if(StrUtil.isNotEmpty(request.getBuildingId()) && StrUtil.isEmpty(request.getFloorId()) && request.getBuildingId().equals("isnull")){
  158. //未明确建筑下的空间
  159. //查询建筑下的空间
  160. /*ObjectNode relationTo = JsonNodeFactory.instance.objectNode();
  161. relationTo.put("relCode", "Bd2Sp");
  162. relationTo.put("graphCode", "ArchSubset");
  163. relationTo.put("$bindFlag", false);
  164. request.setRelationTo(relationTo);
  165. response = query(request);
  166. return response;*/
  167. request.setFilters(request.getFilters()+" ; buildingId isnull");
  168. }else if(StrUtil.isEmpty(request.getBuildingId()) && StrUtil.isEmpty(request.getFloorId())){
  169. }
  170. response = this.query(request);
  171. return response;
  172. }
  173. @Override
  174. public void ensureSpecialValues(InstanceUrlParam context, List<AdmSpace> vos) {
  175. if(CollUtil.isEmpty(vos)) {
  176. return;
  177. }
  178. // 1. 收集空间的设备对象,扩展设备对象赋值
  179. List<AdmEquipment> equipmentList = new ArrayList<>();
  180. for(AdmSpace vo:vos) {
  181. // 收集设备对象
  182. if(CollUtil.isEmpty(vo.getEquipment()) && CollUtil.isEmpty(vo.getEquipmentFor())) {
  183. continue;
  184. }
  185. if(CollUtil.isNotEmpty(vo.getEquipment())) {
  186. equipmentList.addAll(vo.getEquipment());
  187. }
  188. if(CollUtil.isNotEmpty(vo.getEquipmentFor())) {
  189. equipmentList.addAll(vo.getEquipmentFor());
  190. }
  191. }
  192. // 对象扩展属性赋值
  193. equipmentService.ensureSpecialValues(context, equipmentList);
  194. //2.收集空间的系统 扩展系统、及系统下设备对象赋值
  195. List<AdmSystem> admSystemList = new ArrayList<>();
  196. for(AdmSpace vo:vos) {
  197. // 收集设备对象
  198. if(CollUtil.isEmpty(vo.getGeneralSystem())) {
  199. continue;
  200. }
  201. admSystemList.addAll(vo.getGeneralSystem());
  202. }
  203. // 对象扩展属性赋值
  204. systemService.ensureSpecialValues(context, admSystemList);
  205. }
  206. @Override
  207. public List<AdmSpace> querySpaceInShaft(InstanceUrlParam context, String shaftId, String spaceType) {
  208. AdmQueryCriteria request = new AdmQueryCriteria();
  209. request.setName(AdmSpace.OBJ_TYPE);
  210. request.resetRelationCond(false, "ArchSubset", "Sh2Sp", spaceType, shaftId, true);
  211. AdmResponse rs = query(request);
  212. return (List<AdmSpace>) rs.getContent();
  213. }
  214. @Override
  215. public List<AdmSpace> querySpaceInBuilding(InstanceUrlParam context, String buildingId, String spaceType) {
  216. AdmQueryCriteria request = new AdmQueryCriteria();
  217. request.setName(AdmSpace.OBJ_TYPE);
  218. request.resetRelationCond(false, "ArchSubset", "Bd2Sp", spaceType, buildingId, true);
  219. AdmResponse rs = query(request);
  220. return (List<AdmSpace>) rs.getContent();
  221. }
  222. @Override
  223. public Map<String, List<String>> querySpaceMapInFloors(InstanceUrlParam context, List<String> floorIds, String spaceType) {
  224. QueryCriteria queryRequest = new QueryCriteria();
  225. ObjectNode criteriaRel = JsonNodeFactory.instance.objectNode();
  226. criteriaRel.put("graphCode", "ArchSubset");
  227. criteriaRel.put("relCode", "Fl2Sp");
  228. criteriaRel.put("relValue", spaceType);
  229. ArrayNode objNode = criteriaRel.putArray("objFrom");
  230. floorIds.forEach(floorId -> objNode.add(floorId));
  231. queryRequest.setCriteria(criteriaRel);
  232. List<ObjectRelation> relations = relationService.queryByCondition(context, queryRequest);
  233. if(CollUtil.isEmpty(relations)) {
  234. return null;
  235. }
  236. return relations.stream().collect(Collectors.groupingBy(ObjectRelation::getObjFrom,
  237. Collectors.mapping(ObjectRelation::getObjTo, Collectors.toList())));
  238. }
  239. @Override
  240. public Map<String, Set<String>> queryTrafficSpaceMap(InstanceUrlParam context, Iterable<String> spaceIds, String spaceType) {
  241. Map<String, Set<String>> result = new HashMap<>();
  242. loadTrafficSpace(context, spaceIds, spaceType, true, result);
  243. loadTrafficSpace(context, spaceIds, spaceType, false, result);
  244. return result;
  245. }
  246. /***
  247. * Description: 查询多建筑楼层下的空间id
  248. * @param request : 请求参数
  249. * @return : java.util.Set<java.lang.String>
  250. * @author : lijie
  251. * @date :2021/10/28 17:56
  252. * Update By lijie 2021/10/28 17:56
  253. */
  254. @Override
  255. public Set<String> queryMultiBuildingOrFloorSpaceIds(HttpServletRequest request) {
  256. String groupCode = AppContext.getContext().getGroupCode();
  257. String projectId = AppContext.getContext().getProjectId();
  258. // 1.查询楼层下的空间的关系统计数据
  259. CommonResult<List<ObjectRelation>> floorCountByObjIds = DigitalObjectSimpleFacade
  260. .queryRelationCountByObjIds(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID,
  261. getMultiBuildingOrFloorQueryWrapper(request,RelCodeEnum.Fl2Sp.name()));
  262. List<ObjectRelation> floorSpaceData = floorCountByObjIds.getData();
  263. // 2.查询建筑下的空间的关系统计数据
  264. CommonResult<List<ObjectRelation>> buildingCountByObjIds = DigitalObjectSimpleFacade
  265. .queryRelationCountByObjIds(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID,
  266. getMultiBuildingOrFloorQueryWrapper(request, RelCodeEnum.Bd2Sp.name()));
  267. List<ObjectRelation> buildingSpaceData = buildingCountByObjIds.getData();
  268. Set<String> spaceIds = CollUtil.newHashSet(CollUtil.isNotEmpty(floorSpaceData), floorSpaceData
  269. .stream().filter(objectRelation -> objectRelation.getCount()>1)
  270. .map(ObjectRelation::getObjTo).collect(Collectors.toSet()));
  271. spaceIds.addAll(CollUtil.newHashSet(CollUtil.isNotEmpty(buildingSpaceData),
  272. buildingSpaceData.stream().filter(objectRelation -> objectRelation.getCount()>1)
  273. .map(ObjectRelation::getObjTo).collect(Collectors.toSet())));
  274. return spaceIds;
  275. }
  276. /***
  277. * Description: 查询多建筑楼层下的空间列表
  278. * @param request : 请求参数
  279. * @return : java.util.Set<java.lang.String>
  280. * @author : lijie
  281. * @date :2021/10/28 17:56
  282. * Update By lijie 2021/10/28 17:56
  283. */
  284. @Override
  285. public AdmResponse queryMultiBuildingOrFloorSpaces(HttpServletRequest request) {
  286. // 1.查询多建筑楼层下的空间id列表
  287. Set<String> spaceIds = queryMultiBuildingOrFloorSpaceIds(request);
  288. AdmQueryCriteria spaceCriteria = new AdmQueryCriteria();
  289. String filters = " id in ['"+ String.join("', '", spaceIds) +"'] ";
  290. spaceCriteria.setFilters(filters);
  291. AdmQueryCriteria floorCriteria = new AdmQueryCriteria();
  292. floorCriteria.setName("floorList");
  293. AdmQueryCriteria buildingCriteria = new AdmQueryCriteria();
  294. buildingCriteria.setName("building");
  295. floorCriteria.setCascade(CollUtil.newArrayList(buildingCriteria));
  296. spaceCriteria.setCascade(CollUtil.newArrayList(floorCriteria));
  297. return super.doQuery(AdmContextUtil.toDmpContext(), spaceCriteria, AdmSpace.class);
  298. }
  299. /***
  300. * Description: 根据查询条件查询空间统计数据
  301. * @param request : 请求参数
  302. * @return : com.persagy.proxy.adm.request.AdmResponse
  303. * @author : lijie
  304. * @date :2021/10/28 21:07
  305. * Update By lijie 2021/10/28 21:07
  306. */
  307. @Override
  308. public AdmResponse query(AdmQueryCriteria request) {
  309. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  310. AdmResponse response = super.doQuery(context, request, AdmSpace.class);
  311. // 系统需要补充分类名称属性
  312. ensureSpecialValues(context, (List<AdmSpace>) response.getContent());
  313. return response;
  314. }
  315. /***
  316. * Description: 根据查询条件查询空间统计数据,定制接口,后续可优化口
  317. * @param request : 请求参数
  318. * @return : com.persagy.proxy.adm.request.AdmResponse
  319. * @author : lijie
  320. * @date :2021/10/28 21:07
  321. * Update By lijie 2021/10/28 21:07
  322. */
  323. private AdmResponse query(AdmDCSQueryRequest request) {
  324. String orders = request.getOrders();
  325. List<OrderItem> orderItem = AdmQueryCriteriaHelper.toOrderItem(orders);
  326. Set<String> classCodes = StrUtil.isBlank(request.getZoneType()) ? request.getZoneTypeList() : Sets.newHashSet(request.getZoneType());
  327. /*ObjectNode objectNode = objectMapper.createObjectNode();
  328. ObjectNode condition = objectMapper.createObjectNode();
  329. condition.put("$null", true);
  330. objectNode.putPOJO("floorId", condition);*/
  331. QueryCriteria queryCriteria = AdmQueryCriteriaHelper.toDmpCriteria(request);
  332. ObjectNode criteria = queryCriteria.getCriteria();
  333. List<ObjectNode> queryObjects = this.relationReportService.queryObjects(criteria, AppContext.getContext().getGroupCode(), AppContext.getContext().getProjectId(),
  334. null, classCodes, Sets.newHashSet("space"), orderItem);
  335. List<AdmSpace> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(queryObjects, request.getProjection(), AdmSpace.class);
  336. // 级联查询
  337. processCascade(AdmContextUtil.toDmpContext(), admVOs, request.getName(), request.getCascade());
  338. // 设置返回值
  339. AdmResponse response = AdmResponse.success(admVOs);
  340. if(CollectionUtil.isEmpty(queryObjects)){
  341. response.setTotal(0L);
  342. response.setCount(0L);
  343. }else {
  344. response.setTotal((long) queryObjects.size());
  345. }
  346. response.setPageNumber(request.getPageNumber() == null ? 50 : request.getPageNumber());
  347. response.setPageSize(request.getPageSize() == null ? 1 : request.getPageSize());
  348. // 系统需要补充分类名称属性
  349. ensureSpecialValues(AdmContextUtil.toDmpContext(), admVOs);
  350. return response;
  351. }
  352. /***
  353. * Description: 子类重写,以便处理带buildingId
  354. * @param dmpRequest : 中台查询参数
  355. * @return : void
  356. * @author : lijie
  357. * @date :2021/10/28 20:25
  358. * Update By lijie 2021/10/28 20:25
  359. */
  360. @Override
  361. protected void processDmpCriteria(QueryCriteria dmpRequest) {
  362. ObjectNode criteria = dmpRequest.getCriteria();
  363. if (null==criteria){
  364. return;
  365. }
  366. ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
  367. if (criteria.has("floorId")){
  368. // 有楼层id过滤
  369. if(!criteria.get("floorId").toString().contains("$null")) {
  370. ObjectNode relationTo = objectMapper.createObjectNode();
  371. relationTo.put("graphCode", GraphCodeEnum.ArchSubset.name());
  372. relationTo.put("relCode", RelCodeEnum.Fl2Sp.name());
  373. relationTo.set("objFrom", criteria.get("floorId"));
  374. criteria.set("relationTo", relationTo);
  375. criteria.remove("floorId");
  376. if (criteria.has("buildingId"))
  377. criteria.remove("buildingId");
  378. return;
  379. }
  380. }
  381. if (criteria.has("buildingId")){
  382. // 有建筑id过滤
  383. ObjectNode relationTo = objectMapper.createObjectNode();
  384. relationTo.put("graphCode", GraphCodeEnum.ArchSubset.name());
  385. relationTo.put("relCode", RelCodeEnum.Bd2Sp.name());
  386. if(!criteria.get("buildingId").toString().contains("$null")) {
  387. relationTo.set("objFrom",criteria.get("buildingId"));
  388. }else{
  389. relationTo.put("$bindFlag", false);
  390. }
  391. criteria.set("relationTo",relationTo);
  392. criteria.remove("buildingId");
  393. }
  394. }
  395. /***
  396. * Description: 获得多建筑楼层下的空间的查询条件
  397. * @param request : 请求参数
  398. * @param relCode : 边类型
  399. * @return : com.fasterxml.jackson.databind.node.ObjectNode
  400. * @author : lijie
  401. * @date :2021/10/28 17:55
  402. * Update By lijie 2021/10/28 17:55
  403. */
  404. private ObjectNode getMultiBuildingOrFloorQueryWrapper(HttpServletRequest request,String relCode) {
  405. ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
  406. ObjectNode objectNode = objectMapper.createObjectNode();
  407. if (StrUtil.isNotBlank(AppContext.getContext().getProjectId())){
  408. objectNode.putPOJO("projectIds", Sets.newHashSet(AppContext.getContext().getProjectId()));
  409. }
  410. objectNode.putPOJO("graphCodes", Sets.newHashSet(GraphCodeEnum.ArchSubset.name()));
  411. objectNode.putPOJO("relCodes", Sets.newHashSet(relCode));
  412. objectNode.putPOJO("groups", Sets.newHashSet("objTo"));
  413. return objectNode;
  414. }
  415. /**
  416. * 装载交通关系
  417. * @param context
  418. * @param spaceIds
  419. * @param spaceType
  420. * @param isFrom
  421. * @param result
  422. */
  423. private void loadTrafficSpace(InstanceUrlParam context, Iterable<String> spaceIds, String spaceType, boolean isFrom, Map<String, Set<String>> result) {
  424. // 关系查询条件
  425. QueryCriteria queryRequest = new QueryCriteria();
  426. ObjectNode criteriaRel = JsonNodeFactory.instance.objectNode();
  427. criteriaRel.put("graphCode", "TrafficNetwork");
  428. criteriaRel.put("relValue", spaceType);
  429. ArrayNode objNode = criteriaRel.putArray(isFrom?"objFrom":"objTo");
  430. spaceIds.forEach(spaceId -> objNode.add(spaceId));
  431. queryRequest.setCriteria(criteriaRel);
  432. List<ObjectRelation> relations = relationService.queryByCondition(context, queryRequest);
  433. if(CollUtil.isEmpty(relations)) {
  434. return;
  435. }
  436. // 存入Map
  437. for(ObjectRelation relation:relations) {
  438. String key = isFrom ? relation.getObjFrom() : relation.getObjTo();
  439. String value = isFrom ? relation.getObjTo() : relation.getObjFrom();
  440. Set<String> valueList = result.get(key);
  441. if(CollUtil.isEmpty(valueList)) {
  442. valueList = new HashSet<>();
  443. result.put(key, valueList);
  444. }
  445. valueList.add(value);
  446. }
  447. }
  448. /**
  449. * 空间的类型是根据项目自定义的
  450. * 参见中心
  451. * @Select("SELECT s.object_type as type,d.name FROM zone_space_base s
  452. * LEFT JOIN dict.dict_space_project d ON s.object_type = d.id WHERE s.project_id = #{projectId} AND d.project_id = #{projectId}
  453. * GROUP BY object_type, d.name")
  454. * @param context
  455. * @return
  456. */
  457. @Override
  458. public List<JSONObject> doQuerySpaceTypes(InstanceUrlParam context) {
  459. //根据项目id查询空间类型
  460. Map<String, String> codeNameMap = queryProjectDefineType(context);
  461. if(CollUtil.isNotEmpty(codeNameMap)){
  462. //查询object
  463. JSONArray demObjs = queryAndDealObjs(context, codeNameMap.keySet());
  464. if(CollUtil.isNotEmpty(demObjs)){
  465. List<AdmSpace> spaceList = JSONArray.parseArray(JSONUtil.toJsonStr(demObjs), AdmSpace.class);
  466. Set<String> classCodes = spaceList.stream().map(space -> space.getClassCode()).collect(Collectors.toSet());
  467. if(CollUtil.isNotEmpty(classCodes)){
  468. codeNameMap.keySet().removeIf(code -> !classCodes.contains(code));
  469. }
  470. }
  471. }
  472. if(CollUtil.isNotEmpty(codeNameMap)){
  473. List<JSONObject> list = new ArrayList<>(codeNameMap.size());
  474. codeNameMap.keySet().stream().forEach(code ->{
  475. JSONObject jsonObject = new JSONObject();
  476. jsonObject.put("name",codeNameMap.get(code));
  477. jsonObject.put("code",code);
  478. list.add(jsonObject);
  479. });
  480. return list;
  481. }else{
  482. return null;
  483. }
  484. }
  485. private List<ObjectNode> queryObject(InstanceUrlParam context, QueryCriteria criteriaObject){
  486. JSONObject paraPe = null;
  487. String groupCode = AppContext.getContext().getGroupCode();
  488. String projectId = AppContext.getContext().getProjectId();
  489. List<ObjectNode> query = DigitalObjectFacade.query(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, criteriaObject);
  490. return query;
  491. }
  492. private DmpResult<JSONArray> queryObj(InstanceUrlParam context, QueryCriteria criteriaObject){
  493. JSONObject paraPe = null;
  494. try {
  495. String paraStr = objectMapper.writeValueAsString(criteriaObject);
  496. paraPe = JSONObject.parseObject(paraStr);
  497. } catch (JsonProcessingException e) {
  498. throw new RuntimeException("转换查询条件出错:" + e.getMessage());
  499. }
  500. return rwdClient.queryObject(context, paraPe);
  501. }
  502. private QueryCriteria ensureQueryCommParm(QueryCriteria dmpRequest, InstanceUrlParam context, Collection<String> classCodes, String objType){
  503. ObjectNode criteriaEq = null;
  504. if(JSONUtil.isNull(dmpRequest.getCriteria())){
  505. criteriaEq = JsonNodeFactory.instance.objectNode();
  506. }else{
  507. criteriaEq = dmpRequest.getCriteria();
  508. }
  509. if(CollUtil.isNotEmpty(classCodes)){
  510. ArrayNode array = criteriaEq.putObject("classCode").putArray("$in");
  511. classCodes.forEach(code -> array.add(code));
  512. }
  513. criteriaEq.put("objType", objType);
  514. dmpRequest.setCriteria(criteriaEq);
  515. return dmpRequest;
  516. }
  517. private JSONArray queryAndDealObjs(InstanceUrlParam context, Set<String> codes){
  518. QueryCriteria criteriaObject = ensureQueryCommParm(new QueryCriteria(), context, codes,AdmSpace.OBJ_TYPE);
  519. criteriaObject.setSize(50L);
  520. DmpResult<JSONArray> dmpResult = queryObj(context, criteriaObject);
  521. if(CollUtil.isEmpty(dmpResult.getData())){
  522. return null;
  523. }
  524. JSONArray demObjs = dmpResult.getData();
  525. if(dmpResult.getCount() > criteriaObject.getSize()){
  526. for(int page = 2; page < dmpResult.getCount()/criteriaObject.getSize()+1; page++){
  527. criteriaObject.setPage(page*1L);
  528. demObjs.addAll(queryObj(context, criteriaObject).getData());
  529. }
  530. }
  531. return demObjs;
  532. }
  533. private Map<String, String> queryProjectDefineType(InstanceUrlParam context){
  534. Map<String, String> codeNameMap = null;
  535. ObjectNode criteriaEq = JsonNodeFactory.instance.objectNode();
  536. criteriaEq.put("projectId", context.getProjectId());
  537. criteriaEq.put("objType", AdmSpace.OBJ_TYPE);
  538. DmpResult<JSONArray> resultDef = queryDefineType(context, criteriaEq);
  539. if(resultDef.getResult().equals(DmpResult.SUCCESS) && CollUtil.isNotEmpty(resultDef.getData())){
  540. List<ObjectTypeDefine> defineList = JSONUtil.toList(resultDef.getData().toString(), ObjectTypeDefine.class);
  541. codeNameMap = defineList.stream().collect(Collectors.toMap(ObjectTypeDefine::getCode, ObjectTypeDefine::getName, (k1,k2)->k1));
  542. }
  543. return codeNameMap;
  544. }
  545. /**
  546. * 查询空间flooId对应的infos中height存在的 flool
  547. * @param context
  548. * @param floolIds
  549. * @return
  550. */
  551. private List<ObjectNode> queryFloorsByFloorIds(InstanceUrlParam context, Collection<String> floolIds){
  552. QueryCriteria queryCriteria = ensureQueryCommParm(new QueryCriteria(),context, CollUtil.newArrayList("floor"),"floor");
  553. ObjectNode criteriaPe = queryCriteria.getCriteria();
  554. if(CollUtil.isNotEmpty(floolIds)){
  555. ArrayNode array = criteriaPe.putObject("id").putArray("$in");
  556. floolIds.forEach(floorId -> array.add(floorId));
  557. }
  558. JSONObject height = new JSONObject();
  559. height.put("$gt", 0f);
  560. criteriaPe.putPOJO("height", height);
  561. queryCriteria.setCriteria(criteriaPe);
  562. queryCriteria.setWithoutCount(true);
  563. return queryObject(context, queryCriteria);
  564. }
  565. /**
  566. * space.height = floor.infos.height
  567. * @param spaceMapByFlId
  568. * @param floorList
  569. * @return
  570. */
  571. private List<AdmSpace> processHeight(Map<String, List<AdmSpace>> spaceMapByFlId, List<ObjectNode> floorList){
  572. List<AdmSpace> resultList = new ArrayList<>();
  573. floorList.forEach(node -> {
  574. try {
  575. if (!node.has("id")
  576. || !node.get("id").isTextual()){
  577. return;
  578. }
  579. List<AdmSpace> spaces = spaceMapByFlId.get(node.get("id").asText());
  580. if (CollUtil.isEmpty(spaces)){
  581. return;
  582. }
  583. if(node.has("height")){
  584. String height = node.get("height").asText();
  585. if (StrUtil.isNotBlank(height)) {
  586. for (AdmSpace admSpace : spaces) {
  587. admSpace.setHeight(Float.parseFloat(height));
  588. resultList.add(admSpace);
  589. }
  590. }
  591. }
  592. } catch (Exception e) {}
  593. });
  594. return resultList;
  595. }
  596. /**
  597. * 添加绑定关系
  598. */
  599. private void processRelations(List<AdmSpace> vosR){
  600. if(CollUtil.isNotEmpty(vosR)) {
  601. List<RelationDTO> relationDTOS = new ArrayList<>(vosR.size());
  602. vosR.forEach(admSpace -> {
  603. if (StrUtil.isNotEmpty(admSpace.getBuildingId())) {
  604. relationDTOS.add(new RelationDTO("GtArchSubset001", "ArchSubset", "Bd2Sp", admSpace.getClassCode(), admSpace.getBuildingId(), admSpace.getId()));
  605. }
  606. if (StrUtil.isNotEmpty(admSpace.getFloorId())) {
  607. relationDTOS.add(new RelationDTO("GtArchSubset001", "ArchSubset", "Fl2Sp", admSpace.getClassCode(), admSpace.getFloorId(), admSpace.getId()));
  608. }
  609. });
  610. if (CollUtil.isNotEmpty(relationDTOS)) {
  611. relationService.doSave(AdmContextUtil.toDmpContext(), relationDTOS);
  612. }
  613. }
  614. }
  615. /**
  616. * 转换为目标实体,并增加属性
  617. */
  618. private void assembleDefineTypes(InstanceUrlParam context, List<JSONObject> defineList, String floorId){
  619. boolean needCount = true;
  620. defineList.stream().forEach(defineType -> {
  621. //查询空间数量
  622. int count = needCount ? countSpace(context, defineType.getString("code"), floorId) : 0;
  623. //处理单个对象
  624. assembleDefineObj(defineType, count, context.getProjectId());
  625. });
  626. }
  627. /**
  628. * 单个转换为目标实体,并增加属性
  629. */
  630. private void assembleDefineObj(JSONObject defineType, int count, String projectId){
  631. defineType.put("count", count);
  632. defineType.put("projectId", projectId);
  633. defineType.remove("aliasName");
  634. defineType.remove("aliasCode");
  635. defineType.remove("valid");
  636. defineType.remove("objType");
  637. defineType.remove("groupCode");
  638. defineType.remove("parentCode");
  639. defineType.remove("id");
  640. }
  641. private void addRelationTo(QueryCriteria rwdQueryCriteria, String objFrom, String relCode){
  642. ObjectNode relationTO = JsonNodeFactory.instance.objectNode();
  643. relationTO.put("relCode", relCode);
  644. relationTO.put("graphCode", "ArchSubset");
  645. relationTO.put("objFrom", objFrom);
  646. ObjectNode criteria = rwdQueryCriteria.getCriteria();
  647. criteria.putPOJO("relationTo", relationTO);
  648. rwdQueryCriteria.setCriteria(criteria);
  649. }
  650. /**
  651. * 查询存在空间的数量
  652. * @param context
  653. * @param classCode
  654. * @param objFrom
  655. * @return
  656. */
  657. private int countSpace(InstanceUrlParam context, String classCode, String objFrom){
  658. QueryCriteria queryCriteria = ensureQueryCommParm(new QueryCriteria(), context, CollUtil.newArrayList(classCode),"space");
  659. addRelationTo(queryCriteria, objFrom,"Fl2Sp");
  660. queryCriteria.setOnlyCount(true);
  661. DmpResult<JSONArray> dmpResult = queryObj(context, queryCriteria);
  662. return dmpResult.getCount();
  663. }
  664. private DmpResult<JSONArray> queryDefineType(InstanceUrlParam context, ObjectNode criteriaEq){
  665. QueryCriteria criteriaObject = new QueryCriteria();
  666. criteriaObject.setCriteria(criteriaEq);
  667. JSONObject paraPe = null;
  668. try {
  669. String paraStr = objectMapper.writeValueAsString(criteriaObject);
  670. paraPe = JSONObject.parseObject(paraStr);
  671. } catch (JsonProcessingException e) {
  672. throw new RuntimeException("查询公共定义的空间类型,转换查询条件出错:" + e.getMessage());
  673. }
  674. return rwdClient.queryDefClass(context, paraPe);
  675. }
  676. }