public R<List<BankBranchInfo>> getBankBranchInfo() {
String sql = "select ORG_ID, ORG_NAME, PARENT_ID from auth_org WHERE TENANT_ID <> 100001 ";
List<OrgEntity> listAll = beanCruder.selectList(OrgEntity.class, sql);
List<BankBranchInfo> bankBranchInfos = new ArrayList<>();
listAll.forEach(org -> {
BankBranchInfo bankBranchInfo = new BankBranchInfo();
bankBranchInfo.setId(String.valueOf(org.getOrgId()));
bankBranchInfo.setBankName(org.getOrgName());
bankBranchInfo.setParentId(org.getParentId() == null ? null : Long.valueOf(org.getParentId()));
bankBranchInfos.add(bankBranchInfo);
});
//一级
List<BankBranchInfo> rootList = bankBranchInfos.stream().filter(e -> e.getParentId() == null).collect(Collectors.toList());
//其他级
List<BankBranchInfo> other = bankBranchInfos.stream().filter(e -> e.getParentId() != null).collect(Collectors.toList());
setTree(rootList, other);
return R.ok(rootList, "查询完成");
}
private void setTree(List<BankBranchInfo> children, List<BankBranchInfo> other) {
children.forEach(root -> {
List<BankBranchInfo> childrenList = new ArrayList<>();
root.setBankBranchInfos(childrenList);
//该级子级
List<BankBranchInfo> temp = other.stream().filter(e -> root.getId().equals(e.getParentId().toString())).collect(Collectors.toList());
childrenList.addAll(temp);
if (!childrenList.isEmpty()) {
setTree(childrenList, other);
}
});
}