feat(路段分组控制层获取树形数据):

This commit is contained in:
fuhao 2024-09-03 16:21:42 +08:00
parent 3606c978bf
commit 2269d92c6b
No known key found for this signature in database
5 changed files with 94 additions and 3 deletions

View File

@ -86,4 +86,13 @@ public class RoadGroupController extends BaseController {
public AjaxResult remove(@PathVariable List<Long> ids) {
return toAjax(roadGroupService.removeByIds(ids));
}
/**
* 获取路段分组树列表
*/
@PreAuthorize("@ss.hasPermi('road:group:list')")
@GetMapping("/roadTree")
public AjaxResult deptTree(RoadGroup roadGroup) {
return success(roadGroupService.listRoadTree(roadGroup));
}
}

View File

@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* 路段分组
*/
@ -13,7 +16,7 @@ import lombok.Data;
@TableName(value = "pub_road_group")
public class RoadGroup {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private Long id;
/**
* 路段名称
@ -25,5 +28,8 @@ public class RoadGroup {
* 路段父id
*/
@TableField(value = "parent_id")
private Integer parentId;
private Long parentId;
@TableField(select = false)
private List<RoadGroup> children = new ArrayList<>();
}

View File

@ -7,6 +7,5 @@ import com.ruoyi.common.core.domain.TreeSelect;
import java.util.List;
public interface IPlanTypeService extends MPJBaseService<PlanType>{
List<TreeSelect> listPlanTree(PlanType planType);
}

View File

@ -2,6 +2,10 @@ package com.ruoyi.board.service;
import com.github.yulichang.base.MPJBaseService;
import com.ruoyi.board.domain.RoadGroup;
import com.ruoyi.common.core.domain.TreeSelect;
import java.util.List;
public interface IRoadGroupService extends MPJBaseService<RoadGroup> {
List<TreeSelect> listRoadTree(RoadGroup roadGroup);
}

View File

@ -4,9 +4,82 @@ import com.github.yulichang.base.MPJBaseServiceImpl;
import com.ruoyi.board.domain.RoadGroup;
import com.ruoyi.board.mapper.RoadGroupMapper;
import com.ruoyi.board.service.IRoadGroupService;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class RoadGroupServiceImpl extends MPJBaseServiceImpl<RoadGroupMapper, RoadGroup> implements IRoadGroupService {
@Override
public List<TreeSelect> listRoadTree(RoadGroup roadGroup) {
List<RoadGroup> list = list();
List<RoadGroup> roadGroups = buildRoadTree(list);
return roadGroups.stream().map(this::buildTreeSelect).collect(Collectors.toList());
}
private TreeSelect buildTreeSelect(RoadGroup roadGroup) {
List<TreeSelect> childTreeSelectList = roadGroup.getChildren().stream().map(this::buildTreeSelect).collect(Collectors.toList());
TreeSelect treeSelect = new TreeSelect();
treeSelect.setId(roadGroup.getId());
treeSelect.setLabel(roadGroup.getRoadName());
treeSelect.setChildren(childTreeSelectList);
return treeSelect;
}
private List<RoadGroup> buildRoadTree(List<RoadGroup> list) {
List<RoadGroup> returnList = new ArrayList<>();
List<Long> tempList = list.stream().map(RoadGroup::getId).collect(Collectors.toList());
for (RoadGroup roadGroup : list) {
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(roadGroup.getParentId())) {
recursionFn(list, roadGroup);
returnList.add(roadGroup);
}
}
if (returnList.isEmpty()) {
returnList = list;
}
return returnList;
}
/**
* 递归列表
*/
private void recursionFn(List<RoadGroup> list, RoadGroup t) {
// 得到子节点列表
List<RoadGroup> childList = getChildList(list, t);
t.setChildren(childList);
for (RoadGroup tChild : childList) {
if (hasChild(list, tChild)) {
recursionFn(list, tChild);
}
}
}
/**
* 得到子节点列表
*/
private List<RoadGroup> getChildList(List<RoadGroup> list, RoadGroup t) {
List<RoadGroup> tlist = new ArrayList<>();
for (RoadGroup n : list) {
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<RoadGroup> list, RoadGroup t) {
return !getChildList(list, t).isEmpty();
}
}