系统对接时导入具有父子结点关系的树状数据
做项目时,大部分情况都会遇到新老系统数据同步的问题,最近做的一个项目需要导入集团下的行政组织数据,其实就是集团下的各个子公司和行政部门,它再转入时提供的实体属性如下:
//组织ID
private String OBJID;
//组织名称
private String ORGTX;
//上级ID
private String OBJIDUP;
非常简单,但是新系统这边存储时要求该节点的父节点必须存在,所以就要求我这边组装好数据才能去存储。
下面就是我的代码思路,如果有人有更好的思路欢迎在评论区提出或是私信我。
public void syncSapAdministration(List<AdministrationDto> list) {
if(CollectionUtils.isNotEmpty(list)){
//获得所有的上级对象id并排序
List<String> collect1 = list.stream()
.filter(Objects::isNull)
.filter(s -> StringUtils.isNoneBlank(s.getOBJID()))
.sorted(Comparator.comparing(AdministrationDto::getOBJIDUP))
.map(AdministrationDto::getOBJIDUP)
.distinct()
.collect(Collectors.toList());
//对数据进行分组,key是上级对象id,value是对象的list
Map<String, List<AdministrationDto>> collect2 = list.stream()
.filter(Objects::isNull)
.filter(s -> StringUtils.isNoneBlank(s.getOBJID()))
.distinct()
.collect(Collectors.groupingBy(AdministrationDto::getOBJIDUP));
//将数据变成Map类型,key是对象id,value是该id的对象
Map<String, AdministrationDto> collect3 = list.stream()
.filter(Objects::isNull)
.filter(s -> StringUtils.isNoneBlank(s.getOBJID()))
.distinct()
.collect(Collectors.toMap(AdministrationDto::getOBJID, Function.identity()));
//遍历所有上级对象id
for (String s : collect1) {
//从collect2中拿到相应的上级对象
Boolean haveParent = isHaveParent(s, collect3);
if(haveParent){
List<AdministrationDto> administrationDtos = collect2.get(s);
//遍历集合开始保存
for (AdministrationDto administrationDto : administrationDtos) {
OrgViewNodeReqDto orgViewNodeReqDto = adminn2OrgView(administrationDto);
OrgViewNodeResDto oneOrgViewNode = orgViewNodeAdapterService.getOneOrgViewNode(orgViewNodeReqDto.getId());
//节点不存在
if(null==oneOrgViewNode){
OrgViewNodeResDto orgViewNodeResDto = orgViewNodeAdapterService.addOrgViewNode(orgViewNodeReqDto);
//节点存在
}else {
OrgViewNodeResDto orgViewNodeResDto = orgViewNodeAdapterService.patchUpdateOrgViewNode(orgViewNodeReqDto.getId(), orgViewNodeReqDto);
}
}
}else{
log.info("此集合的父节点不存在,添加失败,父节点id为:"+s);
}
}
}
}
判断节点父节点是否存在方法就是使用了递归的方法去实现,代码如下:
private Boolean isHaveParent(String pid,Map<String,AdministrationDto> map){
if(map.containsKey(pid)){
AdministrationDto administrationDto = map.get(pid);
if(!administrationDto.getOBJIDUP().equals("10000000")){
Boolean haveParent = isHaveParent(administrationDto.getOBJIDUP(), map);
if(haveParent){
return isHaveParent(pid, map);
}else {
return false;
}
}else{
OrgViewNodeReqDto orgViewNodeReqDto = adminn2OrgView(administrationDto);
OrgViewNodeResDto oneOrgViewNode = orgViewNodeAdapterService.getOneOrgViewNode(orgViewNodeReqDto.getId());
//节点不存在
if(null==oneOrgViewNode){
OrgViewNodeResDto orgViewNodeResDto = orgViewNodeAdapterService.addOrgViewNode(orgViewNodeReqDto);
return true;
//节点存在
}else {
OrgViewNodeResDto orgViewNodeResDto = orgViewNodeAdapterService.patchUpdateOrgViewNode(orgViewNodeReqDto.getId(), orgViewNodeReqDto);
return true;
}
}
}else {
OrgViewNodeResDto oneOrgViewNode = orgViewNodeAdapterService.getOneOrgViewNode(Long.valueOf(pid));
if(null==oneOrgViewNode){
log.info("该节点的父节点不存在,添加失败,父节点id为:"+pid);
return false;
}else {
return true;
}
}
}
到这就结束了,其中adminn2OrgView()方法是将旧系统的数据实体转化新系统实体的方法,大家不必关心。