处理前:
public List<XtreeNode> getTreeNodeData(Long roleId,Short type) { //获取顶级节点 List<XtreeNode> childNodeData = getChildNodeData(Long.valueOf(0), roleId); List<XtreeNode> childByType=new ArrayList<>(); for (XtreeNode childNodeDatum : childNodeData) { if(childNodeDatum.getType()!=null){ if(childNodeDatum.getType().toString().equals(type.toString())||childNodeDatum.getType().toString().equals("3")){ childByType.add(childNodeDatum); } } } return childByType; } /** * 获取子节点 * * @return */ private List<XtreeNode> getChildNodeData(Long parentId, Long roleId) { List<XtreeNode> treeList = new ArrayList<>(); List<Moudle> list = moudleMapper.getChildModules(parentId); //添加moudle if (list != null && list.size() > 0) { for (Moudle moudle : list) { XtreeNode xtreeNode = new XtreeNode(); xtreeNode.setTitle(moudle.getName()); xtreeNode.setType(moudle.getType()); //m代表模块用于前端处理 xtreeNode.setValue("m"+moudle.getId().toString()); xtreeNode.setData(getChildNodeData(moudle.getId(), roleId)); treeList.add(xtreeNode); } } //添加resource List<Permission> resList = permissionMapper.getResourcesInmoudleId(parentId); if (resList != null && resList.size() > 0) { for (Permission resource : resList) { XtreeNode xtreeNode = new XtreeNode(); xtreeNode.setTitle(resource.getName()); //r代表资源,用于前端处理 xtreeNode.setValue("r"+resource.getId().toString()); RolePermission roleResource = rolePermissionMapper.getResourceByRoleIdAndResId(roleId,resource.getId()); xtreeNode.setChecked(roleResource==null?false:true); xtreeNode.setData(new ArrayList<>()); treeList.add(xtreeNode); } } return treeList; }
处理后:
public List<XtreeNode> getChildNodeDatas(Short type,Long roleId) { List<XtreeNode> modules = moudleMapper.getModules(type); HashMap<Long, XtreeNode> map = new HashMap<>(); for (XtreeNode module : modules) { map.put(module.getId(),module); } for (XtreeNode module : modules) { module.setValue("m"+module.getId().toString()); } List<XtreeNode> treeList = new ArrayList<>(); for (XtreeNode module : modules) { if(module.getPid().toString().equals("0")){ treeList.add(module); }else{ XtreeNode xtreeNode = map.get(module.getPid()); xtreeNode.getData().add(module); } } //权限 List<XtreeNode> resources = permissionMapper.getResources(); List<Long> resourceByRoleId = rolePermissionMapper.getResourceByRoleId(roleId); for (XtreeNode resource : resources) { resource.setValue("r"+resource.getId().toString()); if(resourceByRoleId.contains(resource.getId())){ resource.setChecked(true); } XtreeNode xtreeNode = map.get(resource.getPid()); if(xtreeNode!=null){ xtreeNode.getData().add(resource); } } return treeList; }