| | |
| | | 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.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | 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: 保养标准 |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 二保导入 |
| | | */ |
| | | */ |
| | | @PostMapping("/importSecondMaintenanceStandard") |
| | | public Result<?> 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 (importResult.isSuccess()) { |
| | | // 检查是否包含部分成功信息 |
| | | 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(";但"))); |
| | | } |
| | | successCount++; |
| | | } else { |
| | | results.put(fileName, importResult.getMessage()); |
| | | failureCount++; |
| | | } |
| | | Result<?> importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "SECOND", null); |
| | | successDeviceCount = getSuccessDeviceCount(successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes, fileName, importResult); |
| | | } |
| | | |
| | | // 如果有未找到的设备编码,添加到最终结果 |
| | | if (!allNotFoundCodes.isEmpty()) { |
| | | String notFoundMsg = "以下设备编码未找到" + String.join(", ", allNotFoundCodes); |
| | | if (successCount > 0) { |
| | | // 部分成功 |
| | | return Result.ok("部分导入成功,成功文件数:" + successCount + |
| | | ",失败文件数:" + failureCount + ";" + notFoundMsg); |
| | | } else { |
| | | // 全部失败 |
| | | return Result.error("导入失败:" + notFoundMsg); |
| | | } |
| | | } |
| | | |
| | | if (successCount == 1) { |
| | | return importResult; |
| | | } |
| | | |
| | | // 构建最终响应 |
| | | return getResult(results, fileMap.size(), successCount, failureCount); |
| | | |
| | | // 构建响应 |
| | | return getResult(totalFiles, successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes); |
| | | } catch (Exception e) { |
| | | log.error("导入处理异常", e); |
| | | return Result.error("导入处理失败: " + 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | public Result<?> 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 (importResult.isSuccess()) { |
| | | // 检查是否包含部分成功信息 |
| | | 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(";但"))); |
| | | } |
| | | successCount++; |
| | | } else { |
| | | results.put(fileName, importResult.getMessage()); |
| | | failureCount++; |
| | | } |
| | | Result<?> importResult = eamMaintenanceStandardService.importMaintenanceStandard(file, "THIRD", null); |
| | | successDeviceCount = getSuccessDeviceCount(successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes, fileName, importResult); |
| | | } |
| | | |
| | | // 如果有未找到的设备编码,添加到最终结果 |
| | | if (!allNotFoundCodes.isEmpty()) { |
| | | String notFoundMsg = "以下设备编码未找到" + String.join(", ", allNotFoundCodes); |
| | | if (successCount > 0) { |
| | | // 部分成功 |
| | | return Result.ok("部分导入成功,成功文件数:" + successCount + |
| | | ",失败文件数:" + failureCount + ";" + notFoundMsg); |
| | | } else { |
| | | // 全部失败 |
| | | return Result.error("导入失败:" + notFoundMsg); |
| | | } |
| | | } |
| | | |
| | | if (successCount == 1) { |
| | | return importResult; |
| | | } |
| | | |
| | | // 构建最终响应 |
| | | return getResult(results, fileMap.size(), successCount, failureCount); |
| | | |
| | | // 构建响应 |
| | | return getResult(totalFiles, successFileNames, failedFileNames, failedFiles, successDeviceCount, notFoundCodes); |
| | | } catch (Exception e) { |
| | | log.error("导入处理异常", e); |
| | | return Result.error("导入处理失败: " + e.getMessage()); |
| | |
| | | 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) { |