实体类
public class ItimDoucumentManager { private String id; private String delFlag; private String remark; private String createBy; @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date createTime; private String updateBy; @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date updateTime; private String lable; private String parentId; private String sort; private List<ItimDoucumentManager> children; @Override public String getId() { return id; } @Override public void setId(String id) { this.id = id; } @Override public String getDelFlag() { return delFlag; } @Override public void setDelFlag(String delFlag) { this.delFlag = delFlag; } @Override public String getRemark() { return remark; } @Override public void setRemark(String remark) { this.remark = remark; } @Override public String getCreateBy() { return createBy; } @Override public void setCreateBy(String createBy) { this.createBy = createBy; } @Override public Date getCreateTime() { return createTime; } @Override public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String getUpdateBy() { return updateBy; } @Override public void setUpdateBy(String updateBy) { this.updateBy = updateBy; } @Override public Date getUpdateTime() { return updateTime; } @Override public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public String getLable() { return lable; } public void setLable(String lable) { this.lable = lable; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public List<ItimDoucumentManager> getChildren() { return children; } public void setChildren(List<ItimDoucumentManager> children) { this.children = children; } }
@Service
public List<ItimDoucumentManager> selectAllList(List<ItimDoucumentManager> itimDoucumentManagerList) {
List<ItimDoucumentManager> itimDoucumentManagers = new ArrayList<>();
for (int i = 0; i < itimDoucumentManagerList.size(); i++) {
ItimDoucumentManager itimDoucumentManager = itimDoucumentManagerList.get(i);
//如果父级为0,则是顶级菜单
if (itimDoucumentManager.getParentId().equals("0")) {
itimDoucumentManagers.add(itimDoucumentManager);
}
}
for (ItimDoucumentManager manager : itimDoucumentManagerList) {
manager.setChildren(getChild(manager.getId(), itimDoucumentManagerList));
}
return itimDoucumentManagers;
}
public List<ItimDoucumentManager> getChild(String id, List<ItimDoucumentManager> list) {
// 子菜单
List<ItimDoucumentManager> childList = new ArrayList<>();
for (ItimDoucumentManager manager : list) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (StringUtils.isNotBlank(manager.getParentId())) {
if (manager.getParentId().equals(id)) {
childList.add(manager);
}
}
}
// 把子菜单的子菜单再循环一遍
for (ItimDoucumentManager rootMenu : childList) {//
if (StringUtils.isBlank(rootMenu.getParentId())) {
// 递归
rootMenu.setChildren(getChild(rootMenu.getId(), list));
}
} // 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}