package com.persagy.proxy.adm.controller; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.persagy.dmp.basic.dto.RequestData; import com.persagy.dmp.basic.model.QueryCriteria; import com.persagy.dmp.common.model.entity.BaseEntity; import com.persagy.dmp.define.client.DigitalDefineFacade; import com.persagy.dmp.define.entity.ObjectTypeDefine; import com.persagy.dmp.digital.client.DigitalObjectFacade; import com.persagy.dmp.digital.client.DigitalRelationFacade; import com.persagy.dmp.digital.entity.ObjectRelation; import com.persagy.dmp.simple.client.DigitalObjectSimpleFacade; import com.persagy.proxy.adm.model.*; import com.persagy.proxy.adm.request.AdmCreateRequest; import com.persagy.proxy.adm.request.AdmQueryCriteria; import com.persagy.proxy.adm.request.AdmResponse; import com.persagy.proxy.adm.service.*; import com.persagy.proxy.adm.utils.AdmContextUtil; import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper; import com.persagy.proxy.adm.utils.ObjectNameUtil; import com.persagy.proxy.common.entity.DmpResult; import com.persagy.proxy.common.entity.RelationDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.json.JsonObject; import javax.validation.constraints.NotNull; import javax.ws.rs.QueryParam; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; /** * Adm 系统 API * @author lvxy * @date 2021/8/16 */ @RestController @RequestMapping("/object/general-system") public class AdmSystemController { @Autowired private IAdmSystemService service; @Autowired private IAdmBuildingService buildingService; @Autowired private IAdmFloorService floorService; @Autowired private IAdmDictService dictService; @Autowired private IAdmRelationService relationService; /** * 统计 * @param request * @return * @throws Exception */ @PostMapping("/count") public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmSystem.OBJ_TYPE); request.setOnlyCount(true); return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmSystem.class); } /** * 创建 * @param createRequest * @return * @throws Exception */ @PostMapping("/create") public AdmResponse create(@RequestBody AdmCreateRequest createRequest, @QueryParam("projectId") String projectId) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmSystem.class, vos); return AdmResponse.success(vos); } /** * 创建系统信息-处理建筑楼层 * @param createRequest * @return * @throws Exception */ @PostMapping("/create-bd-fl") public AdmResponse createBdFl(@RequestBody AdmCreateRequest createRequest) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); Map admSystemMap = CollUtil.fieldValueMap(vos, "classCode"); QueryCriteria defineCriteria = new QueryCriteria(); ObjectNode criteria = JsonNodeFactory.instance.objectNode(); ArrayNode array = criteria.putObject("code").putArray("$in"); admSystemMap.keySet().stream().forEach(classCode -> array.add(classCode)); defineCriteria.setCriteria(criteria); List defineList = DigitalDefineFacade.queryObjectType(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(),AdmContextUtil.toDmpContext().getAppId(),null,defineCriteria); Map defineMap = CollUtil.fieldValueMap(defineList, "code"); vos.stream().forEach(admSystem -> { if(StrUtil.isEmpty(admSystem.getName())){ if(CollUtil.isNotEmpty(defineList) && defineMap.containsKey(admSystem.getClassCode())){ admSystem.setName(ObjectNameUtil.objectName(defineMap.get(admSystem.getClassCode()).getName()+"系统-")); }else { admSystem.setName(ObjectNameUtil.objectName("系统-")); } } }); List unRelationSys = new ArrayList<>(); List admSystemList = new ArrayList<>(vos.size()); vos.stream().forEach(admSystem -> { if(CollUtil.isNotEmpty(admSystem.getBuildingFloorInfoList())){ List admSystems = service.doInsert(AdmContextUtil.toDmpContext(), AdmSystem.class, CollUtil.newArrayList(admSystem)); //添加关系 String systemId = admSystems.get(0).getId(); List relationDTOS = new ArrayList<>(admSystem.getBuildingFloorInfoList().size()); admSystem.getBuildingFloorInfoList().forEach(info -> { String floorId = info.get("FloorID").textValue(); String buildID = info.get("BuildID").textValue(); if(StrUtil.isNotEmpty(floorId)){ relationDTOS.add(new RelationDTO(null, "MechInArch", "Bd2Fl", null, systemId , floorId)); } if(StrUtil.isNotEmpty(buildID)){ relationDTOS.add(new RelationDTO(null, "MechForArch", "Sy2Bd", null, systemId , buildID)); } }); if(CollUtil.isNotEmpty(relationDTOS)){ relationService.doSave(AdmContextUtil.toDmpContext(), relationDTOS); } admSystemList.addAll(admSystems); }else{ unRelationSys.add(admSystem); } }); if(CollUtil.isNotEmpty(unRelationSys)){ List systemList = service.doInsert(AdmContextUtil.toDmpContext(), AdmSystem.class, unRelationSys); admSystemList.addAll(systemList); } return AdmResponse.success(admSystemList); } /** * 根据ID删除 * @param vos * @return * @throws Exception */ @PostMapping("/delete") public AdmResponse delete( @RequestBody List vos) throws Exception { service.doDelete(AdmContextUtil.toDmpContext(), vos); return AdmResponse.success(); } /** * 更新 * @param createRequest * @return * @throws Exception */ @PostMapping("/update") public AdmResponse update(@RequestBody AdmCreateRequest createRequest, @QueryParam("projectId") String projectId) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmSystem.class, vos); return AdmResponse.success(vos); } /** * 系统绑定的动态信息点或静态信息点 * type: static(静态),iot (动态) * @param request * @return * @throws Exception */ @GetMapping("/export") public AdmResponse exportInfos( @RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmSystem.OBJ_TYPE); return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmSystem.class); } /** * 创建系统下的设备关系 * @param createRequest * @return * @throws Exception */ @PostMapping("/link-equip") public AdmResponse createRelateEquip(@RequestBody JsonObject createRequest, @QueryParam("projectId") String projectId) throws Exception { String sysId = createRequest.get("sysId").toString(); List equipIdList = (List)createRequest.get("equipIdList"); List relations = new ArrayList<>(equipIdList.size()); equipIdList.stream().forEach(euipId -> { ObjectNode objectNode = JsonNodeFactory.instance.objectNode(); objectNode.put("relCode", "Sy2Eq"); objectNode.put("graphCode","MechSubset"); objectNode.put("objFrom", sysId); objectNode.put("objTo", euipId); }); List list = DigitalRelationFacade.create(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, relations); return AdmResponse.success(list); } /** * 查询 * @param request * @return * @throws Exception */ @PostMapping("/query") public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmSystem.OBJ_TYPE); AdmResponse admResponse = service.doQuery(AdmContextUtil.toDmpContext(), request, AdmSystem.class); List systemList = (List) admResponse.getContent(); if(CollUtil.isNotEmpty(systemList)){ AdmDict admDict = new AdmDict(); admDict.setType(AdmSystem.OBJ_TYPE); AdmResponse admResponseDic = dictService.queryCategory(AdmContextUtil.toDmpContext(), admDict, AdmObjectType.class); if(CollUtil.isNotEmpty(admResponseDic.getContent())){ List admDictList = (List) admResponseDic.getContent(); Map admDictMap = CollUtil.fieldValueMap(admDictList, "code"); systemList.stream().forEach(admSystem -> { if(admDictMap.keySet().contains(admSystem.getClassCode())){ AdmObjectType admDictTemp = admDictMap.get(admSystem.getClassCode()); admSystem.setCategoryName(admDictTemp.getName()); } }); admResponse.setContent(systemList); } } return admResponse; } /** * 查询系统信息-系统建筑楼层一对多 * MajorName 专业名称 ,级联查询 系统分类名(categoryNames),系统下的设备(equipmentList),系统下的竖井(shaftList),业务空间(zoneSpaceBaseTableList) * * 明确建筑楼层的 adm级联 查询获取建筑楼层信息 使用正常接口 先查询楼层 再根据楼层信息拼接建筑 * 未明确建筑/未明确楼层 中台定制接口 * * 这个参见中心代码, * @param buildingId buildingId=1,未明确建筑 * @param floorId floorId=1,未明确楼层 * @param request * @return * @throws Exception */ @PostMapping("/query-new") public AdmResponse queryNew(@QueryParam("buildingId") String buildingId, @QueryParam("floorId") String floorId, @RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmSystem.OBJ_TYPE); AdmResponse syses = new AdmResponse(); List admSystemList = new ArrayList<>(); if(StrUtil.isAllEmpty(buildingId,floorId)){ //case one 查询全部系统,级联建筑,建筑级联楼层 AdmQueryCriteria admQueryCriteriaBd = new AdmQueryCriteria(); admQueryCriteriaBd.setName("buildingList"); AdmQueryCriteria admQueryCriteriaFl = new AdmQueryCriteria(); admQueryCriteriaFl.setName("floorList"); List cascade = new ArrayList<>(1); cascade.add(admQueryCriteriaBd); cascade.add(admQueryCriteriaFl); request.setCascade(cascade); syses = service.doQuery(AdmContextUtil.toDmpContext(),request,AdmSystem.class); //处理数据 BuildingFloorInfo if(CollUtil.isNotEmpty(syses.getContent())){ admSystemList = (List) syses.getContent(); admSystemList.stream().forEach(admSystem -> { List buildingFloorInfoList = new ArrayList<>(); List buildingList = admSystem.getBuildingList(); if(CollUtil.isNotEmpty(buildingList)){ buildingList.stream().forEach(admBuilding -> { ObjectNode info = JsonNodeFactory.instance.objectNode(); info.put("BuildID",admBuilding.getId()); info.put("BuildName",admBuilding.getLocalName()); List admFloorList = admSystem.getFloorList(); if(CollUtil.isNotEmpty(admFloorList)){ Map admFloorMap = CollUtil.fieldValueMap(admFloorList, "buildingId"); if(admFloorMap.containsKey(admBuilding.getId())){ info.put("FloorID",admFloorMap.get(admBuilding.getId()).getId()); info.put("FloorName",admFloorMap.get(admBuilding.getId()).getLocalName()); } } buildingFloorInfoList.add(info); }); admSystem.setBuildingFloorInfoList(buildingFloorInfoList); admSystem.setBuildingList(null); admSystem.setFloorList(null); } else{ admSystem.setBuildingFloorInfoList(buildingFloorInfoList); } }); syses.setContent(CollUtil.isEmpty(admSystemList) ? new ArrayList<>(1) : admSystemList); } }else{ QueryCriteria rwdQueryCriteria = AdmQueryCriteriaHelper.toDmpCriteria(request); ObjectNode criteria = rwdQueryCriteria.getCriteria(); if(JSONUtil.isNull(criteria)){ return AdmResponse.failure("请求参数错误"); } if (StrUtil.isAllNotEmpty(buildingId, floorId)) { if(!"1".equals(floorId)){ //case two 明确建筑、明确楼层的 系统 JSONObject relationFrom = new JSONObject(); relationFrom.put("relCode", "Sy2Fl"); relationFrom.put("graphCode", "MechInArch"); relationFrom.put("objTo", floorId); criteria.putPOJO("relationFrom",relationFrom); rwdQueryCriteria.setCriteria(criteria); syses = service.doQuery(AdmContextUtil.toDmpContext(), rwdQueryCriteria, AdmSystem.class); if (CollUtil.isNotEmpty(syses.getContent())) { admSystemList = (List) syses.getContent(); } }else if("1".equals(floorId)){ //case three 明确建筑 不明确楼层 admSystemList = queryNotSureObj(buildingId, rwdQueryCriteria); //处理通用返回结果 dealRespose(syses, request); } if(CollUtil.isNotEmpty(admSystemList)){ //处理数据 BuildingFloorInfo admSystemList = dealBdFlInfo(floorId,buildingId,admSystemList); syses.setContent(admSystemList); } }else{ //处理通用返回结果 dealRespose(syses, request); if(StrUtil.isNotEmpty(buildingId) && buildingId.equals("1")){ //case four 未明确建筑 admSystemList = queryNotSureObj(null, rwdQueryCriteria); if(CollUtil.isNotEmpty(admSystemList)){ //处理数据 BuildingFloorInfo admSystemList = dealBdFlInfo(null,null, admSystemList); } } } if(CollUtil.isNotEmpty(admSystemList)) syses.setTotal(admSystemList.size()*1l); } if(CollUtil.isNotEmpty(admSystemList)){ syses.setContent(admSystemList); }else{ syses.setContent(new ArrayList<>(1)); } return syses; } private void dealRespose(AdmResponse syses, AdmQueryCriteria request){ syses.setPageSize(request.getPageSize()); syses.setPageNumber(request.getPageNumber()); syses.setResult(DmpResult.SUCCESS); syses.setTotal(0L); } /** * 查询不确定建筑/楼层的系统 * @param buildingId * @param rwdQueryCriteria * @return */ private List queryNotSureObj(String buildingId, QueryCriteria rwdQueryCriteria){ RequestData requestData = new RequestData(); String classCode = rwdQueryCriteria.getCriteria().get("classCode").textValue(); if(StrUtil.isNotEmpty(classCode)){ requestData.setClassCode(classCode); } if(StrUtil.isNotEmpty(buildingId)){ requestData.setBuildingId(buildingId); } requestData.setObjType(AdmSystem.OBJ_TYPE); requestData.setPage(rwdQueryCriteria.getPage()); requestData.setSize(rwdQueryCriteria.getSize()); List result = DigitalObjectSimpleFacade.queryNotSureSystem(AdmContextUtil.toDmpContext().getGroupCode(), AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(),null, requestData); if(CollUtil.isNotEmpty(result)){ List admSystemList = JSONUtil.toList(result.toString(), AdmSystem.class); return admSystemList; }else{ return null; } } /** * 处理建筑楼层信息 建筑确定,楼层可为空 * @param floorId * @param buildingId * @param admSystemList * @return */ private List dealBdFlInfo(String floorId, String buildingId, List admSystemList){ if(CollUtil.isEmpty(admSystemList)){ return null; } if(StrUtil.isEmpty(buildingId)){//处理成adm目标格式 List nullList = new ArrayList<>(0); admSystemList.stream().forEach(admSystem -> { admSystem.setBuildingFloorInfoList(nullList); }); return admSystemList; } AdmResponse buildings = null; if(StrUtil.isNotEmpty(buildingId)){ AdmQueryCriteria admQueryCriteria = getAdmQueryCriteria(AdmBuilding.OBJ_TYPE, buildingId); buildings = buildingService.doQuery(AdmContextUtil.toDmpContext(), admQueryCriteria, AdmBuilding.class); } AdmResponse floors = null; if(StrUtil.isNotEmpty(floorId)){ AdmQueryCriteria admQueryCriteria = getAdmQueryCriteria(AdmFloor.OBJ_TYPE, floorId); floors = floorService.doQuery(AdmContextUtil.toDmpContext(), admQueryCriteria, AdmFloor.class); } ObjectNode buildingFloorInfo = JsonNodeFactory.instance.objectNode(); if (CollUtil.isNotEmpty(buildings.getContent())) { AdmBuilding admBuilding = (AdmBuilding) buildings.getContent().get(0); buildingFloorInfo.put("BuildID",admBuilding.getId()); buildingFloorInfo.put("BuildName",admBuilding.getLocalName()); } if (null!=floors && CollUtil.isNotEmpty(floors.getContent())) { AdmFloor floor = (AdmFloor) floors.getContent().get(0); buildingFloorInfo.put("FloorID",floor.getId()); buildingFloorInfo.put("FloorName",floor.getLocalName()); } admSystemList.stream().forEach(admSystem -> { admSystem.setBuildingFloorInfoList(Arrays.asList(buildingFloorInfo)); }); return admSystemList; } private AdmQueryCriteria getAdmQueryCriteria(String objType, String Id){ AdmQueryCriteria admQueryCriteria = new AdmQueryCriteria(); admQueryCriteria.setName(objType); admQueryCriteria.setFilters("id = " + Id); return admQueryCriteria; } /** * 查询系统信息-试图级联建筑楼层 暂未使用 * @param request * @return * @throws Exception */ @PostMapping("/query-bdfl") public AdmResponse queryUnBdFl(@RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmSystem.OBJ_TYPE); return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmSystem.class); } /** * 查询没有和当前系统关联的设备 * id 为系统id * @param request * @return * @throws Exception */ @PostMapping("/un-sys-equip") public AdmResponse queryUnEquip( @NotNull @QueryParam("sysId") String sysId, @RequestBody AdmQueryCriteria request) throws Exception { AdmResponse admResponse = queryObjectByType(sysId,request,"Sy2Eq","MechSubset", AdmEquipment.OBJ_TYPE); return admResponse; } /** * 查询没有和当前系统关联的竖井 * id 为系统id * @param request * @return * @throws Exception */ @PostMapping("/un-sys-shaft") public AdmResponse queryUnSh( @NotNull @QueryParam("sysId") String sysId, @RequestBody AdmQueryCriteria request) throws Exception { AdmResponse admResponse = queryObjectByType(sysId,request,"Sy2Sh",null, AdmShaft.OBJ_TYPE); return admResponse; } /** * 查询没有和当前系统关联的业务空间 * id 为系统id * @param request * @return * @throws Exception */ @PostMapping("/un-sys-zoneSpaceBaseTable") public AdmResponse queryUnZoneSp(@NotNull @QueryParam("sysId") String sysId, @RequestBody AdmQueryCriteria request) throws Exception { AdmResponse admResponse = queryObjectByType(sysId,request,"Sy2Sp",null, AdmSpace.OBJ_TYPE); return admResponse; } /** * 查询没有和当前空间绑定的系统 * 例如:prijectId=Pj4201050001,type=AirConditioningZone spaceId=Sp4201050001df2091cdf719443b80bb737aeabf0525 spaceId 为当前空间id type为当前空间类型 * @param request * @return * @throws Exception */ @PostMapping("/unspace-sys") public AdmResponse queryUnSpSy( @QueryParam("type") String type, @QueryParam("spaceId") String spaceId, @RequestBody AdmQueryCriteria request) throws Exception { if(StrUtil.isNotEmpty(spaceId)){ AdmResponse admResponse = queryObjectByType(spaceId, request,"Sy2Sp",null, AdmSystem.OBJ_TYPE); return admResponse; }else{ if(StrUtil.isNotEmpty(type)){ QueryCriteria queryCriteriaObj = AdmQueryCriteriaHelper.toDmpCriteria(request); ObjectNode criteriaObj = JsonNodeFactory.instance.objectNode(); criteriaObj.put("objType", AdmSpace.OBJ_TYPE); queryCriteriaObj.setCriteria(criteriaObj); List objs = DigitalObjectFacade.query(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, queryCriteriaObj); QueryCriteria queryCriteriaObjSys = AdmQueryCriteriaHelper.toDmpCriteria(request); ObjectNode criteriaSys = JsonNodeFactory.instance.objectNode(); criteriaObj.put("objType", AdmSystem.OBJ_TYPE); if(CollUtil.isNotEmpty(objs)){ ArrayNode arrayNode = criteriaObj.putObject("id").putArray("$notcontains"); for(ObjectNode objectNode: objs){ arrayNode.add(objectNode.get("id").toString()); } } queryCriteriaObj.setCriteria(criteriaObj); List syses = DigitalObjectFacade.query(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, queryCriteriaObjSys); return AdmResponse.success(syses); } return AdmResponse.success(); } } private AdmResponse queryObjectByType(String sysId, AdmQueryCriteria request, String relCode, String graphCode, String objType){ QueryCriteria queryCriteria = new QueryCriteria(); ObjectNode criteria = JsonNodeFactory.instance.objectNode(); criteria.put("objFrom", sysId); criteria.put("relCode", relCode); if(StrUtil.isNotEmpty(graphCode)){ criteria.put("graphCode", graphCode); } queryCriteria.setCriteria(criteria); queryCriteria.setSize(request.getPageSize()*1L); queryCriteria.setPage(request.getPageNumber()*1L); List relations = DigitalRelationFacade.query(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, queryCriteria); QueryCriteria queryCriteriaObj = AdmQueryCriteriaHelper.toDmpCriteria(request); ObjectNode criteriaObj = JsonNodeFactory.instance.objectNode(); criteriaObj.put("objType", objType); if(CollUtil.isNotEmpty(relations)){ Map objectRelationMap = CollUtil.fieldValueMap(relations,BaseEntity.PROP_ID); ArrayNode arrayNode = criteriaObj.putObject("id").putArray("$notcontains"); objectRelationMap.keySet().stream().forEach(id -> arrayNode.add(id)); } queryCriteriaObj.setCriteria(criteriaObj); List obj = DigitalObjectFacade.query(AdmContextUtil.toDmpContext().getGroupCode(),AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, queryCriteriaObj); return AdmResponse.success(obj); } }