xyc
2026-11-06 0af83ad25324d11527f0380f84e515de55f5ced6
fix(货运汇总): 规上指标统一取H2032月报
3个文件已修改
224 ■■■■■ 已修改文件
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/calc/FreightCalc.java 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/calc/SummaryMissingDataCheck.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java 161 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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());
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/calc/SummaryMissingDataCheck.java
@@ -225,7 +225,7 @@
            List<ScaleSplitTransport> s = scaleSplitMapper.selectList(
                new LambdaQueryWrapper<ScaleSplitTransport>().eq(ScaleSplitTransport::getReportPeriod, p));
            if (s.isEmpty()) {
                issues.add(String.format("缺数:货运 %s 无③《规上规下拆分表》→ 规上/合计周转量取不到(该月周转量将为空/0)", p));
                issues.add(String.format("缺数:货运 %s 无③《规上规下拆分表》→ 合计/规下周转量取不到(规上货运量/周转量由① H203-2 计算)", p));
            } else {
                Set<String> month = new LinkedHashSet<>();
                Set<String> cum = new LinkedHashSet<>();
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java
@@ -1003,16 +1003,14 @@
        Map<String, ScaleSplitTransport> cumMap = loadCumulativeMap(period);
        ScaleSplitTransport province = getProvinceCumulative(period);
        // 当月(第二页):周转量拆分表 MONTH 行(含当月同比)
        // 当月(第二页):③提供合计/规下,规上字段由 ① H203-2 统一覆盖
        Map<String, Map<Integer, ScaleSplitTransport>> monthData = loadMonthData(period);
        Map<String, ScaleSplitTransport> monthTurnover = new HashMap<>();
        for (ScaleSplitTransport rec : scaleSplitMapper.selectList(
                new LambdaQueryWrapper<ScaleSplitTransport>()
                    .eq(ScaleSplitTransport::getPeriodType, "MONTH")
                    .eq(ScaleSplitTransport::getReportPeriod, period))) {
            String name = RegionUtil.normalizeCityName(rec.getRegionName());
            if (name != null) monthTurnover.put(name, rec);
        for (String city : RegionUtil.cityList()) {
            ScaleSplitTransport rec = getRecord(monthData, city, month);
            if (rec != null) monthTurnover.put(city, rec);
        }
        ScaleSplitTransport provinceMonth = monthTurnover.remove("湖北省");
        ScaleSplitTransport provinceMonth = loadProvinceMonthMap(period).get(month);
        // 以 docs/货运/生成_周转量排名.xlsx 为底稿:保留表头/合并/列宽/样式,仅替换数据;Q/R 占比为模板缓存值,按新数据重算
        File template = resolveFreightTemplate("生成_周转量排名.xlsx");
@@ -1436,6 +1434,7 @@
            int month = parseMonth(record.getReportPeriod());
            result.computeIfAbsent(city, k -> new HashMap<>()).put(month, record);
        }
        applyH2032MonthlyTurnoverAbove(result, period);
        return result;
    }
@@ -1457,6 +1456,7 @@
                result.put(city, record);
            }
        }
        applyH2032CumulativeTurnoverAbove(result, period);
        return result;
    }
@@ -1474,6 +1474,7 @@
            int month = parseMonth(record.getReportPeriod());
            result.put(month, record);
        }
        applyH2032ProvinceMonthTurnoverAbove(result, period);
        return result;
    }
@@ -1491,7 +1492,7 @@
            if (!"湖北省".equals(name)) continue;
            if (best == null || record.getReportPeriod().compareTo(best.getReportPeriod()) > 0) best = record;
        }
        return best;
        return applyH2032ProvinceCumulativeTurnoverAbove(best, period);
    }
    /** H2032规上: city -> month -> 货运量(吨) */
