| | |
| | | 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; |
| | |
| | | * 二保导入 |
| | | */ |
| | | @PostMapping("/importSecondMaintenanceStandard") |
| | | public Result<?> importSecondMaintenanceStandard(HttpServletRequest request) { |
| | | public ResponseEntity<?> importSecondMaintenanceStandard(HttpServletRequest request) { |
| | | try { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | MultipartFile file = entity.getValue(); |
| | | String fileName = file.getOriginalFilename(); |
| | | if (fileName == null || fileName.contains("undefined") || fileName.trim().isEmpty()) { |
| | | fileName = "file_" + System.currentTimeMillis() + ".tmp"; |
| | | } |
| | | Result<?> importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND", null); |
| | | successDeviceCount = getSuccessDeviceCount(successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes, fileName, 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("但以下设备编码未找到:")) { |
| | | // 提取未找到的设备编码 |
| | | Pattern codePattern = Pattern.compile("但以下设备编码未找到:\\s*(\\d+)"); |
| | | Matcher codeMatcher = codePattern.matcher(message); |
| | | while (codeMatcher.find()) { |
| | | notFoundCodes.add(codeMatcher.group(1)); |
| | | } |
| | | } |
| | | } else { |
| | | 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)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 构建响应 |
| | | return getResult(totalFiles, successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes); |
| | | // 检查是否存在未找到的设备编码 |
| | | if (!notFoundCodes.isEmpty()) { |
| | | // 创建包含未找到编码的CSV文件 |
| | | String csvContent = "未找到的设备编码\n" + String.join("\n", notFoundCodes); |
| | | byte[] csvBytes = csvContent.getBytes(StandardCharsets.UTF_8); |
| | | |
| | | // 设置响应头 |
| | | 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())); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 三保导入 |
| | | */ |
| | | @PostMapping("/importThirdMaintenanceStandard") |
| | | public ResponseEntity<?> importThirdMaintenanceStandard(HttpServletRequest request) { |
| | | try { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | 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(); |
| | | 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(); |
| | | 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 { |
| | | 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); |
| | | |
| | | // 设置响应头 |
| | | 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 ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| | | .body(Result.error("导入处理失败: " + e.getClass().getSimpleName() + " - " + e.getMessage())); |
| | | } |
| | | } |
| | | |
| | |
| | | failedFiles.put(fileName, importResult.getMessage()); |
| | | } |
| | | return successDeviceCount; |
| | | } |
| | | |
| | | /** |
| | | * 三保导入 |
| | | */ |
| | | @PostMapping("/importThirdMaintenanceStandard") |
| | | public Result<?> importThirdMaintenanceStandard(HttpServletRequest request) { |
| | | try { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | 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); |
| | | successDeviceCount = getSuccessDeviceCount(successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes, fileName, importResult); |
| | | } |
| | | // 构建响应 |
| | | return getResult(totalFiles, successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes); |
| | | } catch (Exception e) { |
| | | log.error("导入处理异常", e); |
| | | return Result.error("导入处理失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |