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/subsidyaudit/service/SubsidyApplicationExcelReader.java |  201 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 201 insertions(+), 0 deletions(-)

diff --git a/traffic-audit-server/src/main/java/com/trafficaudit/subsidyaudit/service/SubsidyApplicationExcelReader.java b/traffic-audit-server/src/main/java/com/trafficaudit/subsidyaudit/service/SubsidyApplicationExcelReader.java
new file mode 100644
index 0000000..5ce3451
--- /dev/null
+++ b/traffic-audit-server/src/main/java/com/trafficaudit/subsidyaudit/service/SubsidyApplicationExcelReader.java
@@ -0,0 +1,201 @@
+package com.trafficaudit.subsidyaudit.service;
+
+import com.trafficaudit.subsidyaudit.dto.SubsidyApplicationRow;
+import org.apache.poi.ss.usermodel.Cell;
+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 org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+@Component
+public class SubsidyApplicationExcelReader {
+
+    private static final List<String> PLATE_HEADERS = Arrays.asList(
+            "杞︾墝鍙风爜", "杞︾墝鍙�", "鍙风墝鍙风爜", "鏂板杞﹁締鍙风墝", "杞﹁締鍙风墝");
+    private static final List<String> VIN_HEADERS = Arrays.asList(
+            "杞﹁締璇嗗埆浠e彿", "杞︽灦鍙�", "VIN", "鏂板杞﹁締杞︽灦鍙�");
+    private static final List<String> STANDARD_HEADERS = Arrays.asList(
+            "琛ヨ创鏍囧噯", "鐢宠琛ヨ创鏍囧噯", "琛ヨ创閲戦鏍囧噯", "琛ヨ创鏍囧噯涓囧厓");
+    private static final List<String> TYPE_HEADERS = Arrays.asList(
+            "鐢宠璧勯噾绫诲瀷", "鐢宠绫诲瀷", "璧勯噾绫诲瀷", "琛ヨ创绫诲瀷");
+
+    public List<SubsidyApplicationRow> read(MultipartFile file) {
+        if (file == null || file.isEmpty()) {
+            throw new IllegalArgumentException("璇蜂笂浼犵敵璇疯〃 Excel");
+        }
+        String filename = file.getOriginalFilename() == null ? "" : file.getOriginalFilename().toLowerCase(Locale.ROOT);
+        if (!filename.endsWith(".xlsx") && !filename.endsWith(".xls")) {
+            throw new IllegalArgumentException("鐢宠琛ㄥ繀椤绘槸 .xlsx 鎴� .xls 鏂囦欢");
+        }
+        try (InputStream input = file.getInputStream(); Workbook workbook = WorkbookFactory.create(input)) {
+            Map<Integer, Integer> columns = null;
+            Sheet targetSheet = null;
+            int headerRowIndex = -1;
+            for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
+                Sheet sheet = workbook.getSheetAt(sheetIndex);
+                HeaderMatch match = findHeader(sheet);
+                if (match != null) {
+                    targetSheet = sheet;
+                    columns = match.columns;
+                    headerRowIndex = match.rowIndex;
+                    break;
+                }
+            }
+            if (targetSheet == null) {
+                throw new IllegalArgumentException("鏈壘鍒扮敵璇疯〃琛ㄥご锛岃嚦灏戦渶瑕佽溅鐗屽彿鐮併�佽溅杈嗚瘑鍒唬鍙枫�佽ˉ璐存爣鍑嗕笁鍒�");
+            }
+            return readRows(targetSheet, headerRowIndex, columns);
+        } catch (IllegalArgumentException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new IllegalArgumentException("璇诲彇鐢宠琛ㄥけ璐�: " + e.getMessage(), e);
+        }
+    }
+
+    private HeaderMatch findHeader(Sheet sheet) {
+        int last = Math.min(sheet.getLastRowNum(), 50);
+        DataFormatter formatter = new DataFormatter(Locale.CHINA);
+        for (int rowIndex = 0; rowIndex <= last; rowIndex++) {
+            Row row = sheet.getRow(rowIndex);
+            if (row == null) continue;
+            Map<String, Integer> found = new HashMap<>();
+            for (Cell cell : row) {
+                String value = normalizeHeader(formatter.formatCellValue(cell));
+                if (value.isEmpty()) continue;
+                matchHeader(found, "plate", value, PLATE_HEADERS, cell.getColumnIndex());
+                matchHeader(found, "vin", value, VIN_HEADERS, cell.getColumnIndex());
+                matchHeader(found, "standard", value, STANDARD_HEADERS, cell.getColumnIndex());
+                matchHeader(found, "type", value, TYPE_HEADERS, cell.getColumnIndex());
+            }
+            if (found.containsKey("plate") && found.containsKey("vin") && found.containsKey("standard")) {
+                Map<Integer, Integer> columns = new HashMap<>();
+                columns.put(0, found.get("plate"));
+                columns.put(1, found.get("vin"));
+                columns.put(2, found.get("standard"));
+                if (found.containsKey("type")) columns.put(3, found.get("type"));
+                return new HeaderMatch(rowIndex, columns);
+            }
+        }
+        return null;
+    }
+
+    private void matchHeader(Map<String, Integer> found, String key, String value, List<String> headers, int columnIndex) {
+        if (found.containsKey(key)) return;
+        for (String header : headers) {
+            String normalized = normalizeHeader(header);
+            if (value.equals(normalized) || value.contains(normalized)) {
+                found.put(key, columnIndex);
+                return;
+            }
+        }
+    }
+
+    private List<SubsidyApplicationRow> readRows(Sheet sheet, int headerRowIndex, Map<Integer, Integer> columns) {
+        DataFormatter formatter = new DataFormatter(Locale.CHINA);
+        List<SubsidyApplicationRow> rows = new ArrayList<>();
+        List<String> errors = new ArrayList<>();
+        Set<String> seenVins = new HashSet<>();
+        for (int rowIndex = headerRowIndex + 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
+            Row row = sheet.getRow(rowIndex);
+            if (row == null) continue;
+            String plate = text(row, columns.get(0), formatter);
+            String vin = normalizeVin(text(row, columns.get(1), formatter));
+            String standardText = text(row, columns.get(2), formatter);
+            String type = columns.containsKey(3) ? text(row, columns.get(3), formatter) : "";
+            if (plate.isEmpty() && vin.isEmpty() && standardText.isEmpty() && type.isEmpty()) continue;
+            int excelRow = rowIndex + 1;
+            if (!includeNewVehicle(type)) continue;
+            if (plate.isEmpty()) errors.add("绗� " + excelRow + " 琛岋細杞︾墝鍙风爜涓虹┖");
+            if (!isValidVin(vin)) errors.add("绗� " + excelRow + " 琛岋細杞﹁締璇嗗埆浠e彿涓嶆槸 17 浣嶆湁鏁� VIN");
+            if (!seenVins.add(vin)) errors.add("绗� " + excelRow + " 琛岋細杞﹁締璇嗗埆浠e彿閲嶅");
+            Double standard = parseSubsidyStandard(standardText);
+            if (standard == null) errors.add("绗� " + excelRow + " 琛岋細琛ヨ创鏍囧噯鏃犳硶璇嗗埆锛堟敮鎸� 5.5涓囧厓 鎴� 55000鍏冿級");
+            SubsidyApplicationRow item = new SubsidyApplicationRow();
+            item.setExcelRow(excelRow);
+            item.setPlateNumber(plate.replaceAll("\\s+", "").toUpperCase(Locale.ROOT));
+            item.setVin(vin);
+            item.setSubsidyStandard(standard == null ? 0.0 : standard);
+            item.setApplicationType(type);
+            rows.add(item);
+        }
+        if (!errors.isEmpty()) {
+            throw new IllegalArgumentException(joinErrors(errors));
+        }
+        if (rows.isEmpty()) {
+            throw new IllegalArgumentException("鐢宠琛ㄦ湭璇诲彇鍒版柊澧炶溅杈嗘暟鎹�");
+        }
+        return rows;
+    }
+
+    private boolean includeNewVehicle(String type) {
+        String value = type == null ? "" : type.replaceAll("\\s+", "");
+        if (value.isEmpty()) return true;
+        boolean newVehicle = value.contains("鏇存柊") || value.contains("鏂板") || value.contains("鏂拌喘") || value.contains("鏂拌喘缃�");
+        if (newVehicle) return true;
+        return !value.contains("鎶ュ簾");
+    }
+
+    private Double parseSubsidyStandard(String raw) {
+        if (raw == null || raw.trim().isEmpty()) return null;
+        String value = raw.replace(",", "").replace("锛�", "").trim();
+        boolean tenThousand = value.contains("涓�");
+        value = value.replace("涓囧厓", "").replace("涓�", "").replace("鍏�", "").trim();
+        try {
+            double number = Double.parseDouble(value);
+            if (tenThousand || number < 10000.0) number *= 10000.0;
+            return number;
+        } catch (NumberFormatException e) {
+            return null;
+        }
+    }
+
+    private String text(Row row, Integer column, DataFormatter formatter) {
+        if (column == null || column < 0) return "";
+        Cell cell = row.getCell(column);
+        return cell == null ? "" : formatter.formatCellValue(cell).trim();
+    }
+
+    private String normalizeHeader(String value) {
+        return value == null ? "" : value.replaceAll("[\\s\\p{Punct}锛堬級()銆愩�慭\[\\]]", "")
+                .replace("锛�", "").replace("锛�", "").toUpperCase(Locale.ROOT);
+    }
+
+    private String normalizeVin(String value) {
+        return value == null ? "" : value.replaceAll("[^A-Za-z0-9]", "").toUpperCase(Locale.ROOT);
+    }
+
+    private boolean isValidVin(String vin) {
+        return vin != null && vin.matches("[A-HJ-NPR-Z0-9]{17}");
+    }
+
+    private String joinErrors(List<String> errors) {
+        int limit = Math.min(errors.size(), 100);
+        StringBuilder builder = new StringBuilder("鐢宠琛ㄥ瓨鍦ㄩ敊璇細");
+        for (int i = 0; i < limit; i++) builder.append("\n").append(errors.get(i));
+        if (errors.size() > limit) builder.append("\n鈥︹�﹀叡 ").append(errors.size()).append(" 琛岄敊璇�");
+        return builder.toString();
+    }
+
+    private static class HeaderMatch {
+        private final int rowIndex;
+        private final Map<Integer, Integer> columns;
+
+        private HeaderMatch(int rowIndex, Map<Integer, Integer> columns) {
+            this.rowIndex = rowIndex;
+            this.columns = columns;
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1