From 9483eb4f6d7a75261dbde71ace7878df2f7eda07 Mon Sep 17 00:00:00 2001
From: xyc <jc_xiong@hotmail.com>
Date: 星期三, 11 十一月 2026 20:10:10 +0800
Subject: [PATCH] docs(交接): 更新H202等验收状态
---
traffic-audit-server/src/main/java/com/trafficaudit/dataimport/service/H202WorkbookParser.java | 397 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 397 insertions(+), 0 deletions(-)
diff --git a/traffic-audit-server/src/main/java/com/trafficaudit/dataimport/service/H202WorkbookParser.java b/traffic-audit-server/src/main/java/com/trafficaudit/dataimport/service/H202WorkbookParser.java
new file mode 100644
index 0000000..a13f047
--- /dev/null
+++ b/traffic-audit-server/src/main/java/com/trafficaudit/dataimport/service/H202WorkbookParser.java
@@ -0,0 +1,397 @@
+package com.trafficaudit.dataimport.service;
+
+import com.trafficaudit.common.util.RegionUtil;
+import com.trafficaudit.dataimport.entity.H202FreightVehicleOwnership;
+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.DateUtil;
+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.util.CellReference;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Strict parser for the transport authority H202 workbook.
+ * Excel numbers are read from the raw XLSX decimal text before BigDecimal.
+ */
+public final class H202WorkbookParser {
+ public static final String TYPE_LABEL = "\u516c\u8def\u8d27\u7269\u8425\u8fd0\u8f66\u8f86\u62e5\u6709\u60c5\u51b5(H202)";
+ public static final String SHEET_NAME = "RP_JTB_FreightVehicle";
+ private static final String TITLE = "\u516c\u8def\u8d27\u7269\u8425\u8fd0\u8f66\u8f86\u62e5\u6709\u60c5\u51b5";
+ private static final ThreadLocal<DataFormatter> FORMATTER = new ThreadLocal<DataFormatter>() {
+ @Override
+ protected DataFormatter initialValue() {
+ return new DataFormatter();
+ }
+ };
+ private static final Pattern FILE_REGION = Pattern.compile(
+ "^RP_JTB_FreightVehicle_(.+?)\\.xlsx$", Pattern.CASE_INSENSITIVE);
+ private static final Pattern DATE_PATTERN = Pattern.compile(
+ "(\\d{4})\\s*[/.-]\\s*(\\d{1,2})\\s*[/.-]\\s*(\\d{1,2})");
+ private static final String[] COLUMN_LABELS = {
+ "\u603b\u8ba1", "\u4e2a\u4f53", "\u5927\u578b", "\u5927\u578b-\u91cd\u578b",
+ "\u5927\u578b-\u91cd\u578b-\u4e2a\u4f53", "\u5927\u578b-\u4e2a\u4f53", "\u4e2d\u578b",
+ "\u4e2d\u578b-\u4e2a\u4f53", "\u5c0f\u578b", "\u5c0f\u578b-\u4e2a\u4f53"
+ };
+
+ private H202WorkbookParser() {
+ }
+
+ public static ParsedWorkbook parse(Workbook workbook, String fileName, String reportPeriod) {
+ if (workbook == null) {
+ throw new IllegalArgumentException("\u672a\u63d0\u4f9b H202 \u5de5\u4f5c\u7c3f");
+ }
+ Sheet sheet = workbook.getSheet(SHEET_NAME);
+ if (sheet == null) {
+ throw new IllegalArgumentException(TYPE_LABEL + " \u4e0e\u6587\u4ef6\u5185\u5bb9\u4e0d\u7b26\uff1a\u672a\u627e\u5230 sheet\u3010"
+ + SHEET_NAME + "\u3011");
+ }
+ Row titleRow = sheet.getRow(1);
+ String title = text(titleRow == null ? null : titleRow.getCell(0));
+ if (!TITLE.equals(title)) {
+ throw new IllegalArgumentException(TYPE_LABEL + " \u4e0e\u6587\u4ef6\u5185\u5bb9\u4e0d\u7b26\uff1aA2 \u5e94\u4e3a\u3010"
+ + TITLE + "\u3011\uff0c\u5b9e\u9645\u4e3a\u3010" + title + "\u3011");
+ }
+ validateHeaders(sheet);
+ String regionName = regionFromFileName(fileName);
+ Row metaRow = sheet.getRow(2);
+ LocalDate reportDate = parseReportDate(metaRow == null ? null : metaRow.getCell(11));
+ if (reportDate == null) {
+ throw new IllegalArgumentException("\u672a\u8bfb\u53d6\u5230\u62a5\u8868\u65e5\u671f\uff0c\u8bf7\u68c0\u67e5 L3 \u5355\u5143\u683c\uff1a\u5e94\u4e3a\u300c\u62a5\u8868\u65e5\u671f\uff1a2026/9/30\u300d");
+ }
+ String reportMonth = String.format("%04d-%02d", reportDate.getYear(), reportDate.getMonthValue());
+ if (reportPeriod == null || !reportPeriod.matches("\\d{4}-\\d{2}")) {
+ throw new IllegalArgumentException("\u62a5\u8868\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u5e94\u4e3a yyyy-MM");
+ }
+ if (!reportPeriod.equals(reportMonth)) {
+ throw new IllegalArgumentException("\u6587\u4ef6\u62a5\u8868\u65e5\u671f " + reportDate
+ + " \u4e0e\u6240\u9009\u62a5\u8868\u671f " + reportPeriod + " \u4e0d\u4e00\u81f4");
+ }
+
+ List<H202FreightVehicleOwnership> rows = new ArrayList<H202FreightVehicleOwnership>();
+ Set<String> seenCodes = new LinkedHashSet<String>();
+ String currentItemName = null;
+ for (int rowIndex = 8; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
+ Row row = sheet.getRow(rowIndex);
+ if (row == null) {
+ continue;
+ }
+ String code = normalizeCode(text(row.getCell(2)));
+ boolean hasValue = hasAnyValue(row);
+ if (code.isEmpty()) {
+ if (hasValue) {
+ throw new IllegalArgumentException("\u7b2c " + (rowIndex + 1)
+ + " \u884c\u6709\u6570\u503c\u4f46\u6307\u6807\u4ee3\u7801\u4e3a\u7a7a");
+ }
+ continue;
+ }
+ if (!code.matches("\\d{2}")) {
+ throw new IllegalArgumentException("\u7b2c " + (rowIndex + 1)
+ + " \u884c\u6307\u6807\u4ee3\u7801\u4e0d\u5408\u6cd5\uff1a" + code);
+ }
+ if (!seenCodes.add(code)) {
+ throw new IllegalArgumentException("\u6307\u6807\u4ee3\u7801 " + code + " \u5728\u7b2c "
+ + (rowIndex + 1) + " \u884c\u91cd\u590d");
+ }
+ String sourceName = text(row.getCell(0));
+ if (!sourceName.isEmpty()) {
+ currentItemName = sourceName;
+ }
+ if (currentItemName == null || currentItemName.isEmpty()) {
+ throw new IllegalArgumentException("\u7b2c " + (rowIndex + 1)
+ + " \u884c\u6307\u6807\u540d\u79f0\u4e3a\u7a7a\uff0c\u4e14\u65e0\u4e0a\u65b9\u6307\u6807\u540d\u53ef\u7ee7\u627f");
+ }
+
+ H202FreightVehicleOwnership entity = new H202FreightVehicleOwnership();
+ entity.setReportPeriod(reportPeriod);
+ entity.setRegionName(regionName);
+ entity.setReportDate(reportDate);
+ entity.setItemCode(code);
+ entity.setItemName(currentItemName);
+ entity.setUnit(text(row.getCell(1)));
+ entity.setSourceFileName(fileName);
+ entity.setTotalValue(decimal(row, 3));
+ entity.setIndividualValue(decimal(row, 4));
+ entity.setLargeValue(decimal(row, 5));
+ entity.setLargeHeavyValue(decimal(row, 6));
+ entity.setLargeHeavyIndividualValue(decimal(row, 7));
+ entity.setLargeIndividualValue(decimal(row, 8));
+ entity.setMediumValue(decimal(row, 9));
+ entity.setMediumIndividualValue(decimal(row, 10));
+ entity.setSmallValue(decimal(row, 11));
+ entity.setSmallIndividualValue(decimal(row, 12));
+ rows.add(entity);
+ }
+
+ validateContiguousCodes(seenCodes);
+ recomputeTotals(rows);
+ return new ParsedWorkbook(regionName, reportDate, rows);
+ }
+
+ private static void validateContiguousCodes(Set<String> seenCodes) {
+ int expected = 1;
+ for (String code : seenCodes) {
+ String expectedCode = String.format("%02d", expected);
+ if (!expectedCode.equals(code)) {
+ throw new IllegalArgumentException("H202 \u6307\u6807\u4ee3\u7801\u5fc5\u987b\u4ece 01 \u5f00\u59cb\u8fde\u7eed\uff1a"
+ + "\u671f\u671b " + expectedCode + "\uff0c\u5b9e\u9645 " + code);
+ }
+ expected++;
+ }
+ if (expected <= 2) {
+ throw new IllegalArgumentException("H202 \u6307\u6807\u4ee3\u7801\u4e0d\u5b8c\u6574");
+ }
+ }
+
+ private static void recomputeTotals(List<H202FreightVehicleOwnership> rows) {
+ H202FreightVehicleOwnership vehicleTotal = requireCode(rows, "01", "\u603b\u8ba1", "\u8f86");
+ H202FreightVehicleOwnership tonnageTotal = requireCode(rows, "02", "\u603b\u8ba1", "\u5428\u4f4d");
+ H202FreightVehicleOwnership truckVehicle = requireItem(rows, "1.\u8d27\u8f66", "\u8f86");
+ H202FreightVehicleOwnership tractorVehicle = requireItem(rows, "2.\u7275\u5f15\u8f66", "\u8f86");
+ H202FreightVehicleOwnership trailerVehicle = requireItem(rows, "3.\u6302\u8f66", "\u8f86");
+ H202FreightVehicleOwnership otherVehicle = requireItem(rows, "\u4e8c\u3001\u5176\u5b83\u8f7d\u8d27\u673a\u52a8\u8f66", "\u8f86");
+ H202FreightVehicleOwnership truckTonnage = requireItem(rows, "1.\u8d27\u8f66", "\u5428\u4f4d");
+ H202FreightVehicleOwnership trailerTonnage = requireItem(rows, "3.\u6302\u8f66", "\u5428\u4f4d");
+ H202FreightVehicleOwnership otherTonnage = requireItem(rows, "\u4e8c\u3001\u5176\u5b83\u8f7d\u8d27\u673a\u52a8\u8f66", "\u5428\u4f4d");
+
+ vehicleTotal.setTotalValue(addRequired(vehicleTotal,
+ new H202FreightVehicleOwnership[]{truckVehicle, tractorVehicle, trailerVehicle, otherVehicle}));
+ tonnageTotal.setTotalValue(addRequired(tonnageTotal,
+ new H202FreightVehicleOwnership[]{truckTonnage, trailerTonnage, otherTonnage}));
+ }
+
+ private static H202FreightVehicleOwnership requireCode(List<H202FreightVehicleOwnership> rows,
+ String code, String itemName, String unit) {
+ H202FreightVehicleOwnership found = null;
+ for (H202FreightVehicleOwnership row : rows) {
+ if (!code.equals(row.getItemCode())) {
+ continue;
+ }
+ if (found != null) {
+ throw new IllegalArgumentException("H202 \u6307\u6807\u4ee3\u7801\u91cd\u590d\uff1a" + code);
+ }
+ found = row;
+ }
+ if (found == null) {
+ throw new IllegalArgumentException("H202 \u7f3a\u5c11\u603b\u8ba1\u6307\u6807\uff1a" + code);
+ }
+ if (!itemName.equals(found.getItemName()) || !unit.equals(found.getUnit())) {
+ throw new IllegalArgumentException("H202 \u6307\u6807 " + code + " \u7684\u540d\u79f0\u6216\u5355\u4f4d\u4e0d\u7b26\uff1a"
+ + found.getItemName() + "/" + found.getUnit());
+ }
+ return found;
+ }
+
+ private static H202FreightVehicleOwnership requireItem(List<H202FreightVehicleOwnership> rows,
+ String itemName, String unit) {
+ H202FreightVehicleOwnership found = null;
+ for (H202FreightVehicleOwnership row : rows) {
+ if (!itemName.equals(row.getItemName()) || !unit.equals(row.getUnit())) {
+ continue;
+ }
+ if (found != null) {
+ throw new IllegalArgumentException("H202 \u5206\u9879\u6307\u6807\u91cd\u590d\uff1a" + itemName + "/" + unit);
+ }
+ found = row;
+ }
+ if (found == null) {
+ throw new IllegalArgumentException("H202 \u7f3a\u5c11\u5206\u9879\u6307\u6807\uff1a" + itemName + "/" + unit);
+ }
+ return found;
+ }
+
+ private static BigDecimal addRequired(H202FreightVehicleOwnership totalRow,
+ H202FreightVehicleOwnership[] parts) {
+ BigDecimal sum = BigDecimal.ZERO;
+ for (H202FreightVehicleOwnership part : parts) {
+ if (part.getTotalValue() == null) {
+ throw new IllegalArgumentException("H202 \u5206\u9879\u603b\u8ba1\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u91cd\u7b97\uff1a"
+ + part.getItemCode() + " " + part.getItemName() + "/" + part.getUnit());
+ }
+ sum = sum.add(part.getTotalValue());
+ }
+ if (sum.scale() < 0) {
+ sum = sum.setScale(0);
+ }
+ return sum.stripTrailingZeros();
+ }
+
+ public static String regionFromFileName(String fileName) {
+ if (fileName == null) {
+ throw new IllegalArgumentException("\u6587\u4ef6\u540d\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u8bc6\u522b\u5730\u533a");
+ }
+ String baseName = new java.io.File(fileName).getName();
+ Matcher matcher = FILE_REGION.matcher(baseName);
+ if (!matcher.matches()) {
+ throw new IllegalArgumentException("\u6587\u4ef6\u540d\u4e0d\u7b26\u5408 H202 \u547d\u540d\u89c4\u5219\uff1aRP_JTB_FreightVehicle_\u5730\u533a.xlsx");
+ }
+ String region = RegionUtil.normalizeCityName(matcher.group(1));
+ if (!"\u6e56\u5317\u7701".equals(region) && !RegionUtil.CITY_LIST.contains(region)) {
+ throw new IllegalArgumentException("\u65e0\u6cd5\u4ece\u6587\u4ef6\u540d\u8bc6\u522b\u6e56\u5317\u7701\u6216 17 \u4e2a\u5e02\u5dde\uff1a"
+ + matcher.group(1));
+ }
+ return region;
+ }
+
+ private static void validateHeaders(Sheet sheet) {
+ Row header = sheet.getRow(4);
+ String[] expected = {"\u6307\u6807\u540d\u79f0", "\u8ba1\u91cf", "\u4ee3\u7801", "\u603b\u8ba1", "\u4e2a\u4f53",
+ "\u5927\u578b", "", "", "", "\u4e2d\u578b", "", "\u5c0f\u578b", ""};
+ for (int col = 0; col < expected.length; col++) {
+ if (expected[col].isEmpty()) {
+ continue;
+ }
+ String actual = text(header == null ? null : header.getCell(col));
+ if (!expected[col].equals(actual)) {
+ throw new IllegalArgumentException(TYPE_LABEL + " \u8868\u5934\u4e0e\u6a21\u677f\u4e0d\u4e00\u81f4\uff1a"
+ + CellReference.convertNumToColString(col) + "5 \u5e94\u4e3a\u3010" + expected[col]
+ + "\u3011\uff0c\u5b9e\u9645\u4e3a\u3010" + actual + "\u3011");
+ }
+ }
+ }
+
+ private static boolean hasAnyValue(Row row) {
+ for (int col = 3; col <= 12; col++) {
+ Cell cell = row.getCell(col);
+ if (cell == null || cell.getCellType() == CellType.BLANK) {
+ continue;
+ }
+ String value = text(cell);
+ if (!value.isEmpty() && !isDash(value)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static BigDecimal decimal(Row row, int col) {
+ Cell cell = row.getCell(col);
+ String label = COLUMN_LABELS[col - 3];
+ String itemName = text(row.getCell(0));
+ if (itemName.isEmpty() && row.getRowNum() > 0) {
+ itemName = text(row.getSheet().getRow(row.getRowNum() - 1).getCell(0));
+ }
+ String context = "\u7b2c " + (row.getRowNum() + 1) + " \u884c " + CellReference.convertNumToColString(col)
+ + " \u5217\u300c" + label + "\u300d" + (itemName.isEmpty() ? "" : "\uff08" + itemName + "\uff09");
+ if (cell == null || cell.getCellType() == CellType.BLANK) {
+ return null;
+ }
+ CellType type = cell.getCellType();
+ if (type == CellType.FORMULA) {
+ throw new IllegalArgumentException(context
+ + "\u4e0d\u652f\u6301\u516c\u5f0f\uff0c\u8bf7\u5c06\u5176\u8f6c\u4e3a\u6570\u503c\u6216\u6587\u672c\u540e\u518d\u5bfc\u5165");
+ }
+ if (type == CellType.NUMERIC) {
+ return parseDecimal(rawNumericText(cell, context), context);
+ }
+ if (type == CellType.STRING) {
+ return parseDecimal(cell.getStringCellValue(), context);
+ }
+ throw new IllegalArgumentException(context + "\u4e0d\u662f\u6709\u6548\u6570\u5b57\uff1a" + text(cell));
+ }
+
+ private static BigDecimal parseDecimal(String raw, String context) {
+ if (raw == null) {
+ return null;
+ }
+ String value = raw.trim();
+ if (value.isEmpty() || isDash(value)) {
+ return null;
+ }
+ String compact = value.replace(",", "").replace("\u00a0", "").replace(" ", "");
+ BigDecimal decimal;
+ try {
+ decimal = new BigDecimal(compact).stripTrailingZeros();
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(context + "\u4e0d\u662f\u6709\u6548\u6570\u5b57\uff1a" + value, e);
+ }
+ if (decimal.scale() < 0) {
+ decimal = decimal.setScale(0);
+ }
+ if (decimal.scale() > 15 || decimal.precision() - decimal.scale() > 15) {
+ throw new IllegalArgumentException(context + "\u8d85\u51fa DECIMAL(30,15) \u8303\u56f4\uff1a" + value);
+ }
+ return decimal;
+ }
+
+ private static String rawNumericText(Cell cell, String context) {
+ if (!(cell instanceof XSSFCell)) {
+ throw new IllegalArgumentException(context + "\u4ec5\u652f\u6301 .xlsx \u539f\u59cb\u6570\u503c\uff0c\u8bf7\u53e6\u5b58\u4e3a .xlsx");
+ }
+ String raw = ((XSSFCell) cell).getRawValue();
+ if (raw == null || raw.trim().isEmpty()) {
+ throw new IllegalArgumentException(context + "\u672a\u8bfb\u53d6\u5230\u539f\u59cb\u6570\u503c");
+ }
+ return raw;
+ }
+
+ private static LocalDate parseReportDate(Cell cell) {
+ if (cell == null) {
+ return null;
+ }
+ if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
+ return cell.getLocalDateTimeCellValue().toLocalDate();
+ }
+ String value = text(cell);
+ Matcher matcher = DATE_PATTERN.matcher(value);
+ if (!matcher.find()) {
+ return null;
+ }
+ return LocalDate.of(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)),
+ Integer.parseInt(matcher.group(3)));
+ }
+
+ private static boolean isDash(String value) {
+ return "-".equals(value) || "\u2014".equals(value) || "\u2013".equals(value);
+ }
+
+ private static String normalizeCode(String value) {
+ String code = value == null ? "" : value.trim();
+ if (code.matches("\\d")) {
+ return "0" + code;
+ }
+ return code;
+ }
+
+ private static String text(Cell cell) {
+ if (cell == null) {
+ return "";
+ }
+ return FORMATTER.get().formatCellValue(cell).trim();
+ }
+
+ public static final class ParsedWorkbook {
+ private final String regionName;
+ private final LocalDate reportDate;
+ private final List<H202FreightVehicleOwnership> rows;
+
+ ParsedWorkbook(String regionName, LocalDate reportDate, List<H202FreightVehicleOwnership> rows) {
+ this.regionName = regionName;
+ this.reportDate = reportDate;
+ this.rows = rows;
+ }
+
+ public String getRegionName() {
+ return regionName;
+ }
+
+ public LocalDate getReportDate() {
+ return reportDate;
+ }
+
+ public List<H202FreightVehicleOwnership> getRows() {
+ return rows;
+ }
+ }
+}
--
Gitblit v1.9.1