feat: 通用树形相关接口

This commit is contained in:
2024-08-06 17:31:18 +08:00
parent 3c9825aea8
commit 98b6da89d5
4 changed files with 62 additions and 9 deletions
@@ -178,6 +178,13 @@ public class SysDictItem implements TreeNode {
@TableField(exist = false)
private List<SysDictItem> childrenList;
/**
* 是否还有子集
*/
@TableField(exist = false)
@ApiModelProperty(value = "是否还有子集")
private boolean hasChild;
@Override
public String obtainTreeNodeId() {
return id;
@@ -33,9 +33,10 @@ public class LawsDictTreeController {
@GetMapping("/page")
public Result<TreePage<SysDictItem>> queryTreeList(
@RequestParam(name = "nodeName", required = false, defaultValue = "") @ApiParam("一级节点名称") String nodeName,
@RequestParam("dictId") @ApiParam("字典编码") String dictId,
@RequestParam(name = "pageNo", defaultValue = "1") @ApiParam("页码") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") @ApiParam("页面大小") Integer pageSize) {
TreePage<SysDictItem> list = dictTreeService.queryTreeList(nodeName, pageNo, pageSize);
TreePage<SysDictItem> list = dictTreeService.queryTreeList(nodeName, dictId, pageNo, pageSize);
return Result.OK(list);
}
@@ -47,11 +48,12 @@ public class LawsDictTreeController {
return Result.OK(list);
}
@AutoLog(value = "树形结构-零部件树历史数据处理")
@ApiOperation(value = "树形结构-零部件树历史数据处理", notes = "树形结构-零部件树历史数据处理")
@AutoLog(value = "树形结构-获取子集")
@ApiOperation(value = "树形结构-获取子集", notes = "树形结构-获取子集")
@GetMapping("/getChild")
public Result<List<SysDictItem>> getChild(@RequestParam("dictItemId") String dictItemId) {
return Result.OK(dictTreeService.getChild(dictItemId));
public Result<List<SysDictItem>> getChild(@RequestParam("dictItemId") String dictItemId,
@RequestParam("dictId") @ApiParam("字典编码") String dictId) {
return Result.OK(dictTreeService.getChild(dictItemId, dictId));
}
@@ -1,7 +1,9 @@
package com.jero.modules.laws.standard.entity;
import com.jero.modules.system.service.TreeNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.*;
import java.util.stream.Collectors;
@@ -11,6 +13,8 @@ import java.util.stream.Collectors;
* @Date: 2024/1/19 15:24
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TreePage<T extends TreeNode> {
private List<T> records;
@@ -1,5 +1,8 @@
package com.jero.modules.laws.standard.service;
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.jero.modules.laws.standard.entity.TreePage;
import com.jero.modules.system.entity.SysDictItem;
import com.jero.modules.system.service.ISysDictItemService;
@@ -8,6 +11,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class DictTreeService {
@@ -18,15 +22,51 @@ public class DictTreeService {
@Resource
private ISysDictItemService dictItemService;
public TreePage<SysDictItem> queryTreeList(String nodeName, Integer pageNo, Integer pageSize) {
return null;
public TreePage<SysDictItem> queryTreeList(String nodeName, String dictId, Integer pageNo, Integer pageSize) {
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysDictItem::getDictId, dictId);
queryWrapper.eq(SysDictItem::getParentId, "0");
if (nodeName != null && !nodeName.isEmpty()) {
queryWrapper.like(SysDictItem::getItemText, nodeName);
}
Page<SysDictItem> page = new Page<>(pageNo, pageSize);
IPage<SysDictItem> pageList = dictItemService.page(page, queryWrapper);
List<String> dictItemIds = pageList.getRecords().stream().map(SysDictItem::getId).collect(Collectors.toList());
// 判断有没有子级
queryWrapper.clear();
queryWrapper.eq(SysDictItem::getDictId, dictId);
queryWrapper.in(SysDictItem::getParentId, dictItemIds);
List<SysDictItem> childList = dictItemService.list(queryWrapper);
pageList.getRecords().forEach(item -> {
if (childList.stream().anyMatch(child -> child.getParentId().equals(item.getId()))) {
item.setHasChild(true);
}
});
return new TreePage<>(pageList.getRecords(), pageList.getTotal(), pageList.getSize(), pageList.getCurrent());
}
public List<SysDictItem> queryEcho(String selectNodeCodes) {
return null;
}
public List<SysDictItem> getChild(String dictItemId) {
return null;
public List<SysDictItem> getChild(String dictItemId, String dictId) {
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysDictItem::getDictId, dictId);
queryWrapper.eq(SysDictItem::getParentId, dictItemId);
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);
List<SysDictItem> childList = dictItemService.list(queryWrapper);
list.forEach(item -> {
if (childList.stream().anyMatch(child -> child.getParentId().equals(item.getId()))) {
item.setHasChild(true);
}
});
return list;
}
}