递归查询角色权限资源,公用方法

model 实现TreeEntity 接口,主要是重写TreeEntity 接口里面的方法。进行id 和 父id赋值,以及childList 子节点赋值

mport java.util.List;

/**
 *  EmployeePrivilege
 *
 * @version : Ver 1.0
 * @date	: 2018-7-16
 */
public class EmployeePrivilege  implements Serializable, TreeEntity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
     * 权限ID     
     */	
	private String privilegeId;

	/**
     * 权限名称
     */
	private String privilegeName;

	/**
     * 权限描述
     */
	private String privilegeDesc;

	/**
     * 父权限ID
     */
	private String parentId;

	/**
     * 权限类型:1为菜单,2为按钮,3为接口
     */
	private Integer privilegeType;

	/**
     * 权限创建时间
     */
	private java.util.Date createTime;


	/**
	 * 子节点list
	 */
	@ApiModelProperty(value = "子节点list", required = false, hidden = true)
	private List childList;

	/**
	 * @return 权限ID
	 */
	@ApiModelProperty("权限ID")
	public String getPrivilegeId() {
		return this.privilegeId;
	}

	/**
	 * @param privilegeId 权限ID
	 */
	@ApiModelProperty("权限ID")
	public void setPrivilegeId(String privilegeId) {
		this.privilegeId = privilegeId;
	}

	/**
	 * @return 权限名称
	 */
	@ApiModelProperty("权限名称")
	public String getPrivilegeName() {
		return this.privilegeName;
	}

	/**
	 * @param privilegeName 权限名称
	 */
	@ApiModelProperty("权限名称")
	public void setPrivilegeName(String privilegeName) {
		this.privilegeName = privilegeName;
	}

	/**
	 * @return 权限描述
	 */
	@ApiModelProperty("权限描述")
	public String getPrivilegeDesc() {
		return this.privilegeDesc;
	}

	/**
	 * @param privilegeDesc 权限描述
	 */
	@ApiModelProperty("权限描述")
	public void setPrivilegeDesc(String privilegeDesc) {
		this.privilegeDesc = privilegeDesc;
	}

	/**
	 * @return 父权限ID
	 */
	@ApiModelProperty("父权限ID")
	public String getParentId() {
		return this.parentId;
	}

	/**
	 * @param parentId 父权限ID
	 */
	@ApiModelProperty("父权限ID")
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	/**
	 * @return 权限类型:1为菜单,2为按钮,3为接口
	 */
	@ApiModelProperty("权限类型:1为菜单,2为按钮,3为接口")
	public Integer getPrivilegeType() {
		return this.privilegeType;
	}

	/**
	 * @param privilegeType 权限类型:1为菜单,2为按钮,3为接口
	 */
	@ApiModelProperty("权限类型:1为菜单,2为按钮,3为接口")
	public void setPrivilegeType(Integer privilegeType) {
		this.privilegeType = privilegeType;
	}
	
	/**
	 * @return 权限创建时间
	 */
	@ApiModelProperty("权限创建时间")
	public java.util.Date getCreateTime() {
		return this.createTime;
	}
	
	/**
	 * @param createTime 权限创建时间
	 */
	@ApiModelProperty("权限创建时间")
	public void setCreateTime(java.util.Date createTime) {
		this.createTime = createTime;
	}

	@Override
	public String getResCode() {
		return privilegeId;
	}

	@Override
	public String getParentCode() {
		return parentId;
	}

	public List getChildList() {
		return childList;
	}

	@Override
	public void setChildList(List childList) {
		this.childList = childList;
	}

}

调用

