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
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
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<FreightForecastReportRow> parse(
            InputStream input, Map<String, String> annualCities) throws Exception {
        try (Workbook workbook = WorkbookFactory.create(input)) {
            Sheet sheet = workbook.getSheetAt(0);
            Map<String, Integer> columns = headerMap(sheet.getRow(0));
            require(columns, ENTERPRISE_NAME, FREIGHT_TOTAL, TURNOVER_TOTAL);
            List<FreightForecastReportRow> rows = new ArrayList<>();
            Set<String> 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<String, Integer> headerMap(Row header) {
        Map<String, Integer> 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<String, Integer> columns, String... names) {
        List<String> 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<String, Integer> 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<String, Integer> 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+", "");
    }
}