AdmDictServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package com.persagy.proxy.adm.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.fasterxml.jackson.databind.JsonNode;
  5. import com.fasterxml.jackson.databind.node.ArrayNode;
  6. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  7. import com.fasterxml.jackson.databind.node.ObjectNode;
  8. import com.persagy.dmp.basic.model.QueryCriteria;
  9. import com.persagy.proxy.adm.constant.AdmDictConstant;
  10. import com.persagy.proxy.adm.constant.enums.AdmDictCategoryEnum;
  11. import com.persagy.proxy.adm.model.*;
  12. import com.persagy.proxy.adm.request.AdmCreateRequest;
  13. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  14. import com.persagy.proxy.adm.request.AdmResponse;
  15. import com.persagy.proxy.adm.service.IAdmDictService;
  16. import com.persagy.proxy.common.entity.InstanceUrlParam;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.springframework.context.annotation.Primary;
  20. import org.springframework.stereotype.Service;
  21. import java.util.*;
  22. import java.util.stream.Collectors;
  23. /**
  24. * @ClassName AdmDictServiceImpl
  25. * @Description: 字典查询
  26. * @Author linhuili
  27. * @Date 2021/8/31 18:21
  28. * @Version V1.0
  29. **/
  30. @Slf4j
  31. @Service
  32. public class AdmDictServiceImpl<T> extends AbstractAdmDictBaseServiceImpl<T> implements IAdmDictService<T> {
  33. /**
  34. * 字典类型查询:所有设备类型、部件类型、系统类型、专业类型
  35. * 查询类型:输入设备类型:equipment,输入部件类型:component,输入系统类型:system,输入专业类型:major,业务空间类型 space ,资产 property,
  36. * 专业和系统:majorSystem(树),设备和部件equipmentAndComponent,设备类型要以及对应的部件equipmentComponent(树),
  37. * 专业下的设备类型 majorEquipment (树),专业下的系统,系统下的设备majorSystemEquip (树),systemEquip 系统下的设备类(树)
  38. * @param request
  39. * @return
  40. * @throws Exception
  41. */
  42. @Override
  43. public AdmResponse queryCategory(InstanceUrlParam context , AdmDict request, Class<T> clazz) throws Exception {
  44. String type = request.getType();
  45. QueryCriteria criteria = new QueryCriteria();
  46. ObjectNode node = JsonNodeFactory.instance.objectNode();
  47. if(AdmDictCategoryEnum.MAJOR.getValue().equals(type)){
  48. //查询专业分类
  49. node.put("type",AdmDictCategoryEnum.MAJOR.getValue());
  50. criteria.setCriteria(node);
  51. return doQueryDictData(context, criteria, clazz);
  52. }
  53. List<String> baseCategory = getDictBaseCategory();
  54. if(baseCategory.contains(type)){
  55. //查询设备类型:equipment,部件类型:component,输入系统类型:system,输入专业类型:major,业务空间类型 space ,资产 property
  56. node.put("objType",type);
  57. criteria.setCriteria(node);
  58. return doQueryObjectType(context, criteria, clazz);
  59. }
  60. if(AdmDictCategoryEnum.EQUIPMENT_AND_COMPONENT.getValue().equals(type)){
  61. //查询设备类型:equipment,部件类型:component,输入系统类型:system,输入专业类型:major,业务空间类型 space ,资产 property
  62. ArrayNode arrayNode = node.putObject("objType").putArray("$in");
  63. arrayNode.add(AdmDictCategoryEnum.COMPONENT.getValue());
  64. arrayNode.add(AdmDictCategoryEnum.EQUIPMENT.getValue());
  65. criteria.setCriteria(node);
  66. return doQueryObjectType(context, criteria, clazz);
  67. }
  68. List<String> dictTreeCategory = getDictTreeCategory();
  69. if(dictTreeCategory.contains(type)){
  70. //查询树形结构:专业和系统:majorSystem(树),设备类型要以及对应的部件equipmentComponent(树),专业下的设备类型 majorEquipment(树),
  71. //专业下的系统,系统下的设备majorSystemEquip (树),systemEquip 系统下的设备类(树)
  72. return queryDictObjectTypeTree(context ,type,clazz);
  73. }
  74. return null;
  75. }
  76. /**
  77. * 查询字典树
  78. * @param context
  79. * @param type
  80. * @param clazz
  81. */
  82. private AdmResponse queryDictObjectTypeTree(InstanceUrlParam context ,String type, Class<T> clazz){
  83. //查询所有的专业
  84. List<AdmObjectType> allMajor = getAllMajor(context, clazz);
  85. //查询所有的类型
  86. AdmResponse admResponse = doQueryObjectType(context, new QueryCriteria(), clazz);
  87. List<AdmObjectType> objectTypes = (List<AdmObjectType>) admResponse.getContent();
  88. Map<String, List<AdmObjectType>> objectTypeMap = objectTypes.stream().filter(k-> StringUtils.isNotEmpty(k.getObjType()))
  89. .collect(Collectors.groupingBy(AdmObjectType::getObjType));
  90. //获取专业-设备类关系
  91. List<AdmObjectType> equipments = objectTypeMap.get(AdmDictCategoryEnum.EQUIPMENT.getValue());
  92. Map<String,List<AdmObjectType>> majorEquipMap = equipments.stream().filter(k->StringUtils.isNotEmpty(k.getMajorCode()))
  93. .collect(Collectors.groupingBy(item -> item.getMajorCode()));
  94. //获取专业-系统类关系
  95. List<AdmObjectType> systems = objectTypeMap.get(AdmDictCategoryEnum.SYSTEM.getValue());
  96. Map<String, List<AdmObjectType>> majorSystemMap = systems.stream().filter(k->StringUtils.isNotEmpty(k.getMajorCode())).collect(Collectors.groupingBy(item -> item.getMajorCode()));
  97. //获取系统-设备类关系
  98. Map<String, List<AdmObjectType>> systemEquipMap = equipments.stream().filter(k->StringUtils.isNotEmpty(k.getSystemCode())).collect(Collectors.groupingBy(item -> item.getSystemCode()));
  99. //获取设备类-部件关系
  100. List<AdmObjectType> components = objectTypeMap.get(AdmDictCategoryEnum.COMPONENT.getValue());
  101. Map<String, List<AdmObjectType>> equipComponentMap = components.stream().filter(k->StringUtils.isNotEmpty(k.getEquipmentCode())).collect(Collectors.groupingBy(item -> item.getEquipmentCode()));
  102. List<AdmObjectType> content = new ArrayList<>();
  103. if(AdmDictCategoryEnum.MAJOR_SYSTEM.getValue().equals(type)){
  104. //查询专业下的系统
  105. content = buildDictTree(allMajor,majorSystemMap,null);
  106. }
  107. if(AdmDictCategoryEnum.SYSTEM_EQUIP.getValue().equals(type)){
  108. //查询系统下的设备类
  109. content = buildDictTree(systems,systemEquipMap,null);
  110. }
  111. if(AdmDictCategoryEnum.MAJOR_SYSTEM_EQUIP.getValue().equals(type)){
  112. //专业下的系统,系统下的设备
  113. content = buildDictTree(allMajor,majorSystemMap,systemEquipMap);
  114. }
  115. if(AdmDictCategoryEnum.MAJOR_EQUIPMENT.getValue().equals(type)){
  116. //查询专业下的设备类
  117. content = buildDictTree(allMajor,majorEquipMap,null);
  118. }
  119. if(AdmDictCategoryEnum.EQUIPMENT_COMPONENT.getValue().equals(type)){
  120. //查询设备类下的部件
  121. content = buildDictTree(equipments,equipComponentMap,null);
  122. }
  123. // 设置返回值
  124. AdmResponse response = AdmResponse.success(content);
  125. response.setTotal(Long.valueOf(content.size()));
  126. return response;
  127. }
  128. /**
  129. * 构建专业-系统-设备类树形结构
  130. * @param allMajor
  131. * @param majorSystemMap
  132. * @param systemEquipMap
  133. * @return
  134. */
  135. private ArrayList<AdmObjectType> buildDictTree(List<AdmObjectType> allMajor,Map<String, List<AdmObjectType>> majorSystemMap,Map<String, List<AdmObjectType>> systemEquipMap){
  136. ArrayList<AdmObjectType> result = new ArrayList<>();
  137. if(CollUtil.isEmpty(allMajor)){
  138. return result;
  139. }
  140. //构建专业-系统-设备类树形结构
  141. for (AdmObjectType major : allMajor) {
  142. //设置专业
  143. if(CollUtil.isNotEmpty(majorSystemMap)){
  144. //专业下的系统
  145. List<AdmObjectType> systemCodes = majorSystemMap.get(major.getCode());
  146. major.setChildren(systemCodes);
  147. //系统下的设备类
  148. if(CollUtil.isNotEmpty(systemEquipMap) && CollUtil.isNotEmpty(systemCodes)){
  149. for (AdmObjectType systemType : systemCodes) {
  150. systemType.setChildren(systemEquipMap.get(systemType.getCode()));
  151. }
  152. }
  153. }
  154. result.add(major);
  155. }
  156. return result;
  157. }
  158. /**
  159. * 获取所有的专业
  160. * @param context
  161. * @param clazz
  162. * @return
  163. */
  164. private List<AdmObjectType> getAllMajor(InstanceUrlParam context, Class<T> clazz){
  165. //查询专业分类
  166. QueryCriteria criteria = new QueryCriteria();
  167. ObjectNode node = JsonNodeFactory.instance.objectNode();
  168. node.put("type",AdmDictCategoryEnum.MAJOR.getValue());
  169. criteria.setCriteria(node);
  170. AdmResponse admResponse = doQueryDictData(context, criteria, clazz);
  171. List<AdmObjectType> content = (List<AdmObjectType>) admResponse.getContent();
  172. return content;
  173. }
  174. /**
  175. * 基础数据类型
  176. * @return
  177. */
  178. private List<String> getDictBaseCategory(){
  179. List<String> category = new ArrayList<>();
  180. category.add(AdmDictCategoryEnum.EQUIPMENT.getValue());
  181. category.add(AdmDictCategoryEnum.COMPONENT.getValue());
  182. category.add(AdmDictCategoryEnum.SYSTEM.getValue());
  183. category.add(AdmDictCategoryEnum.SPACE.getValue());
  184. category.add(AdmDictCategoryEnum.PROPRTY.getValue());
  185. return category;
  186. }
  187. /**
  188. * 查询字典树形结构
  189. * @return
  190. */
  191. private List<String> getDictTreeCategory(){
  192. List<String> category = new ArrayList<>();
  193. category.add(AdmDictCategoryEnum.MAJOR_SYSTEM.getValue());
  194. category.add(AdmDictCategoryEnum.MAJOR_EQUIPMENT.getValue());
  195. category.add(AdmDictCategoryEnum.MAJOR_SYSTEM_EQUIP.getValue());
  196. category.add(AdmDictCategoryEnum.EQUIPMENT_COMPONENT.getValue());
  197. category.add(AdmDictCategoryEnum.SYSTEM_EQUIP.getValue());
  198. return category;
  199. }
  200. /**
  201. * 重置项目下的信息点
  202. * @param context
  203. * @param clazz
  204. * @return
  205. * @throws Exception
  206. */
  207. @Override
  208. public AdmResponse resetDic(InstanceUrlParam context, AdmDictQueryCriteria request, String fieldName, Class<T> clazz) throws Exception {
  209. //查询项目下点所有信息点
  210. AdmResponse admResponse = doQueryObjectInfo(context, request, fieldName, clazz);
  211. List<AdmObjectInfo> content = (List<AdmObjectInfo>) admResponse.getContent();
  212. if(CollUtil.isEmpty(content)){
  213. AdmResponse response = AdmResponse.success(new ArrayList<>());
  214. response.setTotal(Long.valueOf(0));
  215. return response;
  216. }
  217. //因BDTP根据项目查询信息点时返回的包含集团+通用信息点,需手动过滤除项目级的信息点
  218. String projectId = context.getProjectId();
  219. Iterator<AdmObjectInfo> infoIterator = content.iterator();
  220. while (infoIterator.hasNext()){
  221. String admProjectId = infoIterator.next().getProjectId();
  222. if(StringUtils.isEmpty(admProjectId) || !projectId.equals(admProjectId)){
  223. infoIterator.remove();
  224. }
  225. }
  226. //删除信息点
  227. doDeleteObjectInfo(context,(List<T>)content);
  228. // 设置返回值
  229. AdmResponse response = AdmResponse.success(content);
  230. response.setTotal(Long.valueOf(content.size()));
  231. return response;
  232. }
  233. /**
  234. * 设置-项目定制化字典
  235. * @param context
  236. * @param request
  237. * @param clazz
  238. * @return
  239. * @throws Exception
  240. */
  241. @Override
  242. public AdmResponse settingDicProject(InstanceUrlParam context, AdmCreateRequest<AdmObjectInfo> request, Class<T> clazz) throws Exception {
  243. List<AdmObjectInfo> content = request.getContent();
  244. //信息点转换为中台格式
  245. List<AdmObjectInfo> dmpObjectInfo = new ArrayList<>();
  246. objectInfosTransforDmp(content, dmpObjectInfo ,context.getProjectId());
  247. request.setContent(dmpObjectInfo);
  248. //修改项目级信息点
  249. List<T> vos = doUpdateObjectInfo(context, clazz, (List<T>) dmpObjectInfo);
  250. return AdmResponse.success(vos);
  251. }
  252. /**
  253. * 查询所有字典信息 M 和 L 类型,包括自定义信息点
  254. * @param context
  255. * @param dictTypeList
  256. * @return
  257. */
  258. @Override
  259. public AdmResponse queryMorlDict(InstanceUrlParam context, List<String> dictTypeList) {
  260. List<AdmObjectInfo> result = new ArrayList<>();
  261. for (String type : dictTypeList) {
  262. List<String> classCodes = new ArrayList<>();
  263. String name = null;
  264. //项目
  265. if(AdmDictCategoryEnum.PROJECT.getValue().equals(type)){
  266. name = AdmDictCategoryEnum.PROJECT.getDesc();
  267. classCodes.add(AdmDictCategoryEnum.PROJECT.getValue());
  268. }
  269. //建筑
  270. if(AdmDictCategoryEnum.BUILDING.getValue().equals(type)){
  271. name = AdmDictCategoryEnum.BUILDING.getDesc();
  272. classCodes.add(AdmDictCategoryEnum.BUILDING.getValue());
  273. }
  274. //楼层
  275. if(AdmDictCategoryEnum.FLOOR.getValue().equals(type)){
  276. name = AdmDictCategoryEnum.FLOOR.getDesc();
  277. classCodes.add(AdmDictCategoryEnum.FLOOR.getValue());
  278. }
  279. //设备
  280. if(AdmDictCategoryEnum.EQUIPMENT.getValue().equals(type)){
  281. name = AdmDictCategoryEnum.EQUIPMENT.getDesc();
  282. classCodes.addAll(queryDefClassType(context, AdmDictCategoryEnum.EQUIPMENT.getValue()));
  283. }
  284. //部件
  285. if(AdmDictCategoryEnum.COMPONENT.getValue().equals(type)){
  286. name = AdmDictCategoryEnum.COMPONENT.getDesc();
  287. classCodes.addAll(queryDefClassType(context, AdmDictCategoryEnum.COMPONENT.getValue()));
  288. }
  289. //系统
  290. if(AdmDictCategoryEnum.SYSTEM.getValue().equals(type)){
  291. name = AdmDictCategoryEnum.SYSTEM.getDesc();
  292. classCodes.addAll(queryDefClassType(context, AdmDictCategoryEnum.SYSTEM.getValue()));
  293. }
  294. //空间
  295. if(AdmDictCategoryEnum.SPACE.getValue().equals(type)){
  296. name = AdmDictCategoryEnum.SPACE.getDesc();
  297. classCodes.addAll(queryDefClassType(context, AdmDictCategoryEnum.SPACE.getValue()));
  298. }
  299. //查询类型下的信息点
  300. statisticObjectInfo(context,type, name, classCodes,result);
  301. }
  302. AdmResponse response = AdmResponse.success(result);
  303. response.setTotal(Long.valueOf(result.size()));
  304. return response;
  305. }
  306. /**
  307. * 统计类型下的非静态信息点
  308. * @param context
  309. * @param code
  310. * @param name
  311. * @param result
  312. */
  313. private void statisticObjectInfo(InstanceUrlParam context,String code,String name,List<String> classCode,List<AdmObjectInfo> result){
  314. //根据类型查询信息点
  315. AdmQueryCriteria criteria = new AdmQueryCriteria();
  316. StringBuilder stringBuilder = new StringBuilder();
  317. classCode.stream().forEach(item -> {
  318. stringBuilder.append("'").append(item).append("'").append(",");
  319. });
  320. String filters = StrUtil.sub(stringBuilder.toString(),0, -1);
  321. criteria.setFilters("classCode in ["+filters+"]");
  322. AdmResponse response = doQueryObjectInfo(context, criteria, AdmDictConstant.CLASS_CODE, (Class<T>) AdmObjectInfo.class);
  323. List<AdmObjectInfo> content = (List<AdmObjectInfo>) response.getContent();
  324. if(CollUtil.isEmpty(content)){
  325. return ;
  326. }
  327. //过滤不需要的信息点
  328. Iterator<AdmObjectInfo> iterator = content.iterator();
  329. while (iterator.hasNext()){
  330. AdmObjectInfo objectInfo = iterator.next();
  331. if(StringUtils.isNotEmpty(objectInfo.getCategory()) && "STATIC".equals(objectInfo.getCategory())){
  332. iterator.remove();
  333. }
  334. }
  335. //返回结果拼装
  336. AdmObjectInfo admObjectInfo = new AdmObjectInfo();
  337. admObjectInfo.setCode(code);
  338. admObjectInfo.setName(name);
  339. admObjectInfo.setContent(content);
  340. result.add(admObjectInfo);
  341. }
  342. /**
  343. * 查询对象类型
  344. * @param context
  345. * @param code
  346. * @return
  347. */
  348. private List<String> queryDefClassType(InstanceUrlParam context,String code){
  349. QueryCriteria criteria = new QueryCriteria();
  350. ObjectNode node = JsonNodeFactory.instance.objectNode();
  351. node.put("parentCode",code);
  352. criteria.setCriteria(node);
  353. AdmResponse response = doQueryObjectType(context, criteria, (Class<T>) AdmObjectType.class);
  354. List<AdmObjectType> content = (List<AdmObjectType>) response.getContent();
  355. if(CollUtil.isEmpty(content)){
  356. return new ArrayList<>();
  357. }
  358. return content.stream().map(AdmObjectType::getCode).collect(Collectors.toList());
  359. }
  360. /**
  361. * 信息点转换为中台格式
  362. * @param content
  363. * @param dmpObjectInfos
  364. */
  365. private void objectInfosTransforDmp(List<AdmObjectInfo> content,List<AdmObjectInfo> dmpObjectInfos,String projectId){
  366. if(CollUtil.isEmpty(content)){
  367. return;
  368. }
  369. for (AdmObjectInfo admObjectInfo : content) {
  370. admObjectInfo.setProjectId(projectId);
  371. if(admObjectInfo.getMultiple() != null){
  372. admObjectInfo.setIsMultiple(admObjectInfo.getMultiple());
  373. }
  374. if(admObjectInfo.getIsRegion() != null){
  375. admObjectInfo.setIsRegion(admObjectInfo.getRegion());
  376. }
  377. if(StringUtils.isNotEmpty(admObjectInfo.getFirstName())){
  378. admObjectInfo.setFirstTag(admObjectInfo.getFirstName());
  379. }
  380. if(StringUtils.isNotEmpty(admObjectInfo.getSecondName())){
  381. admObjectInfo.setSecondTag(admObjectInfo.getSecondName());
  382. }
  383. admObjectInfo.setType(null);
  384. dmpObjectInfos.add(admObjectInfo);
  385. //处理子级
  386. List<AdmObjectInfo> sencondContent = admObjectInfo.getContent();
  387. if(CollUtil.isNotEmpty(sencondContent)){
  388. objectInfosTransforDmp(sencondContent,dmpObjectInfos,projectId);
  389. }
  390. }
  391. }
  392. }