From fbf75b40fb739220240e6becff9598c25dac6060 Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期三, 10 九月 2025 15:42:53 +0800 Subject: [PATCH] 优化保养规范批量导入,改为返回文件让用户自己查询对应设备型号 --- lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java | 250 ++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 211 insertions(+), 39 deletions(-) diff --git a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java index 001f12d..48a668e 100644 --- a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java +++ b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java @@ -20,19 +20,26 @@ import org.jeecg.modules.eam.entity.EamMaintenanceStandard; import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest; -import org.jeecg.modules.eam.service.IEamEquipmentService; import org.jeecg.modules.eam.service.IEamMaintenanceStandardService; import org.jeecg.modules.eam.vo.EamMaintenanceStandardVo; import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; +import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * @Description: 淇濆吇鏍囧噯 @@ -309,39 +316,97 @@ } } - /** * 浜屼繚瀵煎叆 - */ + */ @PostMapping("/importSecondMaintenanceStandard") - public Result<?> importSecondMaintenanceStandard(HttpServletRequest request) { + public ResponseEntity<?> importSecondMaintenanceStandard(HttpServletRequest request) { try { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; - Map<String, String> results = new LinkedHashMap<>(); Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); - - // 缁熻瀵煎叆缁撴灉 - int successCount = 0; - int failureCount = 0; - + int totalFiles = fileMap.size(); + List<String> successFileNames = new ArrayList<>(); + List<String> failedFileNames = new ArrayList<>(); + Map<String, String> failedFiles = new LinkedHashMap<>(); + int successDeviceCount = 0; + List<String> notFoundCodes = new ArrayList<>(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue(); String fileName = file.getOriginalFilename(); - Result<?> importResult=eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND",null); + if (fileName == null || fileName.contains("undefined") || fileName.trim().isEmpty()) { + fileName = "file_" + System.currentTimeMillis() + ".tmp"; + } + Result<?> importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND", null); if (importResult.isSuccess()) { - successCount++; + successFileNames.add(fileName); + // 鎻愬彇鎴愬姛瀵煎叆鐨勮澶囨暟閲� + String message = importResult.getMessage(); + Pattern devicePattern = Pattern.compile("璁惧鏁帮細(\\d+)"); + Matcher deviceMatcher = devicePattern.matcher(message); + if (deviceMatcher.find()) { + successDeviceCount += Integer.parseInt(deviceMatcher.group(1)); + } + // 妫�鏌ユ槸鍚︽湁鏈壘鍒扮殑璁惧缂栫爜 + if (message.contains("浣嗕互涓嬭澶囩紪鐮佹湭鎵惧埌锛�")) { + // 鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 + Pattern codePattern = Pattern.compile("浣嗕互涓嬭澶囩紪鐮佹湭鎵惧埌锛歕\s*(\\d+)"); + Matcher codeMatcher = codePattern.matcher(message); + while (codeMatcher.find()) { + notFoundCodes.add(codeMatcher.group(1)); + } + } } else { - results.put(fileName, importResult.getMessage()); - failureCount++; + log.error("鏂囦欢瀵煎叆澶辫触: {}, 鍘熷洜: {}", fileName, importResult.getMessage()); + failedFileNames.add(fileName); + failedFiles.put(fileName, importResult.getMessage()); + + // 浠庨敊璇秷鎭腑鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 + String errorMessage = importResult.getMessage(); + if (errorMessage.contains("鏈壘鍒扮殑璁惧缂栫爜")) { + Pattern codePattern = Pattern.compile("鏈壘鍒扮殑璁惧缂栫爜\\s*(\\d+)"); + Matcher codeMatcher = codePattern.matcher(errorMessage); + while (codeMatcher.find()) { + notFoundCodes.add(codeMatcher.group(1)); + } + } } } + // 妫�鏌ユ槸鍚﹀瓨鍦ㄦ湭鎵惧埌鐨勮澶囩紪鐮� + if (!notFoundCodes.isEmpty()) { + // 鍒涘缓鍖呭惈鏈壘鍒扮紪鐮佺殑CSV鏂囦欢 + String csvContent = "鏈壘鍒扮殑璁惧缂栫爜\n" + String.join("\n", notFoundCodes); + byte[] csvBytes = csvContent.getBytes(StandardCharsets.UTF_8); - // 鏋勫缓鏈�缁堝搷搴� - return getResult(results, fileMap.size(), successCount, failureCount); + // 璁剧疆鍝嶅簲澶� + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=not_found_codes.csv"); + headers.add(HttpHeaders.CONTENT_TYPE, "text/csv; charset=utf-8"); + headers.setContentLength(csvBytes.length); + return ResponseEntity.ok() + .headers(headers) + .body(csvBytes); + } + if (failedFileNames.isEmpty()) { + return ResponseEntity.ok(Result.ok(String.format( + "鎴愬姛瀵煎叆鏂囦欢鏁帮細%d锛屾枃浠跺悕锛�%s锛屾垚鍔熷鍏ヨ澶囨暟锛�%d", + totalFiles, + String.join(",", successFileNames), + successDeviceCount + ))); + } else { + String errorMsg = String.format("澶辫触鏂囦欢鏁帮細%d锛屽け璐ユ枃浠跺悕锛�%s锛屽け璐ュ師鍥狅細%s", + failedFileNames.size(), + String.join(",", failedFileNames), + failedFiles.entrySet().stream() + .map(e -> e.getKey() + ":" + e.getValue()) + .collect(Collectors.joining("锛�"))); + return ResponseEntity.ok(Result.error(errorMsg)); + } } catch (Exception e) { log.error("瀵煎叆澶勭悊寮傚父", e); - return Result.error("瀵煎叆澶勭悊澶辫触: " + e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Result.error("瀵煎叆澶勭悊澶辫触: " + e.getClass().getSimpleName() + " - " + e.getMessage())); } } @@ -349,50 +414,157 @@ * 涓変繚瀵煎叆 */ @PostMapping("/importThirdMaintenanceStandard") - public Result<?> importThirdMaintenanceStandard(HttpServletRequest request) { + public ResponseEntity<?> importThirdMaintenanceStandard(HttpServletRequest request) { try { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; - Map<String, String> results = new LinkedHashMap<>(); Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); - - // 缁熻瀵煎叆缁撴灉 - int successCount = 0; - int failureCount = 0; - + int totalFiles = fileMap.size(); + List<String> successFileNames = new ArrayList<>(); + List<String> failedFileNames = new ArrayList<>(); + Map<String, String> failedFiles = new LinkedHashMap<>(); + int successDeviceCount = 0; + List<String> notFoundCodes = new ArrayList<>(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue(); String fileName = file.getOriginalFilename(); - Result<?> importResult= eamMaintenanceStandardService.importMaintenanceStandard(file, "THIRD",null); + if (fileName == null || fileName.contains("undefined") || fileName.trim().isEmpty()) { + fileName = "file_" + System.currentTimeMillis() + ".tmp"; + } + Result<?> importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "THIRD", null); if (importResult.isSuccess()) { - successCount++; + successFileNames.add(fileName); + // 鎻愬彇鎴愬姛瀵煎叆鐨勮澶囨暟閲� + String message = importResult.getMessage(); + Pattern devicePattern = Pattern.compile("璁惧鏁帮細(\\d+)"); + Matcher deviceMatcher = devicePattern.matcher(message); + if (deviceMatcher.find()) { + successDeviceCount += Integer.parseInt(deviceMatcher.group(1)); + } + // 妫�鏌ユ槸鍚︽湁鏈壘鍒扮殑璁惧缂栫爜 + if (message.contains("浣嗕互涓嬭澶囩紪鐮佹湭鎵惧埌锛�")) { + // 鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 + Pattern codePattern = Pattern.compile("浣嗕互涓嬭澶囩紪鐮佹湭鎵惧埌锛歕\s*(\\d+)"); + Matcher codeMatcher = codePattern.matcher(message); + while (codeMatcher.find()) { + notFoundCodes.add(codeMatcher.group(1)); + } + } } else { - results.put(fileName, importResult.getMessage()); - failureCount++; + log.error("鏂囦欢瀵煎叆澶辫触: {}, 鍘熷洜: {}", fileName, importResult.getMessage()); + failedFileNames.add(fileName); + failedFiles.put(fileName, importResult.getMessage()); + + // 浠庨敊璇秷鎭腑鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 + String errorMessage = importResult.getMessage(); + if (errorMessage.contains("鏈壘鍒扮殑璁惧缂栫爜")) { + Pattern codePattern = Pattern.compile("鏈壘鍒扮殑璁惧缂栫爜\\s*(\\d+)"); + Matcher codeMatcher = codePattern.matcher(errorMessage); + while (codeMatcher.find()) { + notFoundCodes.add(codeMatcher.group(1)); + } + } } } + // 妫�鏌ユ槸鍚﹀瓨鍦ㄦ湭鎵惧埌鐨勮澶囩紪鐮� + if (!notFoundCodes.isEmpty()) { + // 鍒涘缓鍖呭惈鏈壘鍒扮紪鐮佺殑CSV鏂囦欢 + String csvContent = "鏈壘鍒扮殑璁惧缂栫爜\n" + String.join("\n", notFoundCodes); + byte[] csvBytes = csvContent.getBytes(StandardCharsets.UTF_8); - // 鏋勫缓鏈�缁堝搷搴� - return getResult(results, fileMap.size(), successCount, failureCount); + // 璁剧疆鍝嶅簲澶� + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=not_found_codes.csv"); + headers.add(HttpHeaders.CONTENT_TYPE, "text/csv; charset=utf-8"); + headers.setContentLength(csvBytes.length); + return ResponseEntity.ok() + .headers(headers) + .body(csvBytes); + } + if (failedFileNames.isEmpty()) { + return ResponseEntity.ok(Result.ok(String.format( + "鎴愬姛瀵煎叆鏂囦欢鏁帮細%d锛屾枃浠跺悕锛�%s锛屾垚鍔熷鍏ヨ澶囨暟锛�%d", + totalFiles, + String.join(",", successFileNames), + successDeviceCount + ))); + } else { + String errorMsg = String.format("澶辫触鏂囦欢鏁帮細%d锛屽け璐ユ枃浠跺悕锛�%s锛屽け璐ュ師鍥狅細%s", + failedFileNames.size(), + String.join(",", failedFileNames), + failedFiles.entrySet().stream() + .map(e -> e.getKey() + ":" + e.getValue()) + .collect(Collectors.joining("锛�"))); + return ResponseEntity.ok(Result.error(errorMsg)); + } } catch (Exception e) { log.error("瀵煎叆澶勭悊寮傚父", e); - return Result.error("瀵煎叆澶勭悊澶辫触: " + e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Result.error("瀵煎叆澶勭悊澶辫触: " + e.getClass().getSimpleName() + " - " + e.getMessage())); } + } + + @NotNull + private Result<?> getResult(int totalFiles, List<String> successFileNames, List<String> failedFileNames, Map<String, String> failedFiles, int successDeviceCount, List<String> notFoundCodes) { + if (!failedFiles.isEmpty()) { + String errorMsg = String.format("澶辫触鏂囦欢鏁帮細%d锛屽け璐ユ枃浠跺悕锛�%s锛屽け璐ュ師鍥狅細%s", + totalFiles, + String.join(",", failedFileNames), + failedFiles.entrySet().stream() + .map(e -> e.getKey() + ":" + e.getValue()) + .collect(Collectors.joining("锛�"))); + return Result.error(errorMsg); + } + + if (!notFoundCodes.isEmpty()) { + return Result.ok(String.format("鎴愬姛瀵煎叆鏂囦欢鏁帮細%d锛屾枃浠跺悕锛�%s锛屾垚鍔熷鍏ヨ澶囨暟锛�%d锛屾湭鎵惧埌鐨勮澶囩紪鐮侊細%s", + totalFiles, + String.join(",", successFileNames), + successDeviceCount, + String.join(",", notFoundCodes))); + } + + return Result.ok(String.format("鎴愬姛瀵煎叆鏂囦欢鏁帮細%d锛屾枃浠跺悕锛�%s锛屾垚鍔熷鍏ヨ澶囨暟锛�%d", + totalFiles, + String.join(",", successFileNames), + successDeviceCount)); + } + + private int getSuccessDeviceCount(List<String> successFileNames, List<String> failedFileNames, Map<String, String> failedFiles, int successDeviceCount, List<String> notFoundCodes, String fileName, Result<?> importResult) { + if (importResult.isSuccess()) { + successFileNames.add(fileName); + String message = importResult.getMessage(); + // 鎻愬彇璁惧鏁� + Pattern devicePattern = Pattern.compile("璁惧鏁帮細(\\d+)"); + Matcher deviceMatcher = devicePattern.matcher(message); + if (deviceMatcher.find()) { + successDeviceCount += Integer.parseInt(deviceMatcher.group(1)); + } + // 妫�鏌ユ湭鎵惧埌鐨勮澶囩紪鐮� + if (message.contains("浠ヤ笅璁惧缂栫爜鏈壘鍒�")) { + int colonIndex = message.indexOf("锛�", message.indexOf("浠ヤ笅璁惧缂栫爜鏈壘鍒�")); + if (colonIndex != -1) { + String notFoundPart = message.substring(colonIndex + 1).replaceAll("[銆傦紱锛宂", ""); + notFoundCodes.addAll(Arrays.asList(notFoundPart.split("\\s*,\\s*"))); + } + } + } else { + failedFileNames.add(fileName); + failedFiles.put(fileName, importResult.getMessage()); + } + return successDeviceCount; } /** * 鍗囩増瀵煎叆 - * @param id,file - * @return */ - @ApiOperation(value = "鍗囩増瀵煎叆", notes = "鍗囩増瀵煎叆") @PostMapping(value = "/importUpgrade") - public Result<?> importUpgrade(String id,MultipartFile file) { - EamMaintenanceStandard eamMaintenanceStandard=eamMaintenanceStandardService.getById(id); - if (eamMaintenanceStandard==null) { + public Result<?> importUpgrade(String id, MultipartFile file) { + EamMaintenanceStandard eamMaintenanceStandard = eamMaintenanceStandardService.getById(id); + if (eamMaintenanceStandard == null) { return Result.error("璇烽�夋嫨闇�瑕佸崌鐗堢殑淇濆吇鏍囧噯"); - }else { - switch (eamMaintenanceStandard.getMaintenanceCategory()){ + } else { + switch (eamMaintenanceStandard.getMaintenanceCategory()) { case "POINT_INSPECTION": //鐐规鍗囩増瀵煎叆 return eamMaintenanceStandardService.importPointInspectionExcel(file,id); -- Gitblit v1.9.3