feat: 零部件查询接口支持全部节点名称筛选
This commit is contained in:
+75
-21
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.jero.common.constant.enums.YesOrNoEnum;
|
import com.jero.common.constant.enums.YesOrNoEnum;
|
||||||
import com.jero.common.exception.JeroBootException;
|
import com.jero.common.exception.JeroBootException;
|
||||||
|
import com.jero.modules.laws.standard.entity.LawsTreeNode;
|
||||||
import com.jero.modules.laws.standard.entity.PartName;
|
import com.jero.modules.laws.standard.entity.PartName;
|
||||||
import com.jero.modules.laws.standard.entity.TreePage;
|
import com.jero.modules.laws.standard.entity.TreePage;
|
||||||
import com.jero.modules.laws.standard.service.PartNameService;
|
import com.jero.modules.laws.standard.service.PartNameService;
|
||||||
@@ -15,9 +16,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -38,30 +37,40 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
|
|||||||
@Override
|
@Override
|
||||||
public TreePage<PartName> queryTreeList(String nodeName, Integer pageNo, Integer pageSize) {
|
public TreePage<PartName> queryTreeList(String nodeName, Integer pageNo, Integer pageSize) {
|
||||||
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
List<PartName> topLevelNodes;
|
||||||
queryWrapper.eq(PartName::getDelFlag, YesOrNoEnum.NO.getValue())
|
queryWrapper.eq(PartName::getDelFlag, YesOrNoEnum.NO.getValue())
|
||||||
.like(StrUtil.isNotBlank(nodeName), PartName::getName, nodeName);
|
.like(StrUtil.isNotBlank(nodeName), PartName::getName, nodeName);
|
||||||
List<PartName> allNodes = partNameMapper.selectList(queryWrapper);
|
List<PartName> allNodes = partNameMapper.selectList(queryWrapper);
|
||||||
|
List<String> firstNodeCodeList = allNodes.stream()
|
||||||
|
.map(PartName::getFirstNode)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
// 不做筛选的时候查询加速
|
||||||
|
if (StrUtil.isBlank(nodeName)) {
|
||||||
|
// 构建树结构
|
||||||
|
topLevelNodes = this.buildTree(new HashSet<>(allNodes));
|
||||||
|
} else {
|
||||||
|
// 做筛选的时候需要自己组树
|
||||||
|
queryWrapper.clear();
|
||||||
|
queryWrapper.eq(PartName::getDelFlag, YesOrNoEnum.NO.getValue())
|
||||||
|
.in(PartName::getFirstNode, firstNodeCodeList)
|
||||||
|
.or().in(PartName::getCode, firstNodeCodeList);
|
||||||
|
List<PartName> allNodeList = partNameMapper.selectList(queryWrapper);
|
||||||
|
|
||||||
// 将所有节点转换为映射,以便快速查找
|
// 构建树形结构
|
||||||
Map<String, PartName> nodeMap = allNodes.stream()
|
Set<PartName> nodesSet = new HashSet<>();
|
||||||
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
|
for (PartName node : allNodes) {
|
||||||
|
nodesSet.addAll(findPathToRoot(allNodeList, node));
|
||||||
// 构建树形结构
|
nodesSet.addAll(findAllChildren(allNodeList, node));
|
||||||
List<PartName> topLevelNodes = new ArrayList<>();
|
|
||||||
allNodes.forEach(node -> {
|
|
||||||
if (node.getParentCode().equals("Part")) {
|
|
||||||
topLevelNodes.add(node);
|
|
||||||
} else {
|
|
||||||
PartName parentNode = nodeMap.get(node.getParentCode());
|
|
||||||
if (parentNode != null) {
|
|
||||||
if (parentNode.getChildPartNameList() == null) {
|
|
||||||
parentNode.setChildPartNameList(new ArrayList<>());
|
|
||||||
}
|
|
||||||
parentNode.getChildPartNameList().add(node);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
topLevelNodes = this.buildTree(nodesSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!topLevelNodes.isEmpty()) {
|
||||||
|
if (topLevelNodes.get(0).getParentCode().equals("0")) {
|
||||||
|
topLevelNodes = topLevelNodes.get(0).getChildPartNameList();
|
||||||
|
}
|
||||||
|
}
|
||||||
// 实现分页逻辑
|
// 实现分页逻辑
|
||||||
int start = (pageNo - 1) * pageSize;
|
int start = (pageNo - 1) * pageSize;
|
||||||
int end = Math.min(start + pageSize, topLevelNodes.size());
|
int end = Math.min(start + pageSize, topLevelNodes.size());
|
||||||
@@ -76,6 +85,51 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<PartName> buildTree(Set<PartName> nodesSet) {
|
||||||
|
List<PartName> topLevelNodes = new ArrayList<>();
|
||||||
|
Map<String, PartName> nodeMap = nodesSet.stream()
|
||||||
|
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
|
||||||
|
for (PartName node : nodesSet) {
|
||||||
|
PartName parentNode = nodeMap.get(node.getParentCode());
|
||||||
|
if (parentNode != null) {
|
||||||
|
if (parentNode.getChildPartNameList() == null) {
|
||||||
|
parentNode.setChildPartNameList(new ArrayList<>());
|
||||||
|
}
|
||||||
|
parentNode.getChildPartNameList().add(node);
|
||||||
|
} else {
|
||||||
|
topLevelNodes.add(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return topLevelNodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<PartName> findPathToRoot(List<PartName> allNodes, PartName node) {
|
||||||
|
Set<PartName> pathNodes = new HashSet<>();
|
||||||
|
PartName[] currentHolder = new PartName[1];
|
||||||
|
currentHolder[0] = node;
|
||||||
|
while (currentHolder[0] != null) {
|
||||||
|
pathNodes.add(currentHolder[0]);
|
||||||
|
currentHolder[0] = allNodes.stream()
|
||||||
|
.filter(n -> n.getCode().equals(currentHolder[0].getParentCode()))
|
||||||
|
.findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
return pathNodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<PartName> findAllChildren(List<PartName> allNodes, PartName node) {
|
||||||
|
Set<PartName> childrenSet = new HashSet<>();
|
||||||
|
List<PartName> directChildren = allNodes.stream()
|
||||||
|
.filter(n -> n.getParentCode().equals(node.getCode()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
for (PartName child : directChildren) {
|
||||||
|
childrenSet.add(child);
|
||||||
|
childrenSet.addAll(findAllChildren(allNodes, child));
|
||||||
|
}
|
||||||
|
return childrenSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PartName> queryEcho(String selectNodeCodes) {
|
public List<PartName> queryEcho(String selectNodeCodes) {
|
||||||
List<String> codeList = StrUtil.split(selectNodeCodes, ',');
|
List<String> codeList = StrUtil.split(selectNodeCodes, ',');
|
||||||
|
|||||||
Reference in New Issue
Block a user