java中后台权限树

9 篇文章 1 订阅

```java
package com.unisound.iot.smart.dao.model.saas;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangtonghao
 * @create 2021-04-27 14:34
 */
@Data
@ApiModel(value = "Saas功能模块树节点", description = "Saas功能模块树节点")
public class TenantModuleTreeNode implements Comparable<TenantModuleTreeNode> {

    @ApiModelProperty(value = "id唯一标识", position = 1, required = true)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    @ApiModelProperty(value = "模块名称", position = 1, required = true)
    private String moduleName;
    @ApiModelProperty(value = "父模块id", position = 1, required = false)
    private Long parentId;
    @ApiModelProperty(value = "模块路径", position = 1, required = false)
    private String modulePath;
    @ApiModelProperty(value = "模块图标", position = 1, required = false)
    private String moduleIcon;
    @ApiModelProperty(value = "http方法", position = 1, required = false)
    private String httpMethod;
    @ApiModelProperty(value = "排序", position = 1, required = false)
    private Integer sortNum;
    @ApiModelProperty(value = "模块代码", position = 1, required = false)
    private String moduleCode;
    @ApiModelProperty(value = "是否可用", position = 1, required = false)
    private Integer active;
    @ApiModelProperty(value = "版本:pro、lite", position = 1, required = false)
    private String serviceVersion;
    @ApiModelProperty(value = "管理模式:1:标准模式 2:直客  3:非直客", position = 1, required = false)
    private Integer custMode;
    @ApiModelProperty(value = "是否具备此模块", position = 1, required = false)
    private Boolean isHas = false;
    @ApiModelProperty(value = "子菜单列表", position = 1, required = false)
    private List<TenantModuleTreeNode> children;


    public static List<TenantModuleTreeNode> listToTree(List<TenantModuleTreeNode> list) {
        //用递归找子。
        List<TenantModuleTreeNode> treeList = new ArrayList<TenantModuleTreeNode>();
        for (TenantModuleTreeNode tree : list) {
            if (tree.getParentId() == -1) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }

    public static TenantModuleTreeNode findChildren(TenantModuleTreeNode tree, List<TenantModuleTreeNode> list) {
        for (TenantModuleTreeNode node : list) {
            if (node.getParentId().longValue() == tree.getId()) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<TenantModuleTreeNode>());
                }
                tree.getChildren().add(findChildren(node, list));
            }
        }
        return tree;
    }

    /**
     * 转换为树
     *
     * @param moduleList
     * @return
     */
    public static List<TenantModuleTreeNode> convertToTree(List<TenantModule> moduleList) {
        if (moduleList == null || moduleList.size() == 0) {
            return null;
        }
        List<TenantModuleTreeNode> list = new ArrayList<>();
        for (TenantModule module : moduleList) {
            TenantModuleTreeNode node = new TenantModuleTreeNode();
            BeanUtils.copyProperties(module, node);
            list.add(node);
        }
        return sort(listToTree(list));
    }

    /**
     * 转换为树并设置是否具备对应模块
     */
    public static List<TenantModuleTreeNode> convertToTreeAndIsHas(List<TenantModule> allModuleList, List<Long> idList) {
        if (allModuleList == null || allModuleList.size() == 0) {
            return null;
        }
        List<TenantModuleTreeNode> list = new ArrayList<>();
        for (TenantModule module : allModuleList) {
            TenantModuleTreeNode node = new TenantModuleTreeNode();
            BeanUtils.copyProperties(module, node);
            if (idList != null && idList.size() > 0) {
                for (Long um : idList) {
                    if (um == module.getId().longValue()) {
                        node.setIsHas(true);
                        break;
                    }
                }
            }
            list.add(node);
        }
        return sort(listToTree(list));
    }


    @Override
    public int compareTo(TenantModuleTreeNode other) {
        if (other == null) {
            return 1;
        }
        if (this.getSortNum() == null || other.getSortNum() == null) {
            return 1;
        }
        return this.getSortNum() - other.getSortNum();
    }

    private static List<TenantModuleTreeNode> sort(List<TenantModuleTreeNode> adminModuleTreeNodeList) {
        if (adminModuleTreeNodeList == null || adminModuleTreeNodeList.size() == 0) {
            return adminModuleTreeNodeList;
        }

        for (TenantModuleTreeNode TenantModuleTreeNode : adminModuleTreeNodeList) {
            if (TenantModuleTreeNode.getChildren() != null) {
                Collections.sort(TenantModuleTreeNode.getChildren());
                sort(TenantModuleTreeNode.getChildren());
            }
        }
        return adminModuleTreeNodeList;
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你可以叫我老白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值