树结构更改

master
rong.liu 2 years ago
parent efe48afa15
commit a197613156
  1. 110
      src/main/java/com/huoran/iasf/common/utils/TreeStructureUtils.java
  2. 8
      src/main/java/com/huoran/iasf/controller/SysColumnController.java
  3. 3
      src/main/java/com/huoran/iasf/mapper/SysColumnMapper.java
  4. 21
      src/main/java/com/huoran/iasf/mapper/xml/SysColumnMapper.xml
  5. 5
      src/main/java/com/huoran/iasf/service/SysColumnService.java
  6. 64
      src/main/java/com/huoran/iasf/service/impl/SysColumnServiceImpl.java

@ -0,0 +1,110 @@
package com.huoran.iasf.common.utils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.huoran.iasf.entity.SysColumn;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class TreeStructureUtils {
// 获取树结构数据
public static List<SysColumn> getList(List<SysColumn> columns) {
try {
List<SysColumn> result = new ArrayList<>();
for (SysColumn test : columns) {
if (test.getFatherId() == 0) {
result.add(test);
}
}
for (SysColumn test : result) {
List<SysColumn> childList = getChildren(test.getId(), columns);
test.setChildren(childList);
}
// 将一层一层的树结构数据返回吧!
return result;
} catch (Exception e) {
// 这里可以抛个异常
}
return null;
}
//把一个List转成树
public static List<SysColumn> buildTree(List<SysColumn> list, Integer pid) {
List<SysColumn> tree = new ArrayList<>();
for (SysColumn node : list) {
if (Objects.equals(node.getFatherId(), pid)) {
tree.add(findChild(node, list));
}
}
return tree;
}
static SysColumn findChild(SysColumn node, List<SysColumn> list) {
for (SysColumn n : list) {
if (Objects.equals(n.getFatherId(), node.getId())) {
if (node.getChildren() == null) {
node.setChildren(new ArrayList<SysColumn>());
}
node.getChildren().add(findChild(n, list));
}
}
return node;
}
public static List<SysColumn> getChildren(Integer id, List<SysColumn> allDept) {
//存放子节点
List<SysColumn> childList = new ArrayList<>();
//遍历所有栏目,如果父id与传来的id相同,则为传来的id这个栏目的子栏目
for (SysColumn dept : allDept) {
Integer parentId = dept.getFatherId();
if (parentId.equals(id)) {
childList.add(dept);
}
}
//自调用来判断是否还有子节点
for (SysColumn dept : childList) {
dept.setChildren(getChildren(dept.getId(), allDept));
}
//如果没有子节点则返回空集合
if (childList.size() == 0) {
return new ArrayList<>();
}
return childList;
}
/**
* 双重for循环方法转换成树形结构
* @param treeList
* @return
*/
public static List<SysColumn> forMethod(List<SysColumn> treeList) {
List<SysColumn> rootTree = new ArrayList<>();
for (SysColumn tree : treeList) {
// 第一步 筛选出最顶级的父节点
if (0 == tree.getFatherId()) {
rootTree.add(tree);
}
// 第二步 筛选出该父节点下的所有子节点列表
for (SysColumn node : treeList) {
if (node.getFatherId().equals(tree.getId())) {
if (CollectionUtils.isEmpty(tree.getChildren())) {
tree.setChildren(new ArrayList<>());
}
tree.getChildren().add(node);
}
}
}
return rootTree;
}
}

@ -215,6 +215,14 @@ public class SysColumnController {
}
@ApiOperation("某一级查全部")
@PostMapping("/oneLevelChecksThemAll")
public R oneLevelChecksThemAll(@ApiParam(name = "id", value = "栏目id", required = true) @RequestParam Integer id) {
Integer pid = service.oneLevelChecksThemAll(id);
String ids = this.getIds(pid);
return service.getSubColumn(pid, ids);
}
}

@ -35,4 +35,7 @@ public interface SysColumnMapper extends BaseMapper<SysColumn> {
List<FatherContentRespVO> getParentInformationBasedOnChild(@Param("id")Integer id);
List<SysColumn> getsTheSubColumn(@Param("parentId")Integer parentId ,@Param("ids")String ids);
Integer oneLevelChecksThemAll(@Param("theChildId")Integer theChildId);
}

