package com.trafficaudit.dataimport.service; import com.trafficaudit.dataimport.entity.TransportAuthVehicle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** Parser for the monthly transport-authority freight vehicle workbook. */ public final class TransportAuthWorkbookParser { public static final String TYPE_LABEL = "运政车辆"; public static final String CONTAINER_COUNT_HEADER = "集装箱车辆数"; public static final String FIRST_CONTAINER_COUNT_PERIOD = "2026-11"; private static final String[] REQUIRED_HEADERS = { "企业名称", "总牵引车数", "总挂车数", "其它车辆数", "挂车总吨位(吨)", "其它车辆总吨位(吨)" }; private static final DataFormatter FORMATTER = new DataFormatter(); private TransportAuthWorkbookParser() { } public static List parse(Workbook workbook, String reportPeriod) { if (workbook == null || workbook.getNumberOfSheets() == 0) { throw new IllegalArgumentException(TYPE_LABEL + "工作簿为空"); } if (reportPeriod == null || !reportPeriod.matches("\\d{4}-\\d{2}")) { throw new IllegalArgumentException("报表期格式不正确,应为 yyyy-MM"); } Sheet sheet = workbook.getSheetAt(0); Map columns = buildHeaderMap(sheet.getRow(0)); requireHeaders(columns); if (isContainerCountRequired(reportPeriod) && !columns.containsKey(CONTAINER_COUNT_HEADER)) { throw new IllegalArgumentException(TYPE_LABEL + "数据从 " + FIRST_CONTAINER_COUNT_PERIOD + " 起必须包含表头【" + CONTAINER_COUNT_HEADER + "】"); } List rows = new ArrayList<>(); for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row row = sheet.getRow(rowIndex); if (row == null) continue; String enterpriseName = stringValue(row, columns, "企业名称"); if (enterpriseName == null || enterpriseName.isEmpty()) continue; TransportAuthVehicle entity = new TransportAuthVehicle(); entity.setReportPeriod(reportPeriod); entity.setEnterpriseName(enterpriseName); entity.setTractorCount(intValue(row, columns, "总牵引车数")); entity.setTrailerCount(intValue(row, columns, "总挂车数")); entity.setOtherCount(intValue(row, columns, "其它车辆数")); entity.setTrailerTons(decimalValue(row, columns, "挂车总吨位(吨)")); entity.setOtherTons(decimalValue(row, columns, "其它车辆总吨位(吨)")); entity.setContainerCount(nullableIntValue(row, columns, CONTAINER_COUNT_HEADER)); rows.add(entity); } return rows; } public static boolean isContainerCountRequired(String reportPeriod) { return reportPeriod != null && reportPeriod.compareTo(FIRST_CONTAINER_COUNT_PERIOD) >= 0; } private static Map buildHeaderMap(Row header) { Map columns = new HashMap<>(); if (header == null) return columns; for (Cell cell : header) { String value = FORMATTER.formatCellValue(cell).trim(); if (!value.isEmpty() && !columns.containsKey(value)) { columns.put(value, cell.getColumnIndex()); } } return columns; } private static void requireHeaders(Map columns) { List missing = new ArrayList<>(); for (String header : REQUIRED_HEADERS) { if (!columns.containsKey(header)) missing.add(header); } if (!missing.isEmpty()) { throw new IllegalArgumentException("所选数据类型【" + TYPE_LABEL + "】与文件内容不符:表头缺少必需列【" + String.join("】【", missing) + "】"); } } private static String stringValue(Row row, Map columns, String header) { Integer column = columns.get(header); if (column == null) return null; Cell cell = row.getCell(column); if (cell == null) return null; if (cell.getCellType() == CellType.FORMULA && cell.getCachedFormulaResultType() == CellType.STRING) { String cached = cell.getStringCellValue(); return cached == null || cached.trim().isEmpty() ? null : cached.trim(); } String value = FORMATTER.formatCellValue(cell).trim(); return value.isEmpty() ? null : value; } private static Integer intValue(Row row, Map columns, String header) { Double value = decimalValue(row, columns, header); return value == null ? 0 : value.intValue(); } private static Integer nullableIntValue(Row row, Map columns, String header) { Integer column = columns.get(header); if (column == null) return null; Cell cell = row.getCell(column); if (cell == null) return null; Double value = numericValue(row, cell, header); return value == null ? null : value.intValue(); } private static Double decimalValue(Row row, Map columns, String header) { Integer column = columns.get(header); if (column == null) return 0.0; Cell cell = row.getCell(column); if (cell == null) return 0.0; Double value = numericValue(row, cell, header); return value == null ? 0.0 : value; } private static Double numericValue(Row row, Cell cell, String header) { if (cell.getCellType() == CellType.NUMERIC) return cell.getNumericCellValue(); if (cell.getCellType() == CellType.FORMULA && cell.getCachedFormulaResultType() == CellType.NUMERIC) { return cell.getNumericCellValue(); } String value = FORMATTER.formatCellValue(cell).trim(); if (value.isEmpty()) return null; try { return Double.parseDouble(value.replace(",", "")); } catch (NumberFormatException e) { throw new IllegalArgumentException("第 " + (row.getRowNum() + 1) + " 行的【" + header + "】不是有效数字:【" + value + "】"); } } }