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 | 322 ++++++++++++++++++++++++++++++++++------------------- 1 files changed, 208 insertions(+), 114 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 59099a6..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,71 +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; - Result<?> importResult = new Result<>(); - - // 鐢ㄤ簬鏀堕泦鎵�鏈夋湭鎵惧埌鐨勮澶囩紪鐮� - List<String> allNotFoundCodes = new ArrayList<>(); - + 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(); - 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()) { - // 妫�鏌ユ槸鍚﹀寘鍚儴鍒嗘垚鍔熶俊鎭� + successFileNames.add(fileName); + // 鎻愬彇鎴愬姛瀵煎叆鐨勮澶囨暟閲� String message = importResult.getMessage(); - if (message.contains("浠ヤ笅璁惧缂栫爜鏈壘鍒帮細")) { - // 鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 - String notFoundPart = message.substring(message.indexOf("浠ヤ笅璁惧缂栫爜鏈壘鍒�") + 9); - allNotFoundCodes.addAll(Arrays.asList(notFoundPart.split(", "))); - - // 淇敼涓洪儴鍒嗘垚鍔熸秷鎭� - importResult.setMessage(message.substring(0, message.indexOf("锛涗絾"))); + Pattern devicePattern = Pattern.compile("璁惧鏁帮細(\\d+)"); + Matcher deviceMatcher = devicePattern.matcher(message); + if (deviceMatcher.find()) { + successDeviceCount += Integer.parseInt(deviceMatcher.group(1)); } - successCount++; + // 妫�鏌ユ槸鍚︽湁鏈壘鍒扮殑璁惧缂栫爜 + 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); - // 濡傛灉鏈夋湭鎵惧埌鐨勮澶囩紪鐮侊紝娣诲姞鍒版渶缁堢粨鏋� - if (!allNotFoundCodes.isEmpty()) { - String notFoundMsg = "浠ヤ笅璁惧缂栫爜鏈壘鍒�" + String.join(", ", allNotFoundCodes); - if (successCount > 0) { - // 閮ㄥ垎鎴愬姛 - return Result.ok("閮ㄥ垎瀵煎叆鎴愬姛锛屾垚鍔熸枃浠舵暟锛�" + successCount + - "锛屽け璐ユ枃浠舵暟锛�" + failureCount + "锛�" + notFoundMsg); - } else { - // 鍏ㄩ儴澶辫触 - return Result.error("瀵煎叆澶辫触锛�" + notFoundMsg); - } + // 璁剧疆鍝嶅簲澶� + 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 (successCount == 1) { - return importResult; + 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)); } - - // 鏋勫缓鏈�缁堝搷搴� - return getResult(results, fileMap.size(), successCount, failureCount); - } 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())); } } @@ -381,68 +414,145 @@ * 涓変繚瀵煎叆 */ @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; - Result<?> importResult = new Result<>(); - - // 鐢ㄤ簬鏀堕泦鎵�鏈夋湭鎵惧埌鐨勮澶囩紪鐮� - List<String> allNotFoundCodes = new ArrayList<>(); - + 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(); - // 淇锛氫笁淇濆鍏ュ簲璇ヤ娇鐢�"THIRD"绫诲瀷 - 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()) { - // 妫�鏌ユ槸鍚﹀寘鍚儴鍒嗘垚鍔熶俊鎭� + successFileNames.add(fileName); + // 鎻愬彇鎴愬姛瀵煎叆鐨勮澶囨暟閲� String message = importResult.getMessage(); - if (message.contains("浠ヤ笅璁惧缂栫爜鏈壘鍒帮細")) { - // 鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 - String notFoundPart = message.substring(message.indexOf("浠ヤ笅璁惧缂栫爜鏈壘鍒�") + 9); - allNotFoundCodes.addAll(Arrays.asList(notFoundPart.split(", "))); - - // 淇敼涓洪儴鍒嗘垚鍔熸秷鎭� - importResult.setMessage(message.substring(0, message.indexOf("锛涗絾"))); + Pattern devicePattern = Pattern.compile("璁惧鏁帮細(\\d+)"); + Matcher deviceMatcher = devicePattern.matcher(message); + if (deviceMatcher.find()) { + successDeviceCount += Integer.parseInt(deviceMatcher.group(1)); } - successCount++; + // 妫�鏌ユ槸鍚︽湁鏈壘鍒扮殑璁惧缂栫爜 + 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); - // 濡傛灉鏈夋湭鎵惧埌鐨勮澶囩紪鐮侊紝娣诲姞鍒版渶缁堢粨鏋� - if (!allNotFoundCodes.isEmpty()) { - String notFoundMsg = "浠ヤ笅璁惧缂栫爜鏈壘鍒�" + String.join(", ", allNotFoundCodes); - if (successCount > 0) { - // 閮ㄥ垎鎴愬姛 - return Result.ok("閮ㄥ垎瀵煎叆鎴愬姛锛屾垚鍔熸枃浠舵暟锛�" + successCount + - "锛屽け璐ユ枃浠舵暟锛�" + failureCount + "锛�" + notFoundMsg); - } else { - // 鍏ㄩ儴澶辫触 - return Result.error("瀵煎叆澶辫触锛�" + notFoundMsg); - } + // 璁剧疆鍝嶅簲澶� + 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 (successCount == 1) { - return importResult; + 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)); } - - // 鏋勫缓鏈�缁堝搷搴� - return getResult(results, fileMap.size(), successCount, failureCount); - } 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; } /** @@ -454,37 +564,21 @@ if (eamMaintenanceStandard == null) { return Result.error("璇烽�夋嫨闇�瑕佸崌鐗堢殑淇濆吇鏍囧噯"); } else { - Result<?> importResult; switch (eamMaintenanceStandard.getMaintenanceCategory()) { case "POINT_INSPECTION": - // 鐐规鍗囩増瀵煎叆 - importResult = eamMaintenanceStandardService.importPointInspectionExcel(file, id); - break; + //鐐规鍗囩増瀵煎叆 + return eamMaintenanceStandardService.importPointInspectionExcel(file,id); case "SECOND_MAINTENANCE": - // 浜屼繚鍗囩増瀵煎叆 - importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND", id); - break; + //浜屼繚鍗囩増瀵煎叆 + return eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND",id); case "THIRD_MAINTENANCE": - // 涓変繚鍗囩増瀵煎叆 - importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "THIRD", id); - break; + //涓変繚鍗囩増瀵煎叆 + return eamMaintenanceStandardService.importMaintenanceStandard(file, "THIRD",id); default: - return Result.error("涓嶆敮鎸佺殑淇濆吇绫诲瀷"); - } - - // 澶勭悊鍗囩増瀵煎叆涓殑璁惧鏈壘鍒版儏鍐� - if (importResult.isSuccess()) { - String message = importResult.getMessage(); - if (message.contains("浠ヤ笅璁惧缂栫爜鏈壘鍒帮細")) { - // 鎻愬彇鏈壘鍒扮殑璁惧缂栫爜 - String notFoundPart = message.substring(message.indexOf("浠ヤ笅璁惧缂栫爜鏈壘鍒�") + 9); - return Result.error("鍗囩増瀵煎叆澶辫触锛�" + notFoundPart); - } - return importResult; - } else { - return importResult; + break; } } + return Result.error("鍗囩増瀵煎叆澶辫触"); } private Result<?> getResult(Map<String, String> results, int fileCount, int successCount, int failureCount) { -- Gitblit v1.9.3