xyc
2026-11-11 841847dbbab3c6fd532dcb747ca290ba6a9e12d0
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/calc/FreightCalc.java
@@ -24,8 +24,9 @@
 * 1. 规上货运量 = h2032_enterprise_monthly.freight_total(吨)按 17 市州汇总 ÷10000(万吨)。
 * 2. 合计货运量 = freight_turnover_import.freight_mMM(当月)/ freight_m01..mMM(累计)。
 * 3. 规下货运量 = 合计 − 规上(拆分表不存货运量列,不能直接取规下)。
 * 4. 规上/合计周转量 = scale_split_transport MONTH|CUMULATIVE 的 above_scale_turnover / total_turnover
 *    (合计与 freight_turnover_import.turnover_mMM 同源,已核对一致);规下周转 = 合计 − 规上。
 * 4. 规上周转量始终取 ① H203-2 的 turnover_total(吨公里 ÷10000 = 万吨公里);
 *    ③ scale_split_transport 只提供合计周转量 total_turnover(缺 ③ 时取 freight_turnover_import),
 *    规下周转 = 合计 − 规上。规上口径不依赖 ③。
 * 5. 同比(去年同口径)不在本服务计算:按 Q1 以母版缓存 2025 参照组为基准,由写回层填表时计算。
 *
 * 输出 key = RegionUtil 规范市州名(武汉市…神农架林区)+ PROVINCE(湖北省,全省行 = 17 市州之和,
@@ -92,8 +93,9 @@
        int year = Integer.parseInt(period.substring(0, 4));
        int month = Integer.parseInt(period.substring(5, 7));
        // H2032 规上货运量(吨):市州 -> 月桶
        // H2032 规上货运量/周转量(吨、吨公里):市州 -> 月桶
        Map<String, Map<Integer, Double>> h2032TonByCityMonth = new HashMap<>();
        Map<String, Map<Integer, Double>> h2032TurnoverByCityMonth = new HashMap<>();
        List<H2032EnterpriseMonthly> h2032List = h2032Mapper.selectList(
            new LambdaQueryWrapper<H2032EnterpriseMonthly>().likeRight(H2032EnterpriseMonthly::getReportPeriod, year));
        for (H2032EnterpriseMonthly rec : h2032List) {
@@ -101,9 +103,13 @@
            if (rp == null || rp.compareTo(period) > 0) continue;
            String city = RegionUtil.cityByCode(rec.getRegionCode());
            if (city == null) continue;
            int hMonth = Integer.parseInt(rp.substring(5, 7));
            double freight = rec.getFreightTotal() == null ? 0.0 : rec.getFreightTotal();
            h2032TonByCityMonth.computeIfAbsent(city, k -> new HashMap<>())
                .merge(Integer.parseInt(rp.substring(5, 7)), freight, Double::sum);
            h2032TonByCityMonth.computeIfAbsent(city, k -> new HashMap<>()).merge(hMonth, freight, Double::sum);
            if (rec.getTurnoverTotal() != null) {
                h2032TurnoverByCityMonth.computeIfAbsent(city, k -> new HashMap<>())
                    .merge(hMonth, rec.getTurnoverTotal(), Double::sum);
            }
        }
        // freight_turnover_import:合计货运量/周转量(仅存城市行;湖北省行由求和得出)
@@ -130,8 +136,10 @@
        Map<String, FreightMetrics> monthMap = new LinkedHashMap<>();
        Map<String, FreightMetrics> cumulativeMap = new LinkedHashMap<>();
        for (String city : RegionUtil.cityList()) {
            monthMap.put(city, buildMetrics(city, month, h2032TonByCityMonth, ftMap, splitMonth, false));
            cumulativeMap.put(city, buildMetrics(city, month, h2032TonByCityMonth, ftMap, splitCumulative, true));
            monthMap.put(city, buildMetrics(city, month, h2032TonByCityMonth, h2032TurnoverByCityMonth,
                ftMap, splitMonth, false));
            cumulativeMap.put(city, buildMetrics(city, month, h2032TonByCityMonth, h2032TurnoverByCityMonth,
                ftMap, splitCumulative, true));
        }
        monthMap.put(PROVINCE, sumOf(monthMap));
        cumulativeMap.put(PROVINCE, sumOf(cumulativeMap));
@@ -141,6 +149,7 @@
    /** 单城市指标。cumulative=true 时 H2032 取 1..month 全桶、合计取 m01..mMM 之和;false 时只取当月。 */
    private FreightMetrics buildMetrics(String city, int month,
                                        Map<String, Map<Integer, Double>> h2032TonByCityMonth,
                                        Map<String, Map<Integer, Double>> h2032TurnoverByCityMonth,
                                        Map<String, FreightTurnoverImport> ftMap,
                                        Map<String, ScaleSplitTransport> splitMap,
                                        boolean cumulative) {
@@ -155,6 +164,20 @@
                // 当月无规上记录 -> 留空(不再写 0);有记录(含真实 0 值)才写数
                Double ton = h2032Buckets.get(month);
                aboveFreight = ton == null ? null : ton / 10000.0;
            }
        }
        // ① H203-2 规上周转量(吨公里 -> 万吨公里),是规上指标的正式取数来源
        Double aboveTurnoverH2032 = null;
        Map<Integer, Double> turnoverBuckets = h2032TurnoverByCityMonth.get(city);
        if (turnoverBuckets != null) {
            if (cumulative) {
                double tonKm = 0.0;
                for (int m = 1; m <= month; m++) tonKm += turnoverBuckets.getOrDefault(m, 0.0);
                aboveTurnoverH2032 = tonKm / 10000.0;
            } else {
                Double tonKm = turnoverBuckets.get(month);
                aboveTurnoverH2032 = tonKm == null ? null : tonKm / 10000.0;
            }
        }
