package com.trafficaudit.freightforecast.service; import com.trafficaudit.freightforecast.entity.FreightForecastBaseMonth; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** Parses monthly below-scale base values from 42-2026年基数.xlsx L14:W15. */ public final class FreightForecastBaseParser { private static final int FREIGHT_ROW = 13; private static final int TURNOVER_ROW = 14; private static final int FIRST_MONTH_COLUMN = 11; private static final int MONTHS = 12; private FreightForecastBaseParser() { } public static List parse(InputStream input, int statYear, String sourceFile) throws Exception { try (Workbook workbook = WorkbookFactory.create(input)) { Sheet sheet = workbook.getSheet("湖北"); if (sheet == null) sheet = workbook.getSheetAt(0); FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); Row freightRow = sheet.getRow(FREIGHT_ROW); Row turnoverRow = sheet.getRow(TURNOVER_ROW); if (freightRow == null || turnoverRow == null) { throw new IllegalArgumentException("基数文件缺少第14、15行;应在湖北页的 L14:W15 填写基期数据"); } String freightLabel = text(freightRow.getCell(0), evaluator); String turnoverLabel = text(turnoverRow.getCell(0), evaluator); if (!freightLabel.contains("货运量") || !turnoverLabel.contains("周转量")) { throw new IllegalArgumentException("基数文件第14、15行不是货运量/货物周转量基期数据"); } List rows = new ArrayList<>(); for (int i = 0; i < MONTHS; i++) { int column = FIRST_MONTH_COLUMN + i; Double freight = number(freightRow.getCell(column), evaluator); Double turnover = number(turnoverRow.getCell(column), evaluator); if (freight == null || turnover == null) { throw new IllegalArgumentException("基数文件第 " + (freight == null ? 14 : 15) + " 行第 " + (column + 1) + " 列为空或不是数字"); } FreightForecastBaseMonth row = new FreightForecastBaseMonth(); row.setStatYear(statYear); row.setReportMonth(i + 1); row.setBelowFreight(freight); row.setBelowTurnover(turnover); row.setSourceFile(sourceFile); rows.add(row); } return rows; } } private static String text(Cell cell, FormulaEvaluator evaluator) { if (cell == null) return ""; if (cell.getCellType() == CellType.FORMULA) { CellType type = evaluator.evaluateFormulaCell(cell); if (type == CellType.STRING) return cell.getStringCellValue().trim(); return String.valueOf(cell.getNumericCellValue()).trim(); } return cell.getCellType() == CellType.STRING ? cell.getStringCellValue().trim() : ""; } private static Double number(Cell cell, FormulaEvaluator evaluator) { if (cell == null) return null; if (cell.getCellType() == CellType.NUMERIC) return cell.getNumericCellValue(); if (cell.getCellType() == CellType.FORMULA) { if (cell.getCachedFormulaResultType() == CellType.NUMERIC) return cell.getNumericCellValue(); CellType evaluated = evaluator.evaluateFormulaCell(cell); return evaluated == CellType.NUMERIC ? cell.getNumericCellValue() : null; } return null; } }