package com.dragoninfo.dcuc.app.facade; import com.dragoninfo.dcuc.app.business.IDataCatalogBusiness; import com.dragoninfo.dcuc.app.cons.CommonCons; import com.dragoninfo.dcuc.app.dto.dataresource.ChildResourceClassify; import com.dragoninfo.dcuc.app.dto.dataresource.ResourceClassify; import com.dragoninfo.dcuc.app.dto.sub.DataClaAcceptDTO; import com.dragoninfo.dcuc.app.dto.sub.FieldClaAcceptDTO; import com.dragoninfo.dcuc.app.entity.sub.*; import com.dragoninfo.dcuc.app.enumresources.AppDataTypeEnum; import com.dragoninfo.dcuc.app.enumresources.DataAttrTypeEnum; import com.dragoninfo.dcuc.app.enumresources.DataResourceEnum; import com.dragoninfo.dcuc.app.enumresources.sub.DataClaTypeEnum; import com.dragoninfo.dcuc.app.enumresources.sub.FieldClaTypeEnum; import com.dragoninfo.dcuc.app.service.IDataResourceService; import com.dragoninfo.dcuc.app.service.sub.*; import com.dragoninfo.dcuc.app.vo.*; import com.dragonsoft.duceap.base.entity.http.ResponseStatus; import com.dragonsoft.duceap.commons.util.collections.CollectionUtils; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Created by lidr on 2021/4/14 */ @Slf4j @RestController @RequestMapping(value = "/dcuc/app/dataResourceFacade") public class DataResourceFacade implements IDataResourceFacade { @Autowired private IDataResourceService dataResourceService; @Autowired private IDataClaService dataClaService; @Autowired private IDataLevelService dataLevelService; @Autowired private IDataSecService dataSecService; @Autowired private IFieldClaService fieldClaService; @Autowired private IAppDataResourceInfoService tabInfoService; @Autowired private IAppDataItemInfoService colInfoService; @Autowired private IDataCatalogBusiness dataCatalogBusiness; @Autowired private IAppColumnRelationService appColumnRelationService; @Override public List getAllDataResourceTree(String attrType) { List result = Lists.newArrayList(); //列:数据安全级别 字段分类 //表:数据分级 数据资源分类 if (DataAttrTypeEnum.DATA_ATTR_TABLE.getValue().equals(attrType)) { // 查询所有的表和列 List tabInfos = tabInfoService.getHasClassifyTables(); result.add(getDataLevelTree()); result.add(getDataClassifyTree(tabInfos)); } else if (DataAttrTypeEnum.DATA_ATTR_COLUMN.getValue().equals(attrType)) { List tabInfos = tabInfoService.getAllTabInfos(); List colInfos = colInfoService.getHasClassifyColInfos(); result.add(getSecurityLevelTree()); result.add(getFieldClassifyTree(tabInfos, colInfos)); } else { List tabInfos = tabInfoService.getAllTabInfos(); List colInfos = colInfoService.getHasClassifyColInfos(); result.add(getDataLevelTree()); result.add(getDataClassifyTree(tabInfos)); result.add(getSecurityLevelTree()); result.add(getFieldClassifyTree(tabInfos, colInfos)); } return result; } @Override public DataResourceClassifyVo getDataResourceTree(String typeCode) { DataResourceClassifyVo result = null; DataResourceEnum dataResourceEnum = Enum.valueOf(DataResourceEnum.class, typeCode); List colInfos = null; List tabInfos = null; switch (dataResourceEnum) { case DATA_CLASSIFY: result = getDataLevelTree(); break; case COLUMN_CLASSIFY: tabInfos = tabInfoService.getAllTabInfos(); colInfos = colInfoService.getHasClassifyColInfos(); result = getFieldClassifyTree(tabInfos, colInfos); break; case DATA_SECURITY_LEVEL: result = getSecurityLevelTree(); break; case DATA_RESOURCE_CLASSIFY: tabInfos = tabInfoService.getHasClassifyTables(); result = getDataClassifyTree(tabInfos); break; default: break; } return result; } private DataResourceClassifyVo getDataLevelTree() { DataResourceEnum dataResourceEnum = DataResourceEnum.DATA_CLASSIFY; DataResourceClassifyVo dataResourceClassifyVo = DataResourceClassifyVo.builder() .id(dataResourceEnum.getCode()) .attrType(dataResourceEnum.getAttrType().getValue()) .label(dataResourceEnum.getLabel()) .build(); DataResourceTreeVo dataLevelTreeVo = DataResourceTreeVo.builder() .id(dataResourceEnum.getCode()) .label("全部") .code(dataResourceEnum.getCode()) .treeNode(true) .build(); List dataLevelList = getDataLevelList(); List child = dataLevelList.stream().map(item -> DataResourceTreeVo.builder() .id(item.getId()) .code(item.getLevelCode()) .label(item.getLevelName()) .dataType(item.getTypeCode()) .treeNode(false) .pId(dataLevelTreeVo.getId()) .build()).collect(Collectors.toList()); dataLevelTreeVo.setChild(child); if (child.size() == 0) { dataResourceClassifyVo.setNodes(new ArrayList<>()); } else { dataResourceClassifyVo.setNodes(new ArrayList() {{ add(dataLevelTreeVo); }}); } //设置总数量 dataResourceClassifyVo.setTotal(child.size()); return dataResourceClassifyVo; } private DataResourceClassifyVo getSecurityLevelTree() { DataResourceEnum dataResourceEnum = DataResourceEnum.DATA_SECURITY_LEVEL; DataResourceClassifyVo dataResourceClassifyVo = DataResourceClassifyVo.builder() .id(dataResourceEnum.getCode()) .attrType(dataResourceEnum.getAttrType().getValue()) .label(dataResourceEnum.getLabel()) .build(); DataResourceTreeVo securityLevelTree = DataResourceTreeVo.builder() .id(dataResourceEnum.getCode()) .label("全部") .code(dataResourceEnum.getCode()) .treeNode(true) .build(); List securityLevelList = getSecurityLevelList(); List child = securityLevelList.stream().map(item -> DataResourceTreeVo.builder() .id(item.getId()) .code(item.getLevelCode()) .label(item.getLevelName()) .dataType(item.getTypeCode()) .treeNode(false) .pId(securityLevelTree.getId()) .build()).collect(Collectors.toList()); securityLevelTree.setChild(child); if (child.size() == 0) { dataResourceClassifyVo.setNodes(new ArrayList<>()); dataResourceClassifyVo.setTotal(0); } else { dataResourceClassifyVo.setNodes(new ArrayList() {{ add(securityLevelTree); }}); //设置总数量 dataResourceClassifyVo.setTotal(child.size()); } return dataResourceClassifyVo; } private DataResourceClassifyVo getDataClassifyTree(List tabInfos) { // 将表分类映射到表 Map> tabLabMap = getTabLabGroupMap(tabInfos); DataResourceEnum dataResourceEnum = DataResourceEnum.DATA_RESOURCE_CLASSIFY; DataResourceClassifyVo resourceVo = DataResourceClassifyVo.builder().id(dataResourceEnum.getCode()) .attrType(dataResourceEnum.getAttrType().getValue()).label(dataResourceEnum.getLabel()).build(); DataResourceTreeVo topTreeVo = DataResourceTreeVo.builder().id(dataResourceEnum.getCode()).label("全部") .code(dataResourceEnum.getCode()).treeNode(true).build(); List dataClaList = dataClaService.getAllDataCla(); Map> levelMap = dataClaList.stream().collect(Collectors.groupingBy(DataCla::getLevel)); //一级节点 List firNodes = Optional.ofNullable(levelMap.get(DataClaTypeEnum.FIR.getLevel())).orElse(new ArrayList<>()) .stream().map(item -> convertDataClaToNode(item, null)).collect(Collectors.toList()); //二级节点 List secNodes = Optional.ofNullable(levelMap.get(DataClaTypeEnum.SEC.getLevel())).orElse(new ArrayList<>()) .stream().map(item -> convertDataClaToNode(item, item.getFirId())).collect(Collectors.toList()); //三级节点 List thirdNodes = Optional.ofNullable(levelMap.get(DataClaTypeEnum.LAB_CLA.getLevel())).orElse(new ArrayList<>()) .stream().map(item -> convertDataClaToNode(item, item.getSecId())).collect(Collectors.toList()); //四级节点 List fourNodes = Optional.ofNullable(levelMap.get(DataClaTypeEnum.LAB.getLevel())).orElse(new ArrayList<>()) .stream().map(item -> convertDataClaToNode(item, item.getLabClaId())).collect(Collectors.toList()); //拼接成树节点 combineTreeNode(firNodes, secNodes); combineTreeNode(secNodes, thirdNodes); combineTreeNode(thirdNodes, fourNodes); topTreeVo.setChild(firNodes); if (firNodes.size() == 0) { resourceVo.setNodes(new ArrayList<>()); resourceVo.setTotal(0); } else { // 拼接表、列、pid boolean b = combinePidAndTableInfo(topTreeVo, tabLabMap); if (b) { resourceVo.setNodes(new ArrayList<>()); resourceVo.setTotal(0); } else { resourceVo.setNodes(new ArrayList() {{ add(topTreeVo); }}); } } return resourceVo; } private Map> getTabLabGroupMap(List tabInfos) { Map> group = new HashMap<>(); tabInfos.forEach(e -> { // 一级分类 String claFir = e.getDataClassifyOneCode(); if (StringUtils.isBlank(claFir)) { return; } // 二级分类 String claSec = e.getDataClassifyTwoCode(); if (StringUtils.isBlank(claSec)) { putInMap(group, e, claFir); return; } // 标签1 String labFir = e.getDataLabelOneCode(); if (StringUtils.isNotBlank(labFir)) { labFir = CommonCons.LAB_CLA_FIR + CommonCons.ID_SEPARATOR + labFir; } // 标签2 String labSec = e.getDataLabelTwoCode(); if (StringUtils.isNotBlank(labSec)) { labSec = CommonCons.LAB_CLA_SEC + CommonCons.ID_SEPARATOR + labSec; } // 标签3 String labThr = e.getDataLabelThreeCode(); if (StringUtils.isNotBlank(labThr)) { labThr = CommonCons.LAB_CLA_THI + CommonCons.ID_SEPARATOR + labThr; } // 标签4 String labFour = e.getDataLabelFourCode(); if (StringUtils.isNotBlank(labFour)) { labFour = CommonCons.LAB_CLA_FOUR + CommonCons.ID_SEPARATOR + labFour; } // 标签5 String labFiv = e.getDataLabelFiveCode(); if (StringUtils.isNotBlank(labFiv)) { labFiv = CommonCons.LAB_CLA_FIV + CommonCons.ID_SEPARATOR + labFiv; } List collect = Stream.of(labFir, labSec, labThr, labFour, labFiv).filter(StringUtils::isNotBlank).collect(Collectors.toList()); String key = claFir + CommonCons.ID_SEPARATOR + claSec; if (CollectionUtils.isEmpty(collect)) { putInMap(group, e, key); return; } collect.forEach(lab -> { String s = key + CommonCons.ID_SEPARATOR + lab; putInMap(group, e, s); }); }); return group; } private void putInMap(Map> group, AppDataResourceInfo e, String claFir) { List list = group.get(claFir); if (null == list) { list = new ArrayList<>(); } list.add(e); group.put(claFir, list); } private DataResourceTreeVo convertDataClaToNode(DataCla item, String pid) { return DataResourceTreeVo.builder() .id(item.getId()) .pId(pid) .dataType(item.getDataType()) .label(item.getName()) .code(item.getCode()) .build(); } private DataResourceClassifyVo getFieldClassifyTree(List tabInfos, List colInfos) { Map tableMap = tabInfos .stream() .collect(Collectors.toMap(AppDataResourceInfo::getDataObjectId, e -> e)); Map> colInfosMap = getColumnClaGroupMap(colInfos); DataResourceEnum dataResourceEnum = DataResourceEnum.COLUMN_CLASSIFY; DataResourceClassifyVo resourceVo = DataResourceClassifyVo.builder().id(dataResourceEnum.getCode()) .attrType(dataResourceEnum.getAttrType().getValue()).label(dataResourceEnum.getLabel()).build(); DataResourceTreeVo topTreeVo = DataResourceTreeVo.builder().id(dataResourceEnum.getCode()).label("全部") .code(dataResourceEnum.getCode()).treeNode(true).build(); List fieldClaList = fieldClaService.getAllFieldCla(); Map> levelMap = fieldClaList.stream().collect(Collectors.groupingBy(FieldCla::getLevel)); List firNodes = Optional.ofNullable(levelMap.get(FieldClaTypeEnum.FIR.getLevel())).orElse(new ArrayList<>()) .stream().map(item -> convertFieldClaToNode(item, null)).collect(Collectors.toList()); List secNodes = Optional.ofNullable(levelMap.get(FieldClaTypeEnum.SEC.getLevel())).orElse(new ArrayList<>()) .stream().map(item -> convertFieldClaToNode(item, item.getFirId())).collect(Collectors.toList()); combineTreeNode(firNodes, secNodes); topTreeVo.setChild(firNodes); if (firNodes.size() == 0) { resourceVo.setNodes(new ArrayList<>()); resourceVo.setTotal(0); } else { boolean b = combinePidTabAndCol(topTreeVo, tableMap, colInfosMap); if (b) { resourceVo.setNodes(new ArrayList<>()); resourceVo.setTotal(0); } else { resourceVo.setNodes(new ArrayList() {{ add(topTreeVo); }}); } } return resourceVo; } private Map> getColumnClaGroupMap(List colInfos) { return colInfos.stream().collect(Collectors.groupingBy(AppDataItemInfo::getColumnClassify)); } private boolean combinePidTabAndCol(DataResourceTreeVo treeVo, Map tableMap, Map> colInfosMap) { List child = treeVo.getChild(); Iterator iterator = child.iterator(); while (iterator.hasNext()) { DataResourceTreeVo childVo = iterator.next(); childVo.setPId(treeVo.getId()); childVo.setId(Joiner.on(CommonCons.ID_SEPARATOR).join(treeVo.getId(), childVo.getCode())); List cc = childVo.getChild(); if (CollectionUtils.isNotEmpty(cc)) { childVo.setTreeNode(true); boolean emptyChild = combinePidTabAndCol(childVo, tableMap, colInfosMap); if (emptyChild) { iterator.remove(); } } else { // 如果为叶子节点,查找对应的表和列 // 如果查找不到表和列则移除 List dataColInfo = colInfosMap.get(childVo.getCode()); if (dataColInfo == null) { iterator.remove(); } else { Map> tableGroup = dataColInfo.stream() .collect(Collectors.groupingBy(AppDataItemInfo::getResourceTableId)); List tableVos = tableGroup.entrySet().stream().map(e -> { String tableId = e.getKey(); AppDataResourceInfo table = tableMap.get(tableId); return convertTableToTreeVo(childVo.getId(), Collections.singletonList(table), tableGroup); }).flatMap(List::stream).collect(Collectors.toList()); childVo.setTreeNode(true); childVo.setChild(tableVos); } } } return CollectionUtils.isEmpty(child); } private DataResourceTreeVo convertFieldClaToNode(FieldCla item, String pid) { return DataResourceTreeVo.builder() .id(item.getId()) .pId(pid) .dataType(item.getDataType()) .label(item.getName()) .code(item.getCode()) .build(); } private void combineTreeNode(List firNodes, List secNodes) { Map> secMap = secNodes.stream().collect(Collectors.groupingBy(DataResourceTreeVo::getPId)); firNodes.forEach(item -> { List childes = secMap.get(item.getId()); item.setChild(childes); }); } @Override public List getDataLevelList() { List all = dataLevelService.getAll(); return all.stream() .map(item -> new DataLevelVo(item.getId(), item.getLevelCode(), item.getLevelName(), item.getDataType())) .collect(Collectors.toList()); } @Override public List getSecurityLevelList() { List all = dataSecService.getAll(); return all.stream() .map(item -> new DataLevelVo(item.getId(), item.getSecCode(), item.getSecName(), item.getDataType())) .collect(Collectors.toList()); } @Override public List getDataClassifyList() { List list = dataClaService.getAll(); return list.stream().map(item -> { DataClassifyVo vo = new DataClassifyVo(); vo.setFirstLevelCode(item.getFirCode()); vo.setFirstLevelName(item.getFirName()); vo.setSecondLevelCode(item.getSecCode()); vo.setSecondLevelName(item.getSecName()); vo.setLabelClassifyCode(item.getLabClaCode()); vo.setLabelClassifyName(item.getLabClaName()); vo.setLabelCode(item.getLabCode()); vo.setLabelName(item.getLabName()); return vo; }).collect(Collectors.toList()); } @Override public List getFieldClassifyList() { List list = fieldClaService.getAll(); return list.stream().map(item -> { DataFieldClassifyVo vo = new DataFieldClassifyVo(); vo.setFirstLevelCode(item.getFirCode()); vo.setFirstLevelName(item.getFirName()); vo.setSecondLevelCode(item.getSecCode()); vo.setSecondLevelName(item.getSecName()); return vo; }).collect(Collectors.toList()); } @Override public ResponseStatus dataLevelSync() { //因为子线程中需要调用总线需要用到请求头中的信息 //需要将requestAttributes设置到子线程中的RequestContextHolder //SpringWeb暂时未提供RequestContextHolder父子线程共享ThreadLocal中的数据的配置,需要手动设置 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //开启父子线程 //方法内部有从ThreadLocal获取信息 //CompletableFuture默认使用ForkJoin线程池,不要使用1.8的新线程开启方法 //如果使用线程池需要用包装类将线程包装后设置RequestContextHolder Runnable r = () -> { RequestContextHolder.setRequestAttributes(requestAttributes); try { List list = getDataLevelListFromRemote(); dataLevelService.sync(list); } finally { RequestContextHolder.resetRequestAttributes(); } }; Thread t = new Thread(r); t.start(); return ResponseStatus.success(); } @Override public ResponseStatus columnRelationSync() { //因为子线程中需要调用总线需要用到请求头中的信息 //需要将requestAttributes设置到子线程中的RequestContextHolder //SpringWeb暂时未提供RequestContextHolder父子线程共享ThreadLocal中的数据的配置,需要手动设置 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //开启父子线程 //方法内部有从ThreadLocal获取信息 //CompletableFuture默认使用ForkJoin线程池,不要使用1.8的新线程开启方法 //如果使用线程池需要用包装类将线程包装后设置RequestContextHolder Runnable r = () -> { RequestContextHolder.setRequestAttributes(requestAttributes); try { List list = getColumnRelationCatalogFromRemote(); appColumnRelationService.sync(list); } finally { RequestContextHolder.resetRequestAttributes(); } }; Thread t = new Thread(r); t.start(); return ResponseStatus.success(); } @Override public ResponseStatus dataSecSync() { //因为子线程中需要调用总线需要用到请求头中的信息 //需要将requestAttributes设置到子线程中的RequestContextHolder //SpringWeb暂时未提供RequestContextHolder父子线程共享ThreadLocal中的数据的配置,需要手动设置 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //开启父子线程 //方法内部有从ThreadLocal获取信息 //CompletableFuture默认使用ForkJoin线程池,不要使用1.8的新线程开启方法 //如果使用线程池需要用包装类将线程包装后设置RequestContextHolder Runnable r = () -> { RequestContextHolder.setRequestAttributes(requestAttributes); try { List list = getSecurityLevelListFromRemote(); dataSecService.sync(list); } finally { RequestContextHolder.resetRequestAttributes(); } }; Thread t = new Thread(r); t.start(); return ResponseStatus.success(); } @Override public ResponseStatus dataClaSync() { //因为子线程中需要调用总线需要用到请求头中的信息 //需要将requestAttributes设置到子线程中的RequestContextHolder //SpringWeb暂时未提供RequestContextHolder父子线程共享ThreadLocal中的数据的配置,需要手动设置 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //开启父子线程 //方法内部有从ThreadLocal获取信息 //CompletableFuture默认使用ForkJoin线程池,不要使用1.8的新线程开启方法 //如果使用线程池需要用包装类将线程包装后设置RequestContextHolder Runnable r = () -> { try { RequestContextHolder.setRequestAttributes(requestAttributes); List nodes = getDataClassifyTreeFromRemote(); dataClaService.sync(nodes, null); } finally { RequestContextHolder.resetRequestAttributes(); } }; Thread t = new Thread(r); t.start(); return ResponseStatus.success(); } @Override public ResponseStatus fieldClaSync() { //因为子线程中需要调用总线需要用到请求头中的信息 //需要将requestAttributes设置到子线程中的RequestContextHolder //SpringWeb暂时未提供RequestContextHolder父子线程共享ThreadLocal中的数据的配置,需要手动设置 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //开启父子线程 //方法内部有从ThreadLocal获取信息 //CompletableFuture默认使用ForkJoin线程池,不要使用1.8的新线程开启方法 //如果使用线程池需要用包装类将线程包装后设置RequestContextHolder Runnable r = () -> { try { RequestContextHolder.setRequestAttributes(requestAttributes); List nodes = getFieldClassifyTreeFromRemote(); fieldClaService.sync(nodes, null); } finally { RequestContextHolder.resetRequestAttributes(); } }; Thread t = new Thread(r); t.start(); return ResponseStatus.success(); } @Override public ResponseStatus syncTableAndColumn() { dataCatalogBusiness.syncTableAndColumn(); return ResponseStatus.success(); } @Override public List getDetailByIdsAndClaType(List ids, String classifyType) { List vos = new ArrayList<>(); if (CollectionUtils.isEmpty(ids)) { return vos; } if (StringUtils.isBlank(classifyType)) { return vos; } DataResourceEnum anEnum = DataResourceEnum.getByCode(classifyType); if (null == anEnum) { return vos; } else if (DataResourceEnum.COLUMN_CLASSIFY.equals(anEnum)) { List list = fieldClaService.getByIds(ids); list.stream().map(item -> { DataLevelVo vo = new DataLevelVo(); vo.setTypeCode(item.getDataType()); vo.setLevelName(item.getName()); vo.setLevelCode(item.getCode()); vo.setId(item.getId()); return vo; }).collect(Collectors.toList()); } else if (DataResourceEnum.DATA_CLASSIFY.equals(anEnum)) { List list = dataLevelService.getByIds(ids); list.stream().map(item -> { DataLevelVo vo = new DataLevelVo(); vo.setTypeCode(item.getDataType()); vo.setLevelName(item.getLevelName()); vo.setLevelCode(item.getLevelCode()); vo.setId(item.getId()); return vo; }).collect(Collectors.toList()); } else if (DataResourceEnum.DATA_RESOURCE_CLASSIFY.equals(anEnum)) { List list = dataClaService.getByIds(ids); list.stream().map(item -> { DataLevelVo vo = new DataLevelVo(); vo.setTypeCode(item.getDataType()); vo.setLevelName(item.getName()); vo.setLevelCode(item.getCode()); vo.setId(item.getId()); return vo; }).collect(Collectors.toList()); } else if (DataResourceEnum.DATA_SECURITY_LEVEL.equals(anEnum)) { List list = dataSecService.getByIds(ids); list.stream().map(item -> { DataLevelVo vo = new DataLevelVo(); vo.setTypeCode(item.getDataType()); vo.setLevelName(item.getSecName()); vo.setLevelCode(item.getSecCode()); vo.setId(item.getId()); return vo; }).collect(Collectors.toList()); } return vos; } @Override public DataLevelVo getDetailByIdAndClaType(String id, String classifyType) { if (StringUtils.isAnyBlank(id, classifyType)) { return null; } DataLevelVo vo = new DataLevelVo(); DataResourceEnum anEnum = DataResourceEnum.getByCode(classifyType); if (null == anEnum) { return null; } else if (DataResourceEnum.COLUMN_CLASSIFY.equals(anEnum)) { FieldCla fieldCla = fieldClaService.getById(id); vo.setId(fieldCla.getId()); vo.setLevelCode(fieldCla.getCode()); vo.setLevelName(fieldCla.getName()); vo.setTypeCode(fieldCla.getDataType()); } else if (DataResourceEnum.DATA_CLASSIFY.equals(anEnum)) { DataLevel dataLevel = dataLevelService.getById(id); vo.setId(dataLevel.getId()); vo.setLevelCode(dataLevel.getLevelCode()); vo.setLevelName(dataLevel.getLevelName()); vo.setTypeCode(dataLevel.getDataType()); } else if (DataResourceEnum.DATA_RESOURCE_CLASSIFY.equals(anEnum)) { DataCla dataCla = dataClaService.getById(id); vo.setId(dataCla.getId()); vo.setLevelCode(dataCla.getId()); vo.setLevelName(dataCla.getName()); vo.setTypeCode(dataCla.getCode()); } else if (DataResourceEnum.DATA_SECURITY_LEVEL.equals(anEnum)) { FieldSec dataSec = dataSecService.getById(id); vo.setId(dataSec.getId()); vo.setLevelCode(dataSec.getSecCode()); vo.setLevelName(dataSec.getSecName()); vo.setTypeCode(dataSec.getDataType()); } return vo; } /** * 数据资源分类 * * @return */ private List getDataClassifyTreeFromRemote() { String id = DataResourceEnum.DATA_RESOURCE_CLASSIFY.getCode(); List collect = getFirstChildResourceClassifies(dataResourceService.getDataClassifyList()) .stream() .map(firstClassify -> { DataResourceTreeVo firstTreeVo = DataResourceTreeVo.builder() .id(Joiner.on(CommonCons.ID_SEPARATOR).join(id, firstClassify.getCodeValue())) .code(firstClassify.getCodeValue()) .label(firstClassify.getCodeName()) .treeNode(true) .pId(id) .dataType(firstClassify.getTypeCode()) .build(); firstTreeVo.setChild(getFirstChildResourceClassifies(firstClassify.getChildrenType()).stream() .map(secondClassify -> { DataResourceTreeVo secondTreeVo = DataResourceTreeVo.builder() .id(Joiner.on(CommonCons.ID_SEPARATOR).join(firstTreeVo.getId(), secondClassify.getCodeValue())) .code(secondClassify.getCodeValue()) .label(secondClassify.getCodeName()) .treeNode(true) .pId(firstTreeVo.getId()) .dataType(secondClassify.getTypeCode()) .build(); secondTreeVo.setChild(getChildrenTypeList(secondClassify.getChildrenType()).stream() .map(thirdClassify -> { DataResourceTreeVo thirdTreeVo = DataResourceTreeVo.builder() .id(Joiner.on(CommonCons.ID_SEPARATOR).join(secondTreeVo.getId(), thirdClassify.getTypeCode())) .code(thirdClassify.getTypeCode()) .label(thirdClassify.getTypeName()) .treeNode(true) .pId(secondTreeVo.getId()) .dataType(thirdClassify.getTypeCode()) .build(); thirdTreeVo.setChild(thirdClassify.getDataList().stream() .map(fourClassify -> { DataResourceTreeVo fourTreeVo = DataResourceTreeVo.builder() .id(Joiner.on(CommonCons.ID_SEPARATOR).join(thirdTreeVo.getId(), fourClassify.getCodeValue())) .code(fourClassify.getCodeValue()) .label(fourClassify.getCodeName()) .treeNode(false).child(null) .pId(thirdTreeVo.getId()) .dataType(thirdClassify.getTypeCode()) .build(); return fourTreeVo; }).collect(Collectors.toList())); return thirdTreeVo; }).collect(Collectors.toList())); return secondTreeVo; }).collect(Collectors.toList())); return firstTreeVo; }).collect(Collectors.toList()); return collect; } /** * 字段分类 * * @return */ private List getFieldClassifyTreeFromRemote() { String id = DataResourceEnum.COLUMN_CLASSIFY.getCode(); List collect = getFirstChildResourceClassifies(dataResourceService.getFieldClassifyList()) .stream() .map(firstClassify -> { DataResourceTreeVo firstTreeVo = DataResourceTreeVo.builder() .id(Joiner.on(CommonCons.ID_SEPARATOR).join(id, firstClassify.getCodeValue())) .code(firstClassify.getCodeValue()) .label(firstClassify.getCodeName()) .dataType(firstClassify.getTypeCode()) .treeNode(true) .pId(id) .build(); firstTreeVo.setChild(getFirstChildResourceClassifies(firstClassify.getChildrenType()) .stream() .map(secondClassify -> { DataResourceTreeVo secondTreeVo = DataResourceTreeVo.builder() .id(Joiner.on(CommonCons.ID_SEPARATOR).join(firstTreeVo.getId(), secondClassify.getCodeValue())) .code(secondClassify.getCodeValue()) .label(secondClassify.getCodeName()) .treeNode(false) .dataType(secondClassify.getTypeCode()) .pId(firstTreeVo.getId()) .build(); return secondTreeVo; } ).collect(Collectors.toList())); return firstTreeVo; }).collect(Collectors.toList()); return collect; } /** * 拼接表、列、pid * * 计算treeVo所有最底层子节点数量,不包含叶节点 * * @param treeVo * @param tabInfosMap * @return */ private boolean combinePidAndTableInfo(DataResourceTreeVo treeVo, Map> tabInfosMap) { List child = treeVo.getChild(); Iterator iterator = child.iterator(); while (iterator.hasNext()) { DataResourceTreeVo childVo = iterator.next(); childVo.setPId(treeVo.getId()); childVo.setId(Joiner.on(CommonCons.ID_SEPARATOR).join(treeVo.getId(), childVo.getCode())); List cc = childVo.getChild(); if (CollectionUtils.isNotEmpty(cc)) { childVo.setTreeNode(true); boolean emptyChild = combinePidAndTableInfo(childVo, tabInfosMap); if (emptyChild) { iterator.remove(); } } else { // 如果为叶子节点,查找对应的表和列 // 如果查找不到表和列则移除 String id = childVo.getId(); String classifyCode = DataResourceEnum.DATA_RESOURCE_CLASSIFY.getCode(); if (id.startsWith(classifyCode)) { String replace = id.replace(classifyCode + CommonCons.ID_SEPARATOR, ""); List dataTabInfo = tabInfosMap.get(replace); if (dataTabInfo == null) { iterator.remove(); } else { List tableVos = convertTableToTreeVo(childVo.getId(), dataTabInfo, null); childVo.setTreeNode(true); childVo.setChild(tableVos); } } } } return CollectionUtils.isEmpty(child); } private List convertTableToTreeVo(String pid, List dataTabInfo, Map> colInfosMap) { return dataTabInfo.stream().map(e -> { DataResourceTreeVo vo = new DataResourceTreeVo(); vo.setId(e.getId()); vo.setPId(pid); vo.setLabel(e.getTableName()); vo.setDataType(AppDataTypeEnum.TAB.getValue()); if (null != colInfosMap) { List colInfoList = colInfosMap.get(e.getDataObjectId()); if (CollectionUtils.isNotEmpty(colInfoList)) { List colVos = convertColumnToTreeVo(e.getId(), colInfoList); vo.setChild(colVos); vo.setTreeNode(true); } else { vo.setChild(Collections.emptyList()); vo.setTreeNode(false); } } return vo; }).collect(Collectors.toList()); } private List convertColumnToTreeVo(String pid, List colInfoList) { return colInfoList.stream().map(e -> { DataResourceTreeVo vo = new DataResourceTreeVo(); vo.setId(e.getId()); vo.setPId(pid); vo.setLabel(e.getColumnName()); vo.setDataType(AppDataTypeEnum.COL.getValue()); return vo; }).collect(Collectors.toList()); } /** * 从数据资源目录获取数据-数据分级 * * @return */ private List getDataLevelListFromRemote() { return getFirstChildResourceClassifies(dataResourceService.getDataLevelList()) .stream() .map(childResourceClassify -> new DataLevelVo(null, childResourceClassify.getCodeValue(), childResourceClassify.getCodeName(), childResourceClassify.getTypeCode())) .collect(Collectors.toList()); } /** * 从数据资源目录获取数据-字段关系 * * @return 列表 */ private List getColumnRelationCatalogFromRemote() { return getFirstChildResourceClassifies(dataResourceService.getColumnRelationCatalog()) .stream() .map(childResourceClassify -> new AppColumnRelationVO(null, childResourceClassify.getCodeValue(), childResourceClassify.getCodeName())) .collect(Collectors.toList()); } /** * 从数据资源目录获取数据-安全级别数据 * * @return */ private List getSecurityLevelListFromRemote() { return getFirstChildResourceClassifies(dataResourceService.getSecurityLevelList()) .stream() .map(childResourceClassify -> new DataLevelVo(null, childResourceClassify.getCodeValue(), childResourceClassify.getCodeName(), childResourceClassify.getTypeCode())) .collect(Collectors.toList()); } private List getFirstChildResourceClassifies(List fieldClassifyList) { return Optional.ofNullable(fieldClassifyList) .map(resourceClassifies -> resourceClassifies.get(0)) .map(resourceClassify -> { //设置节点的dataType,dataType为上级的typeCode字段 List dataList = Optional.ofNullable(resourceClassify.getDataList()).orElse(Lists.newArrayList()); dataList.stream().forEach(item -> item.setTypeCode(resourceClassify.getTypeCode())); return dataList; }) .orElse(Lists.newArrayList()); } private List getChildrenTypeList(List fieldClassifyList) { return Optional.ofNullable(fieldClassifyList) .orElse(Lists.newArrayList()); } }