feat: 自定义树形结构懒加载搜索功能
This commit is contained in:
+103
-57
@@ -6,9 +6,7 @@ import com.jero.modules.system.service.ISysDictItemService;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -18,38 +16,117 @@ public class DictTreeService {
|
|||||||
private ISysDictItemService dictItemService;
|
private ISysDictItemService dictItemService;
|
||||||
|
|
||||||
public List<SysDictItem> queryTreeList(String nodeName, String dictId) {
|
public List<SysDictItem> queryTreeList(String nodeName, String dictId) {
|
||||||
|
// 查询所有节点
|
||||||
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
queryWrapper.eq(SysDictItem::getDictId, dictId);
|
queryWrapper.eq(SysDictItem::getDictId, dictId);
|
||||||
queryWrapper.eq(SysDictItem::getParentId, "0");
|
List<SysDictItem> allNodes = dictItemService.list(queryWrapper);
|
||||||
if (nodeName != null && !nodeName.isEmpty()) {
|
|
||||||
queryWrapper.like(SysDictItem::getItemText, nodeName);
|
|
||||||
}
|
|
||||||
List<SysDictItem> list = dictItemService.list(queryWrapper);
|
|
||||||
|
|
||||||
if (list.isEmpty()) {
|
// 通过 nodeName 过滤符合条件的节点
|
||||||
return list;
|
List<SysDictItem> matchedNodes = allNodes.stream()
|
||||||
|
.filter(node -> node.getItemText() != null && node.getItemText().contains(nodeName))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (matchedNodes.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> dictItemIds = list.stream().map(SysDictItem::getId).collect(Collectors.toList());
|
// 用于存储一级节点
|
||||||
// 判断有没有子级
|
Set<SysDictItem> rootNodes = new HashSet<>();
|
||||||
queryWrapper.clear();
|
|
||||||
|
// 查找每个匹配节点的一级父节点
|
||||||
|
for (SysDictItem matchedNode : matchedNodes) {
|
||||||
|
SysDictItem currentNode = matchedNode;
|
||||||
|
while (currentNode != null && !currentNode.getParentId().equals("0")) {
|
||||||
|
// 查找当前节点的父节点
|
||||||
|
String parentId = currentNode.getParentId();
|
||||||
|
currentNode = allNodes.stream()
|
||||||
|
.filter(node -> node.getId().equals(parentId))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
// 添加一级节点到结果集
|
||||||
|
if (currentNode != null) {
|
||||||
|
rootNodes.add(currentNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置叶子标志的逻辑调整
|
||||||
|
for (SysDictItem node : rootNodes) {
|
||||||
|
Set<String> descendantIds = getAllDescendantIds(node.getId(), allNodes); // 获取所有子孙节点的ID
|
||||||
|
boolean isLeaf = true; // 默认设置为叶子节点
|
||||||
|
|
||||||
|
for (SysDictItem matchedNode : matchedNodes) {
|
||||||
|
if (descendantIds.contains(matchedNode.getId())) {
|
||||||
|
isLeaf = false;
|
||||||
|
break; // 如果在子孙节点中找到匹配节点,当前根节点不是叶子节点
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node.setLeafFlag(isLeaf); // 设置叶子标志
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ArrayList<>(rootNodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 方法:获取给定节点ID的所有子孙节点ID
|
||||||
|
private Set<String> getAllDescendantIds(String parentId, List<SysDictItem> allNodes) {
|
||||||
|
Set<String> descendantIds = new HashSet<>();
|
||||||
|
List<SysDictItem> directChildren = allNodes.stream()
|
||||||
|
.filter(node -> node.getParentId().equals(parentId))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
for (SysDictItem child : directChildren) {
|
||||||
|
descendantIds.add(child.getId());
|
||||||
|
descendantIds.addAll(getAllDescendantIds(child.getId(), allNodes)); // 递归获取子孙节点
|
||||||
|
}
|
||||||
|
|
||||||
|
return descendantIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<SysDictItem> getChild(String dictItemId, String dictId, String nodeName) {
|
||||||
|
// 查询所有节点
|
||||||
|
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
queryWrapper.eq(SysDictItem::getDictId, dictId);
|
queryWrapper.eq(SysDictItem::getDictId, dictId);
|
||||||
queryWrapper.in(SysDictItem::getParentId, dictItemIds);
|
List<SysDictItem> allNodes = dictItemService.list(queryWrapper);
|
||||||
if (nodeName != null && !nodeName.isEmpty()) {
|
|
||||||
queryWrapper.like(SysDictItem::getItemText, nodeName);
|
|
||||||
}
|
|
||||||
List<SysDictItem> childList = dictItemService.list(queryWrapper);
|
|
||||||
|
|
||||||
if (childList.isEmpty()) {
|
// 通过 nodeName 过滤符合条件的节点
|
||||||
list.forEach(e -> e.setLeafFlag(true));
|
List<SysDictItem> matchedNodes = allNodes.stream()
|
||||||
return list;
|
.filter(node -> node.getItemText() != null && node.getItemText().contains(nodeName))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (matchedNodes.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
Map<String, List<SysDictItem>> childMap = childList.stream().collect(Collectors.groupingBy(SysDictItem::getParentId));
|
|
||||||
list.forEach(item -> {
|
// 用于存储一级节点
|
||||||
List<SysDictItem> sysDictItems = childMap.get(item.getId());
|
Set<SysDictItem> rootNodes = new HashSet<>();
|
||||||
item.setLeafFlag(sysDictItems == null || sysDictItems.isEmpty());
|
|
||||||
|
// 查找每个匹配节点的一级父节点
|
||||||
|
for (SysDictItem matchedNode : matchedNodes) {
|
||||||
|
SysDictItem currentNode = matchedNode;
|
||||||
|
while (currentNode != null && !currentNode.getParentId().equals(dictItemId)) {
|
||||||
|
// 查找当前节点的父节点
|
||||||
|
String parentId = currentNode.getParentId();
|
||||||
|
currentNode = allNodes.stream()
|
||||||
|
.filter(node -> node.getId().equals(parentId))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
// 添加一级节点到结果集
|
||||||
|
if (currentNode != null) {
|
||||||
|
rootNodes.add(currentNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置叶子标志
|
||||||
|
Set<String> rootNodeIds = rootNodes.stream().map(SysDictItem::getId).collect(Collectors.toSet());
|
||||||
|
rootNodes.forEach(node -> {
|
||||||
|
node.setLeafFlag(matchedNodes.stream()
|
||||||
|
.noneMatch(n -> rootNodeIds.contains(n.getParentId())));
|
||||||
});
|
});
|
||||||
return list;
|
|
||||||
|
return new ArrayList<>(rootNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SysDictItem> queryEcho(String selectNodeCodes, String dictId) {
|
public List<SysDictItem> queryEcho(String selectNodeCodes, String dictId) {
|
||||||
@@ -58,35 +135,4 @@ public class DictTreeService {
|
|||||||
queryWrapper.in(SysDictItem::getItemValue, Arrays.asList(selectNodeCodes.split(",")));
|
queryWrapper.in(SysDictItem::getItemValue, Arrays.asList(selectNodeCodes.split(",")));
|
||||||
return dictItemService.list(queryWrapper);
|
return dictItemService.list(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SysDictItem> getChild(String dictItemId, String dictId, String nodeName) {
|
|
||||||
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
|
|
||||||
queryWrapper.eq(SysDictItem::getDictId, dictId);
|
|
||||||
queryWrapper.eq(SysDictItem::getParentId, dictItemId);
|
|
||||||
if (nodeName != null && !nodeName.isEmpty()) {
|
|
||||||
queryWrapper.like(SysDictItem::getItemText, nodeName);
|
|
||||||
}
|
|
||||||
List<SysDictItem> list = dictItemService.list(queryWrapper);
|
|
||||||
|
|
||||||
List<String> dictItemIds = list.stream().map(SysDictItem::getId).collect(Collectors.toList());
|
|
||||||
// 判断有没有子级
|
|
||||||
queryWrapper.clear();
|
|
||||||
queryWrapper.eq(SysDictItem::getDictId, dictId);
|
|
||||||
queryWrapper.in(SysDictItem::getParentId, dictItemIds);
|
|
||||||
if (nodeName != null && !nodeName.isEmpty()) {
|
|
||||||
queryWrapper.like(SysDictItem::getItemText, nodeName);
|
|
||||||
}
|
|
||||||
List<SysDictItem> childList = dictItemService.list(queryWrapper);
|
|
||||||
|
|
||||||
if (childList.isEmpty()) {
|
|
||||||
list.forEach(e -> e.setLeafFlag(true));
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
Map<String, List<SysDictItem>> childMap = childList.stream().collect(Collectors.groupingBy(SysDictItem::getParentId));
|
|
||||||
list.forEach(item -> {
|
|
||||||
List<SysDictItem> sysDictItems = childMap.get(item.getId());
|
|
||||||
item.setLeafFlag(sysDictItems == null || sysDictItems.isEmpty());
|
|
||||||
});
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user