@Override
public Map<String, List<SysDic>> getSysDicsByKeys(List<String> keys) {
log.info("SysDicServiceImpl.getSysDicsByKeys receive {}", keys);
//查询根节点的id
LambdaQueryWrapper<SysDic> queryKeyWrapper = new LambdaQueryWrapper<>();
queryKeyWrapper.in(SysDic::getDicKey, keys);
List<SysDic> parentSysDics = sysDicMapper.selectList(queryKeyWrapper);
List<String> parentIds = new ArrayList<>();
parentSysDics.stream().forEach(item -> parentIds.add(item.getDicId()));
//查询根节点下的所有的子节点
LambdaQueryWrapper<SysDic> queryChildrenWrapper = new LambdaQueryWrapper<>();
queryChildrenWrapper.in(SysDic::getParentId, parentIds);
List<SysDic> sysDics = sysDicMapper.selectList(queryChildrenWrapper);
Map<String, List<SysDic>> sysDicByParentId = sysDics.stream().collect(Collectors.groupingBy(SysDic::getParentId));
//将map的可以转换为key
Set<String> keySet = sysDicByParentId.keySet();
Map<String, List<SysDic>> result = new HashMap<>();
keySet.forEach(key -> {
SysDic sysDic = parentSysDics.stream().filter(item -> item.getDicId().equals(key)).findFirst().orElse(new SysDic());
result.put(sysDic.getDicKey(), sysDicByParentId.get(key));
});
log.info("SysDicServiceImpl.getSysDicsByKeys return {}", result);
return result;
}
/**
* 根据根节点获得所有的子树
* @param key
* @param levels
* @return
*/
@Override
public SysDicTree getTreeByKey(String key, List<String> levels) {
log.info("SysDicServiceImpl.getTreeByKey receive {}", key);
LambdaQueryWrapper<SysDic> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.likeRight(SysDic::getDicKey, key);
queryWrapper.in(SysDic::getDicLevel, levels);
List<SysDic> sysDics = sysDicMapper.selectList(queryWrapper);
Map<String, List<SysDic>> dicMapByLevel = sysDics.stream().collect(Collectors.groupingBy(SysDic::getDicLevel));
SysDicTree root = this.toTreeNodeData(getRootData(dicMapByLevel));
buildSysDicTree(root,sysDics);
log.info("SysDicServiceImpl.getTreeByKey return {}", root);
return root;
}
/**
* 获取根节点的数据
* @param dicMapByLevel
* @return
*/
private SysDic getRootData(Map<String, List<SysDic>> dicMapByLevel) {
List<SysDic> rootLevel = dicMapByLevel.get("0");
SysDic rootData = rootLevel.get(0);
return rootData;
}
/**
* 递归构建子树
* @param root
* @param sysDics
*/
public void buildSysDicTree(SysDicTree root, List<SysDic> sysDics){
log.info("1当前节点为:{},层级为:{}",root.getValue());
if (checkSubChildren(root,sysDics)) {
List<SysDicTree> subNode = getSubSysDicTree(root,sysDics);
log.info("子节点为"+subNode);
subNode.stream().forEach(noteItem->{
buildSysDicTree(noteItem,sysDics);
});
root.setChildren(subNode);
}
}
private List<SysDicTree> getSubSysDicTree(SysDicTree node,List<SysDic> sysDics){
List<SysDic> subNodeSysDic = sysDics.parallelStream().filter(item->item.getParentId().equals(node.
getId())).collect(Collectors.toList());
List<SysDicTree> subNode = subNodeSysDic.stream().map(item->toTreeNodeData(item)).collect(Collectors.toList());
return subNode;
}
/**
* 检查子节点数据是否为空
* @param node
* @return
*/
private boolean checkSubChildren(SysDicTree node,List<SysDic> sysDics){
long sum = sysDics.stream().filter(item->item.getParentId().equals(node.getId())).count();
log.info("当前节点子节点个数为:"+sum+"SYSDICS 总数:"+sysDics.size());
if(sum == 0){
return false;
}else {
return true;
}
}
private SysDicTree toTreeNodeData(SysDic sysDic){
SysDicTree node = new SysDicTree();
node.setId(sysDic.getDicId());
node.setPid(sysDic.getParentId());
node.setLabel(sysDic.getDicName());
node.setValue(sysDic.getDicKey());
return node;
}
递归构建菜单子树
最新推荐文章于 2022-03-17 19:59:34 发布