From c8636533a1ffca0fab22d88f29e0a17eb0e6809c Mon Sep 17 00:00:00 2001 From: "rong.liu" Date: Mon, 28 Nov 2022 14:38:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=9F=90?= =?UTF-8?q?=E5=B1=82=E7=BA=A7=E4=B8=8B=E7=9A=84=E5=AD=90=E7=BA=A7=E6=A0=8F?= =?UTF-8?q?=E7=9B=AE(=E5=8C=85=E5=90=AB=E8=87=AA=E5=B7=B1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iasf/controller/SysColumnController.java | 9 ++- .../huoran/iasf/service/SysColumnService.java | 2 +- .../service/impl/SysColumnServiceImpl.java | 58 ++++++++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) 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) { //存放子节点