liding
2025-04-03 db64926ed340724ecbb0e26212c419a487eede7b
设备导入优化
已修改1个文件
66 ■■■■ 文件已修改
cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java 66 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java
@@ -511,19 +511,26 @@
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public void importExcel(List<Device> batch) {
        if (CollectionUtils.isEmpty(batch)) {
        if (CollUtil.isEmpty(batch)) {
            return;
        }
        // 1. 批量查询关联数据(用户、实验室)
        // 提前获取设备名称、型号和管理编号集合
        Set<String> deviceNames = batch.stream().map(Device::getDeviceName).collect(Collectors.toSet());
        Set<String> specificationModels = batch.stream().map(Device::getSpecificationModel).collect(Collectors.toSet());
        Set<String> managementNumbers = batch.stream().map(Device::getManagementNumber).collect(Collectors.toSet());
        // 批量查询关联数据(用户、实验室)
        Map<String, Integer> userMap = queryUserMap(batch);
        Map<String, Integer> labMap = queryLabMap(batch);
        // 2. 内存处理:转换关联字段并去重
        processBatch(batch, userMap, labMap);
        // 内存处理:转换关联字段并去重,同时拆分插入和更新列表
        List<Device> toInsert = new ArrayList<>();
        List<Device> toUpdate = new ArrayList<>();
        processBatch(batch, userMap, labMap, deviceNames, specificationModels, managementNumbers, toInsert, toUpdate);
        // 3. 批量操作数据库
        batchOperate(batch);
        // 批量操作数据库
        batchOperate(toInsert, toUpdate);
    }
    private Map<String, Integer> queryUserMap(List<Device> batch) {
@@ -552,31 +559,35 @@
        ).stream().collect(Collectors.toMap(Laboratory::getLaboratoryName, Laboratory::getId));
    }
    private void processBatch(List<Device> batch, Map<String, Integer> userMap, Map<String, Integer> labMap) {
    private void processBatch(List<Device> batch, Map<String, Integer> userMap, Map<String, Integer> labMap,
                              Set<String> deviceNames, Set<String> specificationModels, Set<String> managementNumbers,
                              List<Device> toInsert, List<Device> toUpdate) {
        // 预计算唯一键(设备名称+型号+管理编号)
        Map<String, Device> existDevices = deviceMapper.selectList(
                new LambdaQueryWrapper<Device>()
                        .in(Device::getDeviceName, batch.stream().map(Device::getDeviceName).collect(Collectors.toSet()))
                        .in(Device::getSpecificationModel, batch.stream().map(Device::getSpecificationModel).collect(Collectors.toSet()))
                        .in(Device::getManagementNumber, batch.stream().map(Device::getManagementNumber).collect(Collectors.toSet()))
                        .in(Device::getDeviceName, deviceNames)
                        .in(Device::getSpecificationModel, specificationModels)
                        .in(Device::getManagementNumber, managementNumbers)
        ).stream().collect(Collectors.toMap(
                d -> d.getDeviceName() + "|" + d.getSpecificationModel() + "|" + d.getManagementNumber(),
                Function.identity()
        ));
        List<Device> toInsert = new ArrayList<>();
        List<Device> toUpdate = new ArrayList<>();
        for (Device device : batch) {
            // 转换关联字段
            device.setEquipmentManager(userMap.get(device.getEquipmentManagerName()));
            device.setSubordinateDepartmentsId(labMap.get(device.getSubordinateDepartments()));
            //设备状态
            if (ObjectUtils.isNotEmpty(device.getDeviceStatusName())){
                //查字典对应的值
                String status = sysDictDataMapper.selectDictValue("device_status", device.getDeviceStatusName());
                device.setDeviceStatus(Integer.parseInt(status));
            // 设备状态
            if (ObjectUtils.isNotEmpty(device.getDeviceStatusName())) {
                try {
                    // 查字典对应的值
                    String status = sysDictDataMapper.selectDictValue("device_status", device.getDeviceStatusName());
                    device.setDeviceStatus(Integer.parseInt(status));
                } catch (Exception e) {
                    // 处理异常,例如记录日志
                    e.printStackTrace();
                }
            }
            // 生成唯一键
@@ -589,27 +600,12 @@
                toInsert.add(device);
            }
        }
        batch.clear();
        batch.addAll(toInsert);
        batch.addAll(toUpdate);
    }
    private void batchOperate(List<Device> batch) {
        // 拆分插入和更新列表
        List<Device> toInsert = new ArrayList<>();
        List<Device> toUpdate = new ArrayList<>();
        for (Device device : batch) {
            if (device.getId() == null) {
                toInsert.add(device);
            } else {
                toUpdate.add(device);
            }
        }
    private void batchOperate(List<Device> toInsert, List<Device> toUpdate) {
        // 批量插入
        if (CollUtil.isNotEmpty(toInsert)) {
            baseMapper.insertBatchSomeColumn(toInsert);
            deviceMapper.insertBatchSomeColumn(toInsert);
        }
        // 批量更新