/**
* 递归转化树形菜单
*/
private List<Map<String, Object>> getMenuTree(List<Authorities> authorities, Integer parentId) {
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < authorities.size(); i++) {
Authorities temp = authorities.get(i);
//第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
//第二次temp.getParentId()=-1 parentId=1 不递归不进判断
//第二次temp.getParentId()=2 parentId=1 不递归不进判断
if (temp.getIsMenu() == 0 && parentId == temp.getParentId()) {
Map<String, Object> map = new HashMap<>();
map.put("menuName", temp.getAuthorityName());
map.put("menuIcon", temp.getMenuIcon());
map.put("menuUrl", StringUtil.isBlank(temp.getMenuUrl()) ? "javascript:;" : temp.getMenuUrl());
map.put("subMenus", getMenuTree(authorities, authorities.get(i).getAuthorityId()));
list.add(map);
}
}
return list;
}
public class Sheng {
private String name;
private int id;
private Integer pid;
private static List<Sheng> shenList;
private List<Shi> shiList;
private List<Xian> xianList;
static {
shenList=new ArrayList() {{
add(new Sheng("河南省",1,0));
add(new Sheng("郑州市",2,1));
add(new Sheng("开封市",3,1));
add(new Sheng("洛阳市",4,1));
add(new Sheng("洛阳市-1",5,4));
add(new Sheng("洛阳市-2",6,4));
add(new Sheng("洛阳市-3",7,4));
add(new Sheng("洛阳市-4",8,4));
}};
}
public static List<Map<String, Object>> bulidTree(List<Sheng> sheng, int pid_) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Sheng item : sheng) {
Sheng shen = item;
//第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
//第二次temp.getParentId()=-1 parentId=1 不递归不进判断
//第二次temp.getParentId()=2 parentId=1 不递归不进判断
if (pid_ == shen.getPid()) {
Map<String, Object> map = new HashMap<>();
map.put("menuName", shen.getName());
map.put("menuIcon", "icon");
map.put("menuUrl", "login/test");
map.put("subMenus", bulidTree(sheng, shen.getId()));
list.add(map);
}
}
return list;
}
public static void main(String[] args) {
Sheng sheng = new Sheng();
System.out.println(bulidTree(sheng.getShenList(), 0));
}
}