秒速提升树形递归造成的速度问题

处理前:

  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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值