| | |
| | | |
| | | @Override |
| | | public List<ProductTreeDto> selectProductList(ProductDto productDto) { |
| | | // 查询根节点(parentId 为 null) |
| | | // 一次性查询所有数据 |
| | | LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.isNull(Product::getParentId); |
| | | |
| | | // 如果有产品名称条件,添加到查询中 |
| | | if (productDto.getProductName() != null && !productDto.getProductName().isEmpty()) { |
| | | queryWrapper.like(Product::getProductName, productDto.getProductName()); |
| | | } |
| | | List<Product> allProducts = productMapper.selectList(queryWrapper); |
| | | |
| | | // 查询根节点列表 |
| | | List<Product> rootProducts = productMapper.selectList(queryWrapper); |
| | | // 在内存中构建树结构 |
| | | return buildTree(allProducts); |
| | | } |
| | | |
| | | // 转换为树节点并递归构建子树 |
| | | /** |
| | | * 构建树结构 |
| | | * @param allProducts 所有产品数据 |
| | | * @return 树形结构列表 |
| | | */ |
| | | private List<ProductTreeDto> buildTree(List<Product> allProducts) { |
| | | // 按 parentId 分组 |
| | | java.util.Map<Long, List<Product>> parentMap = new java.util.HashMap<>(); |
| | | List<Product> rootList = new ArrayList<>(); |
| | | |
| | | for (Product product : allProducts) { |
| | | if (product.getParentId() == null) { |
| | | rootList.add(product); |
| | | } else { |
| | | parentMap.computeIfAbsent(product.getParentId(), k -> new ArrayList<>()).add(product); |
| | | } |
| | | } |
| | | |
| | | // 递归构建子节点 |
| | | List<ProductTreeDto> tree = new ArrayList<>(); |
| | | for (Product product : rootProducts) { |
| | | ProductTreeDto node = convertToTreeDto(product); |
| | | node.setChildren(buildChildrenNodes(product.getId())); |
| | | tree.add(node); |
| | | for (Product root : rootList) { |
| | | tree.add(buildNode(root, parentMap)); |
| | | } |
| | | return tree; |
| | | } |
| | | |
| | | /** |
| | | * 递归构建节点及其子节点 |
| | | * @param product 产品实体 |
| | | * @param parentMap 按parentId分组的map |
| | | * @return 树节点 |
| | | */ |
| | | private ProductTreeDto buildNode(Product product, java.util.Map<Long, List<Product>> parentMap) { |
| | | ProductTreeDto node = convertToTreeDto(product); |
| | | List<Product> children = parentMap.get(product.getId()); |
| | | if (children != null && !children.isEmpty()) { |
| | | List<ProductTreeDto> childNodes = new ArrayList<>(); |
| | | for (Product child : children) { |
| | | childNodes.add(buildNode(child, parentMap)); |
| | | } |
| | | node.setChildren(childNodes); |
| | | } |
| | | return node; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | |
| | | // 递归构建子节点 |
| | | private List<ProductTreeDto> buildChildrenNodes(Long parentId) { |
| | | // 查询当前父节点的子节点 |
| | | LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(Product::getParentId, parentId); |
| | | List<Product> childProducts = productMapper.selectList(queryWrapper); |
| | | |
| | | // 转换子节点并递归构建它们的子树 |
| | | List<ProductTreeDto> children = new ArrayList<>(); |
| | | for (Product child : childProducts) { |
| | | ProductTreeDto childNode = convertToTreeDto(child); |
| | | childNode.setChildren(buildChildrenNodes(child.getId())); |
| | | children.add(childNode); |
| | | } |
| | | |
| | | return children; |
| | | } |
| | | |
| | | |
| | | // 将 Product 转换为 ProductTreeDto |
| | | private ProductTreeDto convertToTreeDto(Product product) { |
| | | ProductTreeDto dto = new ProductTreeDto(); |