Java系统对接时导入具有父子结点关系的树状数据

系统对接时导入具有父子结点关系的树状数据

做项目时,大部分情况都会遇到新老系统数据同步的问题,最近做的一个项目需要导入集团下的行政组织数据,其实就是集团下的各个子公司和行政部门,它再转入时提供的实体属性如下:

	//组织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()方法是将旧系统的数据实体转化新系统实体的方法,大家不必关心。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值