美文网首页
java中后台权限树

java中后台权限树

作者: 你可以叫我老白 | 来源:发表于2021-06-07 11:04 被阅读0次

    对象工具类:

    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;

        }

    }

    相关文章

      网友评论

          本文标题:java中后台权限树

          本文链接:https://www.haomeiwen.com/subject/xpzrsltx.html