@@ -1528,6 +1529,148 @@
        return result;
    }
    /** H2032 规上周转量: city -> month -> 万吨公里(turnover_total 吨公里 ÷10000) */
    private Map<String, Map<Integer, Double>> loadH2032TurnoverByMonth(String period) {
        Map<String, Map<Integer, Double>> result = new HashMap<>();
        String yearPrefix = period.length() >= 4 ? period.substring(0, 4) : null;
        for (H2032EnterpriseMonthly record : h2032Mapper.selectList(null)) {
            String rp = record.getReportPeriod();
            if (rp == null || rp.compareTo(period) > 0) continue;
            if (yearPrefix != null && !rp.startsWith(yearPrefix)) continue;
            if (record.getTurnoverTotal() == null) continue;
            String city = RegionUtil.cityByCode(record.getRegionCode());
            if (city == null) continue;
            int m = parseMonth(rp);
            result.computeIfAbsent(city, k -> new HashMap<>())
                .merge(m, record.getTurnoverTotal() / 10000.0, Double::sum);
        }
        return result;
    }
    /** H2032 规上周转量 1..period 月累计: city -> 万吨公里 */
    private Map<String, Double> loadH2032TurnoverCumulative(String period) {
        Map<String, Map<Integer, Double>> byMonth = loadH2032TurnoverByMonth(period);
        int target = parseMonth(period);
        Map<String, Double> result = new HashMap<>();
        for (Map.Entry<String, Map<Integer, Double>> e : byMonth.entrySet()) {
            double sum = 0.0;
            boolean any = false;
            for (int m = 1; m <= target; m++) {
                Double v = e.getValue().get(m);
                if (v != null) { sum += v; any = true; }
            }
            if (any) result.put(e.getKey(), sum);
        }
        return result;
    }
    /** 规上字段只认 ① H203-2;③记录只保留合计,并据此重算规下。 */
    private void applyH2032MonthlyTurnoverAbove(Map<String, Map<Integer, ScaleSplitTransport>> data,
                                                 String period) {
        int lastMonth = parseMonth(period);
        Map<String, Map<Integer, Double>> current = loadH2032TurnoverByMonth(period);
        Map<String, Map<Integer, Double>> prior = loadH2032TurnoverByMonth(previousYearPeriod(period));
        for (String city : RegionUtil.cityList()) {
            Map<Integer, ScaleSplitTransport> cityData = data.get(city);
            for (int m = 1; m <= lastMonth; m++) {
                ScaleSplitTransport rec = cityData == null ? null : cityData.get(m);
                Double above = monthValueOf(current, city, m);
                if (rec == null && above == null) continue;
                if (cityData == null) {
                    cityData = new HashMap<>();
                    data.put(city, cityData);
                }
                if (rec == null) {
                    rec = new ScaleSplitTransport();
                    rec.setRegionName(city);
                    cityData.put(m, rec);
                }
                applyH2032TurnoverAbove(rec, above, growthYoy(above, monthValueOf(prior, city, m)));
            }
        }
    }
    private void applyH2032CumulativeTurnoverAbove(Map<String, ScaleSplitTransport> data, String period) {
        Map<String, Double> current = loadH2032TurnoverCumulative(period);
        Map<String, Double> prior = loadH2032TurnoverCumulative(previousYearPeriod(period));
        for (String city : RegionUtil.cityList()) {
            Double above = current.get(city);
            ScaleSplitTransport rec = data.get(city);
            if (rec == null && above == null) continue;
            if (rec == null) {
                rec = new ScaleSplitTransport();
                rec.setRegionName(city);
                data.put(city, rec);
            }
            applyH2032TurnoverAbove(rec, above, growthYoy(above, prior.get(city)));
        }
    }
    private void applyH2032ProvinceMonthTurnoverAbove(Map<Integer, ScaleSplitTransport> data, String period) {
        int lastMonth = parseMonth(period);
        Map<String, Map<Integer, Double>> current = loadH2032TurnoverByMonth(period);
        Map<String, Map<Integer, Double>> prior = loadH2032TurnoverByMonth(previousYearPeriod(period));
        for (int m = 1; m <= lastMonth; m++) {
            Double above = sumCitiesOfMonth(current, m);
            ScaleSplitTransport rec = data.get(m);
            if (rec == null && above == null) continue;
            if (rec == null) {
                rec = new ScaleSplitTransport();
                rec.setRegionName("湖北省");
                data.put(m, rec);
            }
            applyH2032TurnoverAbove(rec, above, growthYoy(above, sumCitiesOfMonth(prior, m)));
        }
    }
    private ScaleSplitTransport applyH2032ProvinceCumulativeTurnoverAbove(ScaleSplitTransport province,
                                                                          String period) {
        Map<String, Double> current = loadH2032TurnoverCumulative(period);
        Map<String, Double> prior = loadH2032TurnoverCumulative(previousYearPeriod(period));
        Double above = sumCitiesOf(current);
        if (province == null && above == null) return null;
        if (province == null) {
            province = new ScaleSplitTransport();
            province.setRegionName("湖北省");
        }
        applyH2032TurnoverAbove(province, above, growthYoy(above, sumCitiesOf(prior)));
        return province;
    }
    private void applyH2032TurnoverAbove(ScaleSplitTransport rec, Double above, Double yoy) {
        rec.setAboveScaleTurnover(above);
        rec.setAboveScaleYoy(yoy);
        Double total = rec.getTotalTurnover();
        rec.setBelowScaleTurnover(total == null || above == null ? null : total - above);
    }
    private String previousYearPeriod(String period) {
        return (Integer.parseInt(period.substring(0, 4)) - 1) + period.substring(4);
    }
    private Double sumCitiesOf(Map<String, Double> values) {
        Double sum = null;
        for (String city : RegionUtil.cityList()) {
            Double v = values.get(city);
            if (v != null) sum = sum == null ? v : sum + v;
        }
        return sum;
    }
    private Double sumCitiesOfMonth(Map<String, Map<Integer, Double>> values, int month) {
        Double sum = null;
        for (String city : RegionUtil.cityList()) {
            Double v = monthValueOf(values, city, month);
            if (v != null) sum = sum == null ? v : sum + v;
        }
        return sum;
    }
    private Double monthValueOf(Map<String, Map<Integer, Double>> byMonth, String city, int month) {
        Map<Integer, Double> buckets = byMonth.get(city);
        return buckets == null ? null : buckets.get(month);
    }
    /** H2032规上同比(需去年同期的H2032数据,暂无则为空) */
    private Map<String, Double> computeH2032Yoy(String period) {
        Map<String, Double> result = new HashMap<>();