AdmLabslController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package com.persagy.proxy.dictionary.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.persagy.proxy.adm.constant.AdmDictCategoryEnum;
  6. import com.persagy.proxy.adm.request.AdmDictQueryCriteria;
  7. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  8. import com.persagy.proxy.dictionary.model.AdmDict;
  9. import com.persagy.proxy.dictionary.service.IAdmDictService;
  10. import com.persagy.proxy.object.model.AdmEquipment;
  11. import com.persagy.proxy.object.model.AdmObjectInfo;
  12. import com.persagy.proxy.dictionary.model.AdmObjectType;
  13. import com.persagy.proxy.adm.request.AdmResponse;
  14. import com.persagy.proxy.object.service.IAdmEquipmentService;
  15. import com.persagy.proxy.report.service.IAdmLabslService;
  16. import com.persagy.proxy.adm.utils.AdmContextUtil;
  17. import com.persagy.proxy.common.entity.InstanceUrlParam;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.web.bind.annotation.PostMapping;
  23. import org.springframework.web.bind.annotation.RequestBody;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import javax.ws.rs.QueryParam;
  27. import java.util.ArrayList;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.stream.Collectors;
  32. /**
  33. * @ClassName AdmLabslController
  34. * @Description: 蜡笔森林定制接口
  35. * @Author linhuili
  36. * @Date 2021/9/17 20:01
  37. * @Version V1.0
  38. **/
  39. @RestController
  40. @RequestMapping("/labsl")
  41. @Slf4j
  42. public class AdmLabslController {
  43. @Autowired
  44. private IAdmLabslService service;
  45. @Value("${middleware.group.code}")
  46. private String groupCode ="XG";
  47. @Autowired
  48. private IAdmEquipmentService iAdmEquipmentService;
  49. @Autowired
  50. private IAdmDictService admDictService;
  51. /**
  52. * 查询项目下的设备类型
  53. * @return
  54. * @throws Exception
  55. */
  56. @PostMapping("/category")
  57. public AdmResponse queryCategory() throws Exception {
  58. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  59. if(StringUtils.isEmpty(context.getGroupCode())){
  60. context.setGroupCode(groupCode);
  61. }
  62. return querySystemEquipTree(AdmContextUtil.toDmpContext(),AdmObjectType.class);
  63. //return service.queryCategory(context, AdmObjectInfo.class);
  64. }
  65. /**
  66. * 查询项目下的空间类型
  67. * @return
  68. * @throws Exception
  69. */
  70. @PostMapping("/zone/type")
  71. public AdmResponse queryZoneType() throws Exception {
  72. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  73. if(StringUtils.isEmpty(context.getGroupCode())){
  74. context.setGroupCode(groupCode);
  75. }
  76. return service.queryZoneType(context, AdmObjectInfo.class);
  77. }
  78. /**
  79. * 查询项目下的建筑楼层
  80. * @return
  81. * @throws Exception
  82. */
  83. @PostMapping("/building/floor")
  84. public AdmResponse queryBuildFloor(@QueryParam("projectId") String projectId) throws Exception {
  85. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  86. if(StringUtils.isEmpty(context.getGroupCode())){
  87. context.setGroupCode(groupCode);
  88. }
  89. return service.queryBuildFloor(context, AdmObjectInfo.class);
  90. }
  91. /* *//**
  92. * 查询设备包含设备类型
  93. * @return
  94. * @throws Exception
  95. *//*
  96. @PostMapping("/query/equip")
  97. public AdmResponse queryEquipType(@QueryParam("projectId") AdmQueryCriteria request) throws Exception {
  98. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  99. return iAdmEquipmentService.doQuery(context, AdmObjectInfo.class);
  100. }*/
  101. /**
  102. * 查询设备包含设备类型
  103. * @return
  104. * @throws Exception
  105. */
  106. @PostMapping("/query/equip")
  107. public AdmResponse queryEquipType(@RequestBody AdmQueryCriteria request) throws Exception {
  108. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  109. if(StringUtils.isEmpty(context.getGroupCode())){
  110. context.setGroupCode(groupCode);
  111. }
  112. // 组装上下文条件
  113. request.setName(AdmEquipment.OBJ_TYPE);
  114. //过滤条件处理
  115. String filters = request.getFilters();
  116. Boolean specialQueryFlag = false;
  117. StringBuilder newFilters = new StringBuilder();
  118. StringBuilder specailFilters = new StringBuilder();
  119. if(StringUtils.isEmpty(filters)){
  120. return iAdmEquipmentService.doQuery(context, request, AdmEquipment.class);
  121. }
  122. //处理查询条件
  123. specialQueryFlag = handleRequestCriteria(filters, newFilters, specailFilters, specialQueryFlag);
  124. List<AdmEquipment> result = new ArrayList<>();
  125. List<AdmEquipment> specailContent = new ArrayList<>();
  126. AdmQueryCriteria specifal = new AdmQueryCriteria();
  127. specifal = request;
  128. specifal.setFilters(specailFilters.toString());
  129. int totalCount = 0 ;
  130. request.setFilters(newFilters.toString());
  131. AdmResponse queryResponse = iAdmEquipmentService.doQuery(context, request, AdmEquipment.class);
  132. List<AdmEquipment> content = (List<AdmEquipment>)queryResponse.getContent();
  133. if(queryResponse.getTotal() != null){
  134. totalCount += queryResponse.getTotal().intValue();
  135. }
  136. if(specialQueryFlag){
  137. //查询未明确建筑,未明确楼层
  138. Long count = CollectionUtil.isEmpty(queryResponse.getContent()) ? 0 : Long.valueOf(queryResponse.getContent().size());
  139. Integer pageSize = request.getPageSize();
  140. if(count < pageSize){
  141. long specailPageSize = pageSize - count;
  142. specifal.setPageSize((int) specailPageSize);
  143. AdmResponse specailQueryResponse = iAdmEquipmentService.doQuery(context, specifal, AdmEquipment.class);
  144. specailContent = (List<AdmEquipment>)specailQueryResponse.getContent();
  145. if(specailQueryResponse.getTotal() != null){
  146. totalCount += specailQueryResponse.getTotal().intValue();
  147. }
  148. }
  149. }
  150. if(CollUtil.isNotEmpty(content)){
  151. result.addAll(content);
  152. }
  153. if(CollUtil.isNotEmpty(specailContent)){
  154. result.addAll(specailContent);
  155. }
  156. AdmResponse response = AdmResponse.success(result);
  157. response.setTotal(Long.valueOf(totalCount));
  158. response.setCount(CollectionUtil.isEmpty(result) ? 0 : Long.valueOf(result.size()));
  159. response.setPageNumber(request.getPageNumber());
  160. response.setPageSize(request.getPageSize());
  161. return response;
  162. }
  163. /**
  164. * 处理查询条件
  165. * @param filters
  166. * @param newFilters
  167. * @param specailFilters
  168. * @param specialQueryFlag
  169. */
  170. private boolean handleRequestCriteria(String filters,StringBuilder newFilters,StringBuilder specailFilters,Boolean specialQueryFlag){
  171. String[] filter = filters.split(";");
  172. for(int i =0 ;i< filter.length;i++){
  173. if(filter[i].contains("not")){
  174. String[] param = filter[i].split(" ");
  175. filter[i] = StrUtil.format("{} $notcontains {}",param[1],param[3]);
  176. }
  177. if(filter[i].contains(" in [") && filter[i].contains("]or")){
  178. //针对未明确建筑,未明确楼层查询单独处理
  179. String[] split = filter[i].split("]or");
  180. newFilters.append(split[0]+"]");
  181. specailFilters.append(split[1]);
  182. //标记特殊查询标识
  183. specialQueryFlag = true;
  184. newFilters.append(" ; ");
  185. specailFilters.append(" ; ");
  186. }else{
  187. newFilters.append(filter[i]);
  188. specailFilters.append(filter[i]);
  189. if(i != filter.length -1){
  190. newFilters.append(" ; ");
  191. specailFilters.append(" ; ");
  192. }
  193. }
  194. }
  195. return specialQueryFlag;
  196. }
  197. /**
  198. * 根据设备id统计设备所属的专业
  199. * @return
  200. * @throws Exception
  201. */
  202. @PostMapping("/major/category")
  203. public AdmResponse queryMajorCategory(@QueryParam("projectId") String projectId,
  204. @RequestBody List<String> ids) throws Exception {
  205. InstanceUrlParam context = AdmContextUtil.toDmpContext();
  206. if(StringUtils.isEmpty(context.getGroupCode())){
  207. context.setGroupCode(groupCode);
  208. }
  209. if(CollUtil.isEmpty(ids)){
  210. return AdmResponse.success(new ArrayList<>());
  211. }
  212. List<String> newIds = ids.stream().filter(id->!"fisnull".equals(id)).collect(Collectors.toList());
  213. if(CollUtil.isEmpty(newIds)){
  214. return AdmResponse.success(new ArrayList<>());
  215. }
  216. return service.queryMajorCategory(context, newIds , AdmObjectType.class);
  217. }
  218. /**
  219. * 查询项目下已有的系统设备类型树
  220. * @param context
  221. * @param clazz
  222. * @return
  223. * @throws Exception
  224. */
  225. private <T> AdmResponse querySystemEquipTree(InstanceUrlParam context, Class<T> clazz) throws Exception {
  226. AdmDictQueryCriteria request = new AdmDictQueryCriteria();
  227. //查询项目下已有的设备类型
  228. request.setName("equipment");
  229. AdmResponse dictResponse = iAdmEquipmentService.queryEquipClassCode(context, request, AdmEquipment.class);
  230. List<String> classCodes = (List<String>)dictResponse.getContent();
  231. if(CollUtil.isEmpty(classCodes)){
  232. return AdmResponse.success(new ArrayList<>());
  233. }
  234. Map<String, List<String>> classCodeMap = classCodes.stream().collect(Collectors.groupingBy(item->item.substring(0,4),Collectors.toList()));
  235. //查询系统设备类树
  236. AdmDict admDict = new AdmDict();
  237. admDict.setType(AdmDictCategoryEnum.MAJOR_SYSTEM_EQUIP.getValue());
  238. AdmResponse categoryResponse = admDictService.queryCategory(context, admDict, clazz);
  239. List<AdmObjectType> objectTypes = (List<AdmObjectType>) categoryResponse.getContent();
  240. if(CollUtil.isEmpty(objectTypes)){
  241. return AdmResponse.success(new ArrayList<>());
  242. }
  243. //过滤设备实体中不存在的类型
  244. Iterator<AdmObjectType> it = objectTypes.iterator();
  245. while(it.hasNext()){
  246. AdmObjectType major = it.next();
  247. //过滤系统
  248. List<AdmObjectType> systemList = major.getChildren();
  249. if(CollUtil.isNotEmpty(systemList)){
  250. Iterator<AdmObjectType> systemListIter = systemList.iterator();
  251. while (systemListIter.hasNext()){
  252. AdmObjectType objectType = systemListIter.next();
  253. List<String> objTypes = classCodeMap.get(objectType.getCode());
  254. if(CollUtil.isEmpty(objTypes)){
  255. systemListIter.remove();
  256. continue;
  257. }
  258. //过滤子级:设备类型
  259. List<AdmObjectType> children = objectType.getChildren();
  260. if(CollUtil.isEmpty(children)){
  261. systemListIter.remove();
  262. continue;
  263. }
  264. Iterator<AdmObjectType> chidIterator = children.iterator();
  265. while (chidIterator.hasNext()){
  266. AdmObjectType next = chidIterator.next();
  267. if(!objTypes.contains(next.getCode())){
  268. chidIterator.remove();
  269. }
  270. }
  271. }
  272. }
  273. if(CollUtil.isEmpty(systemList)){
  274. it.remove();
  275. }
  276. }
  277. return AdmResponse.success(objectTypes);
  278. }
  279. }