xyc
2 小时以前 5b41c8896ef7850be2672e45b69bb58e149729ec
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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<TransportAuthVehicle> 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<String, Integer> 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<TransportAuthVehicle> 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<String, Integer> buildHeaderMap(Row header) {
        Map<String, Integer> 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<String, Integer> columns) {
        List<String> 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<String, Integer> 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<String, Integer> columns, String header) {
        Double value = decimalValue(row, columns, header);
        return value == null ? 0 : value.intValue();
    }
 
    private static Integer nullableIntValue(Row row, Map<String, Integer> 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<String, Integer> 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 + "】");
        }
    }
}