From 9fce552f48c898b61fa1cb9be73aa4ae7dfc5bb4 Mon Sep 17 00:00:00 2001 From: liding <756868258@qq.com> Date: 星期五, 28 三月 2025 14:07:53 +0800 Subject: [PATCH] 设备导入优化 --- cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java | 137 +++++++++++++++++++++++++++++++++------------ 1 files changed, 100 insertions(+), 37 deletions(-) diff --git a/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java b/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java index 8285c04..f1aacac 100644 --- a/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java +++ b/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java @@ -1,9 +1,10 @@ package com.ruoyi.device.service.impl; -import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import com.alibaba.fastjson2.JSON; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; @@ -34,16 +35,14 @@ import com.ruoyi.inspect.mapper.InsSampleMapper; import com.ruoyi.inspect.pojo.InsProduct; import com.ruoyi.inspect.util.HackLoopTableRenderPolicy; -import com.ruoyi.performance.dto.AuxiliaryCorrectionHoursDto; -import com.ruoyi.performance.pojo.AuxiliaryCorrectionHours; import com.ruoyi.system.mapper.SysDictDataMapper; import com.ruoyi.system.mapper.UserMapper; import lombok.AllArgsConstructor; import org.apache.logging.log4j.util.Strings; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -54,7 +53,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; -import java.util.regex.Pattern; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -509,49 +508,113 @@ } } - - //瀵煎叆璁惧 @Override - public void importExcel(List<Device> list){ - if (CollectionUtil.isEmpty(list)) { + @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class) + public void importExcel(List<Device> batch) { + if (CollectionUtils.isEmpty(batch)) { return; } - List<Device> deviceList = new ArrayList<>(); - List<Device> devices = new ArrayList<>(); - for (Device device : list) { - Device device1 = deviceMapper.selectOne(Wrappers.<Device>lambdaQuery() - .eq(Device::getDeviceName, device.getDeviceName()) - .eq(Device::getSpecificationModel, device.getSpecificationModel()) - .eq(Device::getManagementNumber, device.getManagementNumber())); - //绠$悊浜� - if (ObjectUtils.isNotEmpty(device.getEquipmentManagerName())){ - //鏌ュ搴旂鐞嗕汉 - User user = userMapper.selectOne(Wrappers.<User>lambdaQuery() - .eq(User::getName, device.getEquipmentManagerName())); - device.setEquipmentManager(user.getId()); - } - //鎵�灞為儴闂� - if (ObjectUtils.isNotEmpty(device.getSubordinateDepartments())){ - //鏌ュ搴旀墍灞為儴闂� - Laboratory laboratory = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery() - .eq(Laboratory::getLaboratoryName,device.getSubordinateDepartments())); - device.setSubordinateDepartmentsId(laboratory.getId()); - } + + // 1. 鎵归噺鏌ヨ鍏宠仈鏁版嵁锛堢敤鎴枫�佸疄楠屽锛� + Map<String, Integer> userMap = queryUserMap(batch); + Map<String, Integer> labMap = queryLabMap(batch); + + // 2. 鍐呭瓨澶勭悊锛氳浆鎹㈠叧鑱斿瓧娈靛苟鍘婚噸 + processBatch(batch, userMap, labMap); + + // 3. 鎵归噺鎿嶄綔鏁版嵁搴� + batchOperate(batch); + } + + private Map<String, Integer> queryUserMap(List<Device> batch) { + Set<String> managerNames = batch.stream() + .map(Device::getEquipmentManagerName) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + if (CollUtil.isEmpty(managerNames)) { + return Collections.emptyMap(); + } + return userMapper.selectList( + new LambdaQueryWrapper<User>().in(User::getName, managerNames) + ).stream().collect(Collectors.toMap(User::getName, User::getId)); + } + + private Map<String, Integer> queryLabMap(List<Device> batch) { + Set<String> labNames = batch.stream() + .map(Device::getSubordinateDepartments) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + if (CollUtil.isEmpty(labNames)) { + return Collections.emptyMap(); + } + return laboratoryMapper.selectList( + new LambdaQueryWrapper<Laboratory>().in(Laboratory::getLaboratoryName, labNames) + ).stream().collect(Collectors.toMap(Laboratory::getLaboratoryName, Laboratory::getId)); + } + + private void processBatch(List<Device> batch, Map<String, Integer> userMap, Map<String, Integer> labMap) { + // 棰勮绠楀敮涓�閿紙璁惧鍚嶇О+鍨嬪彿+绠$悊缂栧彿锛� + 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())) + ).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(device1)) { - devices.add(device1); + + // 鐢熸垚鍞竴閿� + String key = device.getDeviceName() + "|" + device.getSpecificationModel() + "|" + device.getManagementNumber(); + if (existDevices.containsKey(key)) { + Device exist = existDevices.get(key); + BeanUtils.copyProperties(device, exist, "id"); + toUpdate.add(exist); } else { - deviceList.add(device); + toInsert.add(device); } } - //鎵归噺鏂板 - saveBatch(deviceList); - //鎵归噺淇敼 - updateBatchById(devices); + + 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); + } + } + + // 鎵归噺鎻掑叆锛堜娇鐢∕yBatis-Plus鐨刬nsertBatch锛岄粯璁atchSize=1000锛� + if (CollUtil.isNotEmpty(toInsert)) { + saveOrUpdateBatch(toInsert); + } + + // 鎵归噺鏇存柊锛堜娇鐢∕yBatis-Plus鐨剈pdateBatchById锛岄粯璁atchSize=1000锛� + if (CollUtil.isNotEmpty(toUpdate)) { + updateBatchById(toUpdate); + } } } -- Gitblit v1.9.3