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 | 126 ++++++++++++++++++++++++++++++++++++++++- 1 files changed, 121 insertions(+), 5 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 fdb5a55..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,8 +1,10 @@ package com.ruoyi.device.service.impl; +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; @@ -12,7 +14,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; +import com.ruoyi.basic.mapper.LaboratoryMapper; import com.ruoyi.basic.mapper.StructureItemParameterMapper; +import com.ruoyi.basic.pojo.Laboratory; import com.ruoyi.basic.pojo.StructureItemParameter; import com.ruoyi.common.core.domain.Result; import com.ruoyi.common.core.domain.entity.User; @@ -31,11 +35,13 @@ import com.ruoyi.inspect.mapper.InsSampleMapper; import com.ruoyi.inspect.pojo.InsProduct; import com.ruoyi.inspect.util.HackLoopTableRenderPolicy; +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 javax.servlet.http.HttpServletRequest; @@ -47,6 +53,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -59,6 +66,10 @@ private DeviceMapper deviceMapper; private UserMapper userMapper; + + private LaboratoryMapper laboratoryMapper; + + private SysDictDataMapper sysDictDataMapper; private StructureItemParameterMapper structureItemParameterMapper; @@ -114,11 +125,6 @@ @Override public List<Device> search(Integer status, String deviceName, String specificationModel, String largeCategory) { return deviceMapper.search(status, deviceName, specificationModel, largeCategory); - } - - @Override - public List<Device> selectDevicePrincipal() { - return deviceMapper.selectDevicePrincipal(); } @Override @@ -501,4 +507,114 @@ throw new RuntimeException("瀵煎嚭澶辫触"); } } + + @Override + @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class) + public void importExcel(List<Device> batch) { + if (CollectionUtils.isEmpty(batch)) { + return; + } + + // 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)); + } + + // 鐢熸垚鍞竴閿� + 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 { + 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); + } + } + + // 鎵归噺鎻掑叆锛堜娇鐢∕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