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 | 274 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 240 insertions(+), 34 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 0d7d06e..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 @@ -7,30 +7,39 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.apache.shiro.SecurityUtils; import org.jeecg.common.api.vo.Result; import org.jeecg.common.aspect.annotation.AutoLog; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.system.base.controller.JeecgController; +import org.jeecg.common.system.vo.LoginUser; import org.jeecg.modules.eam.constant.BusinessCodeConst; import org.jeecg.modules.eam.constant.MaintenanceStandardStatusEnum; 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: 淇濆吇鏍囧噯 @@ -47,8 +56,6 @@ private IEamMaintenanceStandardService eamMaintenanceStandardService; @Autowired private ISysBusinessCodeRuleService businessCodeRuleService; - @Autowired - private IEamEquipmentService eamEquipmentService; /** * 娴佺▼鍚姩,淇濆瓨瀵瑰簲鐨勬暟鎹� @@ -59,6 +66,11 @@ @ApiOperation(value = "淇濆吇鏍囧噯-娴佺▼鍚姩淇濆瓨瀵瑰簲鐨勬暟鎹�", notes = "淇濆吇鏍囧噯-娴佺▼鍚姩淇濆瓨瀵瑰簲鐨勬暟鎹�") @GetMapping(value = "/saveEamMaintenanceStandardProcess") public Result<?> saveEamMaintenanceStandardProcess(String id) { + // 鏍¢獙棰嗗彇鏉冮檺 + LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); + if (!BusinessCodeConst.PCR0002.equals(user.getPost())) { + return Result.error("濉姤澶辫触,娌℃湁濉姤鏉冮檺锛�,闇�缁翠慨宸ユ彁浜�"); + } return eamMaintenanceStandardService.saveEamMaintenanceStandardProcess(id); } @@ -286,7 +298,7 @@ for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue(); String fileName = file.getOriginalFilename(); - Result<?> importResult=eamMaintenanceStandardService.importPointInspectionExcel(file); + Result<?> importResult=eamMaintenanceStandardService.importPointInspectionExcel(file,null); if (importResult.isSuccess()) { successCount++; } else { @@ -304,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"); + 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())); } } @@ -344,37 +414,173 @@ * 涓変繚瀵煎叆 */ @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"); + 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; + } + + /** + * 鍗囩増瀵煎叆 + */ + @PostMapping(value = "/importUpgrade") + public Result<?> importUpgrade(String id, MultipartFile file) { + EamMaintenanceStandard eamMaintenanceStandard = eamMaintenanceStandardService.getById(id); + if (eamMaintenanceStandard == null) { + return Result.error("璇烽�夋嫨闇�瑕佸崌鐗堢殑淇濆吇鏍囧噯"); + } else { + switch (eamMaintenanceStandard.getMaintenanceCategory()) { + case "POINT_INSPECTION": + //鐐规鍗囩増瀵煎叆 + return eamMaintenanceStandardService.importPointInspectionExcel(file,id); + case "SECOND_MAINTENANCE": + //浜屼繚鍗囩増瀵煎叆 + return eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND",id); + case "THIRD_MAINTENANCE": + //涓変繚鍗囩増瀵煎叆 + return eamMaintenanceStandardService.importMaintenanceStandard(file, "THIRD",id); + default: + break; + } + } + return Result.error("鍗囩増瀵煎叆澶辫触"); + } + private Result<?> getResult(Map<String, String> results, int fileCount, int successCount, int failureCount) { List<String> response = new ArrayList<>(); response.add("鎬绘枃浠舵暟锛�" + fileCount); -- Gitblit v1.9.3