java读取树形excel,然后入库

1、excel格式大致这样
在这里插入图片描述
2、一级不能相同,二三级都可以,但是不能在同级下面出现相同的
3、具体的代码映射我就不写了,直接得到对应的集合

public int importAssetTaskType(List<ImportAssetTaskTypeVo> assetTaskTypeList) {

        List<ItopsAsset> resultAsset = new ArrayList<>();
        Map<String, Long> oneMap = new HashMap<>();//标识不能有重复的一级
        Map<String, Long> twoMap = new HashMap<>();//标识不能有重复的二级(做第一次判断)
        Map<String, String> twoKeyOneKey = new HashMap<>();//放二级名字和一级名字使用
        Map<String, Long> threeMap = new HashMap<>();//识不能有重复的三级(做第一次判断)
        Map<String, String> three2Map = new HashMap<>();//标识在一级下面不能有重复的二级
        Map<String, String> threeMap1 = new HashMap<>();//标识三级上面不能有重复的
        List<ImportAssetTaskTypeVo> resultList = new ArrayList<>();


        for (ImportAssetTaskTypeVo importAssetTaskTypeVo : assetTaskTypeList) {
            String oneLevel = importAssetTaskTypeVo.getOneLevel();
            String twoLevel = importAssetTaskTypeVo.getTwoLevel();
            String threeLevel = importAssetTaskTypeVo.getThreeLevel();

            if (!oneMap.containsKey(oneLevel)){//如果当前一级名称重复,就不会再次添加
                Long oneId = DistributedIdUtil.getDistributedIdBySnowFlake();//唯一主键
                oneMap.put(oneLevel,oneId);//为的是在录入二级的时候得到父id

                ImportAssetTaskTypeVo taskTypeVo = new ImportAssetTaskTypeVo();
                taskTypeVo.setAssetTaskTypeId(oneId);//主键
                taskTypeVo.setAssetName(oneLevel);//级别名称
                taskTypeVo.setTypeLevel(1);//标识几级数据
                taskTypeVo.setParentId(0L);//父id

                resultList.add(taskTypeVo);
            }

            if (!twoMap.containsKey(twoLevel) || !oneLevel.equals(twoKeyOneKey.get(twoLevel))){
                twoKeyOneKey.put(twoLevel,oneLevel);
                Long oneId = oneMap.get(oneLevel);//得到当前二级的父id
                Long twoId = DistributedIdUtil.getDistributedIdBySnowFlake();
                ImportAssetTaskTypeVo taskTypeVo = new ImportAssetTaskTypeVo();
                taskTypeVo.setAssetTaskTypeId(twoId);
                taskTypeVo.setAssetName(twoLevel);
                taskTypeVo.setTypeLevel(2);
                taskTypeVo.setParentId(oneId);
                taskTypeVo.setParentName(oneLevel);

                twoMap.put(twoLevel,twoId);//防止重复的二级进入

                resultList.add(taskTypeVo);
            }

            if (!threeMap.containsKey(threeLevel) || !three2Map.containsKey(oneLevel) || !threeLevel.equals(threeMap1.get(twoLevel))){
                threeMap1.put(twoLevel,threeLevel);//保存当前三级的二级名称,目的是不让当前三级出现在童一个二级下
                three2Map.put(oneLevel,threeLevel);//保存当前二级的父级名称
                Long twoId = twoMap.get(twoLevel);//获取到当前三级的父id
                Long threeId = DistributedIdUtil.getDistributedIdBySnowFlake();
                Long assetTaskTypeId = DistributedIdUtil.getDistributedIdBySnowFlake();//资产台账中使用到的id

                ImportAssetTaskTypeVo taskTypeVo = new ImportAssetTaskTypeVo();
                taskTypeVo.setAssetTaskTypeId(threeId);
                taskTypeVo.setAssetName(threeLevel);
                taskTypeVo.setTypeLevel(3);
                taskTypeVo.setParentId(twoId);
                taskTypeVo.setParentName(twoLevel);

                threeMap.put(threeLevel,threeId);

                resultList.add(taskTypeVo);
                itopsAsset.setAssetTaskTypeId(assetTaskTypeId);
                itopsAsset.setAssetParentId(threeId);
            }
            resultAsset.add(itopsAsset);
        }

        int count = itopsAssetTaskTypeMapper.addBatch(resultList);//批量添加数据
        return count;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值