@ -164,6 +164,27 @@
ORDER BY
s.create_time asc
</select>
<select id="oneLevelChecksThemAll" resultType="java.lang.Integer">
SELECT
id
FROM
(
SELECT
@r AS _id,
( SELECT @r := father_id FROM sys_column WHERE id = _id ) AS fatherId,
@l := @l + 1 AS lvl
FROM
( SELECT @r := #{theChildId}, @l := 0 ) vars,
sys_column h
WHERE
@r != 0
) T1
JOIN sys_column T2 ON T1._id = T2.id
WHERE
deleted = 1
ORDER BY
T1.lvl DESC limit 0,1
</select>
</mapper>

@ -35,5 +35,10 @@ public interface SysColumnService extends IService<SysColumn> {
R getsSublevelColumnsUnderALevel(Integer parentId,String ids);
Integer oneLevelChecksThemAll(Integer theChildId);
R getSubColumn(Integer parentId,String ids);
}

@ -5,6 +5,7 @@ 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.huoran.iasf.common.utils.R;
import com.huoran.iasf.common.utils.TreeStructureUtils;
import com.huoran.iasf.entity.SysColumn;
import com.huoran.iasf.mapper.SysColumnMapper;
import com.huoran.iasf.service.SysColumnService;
@ -18,9 +19,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -74,7 +73,7 @@ public class SysColumnServiceImpl extends ServiceImpl<SysColumnMapper, SysColumn
for (SysColumn sysColumn : rootColumn) {
Integer theColumnId = sysColumn.getId();
//调用工具类获取子节点
childList = getChildren(theColumnId, columnList);
childList = TreeStructureUtils.getChildren(theColumnId, columnList);
sysColumn.setChildren(childList);
}
@ -106,7 +105,7 @@ public class SysColumnServiceImpl extends ServiceImpl<SysColumnMapper, SysColumn
for (SysColumn sysColumn : rootColumn) {
Integer theColumnId = sysColumn.getId();
//调用工具类获取子节点
childList = getChildren(theColumnId, columnList);
childList = TreeStructureUtils.getChildren(theColumnId, columnList);
sysColumn.setChildren(childList);
}
@ -138,65 +137,30 @@ public class SysColumnServiceImpl extends ServiceImpl<SysColumnMapper, SysColumn
public R getsTheSubColumn(Integer pid, String ids) {
List<SysColumn> getsTheSubColumn = baseMapper.getsTheSubColumn(pid, ids);
return R.success( buildTree(getsTheSubColumn,pid));
//return R.success(baseMapper.getsTheSubColumn(parentId, ids));
return R.success(TreeStructureUtils.buildTree(getsTheSubColumn, pid));
}
@Override
public R getsSublevelColumnsUnderALevel(Integer parentId, String ids) {
List<SysColumn> getsTheSubColumn = baseMapper.getsTheSubColumn(parentId, ids);
List<SysColumn> buildTree= buildTree(getsTheSubColumn, parentId);
List<SysColumn> buildTree = TreeStructureUtils.buildTree(getsTheSubColumn, parentId);
return R.success(buildTree);
}
//把一个List转成树
static List<SysColumn> buildTree(List<SysColumn> list,Integer pid){
List<SysColumn> tree=new ArrayList<>();
for(SysColumn node:list){
if(Objects.equals(node.getFatherId(),pid)){
tree.add(findChild(node,list));
}
}
return tree;
@Override
public Integer oneLevelChecksThemAll(Integer theChildId) {
Integer getPid = baseMapper.oneLevelChecksThemAll(theChildId);
return getPid;
}
static SysColumn findChild(SysColumn node, List<SysColumn> list){
for(SysColumn n:list){
if(Objects.equals(n.getFatherId(),node.getId())){
if(node.getChildren() == null){
node.setChildren(new ArrayList<SysColumn>());
}
node.getChildren().add(findChild(n,list));
}
}
return node;
@Override
public R getSubColumn(Integer parentId, String ids) {
List<SysColumn> getsTheSubColumn = baseMapper.getsTheSubColumn(parentId, ids);
return R.success(TreeStructureUtils.forMethod(getsTheSubColumn));
}
public static List<SysColumn> getChildren(Integer id, List<SysColumn> allDept) {
//存放子节点
List<SysColumn> childList = new ArrayList<>();
//遍历所有栏目,如果父id与传来的id相同,则为传来的id这个栏目的子栏目
for (SysColumn dept : allDept) {
Integer parentId = dept.getFatherId();
if (parentId.equals(id)) {
childList.add(dept);
}
}
//自调用来判断是否还有子节点
for (SysColumn dept : childList) {
dept.setChildren(getChildren(dept.getId(), allDept));
}
//如果没有子节点则返回空集合
if (childList.size() == 0) {
return new ArrayList<>();
}
return childList;
}
}

Loading…
Cancel
Save