package com.trafficaudit.freightforecast.service; import com.trafficaudit.common.util.RegionUtil; import com.trafficaudit.freightforecast.dto.FreightForecastReportRow; 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 org.apache.poi.ss.usermodel.WorkbookFactory; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /** Parses the enterprise rows needed by the monthly freight audit report. */ public final class FreightForecastReportParser { private static final String ENTERPRISE_NAME = "企业名称"; private static final String REGION_CODE = "所属地区"; private static final String FREIGHT_TOTAL = "货运量_总计"; private static final String TURNOVER_TOTAL = "货物周转量_总计"; private FreightForecastReportParser() { } public static List parse( InputStream input, Map annualCities) throws Exception { try (Workbook workbook = WorkbookFactory.create(input)) { Sheet sheet = workbook.getSheetAt(0); Map columns = headerMap(sheet.getRow(0)); require(columns, ENTERPRISE_NAME, FREIGHT_TOTAL, TURNOVER_TOTAL); List rows = new ArrayList<>(); Set seen = new HashSet<>(); DataFormatter formatter = new DataFormatter(); for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row row = sheet.getRow(rowIndex); if (row == null) continue; String enterpriseName = text(row, columns, ENTERPRISE_NAME, formatter); if (enterpriseName.isEmpty()) continue; String key = normalizeName(enterpriseName); if (!seen.add(key)) { throw new IllegalArgumentException("第 " + (rowIndex + 1) + " 行企业名称重复:" + enterpriseName); } String regionCode = text(row, columns, REGION_CODE, formatter); String city = annualCities.get(key); if (city == null || city.isEmpty()) city = RegionUtil.cityByCode(regionCode); if (city == null || city.isEmpty()) { throw new IllegalArgumentException("第 " + (rowIndex + 1) + " 行企业无法匹配规上企业名录或市州:" + enterpriseName); } FreightForecastReportRow entity = new FreightForecastReportRow(); entity.setRegionCode(regionCode); entity.setCity(RegionUtil.normalizeCityName(city)); entity.setEnterpriseName(enterpriseName); entity.setFreightTotal(number(row, columns, FREIGHT_TOTAL, formatter)); entity.setTurnoverTotal(number(row, columns, TURNOVER_TOTAL, formatter)); rows.add(entity); } if (rows.isEmpty()) throw new IllegalArgumentException("货运月报未解析到企业数据"); return rows; } } private static Map headerMap(Row header) { Map columns = new HashMap<>(); if (header == null) return columns; DataFormatter formatter = new DataFormatter(); 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 require(Map columns, String... names) { List missing = new ArrayList<>(); for (String name : names) if (!columns.containsKey(name)) missing.add(name); if (!missing.isEmpty()) { throw new IllegalArgumentException("货运月报表头缺少必需列:【" + String.join("】【", missing) + "】"); } } private static String text(Row row, Map columns, String name, DataFormatter formatter) { Integer column = columns.get(name); if (column == null) return ""; Cell cell = row.getCell(column); return cell == null ? "" : formatter.formatCellValue(cell).trim(); } private static Double number(Row row, Map columns, String name, DataFormatter formatter) { Integer column = columns.get(name); Cell cell = column == null ? null : row.getCell(column); if (cell == null || cell.getCellType() == CellType.BLANK) return null; 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().replace(",", ""); if (value.isEmpty()) return null; try { return Double.parseDouble(value); } catch (NumberFormatException e) { throw new IllegalArgumentException("第 " + (row.getRowNum() + 1) + " 行的【" + name + "】不是有效数字:【" + value + "】"); } } public static String normalizeName(String value) { if (value == null) return ""; return value.trim().replace('(', '(').replace(')', ')').replaceAll("\\s+", ""); } }