递归获取树状结构子集

/**
	 * 根据权限名称查询子项
	 * @param bean
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getChildrenListByAuthorityName(AuthorityManagement bean) {
		log.info("根据权限名称查询子项bean======"+bean);
		List<Map<String,Object>>treeList=new ArrayList<>();
		bean.setTenantId(AuthUtil.getTenantId());
		List<AuthorityManagement> list=list(Condition.getQueryWrapper(bean));
		if(Func.isNotEmpty(list)){
			List<AuthorityManagement> returnList=new ArrayList<>();
			List<AuthorityManagement> toTreeList=getList(list,returnList);
			if(Func.isNotEmpty(toTreeList)){
				//将返回的实体类数据list转化成list map类型
				List<Map<String,Object>>toList=new ArrayList<>();
				for(AuthorityManagement beanIfo:toTreeList){
					Map<String,Object>mapIfo=new HashMap<>();
					mapIfo.put("id",String.valueOf(beanIfo.getId()));
					mapIfo.put("parentId",beanIfo.getParentId());
					mapIfo.put("key",String.valueOf(beanIfo.getId()));
					mapIfo.put("value",String.valueOf(beanIfo.getId()));
					mapIfo.put("title",beanIfo.getAuthorityName());
					toList.add(mapIfo);
				}
				treeList= TreeListUtil.getChildrenTreeList("id","parentId","children",toList);
			}
		}
		return treeList;
	}


/**
	 *递归获取树状结构子集
	 * @param list
	 * @param returnList
	 * @return
	 */
	public List<AuthorityManagement>getList(List<AuthorityManagement>list,List<AuthorityManagement>returnList){
		if(Func.isNotEmpty(list)){
			for(AuthorityManagement bean:list){
				AuthorityManagement beanInfo=new AuthorityManagement();
				beanInfo.setParentId(AuthUtil.getTenantId());
				beanInfo.setParentId(String.valueOf(bean.getId()));
				List<AuthorityManagement>list1=list(Condition.getQueryWrapper(beanInfo));
				returnList.add(bean);
				if(Func.isNotEmpty(list1)){
					getList(list1,returnList);
				}
			}

		}
     return returnList;
	}


/**
	 *把带有父子机构的数据变成treelist形式 获取子集
	 * @param id
	 * @param parentId 父id
	 * @param children 子项名称
	 * @param list 传入的list
	 * @return
	 */
	public static List<Map<String, Object>> getChildrenTreeList(String id, String parentId, String children, List<Map<String, Object>>list) {
		//给list的每一项加一个子项
		for(int i=0;i<list.size();i++) {
			list.get(i).put(children, new ArrayList<Map<String, Object>>());
		}
		System.out.println("把带有父子机构的数据变成treelist形式list==============="+list);
		List<Map<String, Object>>treelist=new ArrayList<Map<String, Object>>();
		for(Map<String, Object> outmap:list) {
			//System.out.println("把带有父子机构的数据变成outmap==============="+outmap);
			for(Map<String, Object> inmap:list) {
				String outid=outmap.get(id).toString();
				String inpid=inmap.get(parentId).toString();
				if(outid.equals(inpid)) {
					List<Map<String, Object>>maplist=(ArrayList) outmap.get(children);
					maplist.add(inmap);
				}
			}
			String prid=outmap.get(parentId).toString();
			if(!Func.isEmpty(prid)&& prid.equals("0")) {
				treelist.add(outmap);
			}
		}
		return treelist;
	}

  

posted @ 2021-01-12 11:13  红尘沙漏  阅读(407)  评论(0编辑  收藏  举报