@@ -177,7 +200,8 @@
        }
        ScaleSplitTransport split = splitMap.get(city);
        Double aboveTurnover = split == null ? null : split.getAboveScaleTurnover();
        // 规上货运量/周转量只由 ① H203-2 决定;③只参与合计周转量。
        Double aboveTurnover = aboveTurnoverH2032;
        Double totalTurnover = split != null && split.getTotalTurnover() != null
            ? split.getTotalTurnover() : totalTurnoverFromFt;
@@ -188,22 +212,27 @@
                                  aboveTurnover, belowTurnover, totalTurnover);
    }
    /** 湖北省行 = 17 市州之和(与母版全省行 Σ 公式一致;全市州无数据时输出 0) */
    /** 湖北省行 = 17 市州之和(与母版全省行 Σ 公式一致);全部市州无数据时保持 null,落实“缺数留空” */
    private FreightMetrics sumOf(Map<String, FreightMetrics> byCity) {
        double af = 0.0, bf = 0.0, tf = 0.0, at = 0.0, bt = 0.0, tt = 0.0;
        Double af = null, bf = null, tf = null, at = null, bt = null, tt = null;
        for (String city : RegionUtil.cityList()) {
            FreightMetrics m = byCity.get(city);
            if (m == null) continue;
            af += nz(m.getAboveFreight());
            bf += nz(m.getBelowFreight());
            tf += nz(m.getTotalFreight());
            at += nz(m.getAboveTurnover());
            bt += nz(m.getBelowTurnover());
            tt += nz(m.getTotalTurnover());
            af = add(af, m.getAboveFreight());
            bf = add(bf, m.getBelowFreight());
            tf = add(tf, m.getTotalFreight());
            at = add(at, m.getAboveTurnover());
            bt = add(bt, m.getBelowTurnover());
            tt = add(tt, m.getTotalTurnover());
        }
        return new FreightMetrics(af, bf, tf, at, bt, tt);
    }
    private Double add(Double acc, Double v) {
        if (v == null) return acc;
        return acc == null ? v : acc + v;
    }
    private double monthField(FreightTurnoverImport e, int m, boolean freight) {
        switch (m) {
            case 1: return freight ? nz(e.getFreightM01()) : nz(e.getTurnoverM01());