traffic-audit-server/src/main/java/com/trafficaudit/dataimport/service/DataImportService.java
@@ -56,6 +56,10 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import cn.hutool.core.io.IoUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -1345,6 +1349,7 @@
        CLEAR_TYPE_TO_OP.put("cityTaxiAuth", "CITY_TAXI_AUTH");
        CLEAR_TYPE_TO_OP.put("wycTotal", "WYC_TOTAL");
        CLEAR_TYPE_TO_OP.put("wycOrder", "WYC_ORDER");
        CLEAR_TYPE_TO_OP.put("wycOrderReport", "WYC_ORDER");
        CLEAR_TYPE_TO_AUDIT.put("h2032", new String[]{"H2032"});
        CLEAR_TYPE_TO_AUDIT.put("transportAuth", new String[]{"H2032"});
@@ -1386,7 +1391,8 @@
        CLEAR_TYPE_TO_BATCH.put("cityTaxi", new String[]{"CITY_TAXI"});
        CLEAR_TYPE_TO_BATCH.put("cityTaxiAuth", new String[]{"CITY_TAXI_AUTH"});
        CLEAR_TYPE_TO_BATCH.put("wycTotal", new String[]{"WYC_TOTAL"});
        CLEAR_TYPE_TO_BATCH.put("wycOrder", new String[]{"WYC_ORDER"});
        CLEAR_TYPE_TO_BATCH.put("wycOrder", new String[]{"WYC_ORDER", "WYC_ORDER_REPORT"});
        CLEAR_TYPE_TO_BATCH.put("wycOrderReport", new String[]{"WYC_ORDER_REPORT", "WYC_ORDER"});
    }
    /**
@@ -3144,6 +3150,209 @@
        return ok;
    }
    // ========== 网约车订单运行监测分析报告(docx)导入 ==========
    /** 网约车订单报告导入结果(报表期/订单合计/覆盖市州数/缺失市州) */
    public static class WycOrderReportResult {
        private final String period;
        private final double orderSum;
        private final int cityCount;
        private final List<String> missingCities;
        public WycOrderReportResult(String period, double orderSum, int cityCount, List<String> missingCities) {
            this.period = period;
            this.orderSum = orderSum;
            this.cityCount = cityCount;
            this.missingCities = missingCities;
        }
        public String getPeriod() { return period; }
        public double getOrderSum() { return orderSum; }
        public int getCityCount() { return cityCount; }
        public List<String> getMissingCities() { return missingCities; }
    }
    /**
     * 《网约车行业YYYY年M月运行监测情况分析报告》(docx)导入:
     * 解析报告附件中「地市名称 | … | 订单数总数」表的 17 市州订单数,写入 wyc_order_monthly 的
     * F 订单合计与 G-W 17 市州订单列;全省 pin(B-E)沿用该期已有行,若无该期行则从
     * wyc_total_monthly 同报表期复制。报表期优先取报告标题年月,取不到时用页面所选报表期。
     */
    public synchronized WycOrderReportResult importWycOrderReport(MultipartFile file, String period) throws Exception {
        Map<String, Double> orders = new LinkedHashMap<>();
        double[] sumHolder = new double[]{0.0};
        String docPeriod;
        try (java.io.InputStream in = file.getInputStream();
             XWPFDocument doc = new XWPFDocument(in)) {
            docPeriod = periodOfDocx(doc);
            parseWycOrderReportTables(doc, orders, sumHolder);
        } catch (Exception e) {
            throw new RuntimeException("无法读取 Word 文档,请上传 .docx 格式的《网约车行业YYYY年M月运行监测情况分析报告》;"
                    + "若上传的其实是 Excel/PDF,请改用对应模板导入。(原始错误:" + e.getMessage() + ")");
        }
        if (orders.isEmpty()) {
            throw new RuntimeException("未解析到任何市州订单数,请确认上传的是《网约车行业YYYY年M月运行监测情况分析报告》");
        }
        String rp = null;
        if (docPeriod != null && docPeriod.matches("\\d{4}-\\d{2}")) rp = docPeriod;
        else if (period != null && period.matches("\\d{4}-\\d{2}")) rp = period;
        if (rp == null) {
            throw new RuntimeException("无法确定报表期:报告标题未含「YYYY年M月」,且页面未选择报表期");
        }
        List<String> missing = new ArrayList<>();
        double citySum = 0.0;
        for (String city : RegionUtil.cityList()) {
            Double v = orders.get(city);
            if (v == null) missing.add(city);
            else citySum += v;
        }
        double sum = sumHolder[0] > 0 ? sumHolder[0] : citySum;
        WycOrderMonthly it = wycOrderMapper.selectOne(new LambdaQueryWrapper<WycOrderMonthly>()
                .eq(WycOrderMonthly::getReportPeriod, rp).last("LIMIT 1"));
        if (it == null) it = new WycOrderMonthly();
        it.setReportPeriod(rp);
        it.setOrderSum(sum > 0 ? sum : null);
        it.setOrderWuhan(orderOf(orders, "武汉市"));
        it.setOrderHuangshi(orderOf(orders, "黄石市"));
        it.setOrderShiyan(orderOf(orders, "十堰市"));
        it.setOrderYichang(orderOf(orders, "宜昌市"));
        it.setOrderXiangyang(orderOf(orders, "襄阳市"));
        it.setOrderEzhou(orderOf(orders, "鄂州市"));
        it.setOrderJingmen(orderOf(orders, "荆门市"));
        it.setOrderXiaogan(orderOf(orders, "孝感市"));
        it.setOrderJingzhou(orderOf(orders, "荆州市"));
        it.setOrderHuanggang(orderOf(orders, "黄冈市"));
        it.setOrderXianning(orderOf(orders, "咸宁市"));
        it.setOrderSuizhou(orderOf(orders, "随州市"));
        it.setOrderEnshi(orderOf(orders, "恩施州"));
        it.setOrderXiantao(orderOf(orders, "仙桃市"));
        it.setOrderQianjiang(orderOf(orders, "潜江市"));
        it.setOrderTianmen(orderOf(orders, "天门市"));
        it.setOrderShennong(orderOf(orders, "神农架林区"));
        if (it.getPinK() == null || it.getPinCK() == null || it.getPinZ() == null || it.getPinCZ() == null) {
            WycTotalMonthly tot = wycTotalMapper.selectOne(new LambdaQueryWrapper<WycTotalMonthly>()
                    .eq(WycTotalMonthly::getReportPeriod, rp).last("LIMIT 1"));
            if (tot != null) {
                if (it.getPinK() == null) it.setPinK(tot.getPinK());
                if (it.getPinCK() == null) it.setPinCK(tot.getPinCK());
                if (it.getPinZ() == null) it.setPinZ(tot.getPinZ());
                if (it.getPinCZ() == null) it.setPinCZ(tot.getPinCZ());
            }
        }
        wycOrderMapper.delete(new LambdaQueryWrapper<WycOrderMonthly>().eq(WycOrderMonthly::getReportPeriod, rp));
        wycOrderMapper.insert(it);
        recordBatch(file.getOriginalFilename(), "WYC_ORDER_REPORT", rp, 1, 1, 0, null);
        log.info("WycOrderReport imported: period={} sum={} cities={} missing={}", rp, sum, orders.size(), missing);
        return new WycOrderReportResult(rp, sum, orders.size(), missing);
    }
    /** 报告标题中的报表期:网约车行业2026年7月 -> 2026-07;取不到返回 null */
    private String periodOfDocx(XWPFDocument doc) {
        java.util.regex.Pattern titlePat = java.util.regex.Pattern.compile("网约车行业\\s*(\\d{4})\\s*年\\s*(\\d{1,2})\\s*月");
        java.util.regex.Pattern anyPat = java.util.regex.Pattern.compile("(\\d{4})\\s*年\\s*(\\d{1,2})\\s*月");
        String fallback = null;
        int scanned = 0;
        for (XWPFParagraph para : doc.getParagraphs()) {
            String text = para.getText();
            if (text == null) continue;
            String flat = text.replaceAll("\\s+", "");
            if (flat.isEmpty()) continue;
            java.util.regex.Matcher m = titlePat.matcher(flat);
            if (m.find()) return ymd(m.group(1), m.group(2));
            if (fallback == null && scanned < 10) {
                java.util.regex.Matcher m2 = anyPat.matcher(flat);
                if (m2.find()) fallback = ymd(m2.group(1), m2.group(2));
            }
            scanned++;
        }
        return fallback;
    }
    private String ymd(String y, String mo) {
        try {
            int month = Integer.parseInt(mo);
            if (month < 1 || month > 12) return null;
            return String.format("%04d-%02d", Integer.parseInt(y), month);
        } catch (NumberFormatException e) {
            return null;
        }
    }
    /** 解析报告中的「地市名称 | … | 订单数总数」表,orders=市州订单,sumHolder[0]=合计行订单数 */
    private void parseWycOrderReportTables(XWPFDocument doc, Map<String, Double> orders, double[] sumHolder) {
        for (XWPFTable table : doc.getTables()) {
            List<XWPFTableRow> rows = table.getRows();
            if (rows.size() < 3) continue;
            int headerRow = -1;
            int orderCol = -1;
            for (int r = 0; r < Math.min(rows.size(), 3); r++) {
                List<org.apache.poi.xwpf.usermodel.XWPFTableCell> cells = rows.get(r).getTableCells();
                for (int c = 0; c < cells.size(); c++) {
                    String norm = flat(cells.get(c).getText());
                    if (norm.contains("订单数总数") || "订单数".equals(norm)) {
                        headerRow = r;
                        orderCol = c;
                        break;
                    }
                }
                if (headerRow >= 0) break;
            }
            if (headerRow < 0 || orderCol < 0) continue;
            Map<String, Double> found = new LinkedHashMap<>();
            double citySum = 0.0;
            boolean hasTotal = false;
            for (int r = headerRow + 1; r < rows.size(); r++) {
                List<org.apache.poi.xwpf.usermodel.XWPFTableCell> cells = rows.get(r).getTableCells();
                if (orderCol >= cells.size()) continue;
                String name = flat(cells.get(0).getText());
                if (name.isEmpty()) continue;
                Double val = parsePlainNumber(cells.get(orderCol).getText());
                if (val == null) continue;
                String city = RegionUtil.normalizeCityName(name);
                if (city != null && RegionUtil.cityList().contains(city)) {
                    found.put(city, val);
                    citySum += val;
                } else if (name.contains("合计") || name.contains("总计") || name.contains("全省") || name.contains("湖北")) {
                    hasTotal = true;
                    sumHolder[0] = val;
                }
            }
            if (!found.isEmpty()) {
                orders.clear();
                orders.putAll(found);
                if (!hasTotal) sumHolder[0] = citySum;
                return;
            }
        }
    }
    private String flat(String s) {
        return s == null ? "" : s.replaceAll("\\s+", "").trim();
    }
    /** 「11,183,267 单」这类文本取数值 */
    private Double parsePlainNumber(String s) {
        if (s == null) return null;
        String t = s.replaceAll("[,\\s]", "").trim();
        if (t.isEmpty()) return null;
        java.util.regex.Matcher m = java.util.regex.Pattern.compile("-?\\d+(?:\\.\\d+)?").matcher(t);
        if (!m.find()) return null;
        try {
            return Double.parseDouble(m.group());
        } catch (NumberFormatException e) {
            return null;
        }
    }
    private Double orderOf(Map<String, Double> orders, String city) {
        Double v = orders.get(city);
        return (v == null || v <= 0) ? null : v;
    }
    private String periodOfCell(Cell c) {
        if (c == null) return null;
        if (c.getCellType() == CellType.NUMERIC) {
@@ -3513,6 +3722,7 @@
            case "scaleSplit": return new ImportResult(importScaleSplit(file, period), 0, new ArrayList<>());
            case "wycTotal": return new ImportResult(importWycTotal(file, period), 0, new ArrayList<>());
            case "wycOrder": return new ImportResult(importWycOrder(file, period), 0, new ArrayList<>());
            case "wycOrderReport": return new ImportResult(importWycOrderReport(file, period).getCityCount(), 0, new ArrayList<>());
            default: throw new RuntimeException("不支持的导入类型:" + type);
        }
    }