diff --git a/src/main/java/com/huoran/iasf/controller/SysColumnController.java b/src/main/java/com/huoran/iasf/controller/SysColumnController.java index 31915ba..b1b2ef3 100644 --- a/src/main/java/com/huoran/iasf/controller/SysColumnController.java +++ b/src/main/java/com/huoran/iasf/controller/SysColumnController.java @@ -147,9 +147,16 @@ public class SysColumnController { @ApiOperation("获取一级下的子级栏目(包含自己)") @PostMapping("/getsTheSubColumn") public R getsTheSubColumn(Integer id) { - String ids = this.getIds(id); + return service.getsTheSubColumn(id, ids); + + } + + @ApiOperation("获取某层级下的子级栏目(包含自己)") + @PostMapping("/getsSublevelColumnsUnderALevel") + public R getsSublevelColumnsUnderALevel(Integer id) { + String ids = this.getIds(id); return service.getsTheSubColumn(id, ids); } diff --git a/src/main/java/com/huoran/iasf/service/SysColumnService.java b/src/main/java/com/huoran/iasf/service/SysColumnService.java index 6912fb6..61fd824 100644 --- a/src/main/java/com/huoran/iasf/service/SysColumnService.java +++ b/src/main/java/com/huoran/iasf/service/SysColumnService.java @@ -33,7 +33,7 @@ public interface SysColumnService extends IService { R getsTheSubColumn(Integer parentId,String ids); - + R getsSublevelColumnsUnderALevel(Integer parentId,String ids); } \ No newline at end of file diff --git a/src/main/java/com/huoran/iasf/service/impl/SysColumnServiceImpl.java b/src/main/java/com/huoran/iasf/service/impl/SysColumnServiceImpl.java index 556bb7a..eaa9361 100644 --- a/src/main/java/com/huoran/iasf/service/impl/SysColumnServiceImpl.java +++ b/src/main/java/com/huoran/iasf/service/impl/SysColumnServiceImpl.java @@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -134,10 +135,63 @@ public class SysColumnServiceImpl extends ServiceImpl getsTheSubColumn = baseMapper.getsTheSubColumn(pid, ids); + //用来保存父节点 + List rootColumn = new ArrayList<>(); + //用来保存子节点 + List childList = new ArrayList<>(); + //遍历所有栏目,将所有栏目的父栏目id为0的栏目放入父节点集合中 + for (SysColumn column1 : getsTheSubColumn) { + Integer parentId = column1.getFatherId(); + if (parentId == 0) { + rootColumn.add(column1); + } + } + for (SysColumn sysColumn : rootColumn) { + Integer theColumnId = sysColumn.getId(); + //调用工具类获取子节点 + childList = getChildren(theColumnId, getsTheSubColumn); + sysColumn.setChildren(childList); + } + + return R.success(rootColumn); + + + //return R.success(baseMapper.getsTheSubColumn(parentId, ids)); + } + + @Override + public R getsSublevelColumnsUnderALevel(Integer parentId, String ids) { + + List getsTheSubColumn = baseMapper.getsTheSubColumn(parentId, ids); + List buildTree= buildTree(getsTheSubColumn, parentId); + return R.success(buildTree); + } + + //把一个List转成树 + static List buildTree(List list,Integer pid){ + List 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 list){ + for(SysColumn n:list){ + if(Objects.equals(n.getFatherId(),node.getId())){ + if(node.getChildren() == null){ + node.setChildren(new ArrayList()); + } + node.getChildren().add(findChild(n,list)); + } + } + return node; + } public static List getChildren(Integer id, List allDept) { //存放子节点