关于导入接口的一些理解

上篇文章我们讲了导出接口怎么写,那么这篇文章就来讲讲导入接口怎么写。导入接口相比较与导出接口而言,我感觉会复杂一点,这篇文章我们好好来捋一捋导入接口的思路。

先来看看导入接口的模板,然后我们来理解一下大致的思路:

@SneakyThrows
    @ApiOperation(value = "导入IT设备")
    @PostMapping("/uploadDevice")
    public Result uploadDevice(@RequestParam("file") MultipartFile file, @RequestParam("extraCheck") boolean extraCheck) {
        //必要校验
        final StringBuffer sb = new StringBuffer();
        //额外校验的校验结果
        final StringBuffer extra = new StringBuffer();

        final Result<OrgInfoDTO> orgInfoDTO = orgFeignClient.getOrgInfoByOrgId(UserUtils.getUserOrgId());
        final List<ITDeviceData> cacheDataList = new ArrayList<>(8);
        EasyExcel.read(file.getInputStream(), ITDeviceData.class, new ReadListener<ITDeviceData>() {
            int index = 1;
            private Map<String, Integer> map = new HashMap<>(8);

            @Override
            public void invoke(ITDeviceData data, AnalysisContext analysisContext) {
                try {
                    index++;
                    // 校验参数是否合法
                    ValidateUtil.validate(data);
                    if (map.containsKey(data.getDeviceCode())) {
                        sb.append("第").append(index).append("行参数有误:设备编码重复 ").append(data.getDeviceCode()).append(System.getProperty("line.separator"));
                    } else {
                        map.put(data.getDeviceCode(), index);
                        cacheDataList.add(data);
                    }
                } catch (ValidationException e) {
                    sb.append("第").append(index).append("行参数有误:").append(e.getMessage()).append(System.getProperty("line.separator"));
                } catch (Exception e) {
                    log.error("数据导入异常", e);
                    sb.append("第").append(index).append("行参数有误:").append("数据异常").append(System.getProperty("line.separator"));
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                //校验数据库编码是否重复
                if (sb.length() == 0 && extraCheck) {
                    final int BATCH_SIZE = 10;
                    Set<String> codeSet = map.keySet();

                    for (int i = 0; i < codeSet.size(); i = i + BATCH_SIZE) {
                        List<String> temp = codeSet.stream().skip(i).limit(BATCH_SIZE).collect(Collectors.toList());
                        List<ItDeviceInformationEntity> list = itDeviceInformationService.list(
                                new LambdaQueryWrapper<ItDeviceInformationEntity>().select(ItDeviceInformationEntity::getDeviceCode).eq(ItDeviceInformationEntity::getOrgId, orgInfoDTO.getData().getId())
                                        .in(ItDeviceInformationEntity::getDeviceCode, temp));

                        for (int j = 0; j < list.size(); j++) {
                            String code = list.get(j).getDeviceCode();
                            extra.append("第").append(map.get(code)).append("行:设备编码已存在数据库中 ").append(code).append(System.getProperty("line.separator"));
                        }
                    }
                }
                map = null;
            }

        }).sheet().doRead();

        if (sb.length() > 0) {
            cacheDataList.clear();
            //停止导入
            return Result.fail(sb.toString());
        }

        if (extraCheck && extra.length() > 0) {
            cacheDataList.clear();
            return Result.success(extra.toString());
        }

        //数据导入
        for (ITDeviceData data : cacheDataList) {
            itDeviceInformationService.importDevice(data, orgInfoDTO.getData());
        }

        return Result.success(sb.toString());
    }

大致的思路就是,先对导入的这个excel进行判断,看看格式有没有问题,比如会不会有重复的行啊这些。这个完成了之后,再用excel的设备编码和数据库表中的设备编码进行比较,看看有没有重复的地方,如果发现还是没有重复的话,就可以进行导入了。

导入的代码是这样的:

//数据导入
        for (ITDeviceData data : cacheDataList) {
            itDeviceInformationService.importDevice(data, orgInfoDTO.getData());
        }

这个代码关键的地方就在于那两行代码,其他的代码做的工作都是将excel中的数据进行格式转换,从而使得excel中的数据可以被放入数据库中。不过这些代码不好解释,因为这是要根据业务来的,业务不同写的代码也不同,但是那两行代码是必须的。

猜你喜欢

转载自blog.csdn.net/qq_54432917/article/details/143238745