递归获取所有子节点测试用例:
/**
* 获取子级ids,含自己
* @param id 父节点
* @param TaxBureauList 组织单位列表
* @return
*/
@Override
public String getChildIds(String id, List<Map<String, Object>> TaxBureauList) {
StringBuilder childIds = new StringBuilder();
childIds.append(id + ",");
this.getChildIds(id, childIds, TaxBureauList);
return childIds.toString().substring(0, childIds.length()-1);
}
private void getChildIds(String id, StringBuilder childIds, List<Map<String, Object>> TaxBureauList) {
for (Map<String, Object> bureau : TaxBureauList) {
//过滤父节点为空的数据
if (StringUtils.isEmpty(MapUtils.getString(bureau,"parentId",""))){
continue;
}
// 判断是否存在子节点
if (id.equals(MapUtils.getString(bureau,"parentId"))) {
childIds.append(bureau.get("id").toString()+",");
// 递归遍历下一级
getChildIds(bureau.get("id").toString(), childIds, TaxBureauList);
}
}
return;
}
递归获取所有父节点测试用例:
@Override
public List<String> queryParentIds(String id, List<TaxCompany> taxCompanyList) {
//递归获取父级ids,不包含自己
List<String> parentIds = new ArrayList<>();
this.getParentTaxCompanyIds(taxCompanyList, id, parentIds);
return parentIds;
}
/**
* 递归获取父级ids
* @param taxCompanyList
* @param id
* @param taxCompanyIds
*/
private void getParentTaxCompanyIds(List<TaxCompany> taxCompanyList, String id, List<String> parentIds) {
for (TaxCompany taxCompany : taxCompanyList) {
if (StringUtils.isEmpty(taxCompany.getcParentid())) {
continue;
}
//判断是否有父节点
if (id.equals(taxCompany.getcCompanyid())) {
taxCompanyIds.add(taxCompany.getcParentid());
getParentTaxCompanyIds(taxCompanyList, taxCompany.getcParentid(), parentIds);
}
}
}