所用bean:
@Data
public class BusinessConfigVO {
private Long id;
private String code;
private String name;
private Long parentId;
List<BusinessConfigVO> children;
}
构建树:
@Override
public ResponseEntity<ResultInfo<List<BusinessConfigVO>>> getBusinessConfigWithTree(Long tenantId) {
Map<String, Object> params = new HashMap<>(2);
params.put(Constant.TENANT_ID, tenantId);
params.put(Constant.ACTIVE, Constant.YES);
List<JwpBusinessConfigEntity> jwpBusinessConfigEntityList = getJwpBusinessConfigEntityList(params);
List<BusinessConfigVO> businessConfigVOList = new ArrayList<>();
if (CollectionUtils.isEmpty(jwpBusinessConfigEntityList)) {
log.error("无配置记录");
return new ResponseEntity<>(new ResultInfo<List<BusinessConfigVO>>().ok(businessConfigVOList), HttpStatus.OK);
}
//获取树结构
businessConfigVOList = getTree(jwpBusinessConfigEntityList);
return new ResponseEntity<>(new ResultInfo<List<BusinessConfigVO>>().ok(businessConfigVOList), HttpStatus.OK);
}
获取树节点结构:
private List<BusinessConfigVO> getTree(List<JwpBusinessConfigEntity> lists) {
List<BusinessConfigVO> treeList = new ArrayList<>();
for (JwpBusinessConfigEntity jwpBusinessConfigEntity : lists) {
if (jwpBusinessConfigEntity.getParentId().equals(-1L)) {
BusinessConfigVO businessConfigVO = new BusinessConfigVO();
BeanUtils.copyProperties(jwpBusinessConfigEntity, businessConfigVO);
businessConfigVO.setChildren(getChildrenNode(businessConfigVO.getId(), lists));
treeList.add(businessConfigVO);
}
}
return treeList;
}
private List<BusinessConfigVO> getChildrenNode(Long id, List<JwpBusinessConfigEntity> allList) {
List<BusinessConfigVO> newTreeList = new ArrayList<>();
for (JwpBusinessConfigEntity jwpBusinessConfigEntity : allList) {
if (jwpBusinessConfigEntity.getParentId().equals(-1L)) {
continue;
}
if (jwpBusinessConfigEntity.getParentId().equals(id)) {
BusinessConfigVO businessConfigVO = new BusinessConfigVO();
BeanUtils.copyProperties(jwpBusinessConfigEntity, businessConfigVO);
businessConfigVO.setChildren(getChildrenNode(businessConfigVO.getId(), allList));
newTreeList.add(businessConfigVO);
}
}
return newTreeList;
}
?根据父节点获取所有子节点:
//查询子节点
public void getAllChildren(JwpBusinessConfigEntity parentJwpBusinessConfigEntity, List<JwpBusinessConfigEntity> jwpBusinessConfigEntityList) {
List<JwpBusinessConfigEntity> childrenList = getJwpBusinessConfigEntityByParentId(parentJwpBusinessConfigEntity.getId());
if (!CollectionUtils.isEmpty(childrenList)) {
jwpBusinessConfigEntityList.add(parentJwpBusinessConfigEntity);
for (JwpBusinessConfigEntity jwpBusinessConfigEntity : childrenList) {
getAllChildren(jwpBusinessConfigEntity, jwpBusinessConfigEntityList);
}
} else {
jwpBusinessConfigEntityList.add(parentJwpBusinessConfigEntity);
}
}
public List<JwpBusinessConfigEntity> getJwpBusinessConfigEntityByParentId(Long id) {
QueryWrapper<JwpBusinessConfigEntity> wrapper = new QueryWrapper<>();
wrapper.eq("parent_Id", id);
return baseDao.selectList(wrapper);
}
|