feat: 分页零部件树
This commit is contained in:
+7
-3
@@ -1,10 +1,12 @@
|
|||||||
package com.jero.modules.laws.standard.controller;
|
package com.jero.modules.laws.standard.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.jero.common.api.vo.Result;
|
import com.jero.common.api.vo.Result;
|
||||||
import com.jero.common.aspect.annotation.AutoLog;
|
import com.jero.common.aspect.annotation.AutoLog;
|
||||||
import com.jero.modules.laws.enterprise.entity.vo.LawsTreeNodeModel;
|
import com.jero.modules.laws.enterprise.entity.vo.LawsTreeNodeModel;
|
||||||
import com.jero.modules.laws.standard.entity.LawsTreeNode;
|
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.service.PartNameService;
|
import com.jero.modules.laws.standard.service.PartNameService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
@@ -34,9 +36,11 @@ public class LawsPartNameController {
|
|||||||
@AutoLog(value = "企业标准-零部件树")
|
@AutoLog(value = "企业标准-零部件树")
|
||||||
@ApiOperation(value = "企业标准-零部件树", notes = "企业标准-零部件树")
|
@ApiOperation(value = "企业标准-零部件树", notes = "企业标准-零部件树")
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public Result<List<LawsTreeNodeModel>> queryENTreeList(
|
public Result<TreePage<PartName>> queryENTreeList(
|
||||||
@RequestParam(required = false, defaultValue = "") @ApiParam("一级节点名称") String nodeName) {
|
@RequestParam(required = false, defaultValue = "") @ApiParam("一级节点名称") String nodeName,
|
||||||
List<LawsTreeNodeModel> list = partNameService.queryTreeList(nodeName);
|
@RequestParam(name = "pageNo", defaultValue = "1") @ApiParam("页码") Integer pageNo,
|
||||||
|
@RequestParam(name = "pageSize", defaultValue = "10") @ApiParam("页面大小") Integer pageSize) {
|
||||||
|
TreePage<PartName> list = partNameService.queryTreeList(nodeName, pageNo, pageSize);
|
||||||
return Result.OK(list);
|
return Result.OK(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,6 +86,12 @@ public class PartName implements Serializable {
|
|||||||
@ApiModelProperty(value = "父级零件id")
|
@ApiModelProperty(value = "父级零件id")
|
||||||
private String parentId;
|
private String parentId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一级节点
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "一级节点")
|
||||||
|
private String firstNode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 子零部件
|
* 子零部件
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.jero.modules.laws.standard.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: Mzaxd
|
||||||
|
* @Date: 2024/1/19 15:24
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TreePage<T> {
|
||||||
|
|
||||||
|
private List<T> records;
|
||||||
|
private long total;
|
||||||
|
private long size;
|
||||||
|
private long current;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+6
-2
@@ -1,9 +1,10 @@
|
|||||||
package com.jero.modules.laws.standard.service;
|
package com.jero.modules.laws.standard.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.jero.modules.laws.enterprise.entity.vo.LawsTreeNodeModel;
|
import com.jero.modules.laws.enterprise.entity.vo.LawsTreeNodeModel;
|
||||||
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.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.jero.modules.laws.standard.entity.TreePage;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -16,8 +17,11 @@ public interface PartNameService extends IService<PartName> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询零部件树
|
* 查询零部件树
|
||||||
|
*
|
||||||
* @param nodeName
|
* @param nodeName
|
||||||
|
* @param pageNo
|
||||||
|
* @param pageSize
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<LawsTreeNodeModel> queryTreeList(String nodeName);
|
TreePage<PartName> queryTreeList(String nodeName, Integer pageNo, Integer pageSize);
|
||||||
}
|
}
|
||||||
|
|||||||
+51
-51
@@ -1,81 +1,81 @@
|
|||||||
package com.jero.modules.laws.standard.service.impl;
|
package com.jero.modules.laws.standard.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
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.CommonConstant;
|
import com.jero.common.constant.enums.YesOrNoEnum;
|
||||||
import com.jero.common.exception.JeroBootException;
|
import com.jero.common.exception.JeroBootException;
|
||||||
import com.jero.modules.laws.enterprise.EnterpriseConstants;
|
|
||||||
import com.jero.modules.laws.enterprise.entity.vo.LawsTreeNodeModel;
|
|
||||||
import com.jero.modules.laws.enterprise.util.FindsTreeNodeChildrenUtil;
|
|
||||||
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.service.PartNameService;
|
import com.jero.modules.laws.standard.service.PartNameService;
|
||||||
import com.jero.modules.laws.standard.mapper.PartNameMapper;
|
import com.jero.modules.laws.standard.mapper.PartNameMapper;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ThinkBook
|
* @author ThinkBook
|
||||||
* @description 针对表【laws_part_name】的数据库操作Service实现
|
* @description 针对表【laws_part_name】的数据库操作Service实现
|
||||||
* @createDate 2024-01-19 10:37:39
|
* @createDate 2024-01-19 10:37:39
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@Transactional(rollbackFor = JeroBootException.class)
|
@Transactional(rollbackFor = JeroBootException.class)
|
||||||
public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
|
public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
|
||||||
implements PartNameService{
|
implements PartNameService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private PartNameMapper partNameMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<LawsTreeNodeModel> queryTreeList(String nodeName) {
|
public TreePage<PartName> queryTreeList(String nodeName, Integer pageNo, Integer pageSize) {
|
||||||
LambdaQueryWrapper<PartName> query = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<PartName> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
query.eq(PartName::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
|
queryWrapper.eq(PartName::getDelFlag, YesOrNoEnum.NO.getValue())
|
||||||
List<PartName> allNodesPartName = this.list(query);
|
.like(StrUtil.isNotBlank(nodeName), PartName::getName, nodeName);
|
||||||
|
List<PartName> allNodes = partNameMapper.selectList(queryWrapper);
|
||||||
|
|
||||||
// 将PartName转换为LawsTreeNode
|
// 将所有节点转换为映射,以便快速查找
|
||||||
List<LawsTreeNode> allNodes = this.convertPartNameToLawsTreeNode(allNodesPartName);
|
Map<String, PartName> nodeMap = allNodes.stream()
|
||||||
|
.collect(Collectors.toMap(PartName::getId, Function.identity()));
|
||||||
|
|
||||||
Set<LawsTreeNode> nodesSet = new HashSet<>();
|
// 构建树形结构
|
||||||
|
List<PartName> topLevelNodes = new ArrayList<>();
|
||||||
|
allNodes.forEach(node -> {
|
||||||
|
if (node.getParentId() == null) {
|
||||||
|
topLevelNodes.add(node);
|
||||||
|
} else {
|
||||||
|
PartName parentNode = nodeMap.get(node.getParentId());
|
||||||
|
if (parentNode != null) {
|
||||||
|
if (parentNode.getChildPartNameList() == null) {
|
||||||
|
parentNode.setChildPartNameList(new ArrayList<>());
|
||||||
|
}
|
||||||
|
parentNode.getChildPartNameList().add(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
List<LawsTreeNode> matchedNodes = allNodes.stream()
|
// 实现分页逻辑
|
||||||
.filter(node -> node.getNodeName().contains(nodeName))
|
int start = (pageNo - 1) * pageSize;
|
||||||
.collect(Collectors.toList());
|
int end = Math.min(start + pageSize, topLevelNodes.size());
|
||||||
|
List<PartName> pageData = topLevelNodes.subList(start, end);
|
||||||
|
|
||||||
for (LawsTreeNode matchedNode : matchedNodes) {
|
// 封装分页信息
|
||||||
nodesSet.addAll(LawsTreeNodeServiceImpl.findPathToRoot(allNodes, matchedNode));
|
TreePage<PartName> result = new TreePage<>();
|
||||||
// 需求调整 只带上级不带下级
|
result.setRecords(pageData);
|
||||||
// nodesSet.addAll(findAllChildren(allNodes, matchedNode));
|
result.setTotal(topLevelNodes.size());
|
||||||
}
|
result.setSize(pageSize);
|
||||||
// 如果没有任何匹配或者只匹配到了跟节点,则只返回根节点
|
result.setCurrent(pageNo);
|
||||||
List<LawsTreeNode> lawsTreeNodes = new ArrayList<>();
|
return result;
|
||||||
if (nodesSet.size() <= 1) {
|
|
||||||
LawsTreeNode parentNode = allNodes.stream().filter(node -> node.getParentCode() == null).collect(Collectors.toList()).get(0);
|
|
||||||
lawsTreeNodes.add(parentNode);
|
|
||||||
} else {
|
|
||||||
lawsTreeNodes = new ArrayList<>(nodesSet);
|
|
||||||
}
|
|
||||||
// 调用wrapTreeDataToTreeList方法生成树状数据
|
|
||||||
return FindsTreeNodeChildrenUtil.wrapTreeDataToTreeList(lawsTreeNodes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<LawsTreeNode> convertPartNameToLawsTreeNode(List<PartName> allNodesPartName) {
|
|
||||||
List<LawsTreeNode> allNodes = new ArrayList<>();
|
|
||||||
for (PartName partName : allNodesPartName) {
|
|
||||||
LawsTreeNode lawsTreeNode = new LawsTreeNode();
|
|
||||||
lawsTreeNode.setId(partName.getId());
|
|
||||||
lawsTreeNode.setNodeCode(partName.getCode());
|
|
||||||
lawsTreeNode.setNodeName(partName.getName());
|
|
||||||
lawsTreeNode.setParentId(partName.getParentId());
|
|
||||||
lawsTreeNode.setDelFlag(partName.getDelFlag());
|
|
||||||
allNodes.add(lawsTreeNode);
|
|
||||||
}
|
|
||||||
return allNodes;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user