增加获取某层级下的子级栏目(包含自己)

master
rong.liu 2 years ago
parent 062509c54e
commit c8636533a1
  1. 9
      src/main/java/com/huoran/iasf/controller/SysColumnController.java
  2. 2
      src/main/java/com/huoran/iasf/service/SysColumnService.java
  3. 58
      src/main/java/com/huoran/iasf/service/impl/SysColumnServiceImpl.java

@ -147,9 +147,16 @@ public class SysColumnController {
@ApiOperation("获取一级下的子级栏目(包含自己)") @ApiOperation("获取一级下的子级栏目(包含自己)")
@PostMapping("/getsTheSubColumn") @PostMapping("/getsTheSubColumn")
public R getsTheSubColumn(Integer id) { public R getsTheSubColumn(Integer id) {
String ids = this.getIds(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); return service.getsTheSubColumn(id, ids);
} }

@ -33,7 +33,7 @@ public interface SysColumnService extends IService<SysColumn> {
R getsTheSubColumn(Integer parentId,String ids); R getsTheSubColumn(Integer parentId,String ids);
R getsSublevelColumnsUnderALevel(Integer parentId,String ids);
} }

@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -134,10 +135,63 @@ public class SysColumnServiceImpl extends ServiceImpl<SysColumnMapper, SysColumn
} }
@Override @Override
public R getsTheSubColumn(Integer parentId,String ids) { public R getsTheSubColumn(Integer pid, String ids) {
return R.success(baseMapper.getsTheSubColumn(parentId,ids));
List<SysColumn> getsTheSubColumn = baseMapper.getsTheSubColumn(pid, ids);
//用来保存父节点
List<SysColumn> rootColumn = new ArrayList<>();
//用来保存子节点
List<SysColumn> 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<SysColumn> getsTheSubColumn = baseMapper.getsTheSubColumn(parentId, ids);
List<SysColumn> buildTree= 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;
}
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) { public static List<SysColumn> getChildren(Integer id, List<SysColumn> allDept) {
//存放子节点 //存放子节点

Loading…
Cancel
Save