From eb975b40828b3a930fb38b75c739a7385b14ee12 Mon Sep 17 00:00:00 2001
From: zhuo <2089219845@qq.com>
Date: 星期四, 03 四月 2025 14:48:57 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/radio_frequency' into radio_frequency

---
 cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java |  143 +++++++++++++++++++++++++++++++++--------------
 1 files changed, 101 insertions(+), 42 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..86562c9 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,109 @@
         }
     }
 
-
-    //瀵煎叆璁惧
     @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 (CollUtil.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());
+
+        // 鎻愬墠鑾峰彇璁惧鍚嶇О銆佸瀷鍙峰拰绠$悊缂栧彿闆嗗悎
+        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);
+
+        // 鍐呭瓨澶勭悊锛氳浆鎹㈠叧鑱斿瓧娈靛苟鍘婚噸锛屽悓鏃舵媶鍒嗘彃鍏ュ拰鏇存柊鍒楄〃
+        List<Device> toInsert = new ArrayList<>();
+        List<Device> toUpdate = new ArrayList<>();
+        processBatch(batch, userMap, labMap, deviceNames, specificationModels, managementNumbers, toInsert, toUpdate);
+
+        // 鎵归噺鎿嶄綔鏁版嵁搴�
+        batchOperate(toInsert, toUpdate);
+    }
+
+    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,
+                              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, deviceNames)
+                        .in(Device::getSpecificationModel, specificationModels)
+                        .in(Device::getManagementNumber, managementNumbers)
+        ).stream().collect(Collectors.toMap(
+                d -> d.getDeviceName() + "|" + d.getSpecificationModel() + "|" + d.getManagementNumber(),
+                Function.identity()
+        ));
+
+        for (Device device : batch) {
+            // 杞崲鍏宠仈瀛楁
+            device.setEquipmentManager(userMap.get(device.getEquipmentManagerName()));
+            device.setSubordinateDepartmentsId(labMap.get(device.getSubordinateDepartments()));
+
+            // 璁惧鐘舵��
+            if (ObjectUtils.isNotEmpty(device.getDeviceStatusName())) {
+                try {
+                    // 鏌ュ瓧鍏稿搴旂殑鍊�
+                    String status = sysDictDataMapper.selectDictValue("device_status", device.getDeviceStatusName());
+                    device.setDeviceStatus(Integer.parseInt(status));
+                } catch (Exception e) {
+                    // 澶勭悊寮傚父锛屼緥濡傝褰曟棩蹇�
+                    e.printStackTrace();
+                }
             }
-            //鎵�灞為儴闂�
-            if (ObjectUtils.isNotEmpty(device.getSubordinateDepartments())){
-                //鏌ュ搴旀墍灞為儴闂�
-                Laboratory laboratory = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery()
-                        .eq(Laboratory::getLaboratoryName,device.getSubordinateDepartments()));
-                device.setSubordinateDepartmentsId(laboratory.getId());
-            }
-            //璁惧鐘舵��
-            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);
+    }
+
+    private void batchOperate(List<Device> toInsert, List<Device> toUpdate) {
+        // 鎵归噺鎻掑叆
+        if (CollUtil.isNotEmpty(toInsert)) {
+            deviceMapper.insertBatchSomeColumn(toInsert);
+        }
+
+        // 鎵归噺鏇存柊
+        if (CollUtil.isNotEmpty(toUpdate)) {
+            updateBatchById(toUpdate);
+        }
     }
 }

--
Gitblit v1.9.3