public FindPrivilegesResp findPrivilegesNew(HttpServletRequest request) {
        FindPrivilegesResp resp = new FindPrivilegesResp();
        //先查询当前登陆人的角色ID
        String estateId = EstateUtil.getEstateId(request);
        String acctName="";
        try {
            acctName = AESUtil.decrypt(request.getHeader("acctName"));
        } catch (Exception e) {
            throw new GlobalException("99999");
        }
        CompanyEmployee companyEmployee = employeeAccountDao.getRoleIdByAcctNameAndEstateId(acctName, estateId);
        //查询所有系统权限:(除去privilegeType=3 的接口权限)
        List systems = employeePrivilegeDao.findSystemsAllPrivilege(companyEmployee.getRoleId());

        List oaList = TreeParser.getTreeListAll("1", systems);
        List propertyAppList = TreeParser.getTreeListAll("1200", systems);
        List parkingLotList = TreeParser.getTreeListAll("2530", systems);
        List assistantList = TreeParser.getTreeListAll("401", systems);
        List networkList = TreeParser.getTreeListAll("501", systems);
        List crmList = TreeParser.getTreeListAll("502", systems);
        List estateList = TreeParser.getTreeListAll("65", systems);

            //加入系统权限
        resp.setEstateList(estateList);
        resp.setOaList(oaList);
        resp.setAssistantList(assistantList);
        resp.setNetworkList(networkList);
        resp.setParkingLot(parkingLotList);
        resp.setPropertyappList(propertyAppList);
        resp.setCrmList(crmList);
        return resp;
    }

 

TreeEntity.java 接口类

import java.util.List;


public interface TreeEntity {

    public String getResCode();

    public String getParentCode();

    public void setChildList(List childList);
}

 

TreeParser.java 公用方法类



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


/**
 * @ClassName: TreeParser
 * @Description: 解析树形数据工具类
 * @author zhengjie
 * @date 2018年5月28日 下午3:56:07
 */
public class TreeParser {
    /**
     * 解析树形数据
     * 
     * @param topId
     * @param entityList
     * @return
     * @author jianda
     * @date 2017年5月29日
     */
    public static > List getTreeList(String topId, List entityList) {
        List resultList = new ArrayList<>();

        // 获取顶层元素集合
        String parentCode;
        for (E entity : entityList) {
            parentCode = entity.getParentCode();
            if (parentCode == null || parentCode.equals(topId)) {
                resultList.add(entity);
            }
        }

        // 获取每个顶层元素的子数据集合
        for (E entity : resultList) {
            entity.setChildList(getSubList(entity.getResCode(), entityList));
        }

        return resultList;
    }

    /**
     * 获取子数据集合
     * 
     * @param id
     * @param entityList
     * @return
     * @author jianda
     * @date 2017年5月29日
     */
    private static > List getSubList(String resCode,
                                                                List entityList) {
        List childList = new ArrayList<>();
        String parentCode;

        // 子集的直接子对象
        for (E entity : entityList) {
            parentCode = entity.getParentCode();
            if (resCode.equals(parentCode)) {
                childList.add(entity);
            }
        }

        // 子集的间接子对象
        for (E entity : childList) {
            entity.setChildList(getSubList(entity.getResCode(), entityList));
        }

        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }

    /**
     * 非层级关系
     * @param topId
     * @param entityList
     * @param 
     * @return
     */
    public static > List getTreeListAll(String topId, List entityList) {
        List resultList = new ArrayList<>(); //获取第一集子集

        // 获取顶层元素集合
        for (E entity : entityList) {
            if (entity.getResCode().equals(topId)) {
                resultList.add(entity);
            }
        }
        resultList = getTreeChildList(topId,entityList,resultList);

        return resultList;
    }

    public static > List getTreeChildList(String topId ,List entityList,List resultList) {
        resultList = new ArrayList<>();
        List childList = new ArrayList<>(); //获取子集
        // 获取顶层元素集合
        String parentCode;
        for (E entity : entityList) {
            parentCode = entity.getParentCode();
            if (parentCode == null || parentCode.equals(topId)) {
                childList.add(entity);
                resultList.add(entity);
            }
        }
        if(childList.size()==0){
            return resultList;
        }
        else {
            // 获取每个顶层元素的子数据集合
            for (int i= 0;i cList = getTreeChildList(childList.get(i).getResCode(), entityList,resultList);
                resultList.addAll(cList);
            }
        }

        return resultList;
    }

}

 

 

 

你可能感兴趣的:(java)