xyc
9 小时以前 1c9505d00bd076898cdc5a7d916106e175a91119
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<FreightForecastBaseMonth> 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<FreightForecastBaseMonth> 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;
    }
}