From db64926ed340724ecbb0e26212c419a487eede7b Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 03 四月 2025 11:23:19 +0800
Subject: [PATCH] 设备导入优化
---
cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceServiceImpl.java | 66 +++++++++++++++-----------------
1 files changed, 31 insertions(+), 35 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 3f9b658..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
@@ -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);
}
// 鎵归噺鏇存柊
--
Gitblit v1.9.3