xyc
2 小时以前 5b41c8896ef7850be2672e45b69bb58e149729ec
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java
@@ -42,6 +42,7 @@
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
@@ -1003,16 +1004,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 +1435,7 @@
            int month = parseMonth(record.getReportPeriod());
            result.computeIfAbsent(city, k -> new HashMap<>()).put(month, record);
        }
        applyH2032MonthlyTurnoverAbove(result, period);
        return result;
    }
@@ -1457,6 +1457,7 @@
                result.put(city, record);
            }
        }
        applyH2032CumulativeTurnoverAbove(result, period);
        return result;
    }
@@ -1474,6 +1475,7 @@
            int month = parseMonth(record.getReportPeriod());
            result.put(month, record);
        }
        applyH2032ProvinceMonthTurnoverAbove(result, period);
        return result;
    }
@@ -1491,7 +1493,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 -> 货运量(吨) */
@@ -1526,6 +1528,148 @@
            result.merge(city, freight, Double::sum);
        }
        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数据,暂无则为空) */
@@ -5464,6 +5608,34 @@
        return last == 0.0 ? 0.0 : (cur - last) / last;
    }
    /** Force a data format while preserving all other cell style attributes. */
    static int smSetPercentFormat(Sheet sh, int firstRow0, int lastRow0, int col0, String format) {
        if (sh == null || format == null || format.trim().isEmpty()) return 0;
        DataFormat dataFormat = sh.getWorkbook().createDataFormat();
        short formatIndex = dataFormat.getFormat(format);
        Map<Short, CellStyle> convertedStyles = new HashMap<>();
        int changed = 0;
        for (int r = firstRow0; r <= lastRow0; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            Cell cell = row.getCell(col0);
            if (cell == null) continue;
            CellStyle current = cell.getCellStyle();
            if (current != null && format.equals(current.getDataFormatString())) continue;
            short sourceStyle = current == null ? -1 : current.getIndex();
            CellStyle converted = convertedStyles.get(sourceStyle);
            if (converted == null) {
                converted = sh.getWorkbook().createCellStyle();
                if (current != null) converted.cloneStyleFrom(current);
                converted.setDataFormat(formatIndex);
                convertedStyles.put(sourceStyle, converted);
            }
            cell.setCellStyle(converted);
            changed++;
        }
        return changed;
    }
    /** 写值:公式格不动(保留母版公式,打开即重算);0 且原为空格则不新增 */
    private int smSetVal(Sheet sh, int r0, int c0, double v) {
        Row row = sh.getRow(r0);
@@ -5545,7 +5717,7 @@
    }
    /** 城市分市州页:4 块(累计客运量/累计周转量/当月客运量/当月周转量),每块 全省 1 行 + 17 市州 */
    private int smWriteCityByRegion(XSSFWorkbook wb, int month,
    int smWriteCityByRegion(XSSFWorkbook wb, int month,
                                    Map<String, double[]> curCum, Map<String, double[]> lastCum,
                                    Map<String, double[]> curMon, Map<String, double[]> lastMon) {
        Sheet sh = wb.getSheet("城市分市州");
@@ -5557,8 +5729,8 @@
        int n = 0;
        n += smWriteCityByRegionBlock(sh, 4, true, curCum, lastCum);
        n += smWriteCityByRegionBlock(sh, 26, false, curCum, lastCum);
        n += smWriteCityByRegionBlock(sh, 49, true, curMon, lastMon);
        n += smWriteCityByRegionBlock(sh, 71, false, curMon, lastMon);
        n += smWriteCityByRegionBlock(sh, 48, true, curMon, lastMon);
        n += smWriteCityByRegionBlock(sh, 70, false, curMon, lastMon);
        return n;
    }
@@ -5630,6 +5802,9 @@
            n += smSetVal(sh, rows[i] + 9, 5, v[1]);
            n += smSetVal(sh, rows[i] + 9, 6, v[3]);
        }
        // Older templates may retain an integer format on the monthly turnover YoY cells.
        // Enforce the format here so generated workbooks remain correct even when the expansion source is stale.
        smSetPercentFormat(sh, 11, 15, 6, "0.00%");
        return n;
    }