From 937f7bd68dbd99741e24abb18a9d2e737a46ca41 Mon Sep 17 00:00:00 2001 From: "review512jwy@163.com" <“review512jwy@163.com”> Date: Thu, 8 Jan 2026 11:57:19 +0800 Subject: [PATCH] =?UTF-8?q?importBarFiles=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MonitoringAssetController.java | 2 +- .../async/AsyncExcelProcessingService.java | 111 ++++++++++-------- .../datacenter/business/util/CommonUtil.java | 68 +++++++++++ 3 files changed, 130 insertions(+), 51 deletions(-) diff --git a/data-center-business-controller/src/main/java/com/techsor/datacenter/business/controller/MonitoringAssetController.java b/data-center-business-controller/src/main/java/com/techsor/datacenter/business/controller/MonitoringAssetController.java index 7e8635c..0f1a3b2 100644 --- a/data-center-business-controller/src/main/java/com/techsor/datacenter/business/controller/MonitoringAssetController.java +++ b/data-center-business-controller/src/main/java/com/techsor/datacenter/business/controller/MonitoringAssetController.java @@ -314,7 +314,7 @@ public class MonitoringAssetController { @Operation(summary = "import pdfs and images") @RequestMapping(value = "/edit/importBarFiles", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public SimpleDataResponse uploadBarFiles( + public SimpleDataResponse importBarFiles( @RequestPart("files") MultipartFile detailFile, @Parameter(name="wsClientId", description = "websocket client id", required = false) @RequestParam(required = true) String wsClientId, @Parameter(name="LoginName", description ="Login name", required=true, schema = @Schema(defaultValue = "admin")) @RequestHeader(required=true) String LoginName, diff --git a/data-center-business-service/src/main/java/com/techsor/datacenter/business/service/async/AsyncExcelProcessingService.java b/data-center-business-service/src/main/java/com/techsor/datacenter/business/service/async/AsyncExcelProcessingService.java index 5d13616..175e1f2 100644 --- a/data-center-business-service/src/main/java/com/techsor/datacenter/business/service/async/AsyncExcelProcessingService.java +++ b/data-center-business-service/src/main/java/com/techsor/datacenter/business/service/async/AsyncExcelProcessingService.java @@ -6,6 +6,8 @@ import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; +import com.techsor.datacenter.business.util.CommonUtil; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -99,15 +101,6 @@ public class AsyncExcelProcessingService { BasicSpace space = new BasicSpace(); space.setSpaceId(oldSpace.getSpaceId()); - - // 处理旧图片 - String oldImagesIntroduction = oldSpace.getImagesIntroduction(); - if(StringUtils.isNotBlank(oldImagesIntroduction)) { - Map oldImages = JSON.parseObject(oldImagesIntroduction, new TypeReference>() {}); - for (Map.Entry entry : oldImages.entrySet()) { - commonOpt.deleteS3File(entry.getValue().getS3Path()); - } - } /**20250515需求,直接更新旧数据,不用保留原来的文件**/ Map newImagesMap = new HashMap<>(); @@ -130,7 +123,17 @@ public class AsyncExcelProcessingService { baseDir.resolve("img"), currentUnix, languageType, storageS3FolderImage, s3FileMapping); setImgDescription(newImagesMap, img3Name, img3Description); - space.setImagesIntroduction(JSON.toJSONString(newImagesMap)); + if (MapUtils.isNotEmpty(newImagesMap)) { + space.setImagesIntroduction(JSON.toJSONString(newImagesMap)); + // 处理旧图片 + String oldImagesIntroduction = oldSpace.getImagesIntroduction(); + if(StringUtils.isNotBlank(oldImagesIntroduction)) { + Map oldImages = JSON.parseObject(oldImagesIntroduction, new TypeReference>() {}); + for (Map.Entry entry : oldImages.entrySet()) { + commonOpt.deleteS3File(entry.getValue().getS3Path()); + } + } + } // 处理PDF processAndSetPdfEquipment(space, oldSpace, row, columnIndices, languageType, baseDir, currentUnix, "buildingDevice", @@ -146,7 +149,9 @@ public class AsyncExcelProcessingService { processAndSetPdfEquipment(space, oldSpace, row, columnIndices, languageType, baseDir, currentUnix, "otherDevice", "getPdfOtherEquipment", "setPdfOtherEquipment", s3FileMapping, storageS3FolderPdf); - basicSpaceMapperExt.updateByPrimaryKeySelective(space); + if (CommonUtil.hasAnyOtherFieldNotEmpty(space, "spaceId")) { + basicSpaceMapperExt.updateByPrimaryKeySelective(space); + } } @@ -181,24 +186,26 @@ public class AsyncExcelProcessingService { private void processAndSetPdfEquipment(BasicSpace space, SpacePageVO oldSpace, Row row, Map columnIndices, Integer languageType, Path baseDir, long currentUnix, String equipmentType, String getterMethod, String setterMethod, Map s3FileMapping, String storageS3FolderPdf) throws Exception { - // 获取旧设备数据 - String oldPdfEquipment = (String) oldSpace.getClass().getMethod(getterMethod).invoke(oldSpace); - if (StringUtils.isNotBlank(oldPdfEquipment)) { - Map oldPdfEquipmentMap = JSON.parseObject(oldPdfEquipment, new TypeReference>() {}); - for (Map.Entry entry : oldPdfEquipmentMap.entrySet()) { - commonOpt.deleteS3File(entry.getValue().getS3Path()); - } - } - // 处理文件 /**20250515需求,直接更新旧数据,不用保留原来的文件**/ Map newPdfEquipmentMap = new HashMap<>(); commonOpt.processFile(newPdfEquipmentMap, commonOpt.getCellValue(row.getCell(columnIndices.get(msgLanguageChange.getParameterMapByCode(languageType, equipmentType)))), baseDir.resolve("pdf"), currentUnix, languageType, storageS3FolderPdf, s3FileMapping); - - // 设置更新后的设备数据 - space.getClass().getMethod(setterMethod, String.class).invoke(space, JSON.toJSONString(newPdfEquipmentMap)); + + if (MapUtils.isNotEmpty(newPdfEquipmentMap)) { + // 设置更新后的设备数据 + space.getClass().getMethod(setterMethod, String.class).invoke(space, JSON.toJSONString(newPdfEquipmentMap)); + + // 删除旧设备数据 + String oldPdfEquipment = (String) oldSpace.getClass().getMethod(getterMethod).invoke(oldSpace); + if (StringUtils.isNotBlank(oldPdfEquipment)) { + Map oldPdfEquipmentMap = JSON.parseObject(oldPdfEquipment, new TypeReference>() {}); + for (Map.Entry entry : oldPdfEquipmentMap.entrySet()) { + commonOpt.deleteS3File(entry.getValue().getS3Path()); + } + } + } } @@ -247,15 +254,6 @@ public class AsyncExcelProcessingService { BasicMonitoringAsset asset = new BasicMonitoringAsset(); asset.setEquipmentId(oldAsset.getAssetId()); - - // 处理图片 - String oldImagesIntroduction = oldAsset.getImagesIntroduction(); - if(StringUtils.isNotBlank(oldImagesIntroduction)) { - Map oldImages = JSON.parseObject(oldImagesIntroduction, new TypeReference>() {}); - for (Map.Entry entry : oldImages.entrySet()) { - commonOpt.deleteS3File(entry.getValue().getS3Path()); - } - } Map newImagesMap = new HashMap<>(); @@ -276,17 +274,29 @@ public class AsyncExcelProcessingService { commonOpt.processFile(newImagesMap, img3Name, baseDir.resolve("img"), currentUnix, languageType, storageS3FolderImage, s3FileMapping); setImgDescription(newImagesMap, img3Name, img3Description); - - asset.setImagesIntroduction(JSON.toJSONString(newImagesMap)); - + + if (MapUtils.isNotEmpty(newImagesMap)) { + asset.setImagesIntroduction(JSON.toJSONString(newImagesMap)); + // 处理图片 + String oldImagesIntroduction = oldAsset.getImagesIntroduction(); + if(StringUtils.isNotBlank(oldImagesIntroduction)) { + Map oldImages = JSON.parseObject(oldImagesIntroduction, new TypeReference>() {}); + for (Map.Entry entry : oldImages.entrySet()) { + commonOpt.deleteS3File(entry.getValue().getS3Path()); + } + } + } + // 处理PDF processAndSetPdfEquipment(asset, oldAsset, row, columnIndices, languageType, baseDir, currentUnix, "userManual", "getPdfUserManual", "setPdfUserManual", s3FileMapping, storageS3FolderPdf); processAndSetPdfEquipment(asset, oldAsset, row, columnIndices, languageType, baseDir, currentUnix, "deviceFile", "getPdfDeviceImage", "setPdfDeviceImage", s3FileMapping, storageS3FolderPdf); - - - basicMonitoringAssetMapperExt.updateByPrimaryKeySelective(asset); + + + if (CommonUtil.hasAnyOtherFieldNotEmpty(asset, "equipmentId")) { + basicMonitoringAssetMapperExt.updateByPrimaryKeySelective(asset); + } } @@ -309,23 +319,24 @@ public class AsyncExcelProcessingService { private void processAndSetPdfEquipment(BasicMonitoringAsset asset, MonitoringAssetPageVO oldAsset, Row row, Map columnIndices, Integer languageType, Path baseDir, long currentUnix, String equipmentType, String getterMethod, String setterMethod, Map s3FileMapping, String storageS3FolderPdf) throws Exception { - // 获取旧设备数据 - String oldPdfEquipment = (String) oldAsset.getClass().getMethod(getterMethod).invoke(oldAsset); - if (StringUtils.isNotBlank(oldPdfEquipment)) { - Map oldPdfEquipmentMap = JSON.parseObject(oldPdfEquipment, new TypeReference>() {}); - for (Map.Entry entry : oldPdfEquipmentMap.entrySet()) { - commonOpt.deleteS3File(entry.getValue().getS3Path()); - } - } - // 处理文件 Map newPdfEquipmentMap = new HashMap<>(); commonOpt.processFile(newPdfEquipmentMap, commonOpt.getCellValue(row.getCell(columnIndices.get(msgLanguageChange.getParameterMapByCode(languageType, equipmentType)))), baseDir.resolve("pdf"), currentUnix, languageType, storageS3FolderPdf, s3FileMapping); - - // 设置更新后的设备数据 - asset.getClass().getMethod(setterMethod, String.class).invoke(asset, JSON.toJSONString(newPdfEquipmentMap)); + if (MapUtils.isNotEmpty(newPdfEquipmentMap)) { + // 设置更新后的设备数据 + asset.getClass().getMethod(setterMethod, String.class).invoke(asset, JSON.toJSONString(newPdfEquipmentMap)); + + // 删除旧设备数据 + String oldPdfEquipment = (String) oldAsset.getClass().getMethod(getterMethod).invoke(oldAsset); + if (StringUtils.isNotBlank(oldPdfEquipment)) { + Map oldPdfEquipmentMap = JSON.parseObject(oldPdfEquipment, new TypeReference>() {}); + for (Map.Entry entry : oldPdfEquipmentMap.entrySet()) { + commonOpt.deleteS3File(entry.getValue().getS3Path()); + } + } + } } diff --git a/data-center-business-util/src/main/java/com/techsor/datacenter/business/util/CommonUtil.java b/data-center-business-util/src/main/java/com/techsor/datacenter/business/util/CommonUtil.java index cc216be..5e5661e 100644 --- a/data-center-business-util/src/main/java/com/techsor/datacenter/business/util/CommonUtil.java +++ b/data-center-business-util/src/main/java/com/techsor/datacenter/business/util/CommonUtil.java @@ -1,5 +1,6 @@ package com.techsor.datacenter.business.util; +import java.lang.reflect.Field; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; @@ -837,4 +838,71 @@ public class CommonUtil { } + /** + * 判断:除指定字段外,是否【所有字段都为空】 + * + * @param obj 校验对象 + * @param excludeFields 排除字段名(如 spaceId / equipmentId) + * @return true = 其它字段全部为空 + */ + public static boolean allOtherFieldsEmpty(Object obj, String... excludeFields) { + if (obj == null) { + return true; + } + + Field[] fields = obj.getClass().getDeclaredFields(); + + for (Field field : fields) { + String fieldName = field.getName(); + + // 跳过 serialVersionUID + if ("serialVersionUID".equals(fieldName)) { + continue; + } + + boolean skip = false; + + if (excludeFields != null) { + for (String exclude : excludeFields) { + if (exclude.equals(fieldName)) { + skip = true; + break; + } + } + } + + if (skip) { + continue; + } + + field.setAccessible(true); + try { + Object value = field.get(obj); + if (value == null) { + continue; + } + + if (value instanceof String) { + if (!((String) value).trim().isEmpty()) { + return false; // 有值 + } + } else { + return false; // 非 String,只要不为 null 就算有值 + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + return true; + } + + /** + * 判断:除指定字段外,是否【至少有一个字段有值】 + */ + public static boolean hasAnyOtherFieldNotEmpty(Object obj, String... excludeFields) { + return !allOtherFieldsEmpty(obj, excludeFields); + } + + }