1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
package com.trafficaudit.reportexport.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.trafficaudit.common.util.RegionUtil;
import com.trafficaudit.dataimport.entity.EnergyVehicleQuarterly;
import com.trafficaudit.dataimport.entity.FreightTurnoverImport;
import com.trafficaudit.dataimport.entity.H2032EnterpriseMonthly;
import com.trafficaudit.dataimport.entity.InvestmentMonthly;
import com.trafficaudit.dataimport.entity.CityBusMonthly;
import com.trafficaudit.dataimport.entity.CityTaxiMonthly;
import com.trafficaudit.dataimport.entity.InvestmentProject;
import com.trafficaudit.dataimport.entity.PassengerEnterpriseMonthly;
import com.trafficaudit.dataimport.entity.PassengerIndividualMonthly;
import com.trafficaudit.dataimport.entity.ScaleSplitTransport;
import com.trafficaudit.auditengine.entity.AuditResult;
import com.trafficaudit.auditengine.entity.AuditRun;
import com.trafficaudit.auditengine.mapper.AuditResultMapper;
import com.trafficaudit.auditengine.mapper.AuditRunMapper;
import com.trafficaudit.rulemanage.entity.AuditRule;
import com.trafficaudit.rulemanage.mapper.AuditRuleMapper;
import com.trafficaudit.dataimport.mapper.EnergyVehicleQuarterlyMapper;
import com.trafficaudit.dataimport.mapper.FreightTurnoverImportMapper;
import com.trafficaudit.dataimport.mapper.H2032EnterpriseMonthlyMapper;
import com.trafficaudit.dataimport.mapper.InvestmentMonthlyMapper;
import com.trafficaudit.dataimport.mapper.CityBusMonthlyMapper;
import com.trafficaudit.dataimport.mapper.CityTaxiMonthlyMapper;
import com.trafficaudit.dataimport.mapper.InvestmentProjectMapper;
import com.trafficaudit.dataimport.mapper.PassengerEnterpriseMonthlyMapper;
import com.trafficaudit.dataimport.mapper.PassengerIndividualMonthlyMapper;
import com.trafficaudit.dataimport.mapper.ScaleSplitTransportMapper;
import com.trafficaudit.reportexport.calc.MidCalc;
import com.trafficaudit.reportexport.calc.PaxCalc;
import com.trafficaudit.reportexport.calc.SummaryWorkbookFiller;
import lombok.extern.slf4j.Slf4j;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
 
/**
 * 报表生成服务:3 类输出报表
 * 货运量口径:合计=模板_货运量周转量(左半今年1-N月,右半去年1-12月);规上=H203-2按17市州汇总;规下=合计-规上
 * 周转量口径:全部来自规上规下拆分表(用户手动上传,含忠实导入的全省行)
 */
@Slf4j
@Service
public class ReportExportService {
 
    @Resource
    private H2032EnterpriseMonthlyMapper h2032Mapper;
    @Resource
    private ScaleSplitTransportMapper scaleSplitMapper;
    @Resource
    private FreightTurnoverImportMapper freightTurnoverMapper;
    @Resource
    private PassengerEnterpriseMonthlyMapper passengerMapper;
    @Resource
    private PassengerIndividualMonthlyMapper passengerIndividualMapper;
    @Resource
    private EnergyVehicleQuarterlyMapper energyMapper;
    @Resource
    private InvestmentProjectMapper investProjectMapper;
    @Resource
    private InvestmentMonthlyMapper investMonthlyMapper;
    @Resource
    private CityBusMonthlyMapper cityBusMapper;
    @Resource
    private CityTaxiMonthlyMapper cityTaxiMapper;
    @Resource
    private SummaryWorkbookFiller summaryWorkbookFiller;
    @Resource
    private AuditResultMapper auditResultMapper;
    @Resource
    private AuditRunMapper auditRunMapper;
    @Resource
    private AuditRuleMapper auditRuleMapper;
    /** 城市客运模板目录(application.yml city-passenger.template-dir) */
    @Value("${city-passenger.template-dir:docs/城市客运}")
    private String templateDir;
    /** 能耗汇总模板目录(application.yml energy.template-dir) */
    @Value("${energy.template-dir:docs/公路旅客+能耗}")
    private String energyTemplateDir;
    /** 公路旅客输出模板目录(application.yml passenger.template-dir) */
    @Value("${passenger.template-dir:docs/公路旅客+能耗/输出}")
    private String passengerTemplateDir;
 
    /** 汇总工作簿模板目录(application.yml summary.template-dir) */
    @Value("${summary.template-dir:docs/生成汇总大表}")
    private String summaryTemplateDir;
 
    /** 货运报表模板目录(application.yml freight.template-dir) */
    @Value("${freight.template-dir:docs/货运}")
    private String freightTemplateDir;
 
    /** 投资报表模板目录(application.yml investment.template-dir) */
    @Value("${investment.template-dir:docs/投资/模板}")
    private String investTemplateDir;
 
    // ==================== 导出前数据就绪校验(P0:防止导出空表) ====================
 
    /** 返回每张报表依赖的数据行数与缺失提示 */
    public Map<String, Object> checkExportReady(String period, String mode, List<String> types) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("period", period);
        result.put("mode", mode);
        List<Map<String, Object>> items = new ArrayList<>();
        for (String t : types) {
            Map<String, Object> it = new LinkedHashMap<>();
            it.put("key", t);
            List<String> notes = new ArrayList<>();
            int rows;
            List<String> auditTypes = new ArrayList<>();
            switch (t) {
                case "cityDetail":
                case "freightRank":
                case "turnoverRank":
                    rows = freightReadyRows(period, mode, notes);
                    auditTypes.add("H2032");
                    break;
                case "passengerCityDetail":
                case "passengerMidDetail":
                case "passengerMidRank":
                case "passengerMidAnalysis":
                    rows = passengerReadyRows(period, mode, notes);
                    auditTypes.add("H2031");
                    break;
                case "cityBusDetail":
                case "cityRailFerryDetail":
                    rows = addReadyCount(() -> cityBusMapper.selectCount(periodQw(period, mode, CityBusMonthly::getReportPeriod)), "城市公交月报", notes);
                    auditTypes.add("CITY_BUS");
                    break;
                case "cityTaxiDetail":
                    rows = addReadyCount(() -> cityTaxiMapper.selectCount(periodQw(period, mode, CityTaxiMonthly::getReportPeriod)), "巡游出租月报", notes);
                    auditTypes.add("CITY_TAXI");
                    break;
                case "cityPassengerCitySum":
                case "cityPassengerDetailSum":
                case "cityPassengerSummary":
                    rows = addReadyCount(() -> cityBusMapper.selectCount(periodQw(period, mode, CityBusMonthly::getReportPeriod)), "城市公交月报", notes)
                         + addReadyCount(() -> cityTaxiMapper.selectCount(periodQw(period, mode, CityTaxiMonthly::getReportPeriod)), "巡游出租月报", notes);
                    auditTypes.add("CITY_BUS");
                    auditTypes.add("CITY_TAXI");
                    break;
                case "investPlan":
                case "investFiveYearLogistics":
                case "investBillion":
                case "investCounty":
                    rows = addReadyCount(() -> investMonthlyMapper.selectCount(periodQw(period, mode, InvestmentMonthly::getReportPeriod)), "投资市州单表/汇总数据", notes);
                    auditTypes.add("INVEST");
                    break;
                case "energySummary":
                    rows = addReadyCount(() -> energyMapper.selectCount(periodQw(period, mode, EnergyVehicleQuarterly::getReportPeriod)), "能耗车辆季度数据", notes);
                    auditTypes.add("H204");
                    break;
                case "wycSplit": {
                    rows = addReadyCount(() -> cityTaxiMapper.selectCount(periodQw(period, mode, CityTaxiMonthly::getReportPeriod)), "巡游出租月报", notes);
                    try {
                        Map<String, Object> inp = loadWycMonthlyInput(period, mode);
                        if (inp == null) {
                            rows = 0;
                            notes.add("《网约车订单及全省总量.xlsx》缺 " + period + " 行(需先维护当月订单与全省 pin)");
                        } else {
                            double pinK = (Double) inp.get("pinK"), pinCK = (Double) inp.get("pinCK");
                            double pinZ = (Double) inp.get("pinZ"), pinCZ = (Double) inp.get("pinCZ");
                            double orderSum = (Double) inp.get("orderSum");
                            if (pinK <= 0 || pinCK <= 0 || pinZ <= 0 || pinCZ <= 0 || orderSum <= 0) {
                                rows = 0;
                                notes.add("网约车输入 " + period + " 行不完整(订单/全省 pin 缺失)");
                            }
                        }
                    } catch (Exception ex) {
                        rows = 0;
                        notes.add("网约车输入读取失败:" + ex.getMessage());
                    }
                    break;
                }
                case "summaryWorkbook": {
                    rows = addReadyCount(() -> cityTaxiMapper.selectCount(periodQw(period, mode, CityTaxiMonthly::getReportPeriod)), "巡游出租月报", notes)
                         + addReadyCount(() -> cityBusMapper.selectCount(periodQw(period, mode, CityBusMonthly::getReportPeriod)), "城市公交月报", notes);
                    if (rows <= 0) notes.add("汇总表依赖的公交/出租月报数据缺失");
                    try {
                        if (resolveSummaryMother(period, mode) == null) {
                            rows = 0;
                            notes.add("docs/生成汇总大表 下缺少可复制的母版汇总表");
                        }
                    } catch (Exception ex) {
                        rows = 0;
                        notes.add("母版定位失败:" + ex.getMessage());
                    }
                    break;
                }
                default:
                    rows = 0;
                    notes.add("未知报表类型:" + t);
            }
            int auditRows = 0;
            boolean audited = false;
            for (String at : auditTypes) {
                auditRows += auditRowsOf(period, mode, at);
                audited = audited || auditedMarked(period, mode, at);
            }
            if (auditTypes.isEmpty()) audited = true;
            it.put("rows", rows);
            it.put("ready", rows > 0);
            it.put("auditRows", auditRows);
            it.put("audited", audited);
            it.put("note", String.join(";", notes));
            items.add(it);
        }
        result.put("items", items);
        return result;
    }
 
    // ==================== 报表数据预览(轻量版):就绪 + 月度覆盖 + 核心摘要 + 问题清单 ====================
 
    /** 报表数据预览:在导出前展示每张表的就绪状态、月度覆盖、核心指标摘要与问题清单 */
    public Map<String, Object> reportPreview(String period, String mode, List<String> types) {
        Map<String, Object> ready = checkExportReady(period, mode, types);
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> items = (List<Map<String, Object>>) ready.get("items");
        Map<String, Map<String, Object>> byKey = new LinkedHashMap<>();
        for (Map<String, Object> it : items) byKey.put((String) it.get("key"), it);
 
        String year = period != null && period.length() >= 4 ? period.substring(0, 4) : "";
        String yearPrefix = year + "-";
        int month = monthOf(period, mode);
        int yearNum = 0;
        try { yearNum = Integer.parseInt(year); } catch (Exception ignore) {}
 
        boolean freight = false, passenger = false, cityBus = false, cityTaxi = false, invest = false, energy = false;
        for (String t : types) {
            if (isFreightType(t)) freight = true;
            else if (isPassengerType(t)) passenger = true;
            else if ("cityTaxiDetail".equals(t) || "wycSplit".equals(t)) { cityTaxi = true; }
            else if (isCityPassengerType(t) || "summaryWorkbook".equals(t)) { cityTaxi = true; cityBus = true; }
            else if (isInvestType(t)) invest = true;
            else if ("energySummary".equals(t)) energy = true;
        }
 
        // 月度覆盖:该年 1..month 中哪些月有源数据
        Set<Integer> freightCover = new TreeSet<>();
        Set<Integer> passengerCover = new TreeSet<>();
        Set<Integer> busCover = new TreeSet<>();
        Set<Integer> taxiCover = new TreeSet<>();
        Set<Integer> investCover = new TreeSet<>();
        if (freight) {
            FreightTurnoverImport ftProv = loadFreightTurnover(period).get("湖北省");
            for (int m = 1; m <= month; m++) {
                if (ftProv != null && freightMonth(ftProv, m) != null) freightCover.add(m);
            }
        }
        if (passenger) {
            passengerCover.addAll(monthsWithData(() -> passengerMapper.selectList(new LambdaQueryWrapper<PassengerEnterpriseMonthly>().likeRight(PassengerEnterpriseMonthly::getReportPeriod, yearPrefix)),
                o -> ((PassengerEnterpriseMonthly) o).getReportPeriod(), month));
            passengerCover.addAll(monthsWithData(() -> passengerIndividualMapper.selectList(new LambdaQueryWrapper<PassengerIndividualMonthly>().likeRight(PassengerIndividualMonthly::getReportPeriod, yearPrefix)),
                o -> ((PassengerIndividualMonthly) o).getReportPeriod(), month));
        }
        if (cityBus) {
            busCover.addAll(monthsWithData(() -> cityBusMapper.selectList(new LambdaQueryWrapper<CityBusMonthly>().likeRight(CityBusMonthly::getReportPeriod, yearPrefix)),
                o -> ((CityBusMonthly) o).getReportPeriod(), month));
        }
        if (cityTaxi) {
            taxiCover.addAll(monthsWithData(() -> cityTaxiMapper.selectList(new LambdaQueryWrapper<CityTaxiMonthly>().likeRight(CityTaxiMonthly::getReportPeriod, yearPrefix)),
                o -> ((CityTaxiMonthly) o).getReportPeriod(), month));
        }
        if (invest) {
            investCover.addAll(monthsWithData(() -> investMonthlyMapper.selectList(new LambdaQueryWrapper<InvestmentMonthly>().likeRight(InvestmentMonthly::getReportPeriod, yearPrefix)),
                o -> ((InvestmentMonthly) o).getReportPeriod(), month));
        }
 
        for (Map.Entry<String, Map<String, Object>> e : byKey.entrySet()) {
            String t = e.getKey();
            Map<String, Object> it = e.getValue();
            List<Map<String, Object>> summary = new ArrayList<>();
            List<String> problems = new ArrayList<>();
            List<Integer> missing = new ArrayList<>();
            List<Integer> coverage = new ArrayList<>();
            if (Boolean.FALSE.equals(it.get("audited"))) problems.add("该报表期尚未审核通过");
            if ("wycSplit".equals(t)) {
                coverage.addAll(taxiCover);
                missing = missingMonths(taxiCover, month);
                try {
                    Map<String, Object> inp = loadWycMonthlyInput(period, mode);
                    if (inp == null) {
                        problems.add("缺网约车输入:请在《网约车订单及全省总量.xlsx》补 " + period + " 行");
                    } else {
                        double pinK = (Double) inp.get("pinK"), pinCK = (Double) inp.get("pinCK");
                        double pinZ = (Double) inp.get("pinZ"), pinCZ = (Double) inp.get("pinCZ");
                        double orderSum = (Double) inp.get("orderSum");
                        if (pinK <= 0 || pinCK <= 0 || pinZ <= 0 || pinCZ <= 0 || orderSum <= 0) {
                            problems.add("网约车输入 " + period + " 行不完整(订单/全省 pin 缺失)");
                        } else {
                            addSum(summary, "全省网约车客运量", pinK, "万人");
                            addSum(summary, "其中城市内客运量", pinCK, "万人");
                            addSum(summary, "全省网约车周转量", pinZ, "万人公里");
                            addSum(summary, "其中城市内周转量", pinCZ, "万人公里");
                            addSum(summary, "部级订单合计", orderSum, "单");
                        }
                    }
                } catch (Exception ex) {
                    problems.add("网约车输入读取失败:" + ex.getMessage());
                }
            } else if ("summaryWorkbook".equals(t)) {
                java.util.Set<Integer> cov = new java.util.TreeSet<>();
                cov.addAll(busCover);
                cov.addAll(taxiCover);
                coverage.addAll(cov);
                missing = missingMonths(cov, month);
                try {
                    if (resolveSummaryMother(period, mode) == null) {
                        problems.add("docs/生成汇总大表 下缺少可复制的母版汇总表");
                    } else {
                        summaryWorkbookPreview(period, yearNum, month, summary, problems);
                    }
                } catch (Exception ex) {
                    problems.add(ex.getMessage());
                }
            } else if (isFreightType(t)) {
                coverage.addAll(freightCover);
                missing = missingMonths(freightCover, month);
                freightPreview(period, month, summary, problems);
            } else if (isPassengerType(t)) {
                coverage.addAll(passengerCover);
                missing = missingMonths(passengerCover, month);
                passengerPreview(yearNum, month, t, summary, problems);
            } else if ("cityTaxiDetail".equals(t)) {
                coverage.addAll(taxiCover);
                missing = missingMonths(taxiCover, month);
                cityPassengerPreview(yearPrefix, month, t, summary, problems);
            } else if (isCityPassengerType(t)) {
                if ("cityBusDetail".equals(t) || "cityRailFerryDetail".equals(t)) {
                    // 公交/轨道轮渡明细只依赖公交月报表(轨道/轮渡数据在公交表内)
                    coverage.addAll(busCover);
                    missing = missingMonths(busCover, month);
                } else {
                    Set<Integer> cov = new TreeSet<>();
                    cov.addAll(busCover);
                    cov.addAll(taxiCover);
                    coverage.addAll(cov);
                    missing = missingMonths(cov, month);
                }
                cityPassengerPreview(yearPrefix, month, t, summary, problems);
            } else if (isInvestType(t)) {
                coverage.addAll(investCover);
                // 投资为当月报表(无 1..N 累计口径),只提示当月缺失
                if (!investCover.contains(month)) missing.add(month);
                investPreview(period, mode, summary, problems);
            } else if ("energySummary".equals(t)) {
                Long ec = energyMapper.selectCount(periodQw(period, mode, EnergyVehicleQuarterly::getReportPeriod));
                if (ec != null && ec > 0) coverage.add(month);
                energyPreview(period, mode, summary, problems);
            }
            if (!missing.isEmpty()) {
                problems.add("缺月数据:" + joinMonths(missing) + "(1~" + month + "月累计需逐月齐全)");
            }
            it.put("monthCoverage", coverage);
            it.put("missingMonths", missing);
            it.put("summary", summary);
            it.put("problems", problems);
        }
        return ready;
    }
 
    private boolean isFreightType(String t) {
        return "cityDetail".equals(t) || "freightRank".equals(t) || "turnoverRank".equals(t);
    }
 
    private boolean isPassengerType(String t) {
        return "passengerCityDetail".equals(t) || "passengerMidDetail".equals(t) || "passengerMidRank".equals(t) || "passengerMidAnalysis".equals(t);
    }
 
    private boolean isCityPassengerType(String t) {
        return "cityBusDetail".equals(t) || "cityRailFerryDetail".equals(t) || "cityPassengerCitySum".equals(t)
            || "cityPassengerDetailSum".equals(t) || "cityPassengerSummary".equals(t);
    }
 
    private boolean isInvestType(String t) {
        return "investPlan".equals(t) || "investFiveYearLogistics".equals(t) || "investBillion".equals(t) || "investCounty".equals(t);
    }
 
    private boolean isEnergyType(String t) {
        return "energySummary".equals(t);
    }
 
    /** 按 report_period like 'yyyy-' 统计 1..limit 月中有数据的月份(升序) */
    private Set<Integer> monthsWithData(java.util.function.Supplier<java.util.List<?>> rows, java.util.function.Function<Object, String> periodOf, int limit) {
        Set<Integer> set = new TreeSet<>();
        for (Object o : rows.get()) {
            String rp = periodOf.apply(o);
            if (rp == null) continue;
            String[] parts = rp.split("-");
            if (parts.length < 2) continue;
            try {
                int m = Integer.parseInt(parts[1]);
                if (m >= 1 && m <= limit) set.add(m);
            } catch (Exception ignore) {}
        }
        return set;
    }
 
    /** 1..limit 中缺失的月份 */
    private List<Integer> missingMonths(Set<Integer> coverage, int limit) {
        List<Integer> missing = new ArrayList<>();
        for (int m = 1; m <= limit; m++) if (!coverage.contains(m)) missing.add(m);
        return missing;
    }
 
    private String joinMonths(List<Integer> months) {
        StringBuilder sb = new StringBuilder();
        for (int m : months) {
            if (sb.length() > 0) sb.append("、");
            sb.append(m).append("月");
        }
        return sb.toString();
    }
 
    /** 汇总整本关键指标预览:货运量/周转量、城市客运、中口径的 当月、1..M 累计、当月同比、累计同比(同比取库内去年同期,缺则显示—) */
    private void summaryWorkbookPreview(String period, int year, int month,
                                        List<Map<String, Object>> summary, List<String> problems) {
        String yearPrefix = year + "-";
        // ---- 货运(量=模板_货运量周转量全省行;周转量=规上规下拆分表全省行) ----
        FreightTurnoverImport ft = loadFreightTurnover(period).get("湖北省");
        Double fm = ft == null ? null : freightMonth(ft, month);
        Double fcum = ft == null ? null : freightCum(ft, month);
        Double fy = ft == null ? null : freightYoy(ft, month);
        Double fcy = ft == null ? null : freightCumYoy(ft, month);
        addKpi(summary, "全省货运量", fm, fcum, fy, fcy, "万吨");
        if (fm != null && fy == null) {
            problems.add((year - 1) + "-" + String.format("%02d", month) + " 同期货运量数据未导入库,货运量当月/累计同比暂不显示,待 2025 年定稿数据导入后自动出现");
        }
        ScaleSplitTransport tm = loadProvinceMonthMap(period).get(month);
        ScaleSplitTransport tc = getProvinceCumulative(period);
        Double tvM = tm == null ? null : tm.getTotalTurnover();
        Double tvC = tc == null ? null : tc.getTotalTurnover();
        Double tvMY = tm == null ? null : getYoyMetric(tm, "turnover", "total");
        Double tvCY = tc == null ? null : getYoyMetric(tc, "turnover", "total");
        addKpi(summary, "全省货物周转量", tvM, tvC, tvMY, tvCY, "万吨公里");
        if (fm == null) problems.add("模板_货运量周转量缺 " + period + " 当月数据,货运量仅能提供截至最近导入月的累计");
        if (tvM == null) problems.add("规上规下拆分缺 " + period + " 当月数据,周转量/规上规下当月缺,累计为截至最近导入月");
 
        // ---- 城市客运(公交+出租+轨道+轮渡 全省小计;指标位 0/2/4/6=客运,1/3/5/7=周转) ----
        double[] cumM = cityCumProv(yearPrefix, month);
        double[] cumPrev = cityCumProv(yearPrefix, month - 1);
        double[] monthVec = diff8(cumM, cumPrev);
        double cityPaxM = sumIdx(monthVec, new int[]{0, 2, 4, 6});
        double cityTurnM = sumIdx(monthVec, new int[]{1, 3, 5, 7});
        double cityPaxC = sumIdx(cumM, new int[]{0, 2, 4, 6});
        double cityTurnC = sumIdx(cumM, new int[]{1, 3, 5, 7});
        double[] lastCumM = cityCumProv((year - 1) + "-", month);
        double[] lastCumPrev = cityCumProv((year - 1) + "-", month - 1);
        double[] lastVec = diff8(lastCumM, lastCumPrev);
        double lastPaxM = sumIdx(lastVec, new int[]{0, 2, 4, 6});
        double lastPaxC = sumIdx(lastCumM, new int[]{0, 2, 4, 6});
        double lastTurnM = sumIdx(lastVec, new int[]{1, 3, 5, 7});
        double lastTurnC = sumIdx(lastCumM, new int[]{1, 3, 5, 7});
        addKpiRaw(summary, "城市客运量", cityPaxM, cityPaxC, lastPaxM, lastPaxC, "万人次");
        addKpiRaw(summary, "城市客运周转量", cityTurnM, cityTurnC, lastTurnM, lastTurnC, "万人公里");
 
        // ---- 中口径(公路旅客企业 H203-1 全省,单位换算 /10000) ----
        Map<Integer, Map<String, PassengerAgg>> data = loadPassengerAggMap();
        double paxM = 0.0, paxC = 0.0, turnM = 0.0, turnC = 0.0;
        double lastPaxM2 = 0.0, lastPaxC2 = 0.0, lastTurnM2 = 0.0, lastTurnC2 = 0.0;
        for (int m = 1; m <= month; m++) {
            PassengerAgg agg = aggOf(data, year, m, "湖北省");
            if (agg != null) {
                double pax = agg.passengerTotal / 10000.0;
                double turn = agg.turnoverTotal / 10000.0;
                if (m == month) { paxM = pax; turnM = turn; }
                paxC += pax;
                turnC += turn;
            }
            PassengerAgg last = aggOf(data, year - 1, m, "湖北省");
            if (last != null) {
                double lp = last.passengerTotal / 10000.0;
                double lt = last.turnoverTotal / 10000.0;
                if (m == month) { lastPaxM2 = lp; lastTurnM2 = lt; }
                lastPaxC2 += lp;
                lastTurnC2 += lt;
            }
        }
        addKpiRaw(summary, "中口径客运量", paxM, paxC, lastPaxM2, lastPaxC2, "万人次");
        addKpiRaw(summary, "中口径周转量", turnM, turnC, lastTurnM2, lastTurnC2, "万人公里");
 
        if (paxM <= 0 && paxC <= 0 && cityPaxC <= 0 && fm == null) {
            problems.add("货运量周转量/规上规下/公路旅客/城市客运当月数据均缺失");
        }
        if (cityPaxC > 0 && lastPaxC <= 0) {
            problems.add((year - 1) + "-" + String.format("%02d", month) + " 同期城市客运数据未导入库,城市客运同比暂不显示,待 2025 年定稿数据导入后自动出现");
        }
        if (paxC > 0 && lastPaxC2 <= 0) {
            problems.add((year - 1) + "-" + String.format("%02d", month) + " 同期公路旅客数据未导入库,中口径同比暂不显示,待 2025 年定稿数据导入后自动出现");
        }
    }
 
    /** 城市客运全省累计向量(8 指标:公交客运/周转、出租客运/周转、轨道客运/周转、轮渡客运/周转) */
    private double[] cityCumProv(String yearPrefix, int month) {
        if (month <= 0) return new double[8];
        return loadCityPassengerCumulative(yearPrefix, month).getOrDefault("全省", new double[8]);
    }
 
    private double[] diff8(double[] a, double[] b) {
        double[] out = new double[8];
        for (int i = 0; i < 8; i++) out[i] = a[i] - b[i];
        return out;
    }
 
    private double sumIdx(double[] arr, int[] idx) {
        double sum = 0.0;
        for (int i : idx) sum += (i < arr.length ? arr[i] : 0.0);
        return sum;
    }
 
    /** 百分比同比(值已为比率 0.xx):输出四行 当月/累计/当月同比/累计同比 */
    private void addKpi(List<Map<String, Object>> summary, String name, Double m, Double cum, Double yoyM, Double yoyC, String unit) {
        if (m != null) addSum(summary, name + "·当月", round(m, 2), unit);
        if (cum != null) addSum(summary, name + "·累计", round(cum, 2), unit);
        if (yoyM != null) addSum(summary, name + "·当月同比", round(yoyM * 100.0, 1), "%");
        if (yoyC != null) addSum(summary, name + "·累计同比", round(yoyC * 100.0, 1), "%");
    }
 
    /** 原始数值版(月/累计为 0 视为无数据不出行) */
    private void addKpiRaw(List<Map<String, Object>> summary, String name, double m, double cum, double lastM, double lastC, String unit) {
        if (m > 0) addSum(summary, name + "·当月", round(m, 2), unit);
        if (cum > 0) addSum(summary, name + "·累计", round(cum, 2), unit);
        if (m > 0 && lastM > 0) addSum(summary, name + "·当月同比", round((m - lastM) / lastM * 100.0, 1), "%");
        if (cum > 0 && lastC > 0) addSum(summary, name + "·累计同比", round((cum - lastC) / lastC * 100.0, 1), "%");
    }
 
    private void addSum(List<Map<String, Object>> summary, String label, Double value, String unit) {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("label", label);
        m.put("value", value);
        m.put("unit", unit);
        summary.add(m);
    }
 
    /** 货运摘要:全省累计货运量(合计/规上/规下)+ 周转量(拆分表全省累计行) */
    private void freightPreview(String period, int month, List<Map<String, Object>> summary, List<String> problems) {
        Map<String, FreightTurnoverImport> ft = loadFreightTurnover(period);
        Map<String, Double> h2032Cum = loadH2032FreightCumulative(period);
        ScaleSplitTransport provCum = getProvinceCumulative(period);
        Double total = freightCum(ft.get("湖北省"), month);
        Double totalWan = total == null ? null : round(total, 4); // 模板列已是万吨,不再 ÷10000
        double above = 0.0;
        for (Double v : h2032Cum.values()) if (v != null) above += v;
        double aboveWan = round(above / 10000.0, 4);
        Double belowWan = totalWan == null ? null : round(totalWan - aboveWan, 4);
        addSum(summary, "全省累计货运量(合计)", totalWan, "万吨");
        addSum(summary, "其中:规上", aboveWan, "万吨");
        addSum(summary, "其中:规下", belowWan, "万吨");
        if (provCum != null) {
            addSum(summary, "全省累计周转量(合计)", provCum.getTotalTurnover(), "万吨公里");
            addSum(summary, "其中:规上", provCum.getAboveScaleTurnover(), "万吨公里");
            addSum(summary, "其中:规下", provCum.getBelowScaleTurnover(), "万吨公里");
        } else {
            problems.add("拆分表无全省累计行(周转量/规上规下缺)");
        }
        if (totalWan == null) problems.add("模板_货运量周转量无全省累计,累计货运量为空");
    }
 
    /** 旅客摘要:企业+个体(分市州)或中口径企业(中口径 3 表) */
    private void passengerPreview(int yearNum, int month, String type, List<Map<String, Object>> summary, List<String> problems) {
        Map<Integer, Map<String, PassengerAgg>> data = loadPassengerAggMap();
        Map<Integer, Map<String, double[]>> indi = loadPassengerIndividualMap();
        double entPax = 0.0, entTurn = 0.0, indiPax = 0.0, indiTurn = 0.0;
        boolean anyEnt = false, anyIndi = false;
        for (int m = 1; m <= month; m++) {
            PassengerAgg agg = aggOf(data, yearNum, m, "湖北省");
            if (agg != null) {
                entPax += agg.passengerTotal / 10000.0;
                entTurn += agg.turnoverTotal / 10000.0;
                anyEnt = true;
            }
            Map<String, double[]> mm = indi.get(yearNum * 100 + m);
            if (mm != null) {
                double[] pv = mm.get("湖北省");
                if (pv != null) {
                    indiPax += pv[0];
                    indiTurn += pv[1];
                    anyIndi = true;
                }
            }
        }
        if ("passengerCityDetail".equals(type)) {
            addSum(summary, "企业客运量累计", round(entPax, 4), "万人次");
            addSum(summary, "个体客运量累计", round(indiPax, 4), "万人次");
            addSum(summary, "合计客运量累计", round(entPax + indiPax, 4), "万人次");
            addSum(summary, "合计周转量累计", round(entTurn + indiTurn, 4), "万人公里");
            if (!anyEnt && !anyIndi) problems.add("H203-1 旅客月报与个体数据均无");
        } else {
            addSum(summary, "中口径客运量累计", round(entPax, 4), "万人次");
            addSum(summary, "中口径周转量累计", round(entTurn, 4), "万人公里");
            if (!anyEnt) problems.add("H203-1 旅客月报无企业数据");
        }
    }
 
    /** 城市客运摘要:公交/出租/轨道/轮渡全省累计(指标 0..7) */
    private void cityPassengerPreview(String yearPrefix, int month, String type, List<Map<String, Object>> summary, List<String> problems) {
        double[] prov = loadCityPassengerCumulative(yearPrefix, month).getOrDefault("全省", new double[8]);
        boolean allZero = true;
        for (double v : prov) if (v != 0.0) allZero = false;
        switch (type) {
            case "cityTaxiDetail":
                addSum(summary, "出租客运量累计", round(prov[2], 4), "万人次");
                addSum(summary, "出租周转量累计", round(prov[3], 4), "万人公里");
                break;
            case "cityRailFerryDetail":
                addSum(summary, "轨道客运量累计", round(prov[4], 4), "万人次");
                addSum(summary, "轨道周转量累计", round(prov[5], 4), "万人公里");
                addSum(summary, "轮渡客运量累计", round(prov[6], 4), "万人次");
                addSum(summary, "轮渡周转量累计", round(prov[7], 4), "万人公里");
                break;
            case "cityBusDetail":
                addSum(summary, "公交客运量累计", round(prov[0], 4), "万人次");
                addSum(summary, "公交周转量累计", round(prov[1], 4), "万人公里");
                addSum(summary, "轨道客运量累计", round(prov[4], 4), "万人次");
                addSum(summary, "轨道周转量累计", round(prov[5], 4), "万人公里");
                addSum(summary, "轮渡客运量累计", round(prov[6], 4), "万人次");
                addSum(summary, "轮渡周转量累计", round(prov[7], 4), "万人公里");
                break;
            default:
                addSum(summary, "公交客运量累计", round(prov[0], 4), "万人次");
                addSum(summary, "公交周转量累计", round(prov[1], 4), "万人公里");
                addSum(summary, "出租客运量累计", round(prov[2], 4), "万人次");
                addSum(summary, "出租周转量累计", round(prov[3], 4), "万人公里");
                addSum(summary, "轨道客运量累计", round(prov[4], 4), "万人次");
                addSum(summary, "轨道周转量累计", round(prov[5], 4), "万人公里");
                addSum(summary, "轮渡客运量累计", round(prov[6], 4), "万人次");
                addSum(summary, "轮渡周转量累计", round(prov[7], 4), "万人公里");
        }
        if (allZero) problems.add("全省累计值全为 0(公交/出租月报可能未导入)");
    }
 
    /** 投资摘要:项目数 + 本月完成/自年初/自开始累计 */
    private void investPreview(String period, String mode, List<Map<String, Object>> summary, List<String> problems) {
        List<InvestmentMonthly> list = investMonthlyMapper.selectList(periodQw(period, mode, InvestmentMonthly::getReportPeriod));
        Set<Long> proj = new HashSet<>();
        double monthDone = 0.0, yearCum = 0.0, startCum = 0.0;
        for (InvestmentMonthly m : list) {
            if (m.getProjectId() != null) proj.add(m.getProjectId());
            monthDone += nz(m.getMonthDone());
            yearCum += nz(m.getYearCum());
            startCum += nz(m.getStartCum());
        }
        addSum(summary, "有月度记录项目数", (double) proj.size(), "个");
        addSum(summary, "本月完成投资合计", round(monthDone, 2), "万元");
        addSum(summary, "自年初累计完成", round(yearCum, 2), "万元");
        addSum(summary, "自开始累计完成", round(startCum, 2), "万元");
        if (list.isEmpty()) problems.add("无投资月度数据");
    }
 
    /** 能耗摘要:车辆记录数 + 燃油消耗合计(季度表,按所选季度末月) */
    private void energyPreview(String period, String mode, List<Map<String, Object>> summary, List<String> problems) {
        Long cnt = energyMapper.selectCount(periodQw(period, mode, EnergyVehicleQuarterly::getReportPeriod));
        int rows = cnt == null ? 0 : cnt.intValue();
        double fuel = 0.0;
        if (rows > 0) {
            for (EnergyVehicleQuarterly e : energyMapper.selectList(periodQw(period, mode, EnergyVehicleQuarterly::getReportPeriod))) {
                fuel += nz(e.getFuelConsumption());
            }
        }
        addSum(summary, "车辆记录数", (double) rows, "条");
        addSum(summary, "燃油消耗合计", round(fuel, 2), "—");
        if (rows == 0) problems.add("该季度无能耗车辆数据");
    }
 
    private int freightReadyRows(String period, String mode, List<String> notes) {
        return addReadyCount(() -> freightTurnoverMapper.selectCount(periodQw(period, mode, FreightTurnoverImport::getReportPeriod)), "模板_货运量周转量", notes)
             + addReadyCount(() -> scaleSplitMapper.selectCount(periodQw(period, mode, ScaleSplitTransport::getReportPeriod)), "规上规下拆分表", notes)
             + addReadyCount(() -> h2032Mapper.selectCount(periodQw(period, mode, H2032EnterpriseMonthly::getReportPeriod)), "H203-2 货运月报", notes);
    }
 
    private int passengerReadyRows(String period, String mode, List<String> notes) {
        return addReadyCount(() -> passengerMapper.selectCount(periodQw(period, mode, PassengerEnterpriseMonthly::getReportPeriod)), "H203-1 旅客月报", notes)
             + addReadyCount(() -> passengerIndividualMapper.selectCount(periodQw(period, mode, PassengerIndividualMonthly::getReportPeriod)), "个体客运量/周转量", notes);
    }
 
    private <T> LambdaQueryWrapper<T> periodQw(String period, String mode, SFunction<T, ?> periodGetter) {
        LambdaQueryWrapper<T> qw = new LambdaQueryWrapper<>();
        if ("year".equals(mode)) {
            qw.likeRight(periodGetter, period);
        } else {
            qw.eq(periodGetter, period);
        }
        return qw;
    }
 
    private int addReadyCount(java.util.function.Supplier<Long> counter, String label, List<String> notes) {
        Long cnt = counter.get();
        if (cnt == null || cnt <= 0) notes.add("缺数据:" + label);
        return cnt == null ? 0 : cnt.intValue();
    }
 
    /** 该报表期该审核类型已产生的审核记录数(先审核再出表的前置检查) */
    private int auditRowsOf(String period, String mode, String ruleType) {
        List<AuditRule> rules = auditRuleMapper.selectList(new LambdaQueryWrapper<AuditRule>()
            .eq(AuditRule::getReportType, ruleType)
            .eq(AuditRule::getIsEnabled, 1));
        if (rules.isEmpty()) return 0;
        List<Long> ruleIds = new ArrayList<>();
        for (AuditRule r : rules) ruleIds.add(r.getId());
        LambdaQueryWrapper<AuditResult> qw = new LambdaQueryWrapper<>();
        if ("year".equals(mode)) qw.likeRight(AuditResult::getReportPeriod, period);
        else qw.eq(AuditResult::getReportPeriod, period);
        Long cnt = auditResultMapper.selectCount(qw.in(AuditResult::getRuleId, ruleIds));
        return cnt == null ? 0 : cnt.intValue();
    }
 
    /** 该报表期该审核类型是否已有「审核通过标记」(audit_run 表;线下审核通过/系统审核执行后写入,导出前不再提示未审核) */
    private boolean auditedMarked(String period, String mode, String ruleType) {
        LambdaQueryWrapper<AuditRun> qw = new LambdaQueryWrapper<AuditRun>()
            .eq(AuditRun::getReportType, ruleType);
        if ("year".equals(mode)) qw.likeRight(AuditRun::getReportPeriod, period);
        else qw.eq(AuditRun::getReportPeriod, period);
        Long cnt = auditRunMapper.selectCount(qw);
        return cnt != null && cnt > 0;
    }
 
    // ==================== 1. 生成_货运量分市州明细.xlsx ====================
 
    public byte[] exportCityDetail(String period, String mode) throws Exception {
        int currentMonth = monthOf(period, mode);
        int fillMonths = currentMonth; // 1..N 月(N>6 时模板自动向右扩列到 12 月)
        boolean extended = fillMonths > 6;
        Map<String, Map<Integer, ScaleSplitTransport>> monthData = loadMonthData(period);
        Map<Integer, ScaleSplitTransport> provinceMonthMap = loadProvinceMonthMap(period);
        ScaleSplitTransport provinceCum = getProvinceCumulative(period);
        Map<String, ScaleSplitTransport> cumMap = loadCumulativeMap(period);
        Map<String, Map<Integer, Double>> h2032Freight = loadH2032FreightByMonth(period);
        Map<String, Double> h2032FreightCum = loadH2032FreightCumulative(period);
        Map<String, FreightTurnoverImport> ftMap = loadFreightTurnover(period);
 
        // 以 docs/货运/生成_货运量分市州明细.xlsx 为底稿:保留标题/表头/合并/列宽/样式,仅替换数据区
        File template = resolveFreightTemplate("生成_货运量分市州明细.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            if (extended) extendMonthlyColumns(sheet, 2, Integer.parseInt(period.substring(0, 4)), 4, sheet.getLastRowNum());
            String[][] provinceMetrics = {
                {"货运量         (万吨)", "freight", "total"},
                {"货物周转量     (万吨公里)", "turnover", "total"},
                {"其中规上货运量   (万吨)", "freight", "above"},
                {"其中规上货物周转量  (万吨公里)", "turnover", "above"},
                {"其中规下货运量   (万吨)", "freight", "below"},
                {"其中规下货物周转量  (万吨公里)", "turnover", "below"}
            };
            FreightTurnoverImport ftProvince = ftMap.get("湖北省");
            int rowIdx = 4; // 0-based:第5行起为 全省 6 指标
            for (String[] metric : provinceMetrics) {
                Row row = sheet.getRow(rowIdx);
                if (row == null) row = sheet.createRow(rowIdx);
                fillFreightDetailRow(row, fillMonths, extended ? 26 : 14,
                    m -> getProvinceMonthValue(provinceMonthMap, ftProvince, m, metric[1], metric[2], h2032Freight),
                    m -> getProvinceMonthYoy(provinceMonthMap, ftProvince, m, metric[1], metric[2]),
                    getProvinceCumValue(provinceCum, ftProvince, metric[1], metric[2], h2032FreightCum, currentMonth),
                    getProvinceCumYoy(provinceCum, ftProvince, metric[1], metric[2], currentMonth));
                rowIdx++;
            }
            for (String city : RegionUtil.cityList()) {
                String[][] metrics = {
                    {"规上+规下货运量(万吨)", "freight", "total"},
                    {"规上货运量      (万吨)", "freight", "above"},
                    {"规下货运量      (万吨)", "freight", "below"},
                    {"规上+规下周转量  (万吨公里)", "turnover", "total"},
                    {"规上货物周转量  (万吨公里)", "turnover", "above"},
                    {"规下货物周转量  (万吨公里)", "turnover", "below"}
                };
                FreightTurnoverImport ftCity = ftMap.get(city);
                ScaleSplitTransport cum = cumMap.get(city);
                for (String[] metric : metrics) {
                    Row row = sheet.getRow(rowIdx);
                    if (row == null) row = sheet.createRow(rowIdx);
                    fillFreightDetailRow(row, fillMonths, extended ? 26 : 14,
                        m -> getCityMonthValue(monthData, ftCity, city, m, metric[1], metric[2], h2032Freight),
                        m -> getCityMonthYoy(monthData, ftCity, city, m, metric[1], metric[2]),
                        getCityCumValue(cum, ftCity, metric[1], metric[2], h2032FreightCum, city, currentMonth),
                        getCityCumYoy(cum, ftCity, metric[1], metric[2], currentMonth));
                    rowIdx++;
                }
            }
            return toBytes(wb);
        }
    }
    // ==================== 2. 生成_货运量排名.xlsx ====================
 
    public byte[] exportFreightRank(String period, String mode) throws Exception {
        String year = period.substring(0, 4);
        int month = monthOf(period, mode);
        Map<String, FreightTurnoverImport> ftMap = loadFreightTurnover(period);
        Map<String, Double> h2032FreightCum = loadH2032FreightCumulative(period);
        Map<String, Double> h2032FreightYoy = computeH2032Yoy(period);
        Map<String, Double> h2032FreightCumLast = loadH2032FreightCumulative(
            (Integer.parseInt(period.substring(0, 4)) - 1) + period.substring(4));
 
        Map<String, Double> above = new HashMap<>();
        Map<String, Double> below = new HashMap<>();
        Map<String, Double> total = new HashMap<>();
        Map<String, Double> aboveYoy = new HashMap<>();
        Map<String, Double> belowYoy = new HashMap<>();
        Map<String, Double> totalYoy = new HashMap<>();
 
        double provAbove = 0.0;
        for (Double v : h2032FreightCum.values()) {
            if (v != null) provAbove += v;
        }
        Double provAboveWan = round(provAbove / 10000.0, 4);
        Double provTotal = freightCum(ftMap.get("湖北省"), month);
        Double provBelow = provTotal == null ? null : round(provTotal - provAboveWan, 4);
        Double provTotalYoy = freightCumYoy(ftMap.get("湖北省"), month);
        double provAboveLast = 0.0;
        for (Double v : h2032FreightCumLast.values()) {
            if (v != null) provAboveLast += v;
        }
        Double provAboveYoy = provAboveLast == 0.0 ? null : round((provAbove - provAboveLast) / provAboveLast, 4);
        Double provTotalLast = lastFreightCum(ftMap.get("湖北省"), month);
        Double provBelowLast = provTotalLast == null ? null : round(provTotalLast - provAboveLast / 10000.0, 4);
        Double provBelowYoy = provBelowLast == null || provBelowLast == 0.0 ? null
            : round((provBelow - provBelowLast) / provBelowLast, 4);
 
        for (String city : RegionUtil.cityList()) {
            Double a = h2032FreightCum.get(city);
            double aWan = a == null ? 0.0 : round(a / 10000.0, 4);
            Double t = freightCum(ftMap.get(city), month);
            above.put(city, aWan);
            total.put(city, t);
            below.put(city, t == null ? null : round(t - aWan, 4));
            aboveYoy.put(city, h2032FreightYoy.get(city));
            // 规下同比 = (规下今年 - 规下去年) / 规下去年;规下去年 = 模板去年合计 - H2032去年规上
            Double tLast = lastFreightCum(ftMap.get(city), month);
            Double aLast = h2032FreightCumLast.get(city);
            double aLastWan = aLast == null ? 0.0 : round(aLast / 10000.0, 4);
            Double belowLast = tLast == null ? null : round(tLast - aLastWan, 4);
            Double b = below.get(city);
            belowYoy.put(city, belowLast == null || belowLast == 0.0 || b == null
                ? null : round((b - belowLast) / belowLast, 4));
            totalYoy.put(city, freightCumYoy(ftMap.get(city), month));
        }
 
 
        // ===== 第二页(当月)数据:规上=H2032 当月、合计=模板_货运量周转量当月列、规下=差值;同比=去年同月 =====
        Map<String, Map<Integer, Double>> h2032MonthCur = loadH2032FreightByMonth(period);
        String lastYearPeriod = (Integer.parseInt(year) - 1) + period.substring(4);
        Map<String, Map<Integer, Double>> h2032MonthLast = loadH2032FreightByMonth(lastYearPeriod);
        Map<String, Double> monthAbove = new HashMap<>();
        Map<String, Double> monthBelow = new HashMap<>();
        Map<String, Double> monthTotal = new HashMap<>();
        Map<String, Double> monthAboveYoy = new HashMap<>();
        Map<String, Double> monthBelowYoy = new HashMap<>();
        Map<String, Double> monthTotalYoy = new HashMap<>();
        double provMonthAbove = 0.0, provMonthAboveLast = 0.0;
        for (String city : RegionUtil.cityList()) {
            Double ma = monthFreightWan(h2032MonthCur, city, month);
            Double maLast = monthFreightWan(h2032MonthLast, city, month);
            Double mt = freightMonth(ftMap.get(city), month);
            Double mtLast = lastFreightMonth(ftMap.get(city), month);
            if (ma != null) provMonthAbove += ma;
            if (maLast != null) provMonthAboveLast += maLast;
            monthAbove.put(city, ma);
            monthTotal.put(city, mt);
            monthBelow.put(city, mt == null ? null : round(mt - (ma == null ? 0.0 : ma), 4));
            monthAboveYoy.put(city, growthYoy(ma, maLast));
            Double mb = monthBelow.get(city);
            Double mbLast = mtLast == null ? null : round(mtLast - (maLast == null ? 0.0 : maLast), 4);
            monthBelowYoy.put(city, growthYoy(mb, mbLast));
            monthTotalYoy.put(city, freightYoy(ftMap.get(city), month));
        }
        Double provMonthAboveWan = provMonthAbove == 0.0 ? null : round(provMonthAbove, 4);
        Double provMonthTotal = freightMonth(ftMap.get("湖北省"), month);
        Double provMonthBelow = provMonthTotal == null ? null : round(provMonthTotal - provMonthAbove, 4);
        Double provMonthAboveYoy = growthYoy(provMonthAbove == 0.0 ? null : provMonthAbove, provMonthAboveLast);
        Double provMonthTotalLast = lastFreightMonth(ftMap.get("湖北省"), month);
        Double provMonthBelowLast = provMonthTotalLast == null ? null
            : round(provMonthTotalLast - provMonthAboveLast, 4);
        Double provMonthBelowYoy = growthYoy(provMonthBelow, provMonthBelowLast);
        Double provMonthTotalYoy = freightYoy(ftMap.get("湖北省"), month);
 
        // 以 docs/货运/生成_货运量排名.xlsx 为底稿:保留表头/合并/列宽/样式与 Q/R 占比公式,仅替换数据
        File template = resolveFreightTemplate("生成_货运量排名.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            sheet.getRow(0).getCell(0).setCellValue(year + "年" + cumRange(month) + "全省分市州累计完成公路货运量情况");
            sheet.getRow(22).getCell(0).setCellValue(year + "年" + month + "月全省分市州完成公路货运量情况");
            fillFreightRankBlock(sheet, 3, above, below, total, aboveYoy, belowYoy, totalYoy,
                provAboveWan, provBelow, provTotal, provAboveYoy, provBelowYoy, provTotalYoy);
            renameRankMonthHeader(sheet, 24);
            fillFreightRankBlock(sheet, 25, monthAbove, monthBelow, monthTotal,
                monthAboveYoy, monthBelowYoy, monthTotalYoy,
                provMonthAboveWan, provMonthBelow, provMonthTotal,
                provMonthAboveYoy, provMonthBelowYoy, provMonthTotalYoy);
            try {
                wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
            } catch (Exception e) {
                log.warn("货运量排名模板公式求值失败: {}", e.getMessage());
            }
            return toBytes(wb);
        }
    }
 
    // ==================== 3. 生成_周转量排名.xlsx ====================
 
    public byte[] exportTurnoverRank(String period, String mode) throws Exception {
        String year = period.substring(0, 4);
        int month = monthOf(period, mode);
        Map<String, ScaleSplitTransport> cumMap = loadCumulativeMap(period);
        ScaleSplitTransport province = getProvinceCumulative(period);
 
        // 当月(第二页):周转量拆分表 MONTH 行(含当月同比)
        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);
        }
        ScaleSplitTransport provinceMonth = monthTurnover.remove("湖北省");
 
        // 以 docs/货运/生成_周转量排名.xlsx 为底稿:保留表头/合并/列宽/样式,仅替换数据;Q/R 占比为模板缓存值,按新数据重算
        File template = resolveFreightTemplate("生成_周转量排名.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            sheet.getRow(0).getCell(0).setCellValue(year + "年" + cumRange(month) + "全省分市州累计完成公路货物运输周转量情况");
            sheet.getRow(22).getCell(0).setCellValue(year + "年" + month + "月全省分市州完成公路货物运输周转量情况");
            fillTurnoverRankBlock(sheet, 3, cumMap, province);
            renameRankMonthHeader(sheet, 24);
            fillTurnoverRankBlock(sheet, 25, monthTurnover, provinceMonth);
            return toBytes(wb);
        }
    }
    /** 货运报表模板定位(docs/货运) */
    private File resolveFreightTemplate(String fileName) throws Exception {
        return resolveAnyTemplate(freightTemplateDir, fileName);
    }
 
    /** 写货运模板数据单元格:公式列不动;有值先清空再写(POI 对公式单元格只更新缓存),无值清空模板样例 */
    private void setFreightCell(Row row, int idx, Double v) {
        Cell c = row.getCell(idx);
        if (c != null && c.getCellType() == CellType.FORMULA) return;
        if (c == null) c = row.createCell(idx);
        c.setBlank();
        if (v != null) c.setCellValue(v);
    }
 
    private void setFreightCell(Row row, int idx, Integer v) {
        if (v == null) {
            setFreightCell(row, idx, (Double) null);
        } else {
            setFreightCell(row, idx, v.doubleValue());
        }
    }
 
    // ==================== 月度模板自动扩列(1-6月 → 1-12月) ====================
 
    /** 月度模板向右扩列:累计列(14/15)移到末尾(26/27),7..12 月列由 1 月列克隆(表头/样式/公式引用同步) */
    private void extendMonthlyColumns(Sheet sheet, int headerRow, int year, int dataStartRow, int dataEndRow) {
        moveColumnPair(sheet, headerRow, 14, 26, dataEndRow); // 累计/累计同比 → AA/AB
        for (int m = 7; m <= 12; m++) {
            int dst = 2 + (m - 1) * 2;
            cloneColumnPair(sheet, headerRow, 2, 3, dst, dst + 1, year, m, dataStartRow, dataEndRow);
        }
        fixCumulativeFormulas(sheet, headerRow, dataEndRow); // AA 累计公式 = 12 个月值之和
    }
 
    /** 把一列对(值列+同比列)从 src 移到 dst(含表头/数据/样式/合并/列宽),src 内容清空 */
    private void moveColumnPair(Sheet sheet, int headerRow, int src, int dst, int lastRow) {
        for (int r = headerRow; r <= lastRow; r++) {
            copyCellForColumn(sheet, r, src, r, dst, false);
            copyCellForColumn(sheet, r, src + 1, r, dst + 1, false);
            clearCellContent(sheet, r, src);
            clearCellContent(sheet, r, src + 1);
        }
        moveMergedRegion(sheet, src, dst);
        moveMergedRegion(sheet, src + 1, dst + 1);
        copyColumnWidth(sheet, src, dst);
        copyColumnWidth(sheet, src + 1, dst + 1);
    }
 
    /** 把 1 月列对克隆为 m 月列对:表头文本/日期修正、样式复制、非跨簿公式克隆并同步列引用 */
    private void cloneColumnPair(Sheet sheet, int headerRow, int srcVal, int srcYoy, int dstVal, int dstYoy,
                                 int year, int month, int dataStartRow, int dataEndRow) {
        for (int r = headerRow; r <= dataEndRow; r++) {
            boolean dataZone = r >= dataStartRow;
            cloneCellForMonth(sheet, r, srcVal, dstVal, year, month, dataZone);
            cloneCellForMonth(sheet, r, srcYoy, dstYoy, year, month, dataZone);
        }
        cloneMergedRegion(sheet, srcVal, dstVal);
        cloneMergedRegion(sheet, srcYoy, dstYoy);
        copyColumnWidth(sheet, srcVal, dstVal);
        copyColumnWidth(sheet, srcYoy, dstYoy);
    }
 
    /** 复制单格:move=false 原样复制;move=true 表头文本/日期修正、数据区仅样式+非跨簿公式(列引用同步) */
    private void copyCellForColumn(Sheet sheet, int r, int srcC, int dstR, int dstC, boolean monthClone) {
        Row srcRow = sheet.getRow(r);
        if (srcRow == null) return;
        Cell src = srcRow.getCell(srcC);
        if (src == null) return;
        Cell dst = getOrCreateCell(sheet, dstR, dstC);
        dst.setBlank();
        dst.setCellStyle(src.getCellStyle());
        if (!monthClone) {
            if (src.getCellType() == CellType.FORMULA) {
                String f = src.getCellFormula();
                if (f != null && !f.contains("#REF!")) dst.setCellFormula(f);
            } else if (src.getCellType() == CellType.STRING) {
                dst.setCellValue(src.getStringCellValue());
            } else if (src.getCellType() == CellType.NUMERIC) {
                dst.setCellValue(src.getNumericCellValue());
            } else if (src.getCellType() == CellType.BOOLEAN) {
                dst.setCellValue(src.getBooleanCellValue());
            }
        }
    }
 
    /** 克隆单格到目标月列:表头行(dataZone=false)修正月份/年份文本或日期;数据区仅样式+非跨簿公式克隆 */
    private void cloneCellForMonth(Sheet sheet, int r, int srcC, int dstC, int year, int month, boolean dataZone) {
        Row srcRow = sheet.getRow(r);
        if (srcRow == null) return;
        Cell src = srcRow.getCell(srcC);
        if (src == null) return;
        Cell dst = getOrCreateCell(sheet, r, dstC);
        dst.setBlank();
        dst.setCellStyle(src.getCellStyle());
        if (!dataZone) {
            if (src.getCellType() == CellType.NUMERIC && org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(src)) {
                java.util.Calendar cal = java.util.Calendar.getInstance();
                cal.set(year, month - 1, 1, 0, 0, 0);
                cal.clear(java.util.Calendar.MILLISECOND);
                dst.setCellValue(cal.getTime());
            } else if (src.getCellType() == CellType.STRING) {
                String t = src.getStringCellValue();
                if (t != null && !t.isEmpty()) {
                    dst.setCellValue(t.replaceFirst("^\\d{4}年", year + "年").replaceFirst("(\\d+)月", month + "月"));
                }
            }
            return;
        }
        // 数据区:仅复制样式;非跨簿、非 #REF! 的公式克隆并同步月值列引用
        if (src.getCellType() == CellType.FORMULA) {
            String f = src.getCellFormula();
            if (f != null && !f.contains("[") && !f.contains("#REF!")) {
                dst.setCellFormula(shiftMonthFormula(f, colLetter(dstC)));
            }
        }
    }
 
    /** 公式列引用平移:本表月值列(C/E/G/I/K/M/O/Q/S/U/W/Y)→ 目标列字母;排除跨簿 [..]! 引用 */
    private String shiftMonthFormula(String formula, String dstColLetter) {
        return formula.replaceAll("(?<![A-Za-z0-9\\[\\]!])([CEGIKMOQSUWY])(\\d+)", dstColLetter + "$2");
    }
 
    /** AA 累计列公式重写为 12 个月值之和(模板原为 1-6 月之和) */
    private void fixCumulativeFormulas(Sheet sheet, int headerRow, int dataEndRow) {
        for (int r = headerRow; r <= dataEndRow; r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            Cell c = row.getCell(26);
            if (c == null || c.getCellType() != CellType.FORMULA) continue;
            String f = c.getCellFormula();
            if (f == null) continue;
            java.util.regex.Matcher m = java.util.regex.Pattern
                .compile("(?<![A-Za-z0-9\\[\\]!])([CEGIKMOQSUWY])(\\d+)").matcher(f);
            if (!m.find()) continue;
            int rowNum = Integer.parseInt(m.group(2));
            StringBuilder sb = new StringBuilder();
            for (char col : new char[]{'C', 'E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y'}) {
                if (sb.length() > 0) sb.append("+");
                sb.append(col).append(rowNum);
            }
            c.setCellFormula(sb.toString());
        }
    }
 
    /** 合并单元格:src 单列合并 → dst 列(同形状),并移除原合并 */
    private void moveMergedRegion(Sheet sheet, int srcCol, int dstCol) {
        java.util.List<CellRangeAddress> moves = new java.util.ArrayList<>();
        for (CellRangeAddress m : sheet.getMergedRegions()) {
            if (m.getFirstColumn() == srcCol && m.getLastColumn() == srcCol) moves.add(m);
        }
        for (CellRangeAddress m : moves) {
            sheet.addMergedRegion(new CellRangeAddress(m.getFirstRow(), m.getLastRow(), dstCol, dstCol));
            for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--) {
                if (sheet.getMergedRegion(i).equals(m)) {
                    sheet.removeMergedRegion(i);
                    break;
                }
            }
        }
    }
 
    /** 合并单元格:src 单列合并 → dst 列(同形状,保留原合并) */
    private void cloneMergedRegion(Sheet sheet, int srcCol, int dstCol) {
        for (CellRangeAddress m : sheet.getMergedRegions()) {
            if (m.getFirstColumn() == srcCol && m.getLastColumn() == srcCol) {
                sheet.addMergedRegion(new CellRangeAddress(m.getFirstRow(), m.getLastRow(), dstCol, dstCol));
            }
        }
    }
 
    private void copyColumnWidth(Sheet sheet, int src, int dst) {
        try {
            int w = sheet.getColumnWidth(src);
            if (w > 0) sheet.setColumnWidth(dst, w);
        } catch (Exception ignore) { }
    }
 
    private Cell getOrCreateCell(Sheet sheet, int r, int c) {
        Row row = sheet.getRow(r);
        if (row == null) row = sheet.createRow(r);
        Cell cell = row.getCell(c);
        if (cell == null) cell = row.createCell(c);
        return cell;
    }
 
    private void clearCellContent(Sheet sheet, int r, int c) {
        Row row = sheet.getRow(r);
        if (row == null) return;
        Cell cell = row.getCell(c);
        if (cell != null) cell.setBlank();
    }
 
    private String colLetter(int colIdx) {
        return org.apache.poi.ss.util.CellReference.convertNumToColString(colIdx);
    }
 
    /** 旅客分市州月值(0=客运量/1=周转量/2=个体客运量/3=个体周转量;0/1=企业H2031+个体,2026-09-03 口径确认) */
    private double passengerMonthVal(Map<Integer, Map<String, PassengerAgg>> data, Map<Integer, Map<String, double[]>> indi,
                                     int year, int month, String region, int metricIdx) {
        if (metricIdx == 0 || metricIdx == 1) { // 客运量/周转量总行:企业+个体
            PassengerAgg agg = aggOf(data, year, month, region);
            double ent = agg == null ? 0.0
                : (metricIdx == 0 ? agg.passengerTotal / 10000.0 : agg.turnoverTotal / 10000.0);
            double ind = individualVal(indi, year, month, region, metricIdx == 0 ? 0 : 1);
            return ent + ind;
        }
        return individualVal(indi, year, month, region, metricIdx - 2);
    }
 
    /** 旅客累计同比(去年 1..months 月累计):无去年数据返回 null */
    private Double passengerCumYoy(Map<Integer, Map<String, PassengerAgg>> data, Map<Integer, Map<String, double[]>> indi,
                                   int year, int months, String region, int metricIdx) {
        double v = 0.0, lv = 0.0;
        for (int m = 1; m <= months; m++) {
            v += passengerMonthVal(data, indi, year, m, region, metricIdx);
            lv += passengerMonthVal(data, indi, year - 1, m, region, metricIdx);
        }
        return lv == 0.0 ? null : (v - lv) / lv;
    }
 
    /** 中口径月同比:k=-1 表示总行(班线+公交之和,出租/网约车无源数据按 0);无去年数据留空 */
    private void fillMidYoyCell(Cell c, Map<Integer, Map<String, double[][]>> mid, int year, int month,
                                String area, int k, boolean volume) {
        double v = 0.0, lv = 0.0;
        if (k == -1) {
            for (int kk = 1; kk <= 4; kk++) {
                if (kk >= 3) continue;
                v += midClassVal(mid, year, month, area, kk, volume);
                lv += midClassVal(mid, year - 1, month, area, kk, volume);
            }
        } else if (k <= 2) {
            v = midClassVal(mid, year, month, area, k, volume);
            lv = midClassVal(mid, year - 1, month, area, k, volume);
        }
        c.setBlank();
        if (v != 0.0 && lv != 0.0) c.setCellValue(round((v - lv) / lv, 4));
    }
 
    /** 中口径累计同比列(1..months 月累计;k=-1 总行 = 班线+公交) */
    private void fillMidCumYoyColumn(Sheet sheet, Map<Integer, Map<String, double[][]>> mid, int year,
                                     int months, int colIdx) {
        List<String> areas = midTemplateAreas();
        for (int r0 = 4; r0 <= sheet.getLastRowNum(); r0++) {
            int rr = r0 + 1;
            int blockOffset = -1;
            boolean volume = false;
            if (rr >= 5 && rr <= 94) {
                blockOffset = rr - 5;
                volume = true;
            } else if (rr >= 99 && rr <= 188) {
                blockOffset = rr - 99;
                volume = false;
            }
            if (blockOffset < 0 || blockOffset % 5 == 0) continue;
            int k = blockOffset % 5;
            String area = areas.get(blockOffset / 5);
            double v = 0.0, lv = 0.0;
            for (int m = 1; m <= months; m++) {
                if (k <= 2) {
                    v += midClassVal(mid, year, m, area, k, volume);
                    lv += midClassVal(mid, year - 1, m, area, k, volume);
                }
            }
            Row row = sheet.getRow(r0);
            if (row == null) continue;
            Cell c = row.getCell(colIdx);
            if (c == null) c = row.createCell(colIdx);
            c.setBlank();
            if (v != 0.0 && lv != 0.0) c.setCellValue(round((v - lv) / lv, 4));
        }
    }
 
    /** 货运分市州明细行填充:1..fillMonths 月值/同比(最多 12 月,其余清空)+ 累计/累计同比(cumCol 起两列) */
    private void fillFreightDetailRow(Row row, int fillMonths, int cumCol,
                                      java.util.function.IntFunction<Double> monthValue,
                                      java.util.function.IntFunction<Double> monthYoy,
                                      Double cumValue, Double cumYoy) {
        for (int m = 1; m <= (fillMonths > 6 ? 12 : 6); m++) {
            if (m <= fillMonths) {
                setFreightCell(row, 2 + (m - 1) * 2, monthValue.apply(m));
                setFreightCell(row, 3 + (m - 1) * 2, monthYoy.apply(m));
            } else {
                setFreightCell(row, 2 + (m - 1) * 2, (Double) null);
                setFreightCell(row, 3 + (m - 1) * 2, (Double) null);
            }
        }
        setFreightCell(row, cumCol, cumValue);
        setFreightCell(row, cumCol + 1, cumYoy);
    }
 
    /** H2032 规上某月合计(吨→万吨);无该月数据返回 null */
    private Double monthFreightWan(Map<String, Map<Integer, Double>> byCityMonth, String city, int month) {
        Map<Integer, Double> byMonth = byCityMonth == null ? null : byCityMonth.get(city);
        Double v = byMonth == null ? null : byMonth.get(month);
        return v == null ? null : round(v / 10000.0, 4);
    }
 
    /** 同比 = (本期-基期)/基期;任一期缺失或基期为 0 返回 null */
    private Double growthYoy(Double cur, Double last) {
        if (cur == null || last == null || last == 0.0) return null;
        return (cur - last) / last;
    }
 
    /** 排名表第二页列头「累计完成」→「当月完成」(仅导出内存替换,模板文件不改) */
    private void renameRankMonthHeader(Sheet sheet, int headerRowIdx) {
        Row row = sheet.getRow(headerRowIdx);
        if (row == null) return;
        for (int col : new int[]{1, 6, 11}) {
            Cell c = row.getCell(col);
            if (c == null || c.getCellType() != CellType.STRING) continue;
            String s = c.getStringCellValue();
            if (s != null && s.contains("累计完成")) c.setCellValue(s.replace("累计完成", "当月完成"));
        }
    }
 
    /** 货运量排名块填充:模板 1-based r4 起(全省+17市州),Q/R 占比公式保留不动 */
    private void fillFreightRankBlock(Sheet sheet, int start,
                                      Map<String, Double> above, Map<String, Double> below, Map<String, Double> total,
                                      Map<String, Double> aboveYoy, Map<String, Double> belowYoy, Map<String, Double> totalYoy,
                                      Double provAbove, Double provBelow, Double provTotal,
                                      Double provAboveYoy, Double provBelowYoy, Double provTotalYoy) {
        Row provRow = sheet.getRow(start);
        if (provRow == null) provRow = sheet.createRow(start);
        setFreightCell(provRow, 1, provAbove);
        setFreightCell(provRow, 4, provAboveYoy);
        setFreightCell(provRow, 6, provBelow);
        setFreightCell(provRow, 9, provBelowYoy);
        setFreightCell(provRow, 11, provTotal);
        setFreightCell(provRow, 14, provTotalYoy);
        int rowIdx = start + 1;
        for (String city : RegionUtil.cityList()) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) row = sheet.createRow(rowIdx);
            setFreightCell(row, 1, above.get(city));
            setFreightCell(row, 2, rankOfMap(above, city));
            setFreightCell(row, 3, ratioOf(above.get(city), provAbove));
            setFreightCell(row, 4, aboveYoy.get(city));
            setFreightCell(row, 5, yoyRankOfMap(aboveYoy, city));
            setFreightCell(row, 6, below.get(city));
            setFreightCell(row, 7, rankOfMap(below, city));
            setFreightCell(row, 8, ratioOf(below.get(city), provBelow));
            setFreightCell(row, 9, belowYoy.get(city));
            setFreightCell(row, 10, yoyRankOfMap(belowYoy, city));
            setFreightCell(row, 11, total.get(city));
            setFreightCell(row, 12, rankOfMap(total, city));
            setFreightCell(row, 13, ratioOf(total.get(city), provTotal));
            setFreightCell(row, 14, totalYoy.get(city));
            setFreightCell(row, 15, yoyRankOfMap(totalYoy, city));
            rowIdx++;
        }
    }
 
    /** 周转量排名块填充:模板 1-based r4 起(全省+17市州) */
    private void fillTurnoverRankBlock(Sheet sheet, int start,
                                       Map<String, ScaleSplitTransport> cumMap, ScaleSplitTransport province) {
        if (province == null) province = new ScaleSplitTransport();
        Double provAbove = province.getAboveScaleTurnover();
        Double provBelow = province.getBelowScaleTurnover();
        Double provTotal = province.getTotalTurnover();
        Row provRow = sheet.getRow(start);
        if (provRow == null) provRow = sheet.createRow(start);
        setFreightCell(provRow, 1, provAbove);
        setFreightCell(provRow, 4, province.getAboveScaleYoy());
        setFreightCell(provRow, 6, provBelow);
        setFreightCell(provRow, 9, province.getBelowScaleYoy());
        setFreightCell(provRow, 11, provTotal);
        setFreightCell(provRow, 14, province.getTotalYoy());
        setFreightShare(provRow, provAbove, provTotal);
        int rowIdx = start + 1;
        for (String city : RegionUtil.cityList()) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) row = sheet.createRow(rowIdx);
            ScaleSplitTransport cum = cumMap.get(city);
            Double above = cum == null ? null : cum.getAboveScaleTurnover();
            Double below = cum == null ? null : cum.getBelowScaleTurnover();
            Double total = cum == null ? null : cum.getTotalTurnover();
            setFreightCell(row, 1, above);
            setFreightCell(row, 2, cum == null ? null : rankOfTurnover(cumMap, city, 0));
            setFreightCell(row, 3, ratioOf(above, provAbove));
            setFreightCell(row, 4, cum == null ? null : cum.getAboveScaleYoy());
            setFreightCell(row, 5, cum == null ? null : yoyRankOfTurnover(cumMap, city, 0));
            setFreightCell(row, 6, below);
            setFreightCell(row, 7, cum == null ? null : rankOfTurnover(cumMap, city, 1));
            setFreightCell(row, 8, ratioOf(below, provBelow));
            setFreightCell(row, 9, cum == null ? null : cum.getBelowScaleYoy());
            setFreightCell(row, 10, cum == null ? null : yoyRankOfTurnover(cumMap, city, 1));
            setFreightCell(row, 11, total);
            setFreightCell(row, 12, cum == null ? null : rankOfTurnover(cumMap, city, 2));
            setFreightCell(row, 13, ratioOf(total, provTotal));
            setFreightCell(row, 14, cum == null ? null : cum.getTotalYoy());
            setFreightCell(row, 15, cum == null ? null : yoyRankOfTurnover(cumMap, city, 2));
            setFreightShare(row, above, total);
            rowIdx++;
        }
    }
 
    /** 周转量排名 Q/R 占比:模板为缓存值,按新数据重算(规上占比 1 位小数) */
    private void setFreightShare(Row row, Double above, Double total) {
        if (total != null && total > 0 && above != null) {
            double share = round(above * 10.0 / total, 1);
            setFreightCell(row, 16, share);
            setFreightCell(row, 17, round(10.0 - share, 1));
        } else {
            setFreightCell(row, 16, (Double) null);
            setFreightCell(row, 17, (Double) null);
        }
    }
 
    // ==================== 数据加载 ====================
 
    /** 当月数据: city -> month -> record(不含全省) */
    private Map<String, Map<Integer, ScaleSplitTransport>> loadMonthData(String period) {
        Map<String, Map<Integer, ScaleSplitTransport>> result = new HashMap<>();
        List<ScaleSplitTransport> list = scaleSplitMapper.selectList(
            new LambdaQueryWrapper<ScaleSplitTransport>()
                .eq(ScaleSplitTransport::getPeriodType, "MONTH"));
        for (ScaleSplitTransport record : list) {
            if (record.getReportPeriod() == null) continue;
            if (record.getReportPeriod().compareTo(period) > 0) continue;
            String city = RegionUtil.normalizeCityName(record.getRegionName());
            if (city == null) continue;
            if ("湖北省".equals(city)) continue;
            int month = parseMonth(record.getReportPeriod());
            result.computeIfAbsent(city, k -> new HashMap<>()).put(month, record);
        }
        return result;
    }
 
    /** 累计数据: city -> 目标期及之前最近一期(不含全省) */
    private Map<String, ScaleSplitTransport> loadCumulativeMap(String period) {
        Map<String, ScaleSplitTransport> result = new HashMap<>();
        List<ScaleSplitTransport> list = scaleSplitMapper.selectList(
            new LambdaQueryWrapper<ScaleSplitTransport>()
                .eq(ScaleSplitTransport::getPeriodType, "CUMULATIVE"));
        for (ScaleSplitTransport record : list) {
            if (record.getReportPeriod() == null) continue;
            if (record.getReportPeriod().compareTo(period) > 0) continue;
            String city = RegionUtil.normalizeCityName(record.getRegionName());
            if (city == null) continue;
            if ("湖北省".equals(city)) continue;
            ScaleSplitTransport existing = result.get(city);
            if (existing == null || record.getReportPeriod().compareTo(existing.getReportPeriod()) > 0) {
                result.put(city, record);
            }
        }
        return result;
    }
 
    /** 全省当月: month -> record(忠实读取导入的全省行) */
    private Map<Integer, ScaleSplitTransport> loadProvinceMonthMap(String period) {
        Map<Integer, ScaleSplitTransport> result = new HashMap<>();
        List<ScaleSplitTransport> list = scaleSplitMapper.selectList(
            new LambdaQueryWrapper<ScaleSplitTransport>()
                .eq(ScaleSplitTransport::getPeriodType, "MONTH"));
        for (ScaleSplitTransport record : list) {
            if (record.getReportPeriod() == null) continue;
            if (record.getReportPeriod().compareTo(period) > 0) continue;
            String name = RegionUtil.normalizeCityName(record.getRegionName());
            if (!"湖北省".equals(name)) continue;
            int month = parseMonth(record.getReportPeriod());
            result.put(month, record);
        }
        return result;
    }
 
    /** 全省累计(忠实读取导入的全省行,不求和) */
    private ScaleSplitTransport getProvinceCumulative(String period) {
        List<ScaleSplitTransport> list = scaleSplitMapper.selectList(
            new LambdaQueryWrapper<ScaleSplitTransport>()
                .eq(ScaleSplitTransport::getPeriodType, "CUMULATIVE"));
        ScaleSplitTransport best = null;
        for (ScaleSplitTransport record : list) {
            if (record.getReportPeriod() == null) continue;
            if (record.getReportPeriod().compareTo(period) > 0) continue;
            String name = RegionUtil.normalizeCityName(record.getRegionName());
            if (!"湖北省".equals(name)) continue;
            if (best == null || record.getReportPeriod().compareTo(best.getReportPeriod()) > 0) best = record;
        }
        return best;
    }
 
    /** H2032规上: city -> month -> 货运量(吨) */
    private Map<String, Map<Integer, Double>> loadH2032FreightByMonth(String period) {
        Map<String, Map<Integer, Double>> result = new HashMap<>();
        String yearPrefix = period.length() >= 4 ? period.substring(0, 4) : null;
        List<H2032EnterpriseMonthly> list = h2032Mapper.selectList(null);
        for (H2032EnterpriseMonthly record : list) {
            if (record.getReportPeriod() == null || record.getReportPeriod().compareTo(period) > 0) continue;
            // H2032 表同时存有往年月报,统计当年规上值时只取与报表期同年的记录,避免去年数据叠加
            if (yearPrefix != null && !record.getReportPeriod().startsWith(yearPrefix)) continue;
            String city = RegionUtil.cityByCode(record.getRegionCode());
            if (city == null) continue;
            int month = parseMonth(record.getReportPeriod());
            double freight = record.getFreightTotal() == null ? 0.0 : record.getFreightTotal();
            result.computeIfAbsent(city, k -> new HashMap<>()).merge(month, freight, Double::sum);
        }
        return result;
    }
 
    /** H2032规上累计: city -> 货运量(吨) */
    private Map<String, Double> loadH2032FreightCumulative(String period) {
        Map<String, Double> result = new HashMap<>();
        String yearPrefix = period.length() >= 4 ? period.substring(0, 4) : null;
        List<H2032EnterpriseMonthly> list = h2032Mapper.selectList(null);
        for (H2032EnterpriseMonthly record : list) {
            if (record.getReportPeriod() == null || record.getReportPeriod().compareTo(period) > 0) continue;
            if (yearPrefix != null && !record.getReportPeriod().startsWith(yearPrefix)) continue;
            String city = RegionUtil.cityByCode(record.getRegionCode());
            if (city == null) continue;
            double freight = record.getFreightTotal() == null ? 0.0 : record.getFreightTotal();
            result.merge(city, freight, Double::sum);
        }
        return result;
    }
 
    /** H2032规上同比(需去年同期的H2032数据,暂无则为空) */
    private Map<String, Double> computeH2032Yoy(String period) {
        Map<String, Double> result = new HashMap<>();
        String lastYear = (Integer.parseInt(period.substring(0, 4)) - 1) + period.substring(4);
        Map<String, Double> current = loadH2032FreightCumulative(period);
        Map<String, Double> last = loadH2032FreightCumulative(lastYear);
        for (Map.Entry<String, Double> entry : current.entrySet()) {
            double lastVal = last.getOrDefault(entry.getKey(), 0.0);
            if (lastVal != 0.0) {
                result.put(entry.getKey(), (entry.getValue() - lastVal) / lastVal);
            }
        }
        return result;
    }
 
    /** 模板_货运量周转量: city -> 行数据(含湖北省) */
    private Map<String, FreightTurnoverImport> loadFreightTurnover(String period) {
        Map<String, FreightTurnoverImport> result = new HashMap<>();
        List<FreightTurnoverImport> list = freightTurnoverMapper.selectList(
            new LambdaQueryWrapper<FreightTurnoverImport>()
                .eq(FreightTurnoverImport::getReportPeriod, period));
        for (FreightTurnoverImport e : list) {
            String city = RegionUtil.normalizeCityName(e.getRegionName());
            if (city != null) result.put(city, e);
        }
        return result;
    }
    // ==================== 排名行写入 ====================
 
    /** 排名表双行表头 */
    private int writeRankHead(Sheet sheet, int rowIdx, String metric, String unit) {
        Row h1 = sheet.createRow(rowIdx++);
        h1.createCell(0).setCellValue("市州");
        h1.createCell(1).setCellValue("规上" + metric);
        h1.createCell(4).setCellValue("规上增速");
        h1.createCell(6).setCellValue("规下" + metric);
        h1.createCell(9).setCellValue("规下增速");
        h1.createCell(11).setCellValue("合计" + metric);
        h1.createCell(14).setCellValue("合计增速");
        h1.createCell(16).setCellValue("分市州规上规下占比");
 
        Row h2 = sheet.createRow(rowIdx++);
        h2.createCell(1).setCellValue("累计完成    (万" + unit + ")");
        h2.createCell(2).setCellValue("排名");
        h2.createCell(3).setCellValue("占全省比重");
        h2.createCell(4).setCellValue("同比");
        h2.createCell(5).setCellValue("增速    排名");
        h2.createCell(6).setCellValue("累计完成    (万" + unit + ")");
        h2.createCell(7).setCellValue("排名");
        h2.createCell(8).setCellValue("占全省比重");
        h2.createCell(9).setCellValue("同比");
        h2.createCell(10).setCellValue("增速排名");
        h2.createCell(11).setCellValue("累计完成    (万" + unit + ")");
        h2.createCell(12).setCellValue("排名");
        h2.createCell(13).setCellValue("占全省比重");
        h2.createCell(14).setCellValue("同比");
        h2.createCell(15).setCellValue("增速排名");
        return rowIdx;
    }
 
    /** 货运量排名行:规上=H2032累计、合计=模板累计、规下=合计-规上(单位:万吨) */
    private int writeFreightRankRows(Sheet sheet, int rowIdx, String city,
                                     Map<String, Double> above, Map<String, Double> below, Map<String, Double> total,
                                     Map<String, Double> aboveYoy, Map<String, Double> belowYoy, Map<String, Double> totalYoy,
                                     Double provAbove, Double provBelow, Double provTotal,
                                     Double provAboveYoy, Double provBelowYoy, Double provTotalYoy,
                                     boolean isProvince) {
        Row row = sheet.createRow(rowIdx);
        row.createCell(0).setCellValue(isProvince ? "全省" : RegionUtil.shortName(city));
 
        Double a = isProvince ? provAbove : above.get(city);
        Double b = isProvince ? provBelow : below.get(city);
        Double t = isProvince ? provTotal : total.get(city);
        Double ay = isProvince ? provAboveYoy : aboveYoy.get(city);
        Double by = isProvince ? provBelowYoy : belowYoy.get(city);
        Double ty = isProvince ? provTotalYoy : totalYoy.get(city);
 
        setNumeric(row, 1, a);
        setNumeric(row, 2, isProvince ? null : rankOfMap(above, city));
        setNumeric(row, 3, isProvince ? null : ratioOf(a, provAbove));
        setNumeric(row, 4, ay);
        setNumeric(row, 5, isProvince ? null : yoyRankOfMap(aboveYoy, city));
 
        setNumeric(row, 6, b);
        setNumeric(row, 7, isProvince ? null : rankOfMap(below, city));
        setNumeric(row, 8, isProvince ? null : ratioOf(b, provBelow));
        setNumeric(row, 9, by);
        setNumeric(row, 10, isProvince ? null : yoyRankOfMap(belowYoy, city));
 
        setNumeric(row, 11, t);
        setNumeric(row, 12, isProvince ? null : rankOfMap(total, city));
        setNumeric(row, 13, isProvince ? null : ratioOf(t, provTotal));
        setNumeric(row, 14, ty);
        setNumeric(row, 15, isProvince ? null : yoyRankOfMap(totalYoy, city));
 
        if (t != null && t > 0 && a != null) {
            double share = round(a * 10.0 / t, 1);
            setNumeric(row, 16, share);
            setNumeric(row, 17, round(10.0 - share, 1));
        }
        return rowIdx + 1;
    }
 
    private int writeTurnoverRankRows(Sheet sheet, int rowIdx, String city,
                                      Map<String, ScaleSplitTransport> cumMap,
                                      ScaleSplitTransport province,
                                      boolean isProvince) {
        if (province == null) province = new ScaleSplitTransport();
        Row row = sheet.createRow(rowIdx);
        row.createCell(0).setCellValue(isProvince ? "全省" : RegionUtil.shortName(city));
 
        ScaleSplitTransport cum = isProvince ? null : cumMap.get(city);
        Double above = isProvince ? province.getAboveScaleTurnover() : (cum == null ? null : cum.getAboveScaleTurnover());
        Double below = isProvince ? province.getBelowScaleTurnover() : (cum == null ? null : cum.getBelowScaleTurnover());
        Double total = isProvince ? province.getTotalTurnover() : (cum == null ? null : cum.getTotalTurnover());
 
        Double provinceAbove = province.getAboveScaleTurnover();
        Double provinceBelow = province.getBelowScaleTurnover();
        Double provinceTotal = province.getTotalTurnover();
 
        setNumeric(row, 1, above);
        setNumeric(row, 2, isProvince || cum == null ? null : rankOfTurnover(cumMap, city, 0));
        setNumeric(row, 3, isProvince ? null : ratioOf(above, provinceAbove));
        setNumeric(row, 4, isProvince ? province.getAboveScaleYoy() : (cum == null ? null : cum.getAboveScaleYoy()));
        setNumeric(row, 5, isProvince || cum == null ? null : yoyRankOfTurnover(cumMap, city, 0));
 
        setNumeric(row, 6, below);
        setNumeric(row, 7, isProvince || cum == null ? null : rankOfTurnover(cumMap, city, 1));
        setNumeric(row, 8, isProvince ? null : ratioOf(below, provinceBelow));
        setNumeric(row, 9, isProvince ? province.getBelowScaleYoy() : (cum == null ? null : cum.getBelowScaleYoy()));
        setNumeric(row, 10, isProvince || cum == null ? null : yoyRankOfTurnover(cumMap, city, 1));
 
        setNumeric(row, 11, total);
        setNumeric(row, 12, isProvince || cum == null ? null : rankOfTurnover(cumMap, city, 2));
        setNumeric(row, 13, isProvince ? null : ratioOf(total, provinceTotal));
        setNumeric(row, 14, isProvince ? province.getTotalYoy() : (cum == null ? null : cum.getTotalYoy()));
        setNumeric(row, 15, isProvince || cum == null ? null : yoyRankOfTurnover(cumMap, city, 2));
 
        if (total != null && total > 0 && above != null) {
            double share = round(above * 10.0 / total, 1);
            setNumeric(row, 16, share);
            setNumeric(row, 17, round(10.0 - share, 1));
        }
        return rowIdx + 1;
    }
    // ==================== 明细取值辅助 ====================
 
    /** 全省当月值:周转量取拆分表,货运量取模板(合计)/H2032(规上)/差值(规下) */
    private Double getProvinceMonthValue(Map<Integer, ScaleSplitTransport> provinceMonthMap,
                                         FreightTurnoverImport ft, int month, String metric, String scale,
                                         Map<String, Map<Integer, Double>> h2032Freight) {
        if ("turnover".equals(metric)) {
            return getMetric(provinceMonthMap.get(month), metric, scale);
        }
        if ("total".equals(scale)) return freightMonth(ft, month);
        if ("above".equals(scale)) {
            double sum = 0.0;
            for (Map<Integer, Double> byMonth : h2032Freight.values()) {
                Double v = byMonth == null ? null : byMonth.get(month);
                if (v != null) sum += v;
            }
            return round(sum / 10000.0, 4);
        }
        Double total = freightMonth(ft, month);
        Double above = getProvinceMonthValue(provinceMonthMap, ft, month, "freight", "above", h2032Freight);
        if (total == null) return null;
        return total - above;
    }
 
    /** 全省当月同比:周转量取拆分表,货运量合计取模板(左半vs右半) */
    private Double getProvinceMonthYoy(Map<Integer, ScaleSplitTransport> provinceMonthMap,
                                       FreightTurnoverImport ft, int month, String metric, String scale) {
        if ("turnover".equals(metric)) {
            return getYoyMetric(provinceMonthMap.get(month), metric, scale);
        }
        if ("total".equals(scale)) return freightYoy(ft, month);
        return null;
    }
 
    /** 全省累计:周转量取拆分表累计,货运量合计=模板1-N月之和,规上=H2032累计,规下=差值 */
    private Double getProvinceCumValue(ScaleSplitTransport provinceCum, FreightTurnoverImport ft,
                                       String metric, String scale, Map<String, Double> h2032FreightCum,
                                       int monthCount) {
        if ("turnover".equals(metric)) {
            return getMetric(provinceCum, metric, scale);
        }
        if ("total".equals(scale)) return freightCum(ft, monthCount);
        if ("above".equals(scale)) {
            double sum = 0.0;
            for (Double v : h2032FreightCum.values()) {
                if (v != null) sum += v;
            }
            return round(sum / 10000.0, 4);
        }
        Double total = freightCum(ft, monthCount);
        Double above = getProvinceCumValue(provinceCum, ft, "freight", "above", h2032FreightCum, monthCount);
        if (total == null) return null;
        return total - above;
    }
 
    /** 全省累计同比:周转量取拆分表,货运量合计=模板累计同比 */
    private Double getProvinceCumYoy(ScaleSplitTransport provinceCum, FreightTurnoverImport ft,
                                     String metric, String scale, int monthCount) {
        if ("turnover".equals(metric)) {
            return getYoyMetric(provinceCum, metric, scale);
        }
        if ("total".equals(scale)) return freightCumYoy(ft, monthCount);
        return null;
    }
 
    /** 市州当月值 */
    private Double getCityMonthValue(Map<String, Map<Integer, ScaleSplitTransport>> monthData,
                                     FreightTurnoverImport ft, String city, int month,
                                     String metric, String scale,
                                     Map<String, Map<Integer, Double>> h2032Freight) {
        if ("turnover".equals(metric)) {
            return getMetric(getRecord(monthData, city, month), metric, scale);
        }
        if ("total".equals(scale)) return freightMonth(ft, month);
        if ("above".equals(scale)) {
            Map<Integer, Double> byMonth = h2032Freight.get(city);
            Double v = byMonth == null ? null : byMonth.get(month);
            return v == null ? 0.0 : round(v / 10000.0, 4);
        }
        Double total = freightMonth(ft, month);
        Double above = getCityMonthValue(monthData, ft, city, month, "freight", "above", h2032Freight);
        if (total == null) return null;
        return total - above;
    }
 
    /** 市州当月同比 */
    private Double getCityMonthYoy(Map<String, Map<Integer, ScaleSplitTransport>> monthData,
                                   FreightTurnoverImport ft, String city, int month,
                                   String metric, String scale) {
        if ("turnover".equals(metric)) {
            return getYoyMetric(getRecord(monthData, city, month), metric, scale);
        }
        if ("total".equals(scale)) return freightYoy(ft, month);
        return null;
    }
 
    /** 市州累计 */
    private Double getCityCumValue(ScaleSplitTransport cum, FreightTurnoverImport ft,
                                   String metric, String scale, Map<String, Double> h2032FreightCum,
                                   String city, int monthCount) {
        if ("turnover".equals(metric)) {
            return cum == null ? null : getMetric(cum, metric, scale);
        }
        if ("total".equals(scale)) return freightCum(ft, monthCount);
        if ("above".equals(scale)) {
            Double v = h2032FreightCum.get(city);
            return v == null ? 0.0 : round(v / 10000.0, 4);
        }
        Double total = freightCum(ft, monthCount);
        Double above = getCityCumValue(cum, ft, "freight", "above", h2032FreightCum, city, monthCount);
        if (total == null) return null;
        return total - above;
    }
 
    /** 市州累计同比 */
    private Double getCityCumYoy(ScaleSplitTransport cum, FreightTurnoverImport ft,
                                 String metric, String scale, int monthCount) {
        if ("turnover".equals(metric)) {
            return cum == null ? null : getYoyMetric(cum, metric, scale);
        }
        if ("total".equals(scale)) return freightCumYoy(ft, monthCount);
        return null;
    }
    // ==================== 模板_货运量周转量取值 ====================
 
    private Double freightMonth(FreightTurnoverImport e, int m) {
        if (e == null) return null;
        switch (m) {
            case 1: return e.getFreightM01();
            case 2: return e.getFreightM02();
            case 3: return e.getFreightM03();
            case 4: return e.getFreightM04();
            case 5: return e.getFreightM05();
            case 6: return e.getFreightM06();
            case 7: return e.getFreightM07();
            case 8: return e.getFreightM08();
            case 9: return e.getFreightM09();
            case 10: return e.getFreightM10();
            case 11: return e.getFreightM11();
            case 12: return e.getFreightM12();
            default: return null;
        }
    }
 
    private Double lastFreightMonth(FreightTurnoverImport e, int m) {
        if (e == null) return null;
        switch (m) {
            case 1: return e.getLastFreightM01();
            case 2: return e.getLastFreightM02();
            case 3: return e.getLastFreightM03();
            case 4: return e.getLastFreightM04();
            case 5: return e.getLastFreightM05();
            case 6: return e.getLastFreightM06();
            case 7: return e.getLastFreightM07();
            case 8: return e.getLastFreightM08();
            case 9: return e.getLastFreightM09();
            case 10: return e.getLastFreightM10();
            case 11: return e.getLastFreightM11();
            case 12: return e.getLastFreightM12();
            default: return null;
        }
    }
 
    /** 模板货运量当月同比 = (今年m - 去年m) / 去年m */
    private Double freightYoy(FreightTurnoverImport e, int m) {
        Double cur = freightMonth(e, m);
        Double last = lastFreightMonth(e, m);
        if (cur == null || last == null || last == 0.0) return null;
        return (cur - last) / last;
    }
 
    /** 模板货运量累计 = 左半 1..monthCount 之和 */
    private Double freightCum(FreightTurnoverImport e, int monthCount) {
        if (e == null) return null;
        double sum = 0.0;
        boolean any = false;
        for (int m = 1; m <= monthCount; m++) {
            Double v = freightMonth(e, m);
            if (v != null) {
                sum += v;
                any = true;
            }
        }
        return any ? sum : null;
    }
 
    /** 模板去年货运量累计 = 右半 1..monthCount 之和(去年1-12月固定,取前N个月) */
    private Double lastFreightCum(FreightTurnoverImport e, int monthCount) {
        if (e == null) return null;
        double sum = 0.0;
        boolean any = false;
        for (int m = 1; m <= monthCount; m++) {
            Double v = lastFreightMonth(e, m);
            if (v != null) {
                sum += v;
                any = true;
            }
        }
        return any ? sum : null;
    }
 
    /** 模板货运量累计同比 = (今年1-N累计 - 去年1-N累计) / 去年1-N累计 */
    private Double freightCumYoy(FreightTurnoverImport e, int monthCount) {
        Double cur = freightCum(e, monthCount);
        Double last = lastFreightCum(e, monthCount);
        if (cur == null || last == null || last == 0.0) return null;
        return (cur - last) / last;
    }
    // ==================== 拆分表取值辅助 ====================
 
    private Double getMetric(ScaleSplitTransport record, String metric, String scale) {
        if (record == null) return null;
        if ("freight".equals(metric)) {
            if ("above".equals(scale)) return record.getAboveScaleFreight();
            if ("below".equals(scale)) return record.getBelowScaleFreight();
            return record.getTotalFreight();
        } else {
            if ("above".equals(scale)) return record.getAboveScaleTurnover();
            if ("below".equals(scale)) return record.getBelowScaleTurnover();
            return record.getTotalTurnover();
        }
    }
 
    private Double getYoyMetric(ScaleSplitTransport record, String metric, String scale) {
        if (record == null) return null;
        if ("above".equals(scale)) return record.getAboveScaleYoy();
        if ("below".equals(scale)) return record.getBelowScaleYoy();
        return record.getTotalYoy();
    }
 
    private ScaleSplitTransport getRecord(Map<String, Map<Integer, ScaleSplitTransport>> monthData,
                                          String city, int month) {
        Map<Integer, ScaleSplitTransport> map = monthData.get(city);
        return map == null ? null : map.get(month);
    }
 
    private Integer rankOfMap(Map<String, Double> map, String city) {
        Double self = map.get(city);
        if (self == null) return null;
        int rank = 1;
        for (Double v : map.values()) {
            if (v != null && v > self) rank++;
        }
        return rank;
    }
 
    private Integer yoyRankOfMap(Map<String, Double> map, String city) {
        Double self = map.get(city);
        if (self == null) return null;
        int rank = 1;
        for (Double v : map.values()) {
            if (v != null && v > self) rank++;
        }
        return rank;
    }
 
    private Integer rankOfTurnover(Map<String, ScaleSplitTransport> cumMap, String city, int which) {
        Double self = null;
        for (Map.Entry<String, ScaleSplitTransport> e : cumMap.entrySet()) {
            ScaleSplitTransport r = e.getValue();
            Double v = which == 0 ? r.getAboveScaleTurnover()
                : which == 1 ? r.getBelowScaleTurnover() : r.getTotalTurnover();
            if (v == null) continue;
            if (RegionUtil.normalizeCityName(e.getKey()).equals(RegionUtil.normalizeCityName(city))) self = v;
        }
        if (self == null) return null;
        int rank = 1;
        for (ScaleSplitTransport r : cumMap.values()) {
            Double v = which == 0 ? r.getAboveScaleTurnover()
                : which == 1 ? r.getBelowScaleTurnover() : r.getTotalTurnover();
            if (v != null && v > self) rank++;
        }
        return rank;
    }
 
    private Integer yoyRankOfTurnover(Map<String, ScaleSplitTransport> cumMap, String city, int which) {
        Double self = null;
        for (Map.Entry<String, ScaleSplitTransport> e : cumMap.entrySet()) {
            ScaleSplitTransport r = e.getValue();
            Double v = which == 0 ? r.getAboveScaleYoy()
                : which == 1 ? r.getBelowScaleYoy() : r.getTotalYoy();
            if (v == null) continue;
            if (RegionUtil.normalizeCityName(e.getKey()).equals(RegionUtil.normalizeCityName(city))) self = v;
        }
        if (self == null) return null;
        int rank = 1;
        for (ScaleSplitTransport r : cumMap.values()) {
            Double v = which == 0 ? r.getAboveScaleYoy()
                : which == 1 ? r.getBelowScaleYoy() : r.getTotalYoy();
            if (v != null && v > self) rank++;
        }
        return rank;
    }
 
    // ==================== 4. 生成_公路旅客分市州明细.xlsx(月报/年报) ====================
 
    /**
     * 公路旅客分市州明细
     * @param period 月报: 2026-06(今年1-6月);年报: 2026(全年12个月)
     * @param mode   month/year
     */
    /**
     * 生成_公路旅客分市州.xlsx:以 docs/公路旅客+能耗/输出 模板为底稿,
     * 保留表头/合并/列宽/公式/样式,仅替换 2026 年 1..6 月月度列数据(客运量/周转量/个体行)。
     * 2025/2024 历史列保留模板值;累计/同比/全省行公式保留并重算。
     */
    public byte[] exportPassengerCityDetail(String period, String mode) throws Exception {
        int maxMonth = monthOf(period, mode);
        int currentYear = Integer.parseInt(period.substring(0, 4));
        File template = resolvePassengerTemplate("生成_公路旅客分市州.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            fixPassengerHeaderYear(sheet, currentYear); // 表头年份动态化(不写死 2026)
            Map<Integer, Map<String, PassengerAgg>> data = loadPassengerAggMap();
            Map<Integer, Map<String, double[]>> indi = loadPassengerIndividualMap();
            int fillMonths = maxMonth; // 1..N 月(N>6 时模板自动向右扩列到 12 月)
            boolean extended = fillMonths > 6;
            if (extended) extendMonthlyColumns(sheet, 1, currentYear, 3, sheet.getLastRowNum());
            String currentRegion = null;
            for (int r = 3; r <= sheet.getLastRowNum(); r++) { // 从 R4 起:A 列合并行是区域标记(R4 全省、R8 武汉市…)
                Row row = sheet.getRow(r);
                if (row == null) continue;
                Cell nameCell = row.getCell(0);
                Cell metricCell = row.getCell(1);
                if (metricCell == null) continue;
                String areaName = cellText(nameCell);
                if (areaName != null && !areaName.trim().isEmpty()) {
                    currentRegion = "全省".equals(areaName.trim()) ? "湖北省" : RegionUtil.normalizeCityName(areaName.trim());
                }
                if (currentRegion == null) continue;
                String metric = cellText(metricCell);
                if (metric == null) continue;
                int metricIdx = cityMetricIndexOf(metric);
                if (metricIdx < 0) continue;
                String region = currentRegion;
                for (int m = 1; m <= fillMonths; m++) {
                    int col = 2 + (m - 1) * 2; // C,E,G,I,K,M,O,Q,S,U,W,Y(7-12 月为扩列列)
                    Cell c = row.getCell(col);
                    boolean keepFormula = c != null && c.getCellType() == CellType.FORMULA; // 全省/累计等公式保留
                    double v = passengerMonthVal(data, indi, currentYear, m, region, metricIdx);
                    if (!keepFormula) {
                        if (c == null) c = row.createCell(col);
                        if (v == 0.0) c.setBlank(); else c.setCellValue(round(v, 4));
                    }
                    // 同比列:库内去年同月同比(模板 #REF! 公式替换为数值,公式行也覆写)
                    double lv = passengerMonthVal(data, indi, currentYear - 1, m, region, metricIdx);
                    Cell yc = row.getCell(col + 1);
                    if (yc == null) yc = row.createCell(col + 1);
                    if (v == 0.0 || lv == 0.0) yc.setBlank(); else yc.setCellValue(round((v - lv) / lv, 4));
                }
                // 累计同比列(模板 #REF! → 库内去年 1..N 月累计同比数值)
                int cumCol = extended ? 26 : 14;
                Double cumYoy = passengerCumYoy(data, indi, currentYear, fillMonths, region, metricIdx);
                Cell yc2 = row.getCell(cumCol + 1);
                if (yc2 == null) yc2 = row.createCell(cumCol + 1);
                if (cumYoy == null) yc2.setBlank(); else yc2.setCellValue(round(cumYoy, 4));
            }
            recalc(wb);
            return toBytes(wb);
        }
    }
 
    /** 分市州表指标行:0=客运量 1=周转量 2=个体客运量 3=个体周转量;无法识别返回 -1 */
    private int cityMetricIndexOf(String metric) {
        if (metric == null) return -1;
        boolean indi = metric.contains("个体");
        if (metric.contains("客运量")) return indi ? 2 : 0;
        if (metric.contains("周转量")) return indi ? 3 : 1;
        return -1;
    }
 
    private String cellText(Cell c) {
        if (c == null) return null;
        if (c.getCellType() == CellType.STRING) return c.getStringCellValue();
        if (c.getCellType() == CellType.NUMERIC) return String.valueOf(c.getNumericCellValue());
        return null;
    }
 
    /** 个体客运量/周转量(万人/万人公里) */
    private double individualVal(Map<Integer, Map<String, double[]>> indi, int year, int month, String region, int idx) {
        Map<String, double[]> mm = indi.get(year * 100 + month);
        if (mm == null) return 0;
        double[] arr = mm.get(region);
        if (arr == null) return 0;
        return idx < arr.length ? arr[idx] : 0;
    }
 
    /** 个体客运量/周转量数据:年*100+月 -> (市州/全省 -> [个体客运量(万人), 个体周转量(万人公里)]) */
    private Map<Integer, Map<String, double[]>> loadPassengerIndividualMap() {
        Map<Integer, Map<String, double[]>> data = new HashMap<>();
        for (PassengerIndividualMonthly e : passengerIndividualMapper.selectList(null)) {
            if (e.getReportPeriod() == null) continue;
            String[] parts = e.getReportPeriod().split("-");
            if (parts.length != 2) continue;
            int y, m;
            try {
                y = Integer.parseInt(parts[0]);
                m = Integer.parseInt(parts[1]);
            } catch (NumberFormatException ex) {
                continue;
            }
            String city = RegionUtil.cityByCode(e.getRegionCode());
            if (city == null) continue;
            int key = y * 100 + m;
            double pass = nz(e.getPassengerCount()) / 10000.0;
            double turn = nz(e.getTurnover()) / 10000.0;
            double[] arr = data.computeIfAbsent(key, k -> new HashMap<>()).computeIfAbsent(city, k -> new double[2]);
            arr[0] += pass;
            arr[1] += turn;
            double[] prov = data.get(key).computeIfAbsent("湖北省", k -> new double[2]);
            prov[0] += pass;
            prov[1] += turn;
        }
        return data;
    }
 
    private PassengerAgg aggOf(Map<Integer, Map<String, PassengerAgg>> data, int year, int month, String region) {
        Map<String, PassengerAgg> monthMap = data.get(year * 100 + month);
        return monthMap == null ? null : monthMap.get(region);
    }
 
    private Double cumOf(Map<Integer, Map<String, PassengerAgg>> data, int year, String region, int monthCount, int metricIdx) {
        double sum = 0.0;
        boolean has = false;
        for (int m = 1; m <= monthCount; m++) {
            Double v = metricValue(aggOf(data, year, m, region), metricIdx);
            if (v != null) {
                sum += v;
                has = true;
            }
        }
        return has ? sum : null;
    }
 
    private Double yoy(Double cur, Double base) {
        if (cur == null || base == null || base == 0.0) return null;
        return round((cur - base) / base, 4);
    }
 
    /** 指标取值(已换算为万人/万人公里/公里;无数据返回 null) */
    private Double metricValue(PassengerAgg agg, int metric) {
        if (agg == null) return null;
        switch (metric) {
            case 0: return agg.passengerTotal / 10000.0;
            case 1: return agg.passengerTotal / 10000.0;
            case 2: return agg.passengerClass1 / 10000.0;
            case 3: return agg.passengerClass2 / 10000.0;
            case 4: return agg.passengerClass3 / 10000.0;
            case 5: return agg.passengerClass4 / 10000.0;
            case 6: return agg.passengerCharter / 10000.0;
            case 7:
            case 8:
            case 9: return null; // 城市客运模块数据,暂留空
            case 10: return agg.turnoverTotal / 10000.0;
            case 11: return agg.turnoverClass1 / 10000.0;
            case 12: return agg.turnoverClass2 / 10000.0;
            case 13: return agg.turnoverClass3 / 10000.0;
            case 14: return agg.turnoverClass4 / 10000.0;
            case 15: return agg.turnoverCharter / 10000.0;
            case 16: return ratioOrNull(agg.turnoverTotal, agg.passengerTotal);
            case 17: return ratioOrNull(agg.turnoverClass1, agg.passengerClass1);
            case 18: return ratioOrNull(agg.turnoverClass2, agg.passengerClass2);
            case 19: return ratioOrNull(agg.turnoverClass3, agg.passengerClass3);
            case 20: return ratioOrNull(agg.turnoverClass4, agg.passengerClass4);
            case 21: return ratioOrNull(agg.turnoverCharter, agg.passengerCharter);
            default: return null;
        }
    }
 
    private Double ratioOrNull(double numerator, double denominator) {
        if (denominator == 0.0) return null;
        return numerator / denominator;
    }
 
    /** 单月全市州企业汇总 */
    private static class PassengerAgg {
        double passengerTotal;
        double passengerClass1;
        double passengerClass2;
        double passengerClass3;
        double passengerClass4;
        double passengerCharter;
        double turnoverTotal;
        double turnoverClass1;
        double turnoverClass2;
        double turnoverClass3;
        double turnoverClass4;
        double turnoverCharter;
 
        void add(PassengerEnterpriseMonthly e) {
            passengerTotal += nz(e.getPassengerClass1()) + nz(e.getPassengerClass2()) + nz(e.getPassengerClass3())
                + nz(e.getPassengerClass4()) + nz(e.getPassengerCharter());
            passengerClass1 += nz(e.getPassengerClass1());
            passengerClass2 += nz(e.getPassengerClass2());
            passengerClass3 += nz(e.getPassengerClass3());
            passengerClass4 += nz(e.getPassengerClass4());
            passengerCharter += nz(e.getPassengerCharter());
            turnoverTotal += nz(e.getTurnoverClass1()) + nz(e.getTurnoverClass2()) + nz(e.getTurnoverClass3())
                + nz(e.getTurnoverClass4()) + nz(e.getTurnoverCharter());
            turnoverClass1 += nz(e.getTurnoverClass1());
            turnoverClass2 += nz(e.getTurnoverClass2());
            turnoverClass3 += nz(e.getTurnoverClass3());
            turnoverClass4 += nz(e.getTurnoverClass4());
            turnoverCharter += nz(e.getTurnoverCharter());
        }
 
        private double nz(Double v) {
            return v == null ? 0.0 : v;
        }
    }
 
 
 
    // ==================== 中口径明细/排名/分析(H203-1 公路旅客) ====================
 
    /** 加载 年*100+月 -> (市州/全省 -> 企业汇总) 数据 */
    private Map<Integer, Map<String, PassengerAgg>> loadPassengerAggMap() {
        Map<Integer, Map<String, PassengerAgg>> data = new HashMap<>();
        List<PassengerEnterpriseMonthly> list = passengerMapper.selectList(null);
        for (PassengerEnterpriseMonthly e : list) {
            if (e.getReportPeriod() == null) continue;
            String[] parts = e.getReportPeriod().split("-");
            if (parts.length != 2) continue;
            int y;
            int m;
            try {
                y = Integer.parseInt(parts[0]);
                m = Integer.parseInt(parts[1]);
            } catch (NumberFormatException ex) {
                continue;
            }
            String city = RegionUtil.cityByCode(e.getRegionCode());
            if (city == null) continue;
            int key = y * 100 + m;
            Map<String, PassengerAgg> monthMap = data.computeIfAbsent(key, k -> new HashMap<>());
            monthMap.computeIfAbsent(city, k -> new PassengerAgg()).add(e);
            monthMap.computeIfAbsent("湖北省", k -> new PassengerAgg()).add(e);
        }
        return data;
    }
 
    /**
     * 中口径明细:客运量块 + 旅客周转量块(总/公路班线/城际城乡公交/巡游出租/网约车)
     */
    /**
     * 生成_中口径明细.xlsx:以样例为底稿。数据区外部引用公式(班线/公交/出租/网约车)替换为库内数值,
     * 2025 年列保留模板缓存值(去年数据),内部公式(累计/同比/求和)保留并重算。
     */
    public byte[] exportPassengerMidDetail(String period, String mode) throws Exception {
        int maxMonth = monthOf(period, mode);
        int currentYear = Integer.parseInt(period.substring(0, 4));
        File template = resolvePassengerTemplate("生成_中口径明细.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            fixPassengerHeaderYear(sheet, currentYear); // 表头年份动态化(不写死 2026)
            Map<Integer, Map<String, double[][]>> mid = loadMidClassMap();
            int fillMonths = maxMonth; // 1..N 月(N>6 时模板自动向右扩列到 12 月)
            boolean extended = fillMonths > 6;
            if (extended) extendMonthlyColumns(sheet, 2, currentYear, 4, sheet.getLastRowNum());
            List<String> areas = midTemplateAreas();
            for (Row row : sheet) {
                if (row == null) continue;
                for (Cell c : row) {
                    if (c.getCellType() != CellType.FORMULA) continue;
                    String f = c.getCellFormula();
                    if (f == null) continue;
                    boolean crossBook = f.contains("[");
                    if (!crossBook && !isYoyMonthCol(c.getColumnIndex() + 1)) continue; // 内部公式(合计/累计)保留重算;同比列 #REF! 覆写为数值
                    if (crossBook && !is2026MonthCol(c.getColumnIndex() + 1)) {
                        keepCached(c); // 2025 年列保留模板缓存值
                        continue;
                    }
                    int r = c.getRowIndex() + 1;
                    int col = c.getColumnIndex() + 1;
                    int blockOffset = -1;
                    boolean volume = false;
                    if (r >= 5 && r <= 94) {
                        blockOffset = r - 5;
                        volume = true;
                    } else if (r >= 99 && r <= 188) {
                        blockOffset = r - 99;
                        volume = false;
                    }
                    if (blockOffset < 0) {
                        keepCached(c); // r1/r2 备注等
                        continue;
                    }
                    if (blockOffset % 5 == 0) {
                        if (isYoyMonthCol(col)) {
                            fillMidYoyCell(c, mid, currentYear, monthOfCol(col), areas.get(blockOffset / 5), -1, volume);
                        } else {
                            keepCached(c); // 总行内部公式(防御)
                        }
                        continue;
                    }
                    int k = blockOffset % 5; // 1=班线 2=公交 3=出租 4=网约车
                    String area = areas.get(blockOffset / 5);
                    if (is2026MonthCol(col)) {
                        fillMidCell(c, mid, currentYear, monthOfCol(col), area, k, volume);
                    } else if (isYoyMonthCol(col)) {
                        fillMidYoyCell(c, mid, currentYear, monthOfCol(col), area, k, volume);
                    } else {
                        keepCached(c); // 2025 年列保留模板缓存值
                    }
                }
            }
            // 累计同比列(模板 #REF! → 库内去年 1..N 月累计同比数值)
            fillMidCumYoyColumn(sheet, mid, currentYear, fillMonths, extended ? 27 : 15);
            recalc(wb);
            clearFormulaErrorsAll(wb); // 去年同期明细页残留 #REF! 同比公式 → 清空
            return toBytes(wb);
        }
    }
 
    /** 中口径明细模板地区顺序(与 RegionUtil.CITY_LIST 一致:武汉市…神农架林区) */
    private List<String> midTemplateAreas() {
        List<String> areas = new java.util.ArrayList<>();
        areas.add("湖北省");
        areas.addAll(RegionUtil.CITY_LIST);
        return areas;
    }
 
    private boolean is2026MonthCol(int col) {
        return col >= 3 && col <= 25 && col % 2 == 1;
    }
 
    /** 月同比列(1-based 偶数列 4..26) */
    private boolean isYoyMonthCol(int col) {
        return col >= 4 && col <= 26 && col % 2 == 0;
    }
 
    private int monthOfCol(int col) {
        return (col - 1) / 2;
    }
 
    /** 填中口径数据单元格:出租/网约车(3/4)显式 0,班线/公交填库内值(无值清空) */
    private void fillMidCell(Cell c, Map<Integer, Map<String, double[][]>> mid, int year, int month, String area, int k, boolean volume) {
        if (k == 3 || k == 4) {
            writeExplicitZero(c); // 出租/网约车暂无数据源,显式 0
            return;
        }
        double v = midClassVal(mid, year, month, area, k, volume);
        // POI setCellValue(double) 对公式单元格只更新缓存不移除公式,必须先 setBlank 再写值
        c.setBlank();
        if (v != 0.0) c.setCellValue(round(v, 4));
    }
 
    /** 显式写入 0(POI setCellValue(0.0) 会转 blank,需操作底层 XML) */
    private void writeExplicitZero(Cell c) {
        c.setBlank();
        if (c instanceof XSSFCell) {
            try {
                ((XSSFCell) c).getCTCell().setT(STCellType.N);
                ((XSSFCell) c).getCTCell().setV("0");
            } catch (Exception e) {
                log.warn("writeExplicitZero failed: {}", e.getMessage());
            }
        }
    }
 
    private double midClassVal(Map<Integer, Map<String, double[][]>> mid, int year, int month, String area, int k, boolean volume) {
        Map<String, double[][]> mm = mid.get(year * 100 + month);
        if (mm == null) return 0;
        double[][] arr = mm.get(area);
        if (arr == null) return 0;
        if (k < 0 || k >= arr.length) return 0;
        return volume ? arr[k][0] : arr[k][1];
    }
 
    /** 公式单元格替换为缓存数值(避免跨簿引用断链;非数值缓存置空) */
    private void keepCached(Cell c) {
        try {
            double v = 0.0;
            boolean has = false;
            if (c.getCachedFormulaResultType() == CellType.NUMERIC) {
                v = c.getNumericCellValue();
                has = true;
            }
            c.setBlank();
            if (has && v != 0.0) c.setCellValue(v);
        } catch (Exception e) {
            try { c.setBlank(); } catch (Exception ignore) { }
        }
    }
 
    /**
     * 中口径分类月度数据:年*100+月 -> (市州/全省 -> double[5][2])
     * 维度 0=总量 1=公路班线(h2031+个体) 2=城际城乡公交(cityBus) 3=巡游出租 4=网约车;[客运量(万人), 周转量(万人公里)]
     */
    private Map<Integer, Map<String, double[][]>> loadMidClassMap() {
        Map<Integer, Map<String, double[][]>> mid = new HashMap<>();
        for (PassengerEnterpriseMonthly e : passengerMapper.selectList(null)) {
            if (e.getReportPeriod() == null) continue;
            String[] parts = e.getReportPeriod().split("-");
            if (parts.length != 2) continue;
            int y, m;
            try {
                y = Integer.parseInt(parts[0]);
                m = Integer.parseInt(parts[1]);
            } catch (NumberFormatException ex) {
                continue;
            }
            String city = RegionUtil.cityByCode(e.getRegionCode());
            if (city == null) continue;
            int key = y * 100 + m;
            double pass = (nz(e.getPassengerClass1()) + nz(e.getPassengerClass2()) + nz(e.getPassengerClass3())
                + nz(e.getPassengerClass4()) + nz(e.getPassengerCharter())) / 10000.0;
            double turn = (nz(e.getTurnoverClass1()) + nz(e.getTurnoverClass2()) + nz(e.getTurnoverClass3())
                + nz(e.getTurnoverClass4()) + nz(e.getTurnoverCharter())) / 10000.0;
            addMidClass(mid, key, city, 1, pass, turn);
            addMidClass(mid, key, "湖北省", 1, pass, turn);
        }
        for (PassengerIndividualMonthly e : passengerIndividualMapper.selectList(null)) { // 个体并入公路班线/总量(09-03 口径)
            if (e.getReportPeriod() == null) continue;
            String[] parts = e.getReportPeriod().split("-");
            if (parts.length != 2) continue;
            int y, m;
            try {
                y = Integer.parseInt(parts[0]);
                m = Integer.parseInt(parts[1]);
            } catch (NumberFormatException ex) {
                continue;
            }
            String city = RegionUtil.cityByCode(e.getRegionCode());
            if (city == null) continue;
            int key = y * 100 + m;
            double pass = nz(e.getPassengerCount()) / 10000.0;
            double turn = nz(e.getTurnover()) / 10000.0;
            addMidClass(mid, key, city, 1, pass, turn);
            addMidClass(mid, key, "湖北省", 1, pass, turn);
        }
        for (CityBusMonthly b : cityBusMapper.selectList(null)) {
            if (b.getReportPeriod() == null || b.getCity() == null) continue;
            String[] parts = b.getReportPeriod().split("-");
            if (parts.length != 2) continue;
            int y, m;
            try {
                y = Integer.parseInt(parts[0]);
                m = Integer.parseInt(parts[1]);
            } catch (NumberFormatException ex) {
                continue;
            }
            String city = RegionUtil.normalizeCityName(b.getCity());
            if (city == null) continue;
            int key = y * 100 + m;
            addMidClass(mid, key, city, 2, nz(b.getPassengerChengxiang()), nz(b.getTurnoverChengxiang()));
            addMidClass(mid, key, "湖北省", 2, nz(b.getPassengerChengxiang()), nz(b.getTurnoverChengxiang()));
        }
        return mid;
    }
 
    /** 写入分类值并累加总量(维度0) */
    private void addMidClass(Map<Integer, Map<String, double[][]>> mid, int key, String city, int k, double pass, double turn) {
        double[][] arr = mid.computeIfAbsent(key, kk -> new HashMap<>()).computeIfAbsent(city, kk -> new double[5][2]);
        arr[k][0] += pass;
        arr[k][1] += turn;
        arr[0][0] += pass;
        arr[0][1] += turn;
    }
 
    private int writeMidBlock(Sheet sheet, Map<Integer, Map<String, PassengerAgg>> data, int[] years,
                              int currentYear, int maxMonth, int startRow, String title, boolean volume) {
        Row titleRow = sheet.createRow(startRow);
        titleRow.createCell(0).setCellValue(title);
        Row noteRow = sheet.createRow(startRow + 1);
        noteRow.createCell(0).setCellValue("注:中口径由公路班线、城际城乡公交、城际城乡出租、城际城乡网约车四部分构成");
        Row header = sheet.createRow(startRow + 2);
        header.createCell(0).setCellValue("地区 名称");
        header.createCell(1).setCellValue("指标");
        int col = 2;
        for (int y : years) {
            int monthCount = (y == currentYear) ? maxMonth : 12;
            for (int m = 1; m <= monthCount; m++) {
                header.createCell(col++).setCellValue(y + "年" + m + "月");
                header.createCell(col++).setCellValue(m + "月与去年同比");
            }
            header.createCell(col++).setCellValue(y + "年累计");
            header.createCell(col++).setCellValue("累计与去年同比");
        }
        sheet.createRow(startRow + 3);
 
        String[] metrics = volume ? new String[]{
            "总客运量(万人次)",
            "其中:公路班线客运量(万人次)",
            "城际城乡公交客运量(万人次)",
            "城际城乡巡游出租客运量(万人次)",
            "城际城乡网约车客运量(万人次)"
        } : new String[]{
            "总旅客周转量(万人公里)",
            "其中:公路班线旅客周转量(万人公里)",
            "城际城乡公交旅客周转量(万人公里)",
            "城际城乡巡游出租旅客周转量(万人公里)",
            "城际城乡网约车旅客周转量(万人公里)"
        };
 
        int rowIdx = startRow + 4;
        List<String> regions = new java.util.ArrayList<>();
        regions.add("湖北省");
        regions.addAll(RegionUtil.cityList());
        for (String region : regions) {
            String label = "湖北省".equals(region) ? "全省" : RegionUtil.shortName(region);
            for (int i = 0; i < metrics.length; i++) {
                Row row = sheet.createRow(rowIdx++);
                row.createCell(0).setCellValue(label);
                row.createCell(1).setCellValue(metrics[i]);
                col = 2;
                for (int y : years) {
                    int monthCount = (y == currentYear) ? maxMonth : 12;
                    for (int m = 1; m <= monthCount; m++) {
                        Double cur = midMetricValue(aggOf(data, y, m, region), i, volume);
                        setNumeric(row, col++, cur == null ? null : round(cur, 4));
                        Double base = midMetricValue(aggOf(data, y - 1, m, region), i, volume);
                        setNumeric(row, col++, yoy(cur, base));
                    }
                    Double cum = midCumOf(data, y, region, monthCount, i, volume);
                    setNumeric(row, col++, cum == null ? null : round(cum, 4));
                    Double cumBase = midCumOf(data, y - 1, region, monthCount, i, volume);
                    setNumeric(row, col++, yoy(cum, cumBase));
                }
            }
        }
        return rowIdx;
    }
 
    /** 中口径指标取值:0=总量 1=公路班线 2-4=公交/出租/网约车(城市客运模块未接入,暂空) */
    private Double midMetricValue(PassengerAgg agg, int metric, boolean volume) {
        if (agg == null) return null;
        if (volume) {
            switch (metric) {
                case 0: return agg.passengerTotal / 10000.0;
                case 1: return agg.passengerTotal / 10000.0;
                default: return null;
            }
        }
        switch (metric) {
            case 0: return agg.turnoverTotal / 10000.0;
            case 1: return agg.turnoverTotal / 10000.0;
            default: return null;
        }
    }
 
    private Double midCumOf(Map<Integer, Map<String, PassengerAgg>> data, int year, String region,
                            int monthCount, int metricIdx, boolean volume) {
        double sum = 0.0;
        boolean has = false;
        for (int m = 1; m <= monthCount; m++) {
            Double v = midMetricValue(aggOf(data, year, m, region), metricIdx, volume);
            if (v != null) {
                sum += v;
                has = true;
            }
        }
        return has ? sum : null;
    }
 
    /**
     * 中口径排名:左块=当月(客运量/周转量及排名/同比/增速排名),右块=1-N月累计(同结构)
     */
    /**
     * 生成_中口径排名.xlsx:单块累计(选 1 个月=当月,选 1-6 月=累计),RANK/SUM 公式保留。
     */
    public byte[] exportPassengerMidRank(String period, String mode) throws Exception {
        int maxMonth = monthOf(period, mode);
        int currentYear = Integer.parseInt(period.substring(0, 4));
        File template = resolvePassengerTemplate("生成_中口径排名.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            Map<Integer, Map<String, double[][]>> mid = loadMidClassMap();
            Row title = sheet.getRow(0);
            if (title != null && title.getCell(0) != null) {
                if (maxMonth == 12) {
                    title.getCell(0).setCellValue(currentYear + "年1-12月全省分市州累计完成道路客运生产情况");
                } else if (maxMonth == 1) {
                    title.getCell(0).setCellValue(currentYear + "年1月全省分市州完成道路客运生产情况");
                } else {
                    title.getCell(0).setCellValue(currentYear + "年1-" + maxMonth + "月全省分市州累计完成道路客运生产情况");
                }
            }
            // 右块(当月)标题 K1
            if (title != null && title.getCell(10) != null) {
                title.getCell(10).setCellValue(currentYear + "年" + maxMonth + "月全省分市州完成道路客运生产情况");
            }
            // 全省行 r3:B3/F3 为 SUM 公式保留;D3/H3 同比填值(0-based 列 3/7)
            setValOrBlank(sheet, 2, 3, yoyOf(midCumClass(mid, currentYear, "湖北省", maxMonth, 0, true),
                midCumClass(mid, currentYear - 1, "湖北省", maxMonth, 0, true)));
            setValOrBlank(sheet, 2, 7, yoyOf(midCumClass(mid, currentYear, "湖北省", maxMonth, 0, false),
                midCumClass(mid, currentYear - 1, "湖北省", maxMonth, 0, false)));
            // 右块全省行 N3/R3:当月同比(L3/P3 为 SUM 公式保留)
            setValOrBlank(sheet, 2, 13, yoyOf(midClassVal(mid, currentYear, maxMonth, "湖北省", 0, true),
                midClassVal(mid, currentYear - 1, maxMonth, "湖北省", 0, true)));
            setValOrBlank(sheet, 2, 17, yoyOf(midClassVal(mid, currentYear, maxMonth, "湖北省", 0, false),
                midClassVal(mid, currentYear - 1, maxMonth, "湖北省", 0, false)));
            // 市州行 r4-20:B/F 累计值、D/H 同比;C/E/G/I 排名公式保留
            List<String> cities = RegionUtil.CITY_LIST;
            for (int i = 0; i < cities.size(); i++) {
                String city = cities.get(i);
                int r0 = 3 + i;
                double pass = midCumClass(mid, currentYear, city, maxMonth, 0, true);
                double turn = midCumClass(mid, currentYear, city, maxMonth, 0, false);
                double lastPass = midCumClass(mid, currentYear - 1, city, maxMonth, 0, true);
                double lastTurn = midCumClass(mid, currentYear - 1, city, maxMonth, 0, false);
                setValOrBlank(sheet, r0, 1, pass == 0 ? null : round(pass, 4));
                setValOrBlank(sheet, r0, 3, yoyOf(pass, lastPass));
                setValOrBlank(sheet, r0, 5, turn == 0 ? null : round(turn, 4));
                setValOrBlank(sheet, r0, 7, yoyOf(turn, lastTurn));
                // 右块当月:L/P 值、N/R 当月同比(M/O/Q/S 排名公式保留)
                double mPass = midClassVal(mid, currentYear, maxMonth, city, 0, true);
                double mTurn = midClassVal(mid, currentYear, maxMonth, city, 0, false);
                double mLastPass = midClassVal(mid, currentYear - 1, maxMonth, city, 0, true);
                double mLastTurn = midClassVal(mid, currentYear - 1, maxMonth, city, 0, false);
                setValOrBlank(sheet, r0, 11, mPass == 0 ? null : round(mPass, 4));
                setValOrBlank(sheet, r0, 13, mPass == 0 ? null : yoyOf(mPass, mLastPass));
                setValOrBlank(sheet, r0, 15, mTurn == 0 ? null : round(mTurn, 4));
                setValOrBlank(sheet, r0, 17, mTurn == 0 ? null : yoyOf(mTurn, mLastTurn));
            }
            recalc(wb);
            return toBytes(wb);
        }
    }
 
    /** 写数值或清空(公式单元格保留不动) */
    private void setValOrBlank(Sheet sheet, int r0, int c0, Double v) {
        Row row = sheet.getRow(r0);
        if (row == null) return;
        Cell c = row.getCell(c0);
        if (c == null) return;
        if (c.getCellType() == CellType.FORMULA) return;
        if (v == null || v == 0.0) c.setBlank(); else c.setCellValue(v);
    }
 
    private Double yoyOf(double cur, double base) {
        if (base == 0.0) return null;
        return round((cur - base) / base, 4);
    }
 
    /** 中口径分类累计(1..monthCount 月求和) */
    private double midCumClass(Map<Integer, Map<String, double[][]>> mid, int year, String area, int monthCount, int k, boolean volume) {
        double sum = 0.0;
        boolean has = false;
        for (int m = 1; m <= monthCount; m++) {
            Map<String, double[][]> mm = mid.get(year * 100 + m);
            if (mm == null) continue;
            double[][] arr = mm.get(area);
            if (arr == null) continue;
            sum += volume ? arr[k][0] : arr[k][1];
            has = true;
        }
        return has ? sum : 0.0;
    }
 
    /**
     * 生成_中口径分析.xlsx:以样例为底稿,填累计客运量/周转量/同比,占比公式保留。
     */
    public byte[] exportPassengerMidAnalysis(String period, String mode) throws Exception {
        int maxMonth = monthOf(period, mode);
        int currentYear = Integer.parseInt(period.substring(0, 4));
        File template = resolvePassengerTemplate("生成_中口径分析.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            Map<Integer, Map<String, double[][]>> mid = loadMidClassMap();
            Row title = sheet.getRow(0);
            if (title != null && title.getCell(0) != null) {
                title.getCell(0).setCellValue(currentYear + "年" + cumRange(maxMonth) + "中口径客运量及周转量");
            }
            // r3=总 r4=班线 r5=公交 r6=出租 r7=网约车(1-based);列 B/C/F/G 填值,D/H 占比公式保留
            String[] names = {"总客运量", "公路班线", "城际城乡公交", "城际城乡巡游出租", "城际城乡网约车"};
            int[] dims = {0, 1, 2, 3, 4};
            for (int i = 0; i < names.length; i++) {
                int r0 = 2 + i;
                double pass = midCumClass(mid, currentYear, "湖北省", maxMonth, dims[i], true);
                double turn = midCumClass(mid, currentYear, "湖北省", maxMonth, dims[i], false);
                double lastPass = midCumClass(mid, currentYear - 1, "湖北省", maxMonth, dims[i], true);
                double lastTurn = midCumClass(mid, currentYear - 1, "湖北省", maxMonth, dims[i], false);
                setValOrBlank(sheet, r0, 1, pass == 0 ? null : round(pass, 4));
                setValOrBlank(sheet, r0, 2, yoyOf(pass, lastPass));
                setValOrBlank(sheet, r0, 5, turn == 0 ? null : round(turn, 4));
                setValOrBlank(sheet, r0, 6, yoyOf(turn, lastTurn));
            }
            // 下半块(当月):标题行9、数据行11-15(0-based 8、10-14)
            Row bottomTitle = sheet.getRow(8);
            if (bottomTitle != null && bottomTitle.getCell(0) != null) {
                bottomTitle.getCell(0).setCellValue(currentYear + "年" + maxMonth + "月中口径客运量及周转量");
            }
            for (int i = 0; i < names.length; i++) {
                int r0 = 10 + i;
                double pass = midClassVal(mid, currentYear, maxMonth, "湖北省", dims[i], true);
                double turn = midClassVal(mid, currentYear, maxMonth, "湖北省", dims[i], false);
                double lastPass = midClassVal(mid, currentYear - 1, maxMonth, "湖北省", dims[i], true);
                double lastTurn = midClassVal(mid, currentYear - 1, maxMonth, "湖北省", dims[i], false);
                setValOrBlank(sheet, r0, 1, pass == 0 ? null : round(pass, 4));
                setValOrBlank(sheet, r0, 2, pass == 0 ? null : yoyOf(pass, lastPass));
                setValOrBlank(sheet, r0, 5, turn == 0 ? null : round(turn, 4));
                setValOrBlank(sheet, r0, 6, turn == 0 ? null : yoyOf(turn, lastTurn));
            }
            // 下半块占比公式由样例指向上半块(B$3/F$3),改写为本块总行(B$11/F$11),行12-15
            for (int i = 1; i < names.length; i++) {
                int r0 = 10 + i;
                Row rr = sheet.getRow(r0);
                if (rr == null) continue;
                Cell dc = rr.getCell(3);
                if (dc != null && dc.getCellType() == CellType.FORMULA) {
                    dc.setCellFormula("B" + (r0 + 1) + "/B$11");
                }
                Cell hc = rr.getCell(7);
                if (hc != null && hc.getCellType() == CellType.FORMULA) {
                    hc.setCellFormula("F" + (r0 + 1) + "/F$11");
                }
            }
            recalc(wb);
            return toBytes(wb);
        }
    }
 
    private double nvlOf(Double v) {
        return v == null ? Double.NaN : v;
    }
 
    private Double numOrNull(double v) {
        return Double.isNaN(v) ? null : v;
    }
 
    /** 降序排名(无数据 NaN 排最后),返回各下标对应名次 */
    private int[] rankValues(double[] values) {
        Integer[] order = new Integer[values.length];
        for (int i = 0; i < values.length; i++) order[i] = i;
        java.util.Arrays.sort(order, (a, b) -> {
            boolean na = Double.isNaN(values[a]);
            boolean nb = Double.isNaN(values[b]);
            if (na && nb) return 0;
            if (na) return 1;
            if (nb) return -1;
            return Double.compare(values[b], values[a]);
        });
        int[] ranks = new int[values.length];
        for (int i = 0; i < order.length; i++) ranks[order[i]] = i + 1;
        return ranks;
    }
 
    // ==================== 5. 生成_能运汇总表(H204 道路货运车辆能源消耗情况) ====================
 
    /** 燃料类型码 -> 周转量单耗折算系数(千克标准煤折算) */
    private static final Map<String, Double> ENERGY_FUEL_COEFF = new HashMap<>();
 
    static {
        ENERGY_FUEL_COEFF.put("01", 0.73 * 1.4714);      // 汽油
        ENERGY_FUEL_COEFF.put("02", 0.86 * 1.4571);      // 柴油
        ENERGY_FUEL_COEFF.put("03", 1.7572);             // 压缩天然气
        ENERGY_FUEL_COEFF.put("04", 1.7572);             // 液化天然气
        ENERGY_FUEL_COEFF.put("07", 0.1229);             // 电动
        ENERGY_FUEL_COEFF.put("08", 0.3329 * 12.1951);   // 燃料电池(氢气)
    }
 
    /** 燃料码 -> 汇总表行名(与模板行顺序一致) */
    private static final String[][] ENERGY_FUEL_ROWS = {
        {"柴油车", "02"},
        {"汽油车", "01"},
        {"液化天然气车", "04"},
        {"压缩天然气车", "03"},
        {"纯电动车", "07"},
        {"燃料电池车", "08"}
    };
 
    /**
     * 能运汇总表:按燃料类型分组汇总,全省 + 各市州各一张 sheet
     */
    public byte[] exportEnergySummary(String period) throws Exception {
        List<EnergyVehicleQuarterly> list = energyMapper.selectList(
            new LambdaQueryWrapper<EnergyVehicleQuarterly>()
                .eq(EnergyVehicleQuarterly::getReportPeriod, period));
        int quarter = (parseMonth(period) + 2) / 3;
        String[] QUARTER_CN = {"一", "二", "三", "四"};
        String title = period.substring(0, 4) + "年第" + QUARTER_CN[quarter - 1] + "季度能耗汇总情况表";
 
        Map<String, FuelAgg> province = new HashMap<>();
        Map<String, Map<String, FuelAgg>> cityMap = new HashMap<>();
        for (EnergyVehicleQuarterly e : list) {
            String code = e.getFuelTypeCode();
            if (code == null || code.trim().isEmpty()) continue;
            province.computeIfAbsent(code.trim(), k -> new FuelAgg()).add(e);
            String city = RegionUtil.cityByCode(e.getRegionCode());
            if (city != null) {
                cityMap.computeIfAbsent(city, k -> new HashMap<>())
                    .computeIfAbsent(code.trim(), k2 -> new FuelAgg()).add(e);
            }
        }
 
        // 以 模板_能运汇总表 .xlsx 为底稿:保留标题/表头/合并/公式/列宽/样式,仅替换数据区数值
        File template = resolveAnyTemplate(energyTemplateDir, "生成_能运汇总表.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet base = wb.getSheetAt(0);
            base.getRow(0).getCell(0).setCellValue(title);
            fillEnergyTemplateSheet(base, province);
            for (String city : RegionUtil.cityList()) {
                Map<String, FuelAgg> agg = cityMap.get(city);
                if (agg == null || agg.isEmpty()) continue;
                Sheet s = wb.cloneSheet(0);
                wb.setSheetName(wb.getSheetIndex(s), RegionUtil.shortName(city) + "能耗汇总");
                s.getRow(0).getCell(0).setCellValue(title);
                fillEnergyTemplateSheet(s, agg);
            }
            recalc(wb);
            clearFormulaErrorsAll(wb); // 无去年数据的同比/增速公式 #DIV/0! → 清空
            return toBytes(wb);
        }
    }
 
    /** 能耗汇总模板填充:1-based 第3~8行为 柴油/汽油/液化天然/压缩天然/纯电/燃料电池;D/J/L/M 为公式列,仅填数值列 */
    private void fillEnergyTemplateSheet(Sheet sheet, Map<String, FuelAgg> data) {
        for (int i = 0; i < ENERGY_FUEL_ROWS.length; i++) {
            Row row = sheet.getRow(2 + i);
            if (row == null) row = sheet.createRow(2 + i);
            FuelAgg agg = data.get(ENERGY_FUEL_ROWS[i][1]);
            if (agg == null || agg.count == 0) {
                setEnergyValue(row, 1, null);
                setEnergyValue(row, 2, null);
                setEnergyValue(row, 4, null);
                setEnergyValue(row, 5, null);
                setEnergyValue(row, 6, null);
                setEnergyValue(row, 7, null);
                setEnergyValue(row, 8, null);
                setEnergyValue(row, 10, null);
                continue;
            }
            setEnergyValue(row, 1, (double) agg.count);
            setEnergyValue(row, 2, agg.totalTonnage);
            setEnergyValue(row, 4, agg.totalMileage);
            setEnergyValue(row, 5, agg.loadedMileage);
            setEnergyValue(row, 6, agg.emptyMileage);
            setEnergyValue(row, 7, agg.freight);
            setEnergyValue(row, 8, agg.turnover);
            setEnergyValue(row, 10, agg.fuelConsumption);
        }
    }
 
    /** 写能耗数值列:有值写入,无值清空模板样例;公式列(D/J/L/M)不动 */
    private void setEnergyValue(Row row, int colIdx, Double v) {
        Cell c = row.getCell(colIdx);
        if (c != null && c.getCellType() == org.apache.poi.ss.usermodel.CellType.FORMULA) return;
        if (c == null) c = row.createCell(colIdx);
        if (v == null) c.setBlank(); else c.setCellValue(v);
    }
 
    /** 能耗汇总:按燃料类型聚合 */
    private static class FuelAgg {
        int count;
        double totalTonnage;
        double totalMileage;
        double loadedMileage;
        double emptyMileage;
        double freight;
        double turnover;
        double fuelConsumption;
 
        void add(EnergyVehicleQuarterly e) {
            count++;
            totalTonnage += nz(e.getMarkedTonnage());
            totalMileage += nz(e.getTotalMileage());
            loadedMileage += nz(e.getLoadedMileage());
            emptyMileage += nz(e.getEmptyMileage());
            freight += nz(e.getFreight());
            turnover += nz(e.getTurnover());
            fuelConsumption += nz(e.getFuelConsumption());
        }
 
        private double nz(Double v) {
            return v == null ? 0.0 : v;
        }
    }
 
    // ==================== 通用辅助 ====================
 
    private int parseMonth(String period) {
        return Integer.parseInt(period.split("-")[1]);
    }
 
    private int monthOf(String period, String mode) {
        return "year".equalsIgnoreCase(mode) ? 12 : parseMonth(period);
    }
 
    /** 累计区间文案:1 月显示 "1月",其余显示 "1-N月"(避免出现 "1-1月") */
    private String cumRange(int month) {
        return month <= 1 ? "1月" : "1-" + month + "月";
    }
 
    /** 旅客分市州/中口径明细模板表头年份动态化:把 "2026年1月"…"2026年6月"、"2026年累计" 的年份换成所选年 */
    private void fixPassengerHeaderYear(Sheet sheet, int year) {
        java.util.regex.Pattern p = java.util.regex.Pattern.compile("^\\d{4}年");
        for (int r = 1; r <= 3; r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            for (Cell cell : row) {
                if (cell.getCellType() != CellType.STRING) continue;
                String v = cell.getStringCellValue();
                if (v == null || v.isEmpty()) continue;
                String nv = p.matcher(v).replaceFirst(year + "年");
                if (!nv.equals(v)) cell.setCellValue(nv);
            }
        }
    }
 
    private String pad(int m) {
        return m < 10 ? "0" + m : String.valueOf(m);
    }
 
    private double round(double v, int scale) {
        double factor = Math.pow(10, scale);
        return Math.round(v * factor) / factor;
    }
 
    private Double ratioOf(Double part, Double total) {
        if (part == null || total == null || total == 0.0) return null;
        return part / total;
    }
 
    private void setNumeric(Row row, int idx, Double value) {
        Cell cell = row.createCell(idx);
        if (value != null) cell.setCellValue(value);
    }
 
    private void setNumeric(Row row, int idx, Integer value) {
        if (value != null) setNumeric(row, idx, value.doubleValue());
    }
 
    private void autoWidth(Sheet sheet, int colCount) {
        for (int i = 0; i < colCount; i++) {
            sheet.setColumnWidth(i, 12 * 256);
        }
        sheet.setColumnWidth(0, 16 * 256);
        sheet.setColumnWidth(1, 22 * 256);
    }
 
 
    // ==================== 城市客运(公交)分市州明细 ====================
 
    /** 生成_城市公交客运量分市州明细.xlsx(以 城市公交_模板.xlsx 为底稿,保留模板格式与公式) */
    public byte[] exportCityBusDetail(String period, String mode) throws Exception {
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
        return exportCityByTemplate("生成_城市公交客运量分市州明细.xlsx", period, mode,
            loadCityBusByMonth(year + "-", month),
            loadCityBusByMonth((year - 1) + "-", month));
    }
    /** 加载某年 1..limit 月城市公交企业数据,按 月 → 市州 → [客运量,周转量,城市内客运量,城市内周转量] 汇总;"全省" 为全部市州合计 */
    private Map<Integer, Map<String, double[]>> loadCityBusByMonth(String yearPrefix, int limit) {
        Map<Integer, Map<String, double[]>> monthCity = new java.util.LinkedHashMap<>();
        List<CityBusMonthly> rows = cityBusMapper.selectList(
            new LambdaQueryWrapper<CityBusMonthly>()
                .likeRight(CityBusMonthly::getReportPeriod, yearPrefix));
        for (CityBusMonthly r : rows) {
            int m = monthOf(r.getReportPeriod());
            if (m < 1 || m > limit) continue;
            String city = r.getCity() == null ? "未知" : r.getCity();
            Map<String, double[]> cityMap = monthCity.computeIfAbsent(m, k -> new HashMap<>());
            double[] arr = cityMap.computeIfAbsent(city, k -> new double[4]);
            arr[0] += nz(r.getPassengerVolume());
            arr[1] += nz(r.getTurnover());
            arr[2] += nz(r.getPassengerCity());
            arr[3] += nz(r.getTurnoverCity());
            double[] prov = cityMap.computeIfAbsent("全省", k -> new double[4]);
            prov[0] += nz(r.getPassengerVolume());
            prov[1] += nz(r.getTurnover());
            prov[2] += nz(r.getPassengerCity());
            prov[3] += nz(r.getTurnoverCity());
        }
        return monthCity;
    }
 
    private int monthOf(String reportPeriod) {
        if (reportPeriod == null) return 0;
        String[] parts = reportPeriod.split("-");
        if (parts.length < 2) return 0;
        try {
            return Integer.parseInt(parts[1]);
        } catch (Exception e) {
            return 0;
        }
    }
 
    private double cityBusVal(Map<Integer, Map<String, double[]>> monthCity, String area, int m, int idx) {
        Map<String, double[]> cityMap = monthCity.get(m);
        if (cityMap == null) return 0;
        double[] arr = cityMap.get(area);
        if (arr == null) return 0;
        return idx < arr.length ? arr[idx] : 0;
    }
 
    // ==================== 城市客运(巡游出租)分市州明细 ====================
 
    /** 生成_巡游出租客运量分市州明细.xlsx(以 巡游出租_模板.xlsx 为底稿,保留模板格式与公式) */
    public byte[] exportCityTaxiDetail(String period, String mode) throws Exception {
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
        return exportCityByTemplate("生成_巡游出租客运量分市州明细.xlsx", period, mode,
            loadCityTaxiByMonth(year + "-", month),
            loadCityTaxiByMonth((year - 1) + "-", month));
    }
    /** 加载某年 1..limit 月巡游出租数据,按 月 → 市州 → [客运量,周转量,城市内客运量,城市内周转量] 汇总;"全省" 为全部市州合计 */
    private Map<Integer, Map<String, double[]>> loadCityTaxiByMonth(String yearPrefix, int limit) {
        Map<Integer, Map<String, double[]>> monthCity = new java.util.LinkedHashMap<>();
        List<CityTaxiMonthly> rows = cityTaxiMapper.selectList(
            new LambdaQueryWrapper<CityTaxiMonthly>()
                .likeRight(CityTaxiMonthly::getReportPeriod, yearPrefix));
        for (CityTaxiMonthly r : rows) {
            int m = monthOf(r.getReportPeriod());
            if (m < 1 || m > limit) continue;
            String city = r.getCity() == null ? "未知" : r.getCity();
            Map<String, double[]> cityMap = monthCity.computeIfAbsent(m, k -> new HashMap<>());
            double[] arr = cityMap.computeIfAbsent(city, k -> new double[4]);
            arr[0] += nz(r.getPassengerVolume());
            arr[1] += nz(r.getTurnover());
            arr[2] += nz(r.getPassengerCity());
            arr[3] += nz(r.getTurnoverCity());
            double[] prov = cityMap.computeIfAbsent("全省", k -> new double[4]);
            prov[0] += nz(r.getPassengerVolume());
            prov[1] += nz(r.getTurnover());
            prov[2] += nz(r.getPassengerCity());
            prov[3] += nz(r.getTurnoverCity());
        }
        return monthCity;
    }
 
    private double cityTaxiVal(Map<Integer, Map<String, double[]>> monthCity, String area, int m, int idx) {
        Map<String, double[]> cityMap = monthCity.get(m);
        if (cityMap == null) return 0;
        double[] arr = cityMap.get(area);
        if (arr == null) return 0;
        return idx < arr.length ? arr[idx] : 0;
    }
 
    // ==================== 城市客运(轨道/轮渡)分市州明细 ====================
 
    /** 生成_轨道轮渡客运量分市州明细.xlsx(以 轨道、轮渡_模板.xlsx 为底稿,轨道=全省/武汉/黄石,轮渡=全省/武汉) */
    public byte[] exportCityRailFerryDetail(String period, String mode) throws Exception {
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
        Map<Integer, Map<String, double[]>> monthCity = loadCityRailFerryByMonth(year + "-", month);
        File template = resolveTemplate("生成_轨道轮渡客运量分市州明细.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            dynamicTitle(sheet, year, month); // 标题按报表期动态化(不写死年份/月份)
            // 轨道:客运量行 4(全省)/6(武汉)/8(黄石),周转量行为 +1;指标 0=轨道客运量 1=轨道周转量
            fillRailFerryBlock(sheet, monthCity, month, new String[][]{{"全省", "3"}, {"武汉市", "5"}, {"黄石市", "7"}}, 0);
            // 轮渡:客运量行 13(全省)/15(武汉);指标 2=轮渡客运量 3=轮渡周转量
            fillRailFerryBlock(sheet, monthCity, month, new String[][]{{"全省", "12"}, {"武汉市", "14"}}, 2);
            recalc(wb);
            clearFormulaErrorCells(sheet, 3, sheet.getLastRowNum(), 1, 39);
            fixCityMonthHeaderText(sheet);
            return toBytes(wb);
        }
    }
 
    /** 加载某年 1..limit 月轨道/轮渡数据:月 → 市州 → [轨道客运量,轨道周转量,轮渡客运量,轮渡周转量],含"全省" */
    private Map<Integer, Map<String, double[]>> loadCityRailFerryByMonth(String yearPrefix, int limit) {
        Map<Integer, Map<String, double[]>> monthCity = new java.util.LinkedHashMap<>();
        List<CityBusMonthly> rows = cityBusMapper.selectList(
            new LambdaQueryWrapper<CityBusMonthly>().likeRight(CityBusMonthly::getReportPeriod, yearPrefix));
        for (CityBusMonthly r : rows) {
            int m = monthOf(r.getReportPeriod());
            if (m < 1 || m > limit) continue;
            String city = r.getCity() == null ? "未知" : r.getCity();
            Map<String, double[]> cityMap = monthCity.computeIfAbsent(m, k -> new HashMap<>());
            double[] arr = cityMap.computeIfAbsent(city, k -> new double[4]);
            arr[0] += nz(r.getRailPassengerVolume());
            arr[1] += nz(r.getRailTurnover());
            arr[2] += nz(r.getFerryPassengerVolume());
            arr[3] += nz(r.getFerryTurnover());
            double[] prov = cityMap.computeIfAbsent("全省", k -> new double[4]);
            prov[0] += nz(r.getRailPassengerVolume());
            prov[1] += nz(r.getRailTurnover());
            prov[2] += nz(r.getFerryPassengerVolume());
            prov[3] += nz(r.getFerryTurnover());
        }
        return monthCity;
    }
 
    /** 填充轨道/轮渡块:baseIdx 为客运量指标,周转量指标 = baseIdx+1;2026 月度列 C..O,2025 列 T.. 无数据则清空 */
    private void fillRailFerryBlock(Sheet sheet, Map<Integer, Map<String, double[]>> monthCity, int month,
                                    String[][] areaRows, int baseIdx) {
        for (String[] ar : areaRows) {
            String area = ar[0];
            int paxRow = Integer.parseInt(ar[1]);
            for (int m = 1; m <= month; m++) {
                setDataCell(sheet, paxRow, 2 + (m - 1) * 2, railFerryVal(monthCity, area, m, baseIdx));
                setDataCell(sheet, paxRow + 1, 2 + (m - 1) * 2, railFerryVal(monthCity, area, m, baseIdx + 1));
            }
            for (int m = 1; m <= 12; m++) {
                setDataCell(sheet, paxRow, 19 + (m - 1) * 2, 0);
                setDataCell(sheet, paxRow + 1, 19 + (m - 1) * 2, 0);
            }
        }
    }
 
    private double railFerryVal(Map<Integer, Map<String, double[]>> monthCity, String area, int m, int idx) {
        Map<String, double[]> cityMap = monthCity.get(m);
        if (cityMap == null) return 0;
        double[] arr = cityMap.get(area);
        if (arr == null) return 0;
        return idx < arr.length ? arr[idx] : 0;
    }
 
    /** 写数据单元格:原为公式则保留(全省求和/排名公式);否则有值写入、无值清空模板样例 */
    private void setDataCell(Sheet sheet, int rowIdx, int colIdx, double v) {
        Row row = sheet.getRow(rowIdx);
        if (row == null) row = sheet.createRow(rowIdx);
        Cell c = row.getCell(colIdx);
        if (c != null && c.getCellType() == org.apache.poi.ss.usermodel.CellType.FORMULA) return;
        if (c == null) c = row.createCell(colIdx);
        if (v == 0) c.setBlank(); else c.setCellValue(round(v, 2));
    }
 
    /** 显式写入 0(setDataCell 对 0 置空;汇总表网约车行需要显示 0) */
    private void setZeroCell(Sheet sheet, int rowIdx, int colIdx) {
        Row row = sheet.getRow(rowIdx);
        if (row == null) row = sheet.createRow(rowIdx);
        Cell c = row.getCell(colIdx);
        if (c != null && c.getCellType() == org.apache.poi.ss.usermodel.CellType.FORMULA) return;
        if (c == null) c = row.createCell(colIdx);
        c.setCellValue(0.0);
    }
    /** 重算公式缓存值(保留公式,求值异常忽略) */
    private void recalc(XSSFWorkbook wb) {
        try {
            wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
        } catch (Exception e) {
            log.warn("城市客运模板公式求值失败: {}", e.getMessage());
        }
    }
 
    // ==================== 城市客运 分市州累计(城市分市州_模板 / 城市客运各市州明细表_模板) ====================
 
    /** 生成_城市客运客运量分市州累计.xlsx(以 城市分市州_模板.xlsx / 城市客运各市州明细表_模板.xlsx 为底稿;网约车列无源数据留空) */
    public byte[] exportCityPassengerCitySum(String templateFileName, String period, String mode) throws Exception {
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
        Map<String, double[]> cum = loadCityPassengerCumulative(year + "-", month);
        Map<String, double[]> monthMap = loadCityPassengerMonthMap(year + "-", month);
        File template = resolveTemplate(templateFileName);
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            dynamicTitle(sheet, year, month); // 标题按报表期动态化
            List<String> areas = new java.util.ArrayList<>();
            areas.add("全省");
            areas.addAll(RegionUtil.cityList());
            fillCitySumBlock(sheet, cum, areas, 4, 0);   // 累计客运量区(单位:万人次,POI 0 基,行5-22)
            fillCitySumBlock(sheet, cum, areas, 26, 1);  // 累计周转量区(单位:万人次公里,POI 0 基,行27-44)
            // 当月块标题(0基行44/66)与数据(0基行48-65/70-87)
            Row pTitle = sheet.getRow(44);
            if (pTitle != null && pTitle.getCell(0) != null) {
                pTitle.getCell(0).setCellValue("城市客运客运量分市州" + month + "月情况");
            }
            Row tTitle = sheet.getRow(66);
            if (tTitle != null && tTitle.getCell(0) != null) {
                tTitle.getCell(0).setCellValue("城市客运客运周转量分市州" + month + "月情况");
            }
            fillCitySumBlock(sheet, monthMap, areas, 48, 0);  // 当月客运量区(行49-66)
            fillCitySumBlock(sheet, monthMap, areas, 70, 1);  // 当月周转量区(行71-88)
            // 当月块排名/占比公式由累计块复制而来,RANK 范围与占比分母需改指当月块自身行
            repairCityMonthRankShare(sheet, 50, 66, 49);
            repairCityMonthRankShare(sheet, 72, 88, 71);
            recalc(wb);
            clearFormulaErrorsAll(wb); // 无去年累计的占比/增速公式 #DIV/0! → 清空
            return toBytes(wb);
        }
    }
 
    /** 填充一个累计区:startRow 起每地区 1 行;paxTurnIdx=0 用客运量、1 用周转量 */
    private void fillCitySumBlock(Sheet sheet, Map<String, double[]> cum, List<String> areas, int startRow, int paxTurnIdx) {
        int rowIdx = startRow;
        for (String area : areas) {
            double[] v = cum.get(area);
            double bus = v == null ? 0 : v[paxTurnIdx == 0 ? 0 : 1];
            double taxi = v == null ? 0 : v[paxTurnIdx == 0 ? 2 : 3];
            double rail = v == null ? 0 : v[paxTurnIdx == 0 ? 4 : 5];
            double ferry = v == null ? 0 : v[paxTurnIdx == 0 ? 6 : 7];
            double total = bus + taxi + rail + ferry; // 网约车无源数据,不计
            setDataCell(sheet, rowIdx, 1, total);   // B 总累计
            setDataCell(sheet, rowIdx, 6, bus);     // G 公交
            setDataCell(sheet, rowIdx, 11, taxi);   // L 出租
            setDataCell(sheet, rowIdx, 16, 0);      // Q 网约车(无源数据→清空样例)
            setDataCell(sheet, rowIdx, 21, rail);   // V 轨道
            setDataCell(sheet, rowIdx, 23, ferry);  // X 轮渡
            // 增速列 E/J/O/T/W/Y:无去年同期累计 → 清空模板样例(排名/占比公式保留)
            setDataCell(sheet, rowIdx, 4, 0);
            setDataCell(sheet, rowIdx, 9, 0);
            setDataCell(sheet, rowIdx, 14, 0);
            setDataCell(sheet, rowIdx, 19, 0);
            setDataCell(sheet, rowIdx, 22, 0);
            setDataCell(sheet, rowIdx, 24, 0);
            rowIdx++;
        }
    }
 
    /** 当月块排名/占比公式修正:模板当月块由累计块复制而来,RANK 范围与占比分母需改指当月块自身行 */
    private void repairCityMonthRankShare(Sheet sheet, int firstDataRow1, int lastDataRow1, int provRow1) {
        String[] groups = {"B", "G", "L", "Q"};
        String[] growth = {"E", "J", "O", "T"};
        for (int r = firstDataRow1; r <= lastDataRow1; r++) {
            int r0 = r - 1;
            for (int g = 0; g < 4; g++) {
                String base = groups[g];
                String grow = growth[g];
                setFormulaIfFormula(sheet, r0, 2 + g * 5,
                    "RANK(" + base + r + "," + base + "$" + firstDataRow1 + ":" + base + "$" + lastDataRow1 + ")");
                setFormulaIfFormula(sheet, r0, 3 + g * 5, base + r + "/" + base + "$" + provRow1);
                setFormulaIfFormula(sheet, r0, 5 + g * 5,
                    "RANK(" + grow + r + "," + grow + "$" + firstDataRow1 + ":" + grow + "$" + lastDataRow1 + ")");
            }
        }
    }
 
    /** 单元格原为公式时改写公式(排名/占比列),非公式单元格不动(保持模板式样) */
    private void setFormulaIfFormula(Sheet sheet, int rowIdx, int colIdx, String formula) {
        Row row = sheet.getRow(rowIdx);
        if (row == null) return;
        Cell c = row.getCell(colIdx);
        if (c == null || c.getCellType() != CellType.FORMULA) return;
        c.setCellFormula(formula);
    }
 
    /** 某年 1..limit 月城市客运累计:city → [公交客运量,公交周转量,出租客运量,出租周转量,轨道客运量,轨道周转量,轮渡客运量,轮渡周转量],含"全省" */
    private Map<String, double[]> loadCityPassengerCumulative(String yearPrefix, int limit) {
        Map<String, double[]> cum = new HashMap<>();
        for (CityBusMonthly r : cityBusMapper.selectList(
                new LambdaQueryWrapper<CityBusMonthly>().likeRight(CityBusMonthly::getReportPeriod, yearPrefix))) {
            int m = monthOf(r.getReportPeriod());
            if (m < 1 || m > limit) continue;
            String city = r.getCity() == null ? "未知" : r.getCity();
            double[] a = cum.computeIfAbsent(city, k -> new double[8]);
            a[0] += nz(r.getPassengerVolume());
            a[1] += nz(r.getTurnover());
            a[4] += nz(r.getRailPassengerVolume());
            a[5] += nz(r.getRailTurnover());
            a[6] += nz(r.getFerryPassengerVolume());
            a[7] += nz(r.getFerryTurnover());
            double[] p = cum.computeIfAbsent("全省", k -> new double[8]);
            p[0] += nz(r.getPassengerVolume());
            p[1] += nz(r.getTurnover());
            p[4] += nz(r.getRailPassengerVolume());
            p[5] += nz(r.getRailTurnover());
            p[6] += nz(r.getFerryPassengerVolume());
            p[7] += nz(r.getFerryTurnover());
        }
        for (CityTaxiMonthly r : cityTaxiMapper.selectList(
                new LambdaQueryWrapper<CityTaxiMonthly>().likeRight(CityTaxiMonthly::getReportPeriod, yearPrefix))) {
            int m = monthOf(r.getReportPeriod());
            if (m < 1 || m > limit) continue;
            String city = r.getCity() == null ? "未知" : r.getCity();
            double[] a = cum.computeIfAbsent(city, k -> new double[8]);
            a[2] += nz(r.getPassengerVolume());
            a[3] += nz(r.getTurnover());
            double[] p = cum.computeIfAbsent("全省", k -> new double[8]);
            p[2] += nz(r.getPassengerVolume());
            p[3] += nz(r.getTurnover());
        }
        return cum;
    }
 
    /** 某年单月城市客运数据:city → 8 维数组(口径同累计),含"全省";= month 累计 - (month-1) 累计 */
    private Map<String, double[]> loadCityPassengerMonthMap(String yearPrefix, int month) {
        Map<String, double[]> cum = loadCityPassengerCumulative(yearPrefix, month);
        if (month <= 1) return cum;
        Map<String, double[]> pre = loadCityPassengerCumulative(yearPrefix, month - 1);
        Map<String, double[]> out = new HashMap<>();
        for (Map.Entry<String, double[]> e : cum.entrySet()) {
            double[] prev = pre.get(e.getKey());
            double[] d = new double[8];
            for (int i = 0; i < 8; i++) {
                d[i] = e.getValue()[i] - (prev == null ? 0 : prev[i]);
            }
            out.put(e.getKey(), d);
        }
        return out;
    }
 
    // ==================== 城市客运 全省汇总(城市汇总_模板) ====================
 
    /** 生成_城市客运汇总.xlsx(以 城市汇总_模板.xlsx 为底稿;2026 段填库内 1..N 月累计,同比清空,2025/2024 段保留模板历史参考值) */
    public byte[] exportCityPassengerSummary(String period, String mode) throws Exception {
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
        double[] prov = loadCityPassengerCumulative(year + "-", month).getOrDefault("全省", new double[8]);
        double busPax = prov[0], busTurn = prov[1];
        double taxiPax = prov[2], taxiTurn = prov[3];
        double railPax = prov[4], railTurn = prov[5];
        double ferryPax = prov[6], ferryTurn = prov[7];
        File template = resolveTemplate("生成_城市客运汇总.xlsx");
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            dynamicTitle(sheet, year, month); // 标题按报表期动态化
            // 行3 总客运量=公交+出租车(巡游+网约车)+轨道+轮渡;行4 公交;行5 出租车=巡游+网约车;行6 巡游;行7 网约车;行8 轨道;行9 轮渡
            setDataCell(sheet, 2, 1, busPax + taxiPax + railPax + ferryPax); // B3(POI 0 基行2)
            setDataCell(sheet, 2, 3, busTurn + taxiTurn + railTurn + ferryTurn); // D3
            setDataCell(sheet, 3, 1, busPax);   // B4 城市公交
            setDataCell(sheet, 3, 3, busTurn);  // D4
            setDataCell(sheet, 4, 1, taxiPax);  // B5 城市出租车(网约车无源数据)
            setDataCell(sheet, 4, 3, taxiTurn); // D5
            setDataCell(sheet, 5, 1, taxiPax);  // B6 其中巡游出租
            setDataCell(sheet, 5, 3, taxiTurn); // D6
            setZeroCell(sheet, 6, 1);           // B7 城市网约车(显式 0,setDataCell 对 0 会置空)
            setZeroCell(sheet, 6, 3);           // D7
            setDataCell(sheet, 7, 1, railPax);  // B8 轨道
            setDataCell(sheet, 7, 3, railTurn); // D8
            setDataCell(sheet, 8, 1, ferryPax); // B9 轮渡
            setDataCell(sheet, 8, 3, ferryTurn);// D9
            for (int r = 2; r <= 8; r++) {      // 同比 C/E:无去年同期 → 清空样例
                setDataCell(sheet, r, 2, 0);
                setDataCell(sheet, r, 4, 0);
            }
            // 底部单月表(新版式):标题行11(0基10)、数据行13-19(0基12-18),填当月完成量
            Row mTitle = sheet.getRow(10);
            if (mTitle != null && mTitle.getCell(0) != null) {
                mTitle.getCell(0).setCellValue(year + "年" + month + "月全省完成城市客运量情况");
            }
            double[] mProv = loadCityPassengerMonthMap(year + "-", month).getOrDefault("全省", new double[8]);
            double mBusPax = mProv[0], mBusTurn = mProv[1];
            double mTaxiPax = mProv[2], mTaxiTurn = mProv[3];
            double mRailPax = mProv[4], mRailTurn = mProv[5];
            double mFerryPax = mProv[6], mFerryTurn = mProv[7];
            setDataCell(sheet, 12, 1, mBusPax + mTaxiPax + mRailPax + mFerryPax); // B13 总客运量
            setDataCell(sheet, 12, 3, mBusTurn + mTaxiTurn + mRailTurn + mFerryTurn); // D13 总周转量
            setDataCell(sheet, 13, 1, mBusPax);   // B14 城市公交
            setDataCell(sheet, 13, 3, mBusTurn);  // D14
            setDataCell(sheet, 14, 1, mTaxiPax);  // B15 城市出租车
            setDataCell(sheet, 14, 3, mTaxiTurn); // D15
            setDataCell(sheet, 15, 1, mTaxiPax);  // B16 其中巡游出租
            setDataCell(sheet, 15, 3, mTaxiTurn); // D16
            setZeroCell(sheet, 16, 1);            // B17 城市网约车
            setZeroCell(sheet, 16, 3);            // D17
            setDataCell(sheet, 17, 1, mRailPax);  // B18 轨道
            setDataCell(sheet, 17, 3, mRailTurn); // D18
            setDataCell(sheet, 18, 1, mFerryPax); // B19 轮渡
            setDataCell(sheet, 18, 3, mFerryTurn);// D19
            for (int r = 12; r <= 18; r++) {      // 单月表 C/E 同比:无上年单月源 → 清空样例
                setDataCell(sheet, r, 2, 0);
                setDataCell(sheet, r, 4, 0);
            }
            recalc(wb);
            clearFormulaErrorsAll(wb); // 无去年数据的同比公式 #DIV/0! → 清空
            return toBytes(wb);
        }
    }
 
    // ==================== 城市客运 报表系列(一键打包) ====================
 
    /** 生成_城市客运报表系列.zip:公交/出租/轨道轮渡明细 + 分市州累计 + 各市州明细表 + 汇总 */
    public byte[] exportCityPassengerSeries(String period, String mode) throws Exception {
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        try (java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(bos)) {
            addZipEntry(zos, "生成_城市公交客运量分市州明细.xlsx", exportCityBusDetail(period, mode));
            addZipEntry(zos, "生成_巡游出租客运量分市州明细.xlsx", exportCityTaxiDetail(period, mode));
            addZipEntry(zos, "生成_轨道轮渡客运量分市州明细.xlsx", exportCityRailFerryDetail(period, mode));
            addZipEntry(zos, "生成_城市客运客运量分市州累计.xlsx", exportCityPassengerCitySum("生成_城市客运客运量分市州累计.xlsx", period, mode));
            addZipEntry(zos, "生成_城市客运各市州明细表.xlsx", exportCityPassengerCitySum("生成_城市客运各市州明细表.xlsx", period, mode));
            addZipEntry(zos, "生成_城市客运汇总.xlsx", exportCityPassengerSummary(period, mode));
        }
        return bos.toByteArray();
    }
 
    /** 生成_货运报表系列.zip:货运量分市州明细 + 货运量排名 + 周转量排名 */
    public byte[] exportFreightSeries(String period, String mode) throws Exception {
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        try (java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(bos)) {
            addZipEntry(zos, "生成_货运量分市州明细.xlsx", exportCityDetail(period, mode));
            addZipEntry(zos, "生成_货运量排名.xlsx", exportFreightRank(period, mode));
            addZipEntry(zos, "生成_周转量排名.xlsx", exportTurnoverRank(period, mode));
        }
        return bos.toByteArray();
    }
 
    /** 生成_公路旅客报表系列.zip:分市州明细 + 中口径明细/排名/分析 */
    public byte[] exportPassengerSeries(String period, String mode) throws Exception {
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        try (java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(bos)) {
            addZipEntry(zos, "生成_公路旅客分市州.xlsx", exportPassengerCityDetail(period, mode));
            addZipEntry(zos, "生成_中口径明细.xlsx", exportPassengerMidDetail(period, mode));
            addZipEntry(zos, "生成_中口径排名.xlsx", exportPassengerMidRank(period, mode));
            addZipEntry(zos, "生成_中口径分析.xlsx", exportPassengerMidAnalysis(period, mode));
        }
        return bos.toByteArray();
    }
 
    /**
     * 生成_道路运输量汇总表.xlsx(P0-P1 骨架):整本复制母版《YYYY年M月道路运输量汇总表.xlsx》,
     * 保留全部页签/版式/公式/2025 年缓存值,仅把表头年份动态化为目标年;数据填充按 P2 起逐步接入。
     */
    public byte[] exportSummaryWorkbook(String period, String mode) throws Exception {
        return exportSummaryWorkbook(period, mode, null);
    }
 
    /** 生成_道路运输量汇总表.xlsx(P0-P1 骨架,支持 toMonth 裁剪):
     *  以《YYYY年M月道路运输量汇总表.xlsx》(M 由 period 决定)为母版,按同期备份母版还原月度块;
     *  toMonth 非空且小于 M 时,把 8 个台账/合成页裁到 toMonth 月(toMonth+1..M 的当月值/同比列清空、保留表头,
     *  累计与累计同比公式改为按 toMonth 月重算),用于“先生成 1-6 月、1-7 月扩列”验证。 */
    public byte[] exportSummaryWorkbook(String period, String mode, Integer toMonth) throws Exception {
        int year = Integer.parseInt(period.contains("-") ? period.split("-")[0] : period);
        int month = period.contains("-") ? monthOf(period, mode) : 12;
        File template = resolveSummaryMother(period, mode);
        if (template == null) {
            throw new RuntimeException("未找到汇总表母版《" + year + "年" + month + "月道路运输量汇总表.xlsx》(docs/生成汇总大表)");
        }
        double savedZipRatio = ZipSecureFile.getMinInflateRatio();
        ZipSecureFile.setMinInflateRatio(0.0001); // 新版母版 styles.xml 由 openpyxl 高压缩(0.0099<0.01)触发 POI Zip-bomb 误报,读取后还原
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            File donor = resolveSummaryDonor(year, month);
            if (donor != null) {
                try (InputStream din = new FileInputStream(donor); XSSFWorkbook dwb = new XSSFWorkbook(din)) {
                    int restored = restoreSummaryMonthlyBlocks(wb, dwb);
                    log.info("汇总工作簿月度块还原完成,补齐/覆盖单元格数={}(母版:{},备份母版:{})", restored, template.getName(), donor.getName());
                }
            } else {
                log.warn("汇总工作簿未找到同期备份母版(_备份_{}年{}月…),输出将保持母版现状(可能为 1-4 月骨架)", year, month);
            }
            if (toMonth != null && toMonth > 0 && toMonth < month) {
                int trimmed = trimSummaryMonthlySheets(wb, toMonth);
                log.info("汇总工作簿裁剪至 {} 月完成,清空/改写单元格数={}", toMonth, trimmed);
            }
            dynamicSummaryYear(wb, year);
            cleanSummarySheetPresentation(wb); // 取消各页筛选、还原隐藏行(否则“班线包车”等页看起来像缺数据)
            int effMonth = (toMonth != null && toMonth > 0 && toMonth < month) ? toMonth : month;
            List<String> dbProblems = new ArrayList<>();
            String effPeriod = String.format("%04d-%02d", year, effMonth);
            int dbFilled = summaryWorkbookFiller.fillMonthlyLedger(wb, effPeriod, year, effMonth, dbProblems);
            int yoyFilled = summaryWorkbookFiller.ensureMonthYoyFormulas(wb, year, effMonth);
            int monthFormulaFilled = summaryWorkbookFiller.ensureMonthFormulas(wb, year, effMonth);
            int rankRefreshed = refreshSummaryRankSheets(wb, effPeriod, "month");
            int cumulativeRefreshed = refreshSummaryCumulativeSheets(wb, year, effMonth); // 四张跨页合成页随报表期刷新
            int widthApplied = applySummaryReferenceLayout(wb);
            log.info("汇总工作簿数据驱动回填完成:写入当月值格数={},补写当月同比公式格数={},补写当月合计公式格数={},"
                    + "刷新排名页数值格数={},刷新四页(城市汇总/城市分市州/中口径分析/道路运输周转量)数值格数={},"
                    + "按《生成_道路运输量汇总表》对齐列宽列数={},缺数提示={}",
                    dbFilled, yoyFilled, monthFormulaFilled, rankRefreshed, cumulativeRefreshed, widthApplied, dbProblems);
            applyTwoDecimalFormat(wb); // 数值显示两位小数(保留全精度);% / 日期等既有样式不改变
            int inlineFixed = normalizeStaleInlineStrings(wb); // 清掉「非 inlineStr 却残留 <is>」的非法格,否则 Excel 拒绝打开
            if (inlineFixed > 0) log.warn("汇总工作簿清掉残留 inlineStr 非法格 {} 个(母版由 openpyxl 写出时的典型问题)", inlineFixed);
            int[] recalc = recalcWorkbookFormulas(wb); // 公式重算并写回缓存值:全省行(Σ市州)/累计列/排名 SUM 才能被预览与二次处理读到
            log.info("汇总工作簿公式重算完成:成功 {} 格,失败 {} 格(失败格保留母版旧缓存值,打开时按 fullCalcOnLoad 重算)", recalc[0], recalc[1]);
            if (!wb.getCTWorkbook().isSetCalcPr()) wb.getCTWorkbook().addNewCalcPr();
            wb.getCTWorkbook().getCalcPr().setFullCalcOnLoad(true); // 还原/改写公式后打开即重算
            return toBytes(wb);
        } finally {
            ZipSecureFile.setMinInflateRatio(savedZipRatio);
        }
    }
 
    /**
     * 汇总工作簿公式重算:POI 写文件不带缓存值,导致「全省行(Σ17市州)」「累计列」「排名 SUM」
     * 在预览/二次处理里只看到母版上一期的旧值(2026-09-18 用户报“8月没数据 / 全省行停在上一期”的根因之一)。
     * 逐格 evaluate 并把结果写回缓存值;个别不支持函数/外部引用格失败则跳过并计数,不阻塞导出。
     */
    private int[] recalcWorkbookFormulas(XSSFWorkbook wb) {
        if (wb == null) return new int[]{0, 0};
        org.apache.poi.ss.usermodel.FormulaEvaluator ev;
        try {
            ev = wb.getCreationHelper().createFormulaEvaluator();
        } catch (Exception e) {
            log.warn("汇总工作簿公式重算:创建求值器失败,跳过({})", e.getMessage());
            return new int[]{0, 0};
        }
        int ok = 0, fail = 0;
        java.util.List<String> samples = new java.util.ArrayList<>();
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sh = wb.getSheetAt(i);
            if (sh == null) continue;
            for (Row row : sh) {
                if (row == null) continue;
                for (Cell c : row) {
                    if (c == null || c.getCellType() != CellType.FORMULA) continue;
                    try {
                        ev.evaluateFormulaCell(c); // 注意:evaluate() 无副作用,必须用 evaluateFormulaCell 才会把结果写回缓存值
                        ok++;
                    } catch (Exception ex) {
                        fail++;
                        if (samples.size() < 10) {
                            samples.add(sh.getSheetName() + "!" + c.getAddress() + " :: " + ex.getClass().getSimpleName());
                        }
                    }
                }
            }
        }
        if (!samples.isEmpty()) log.warn("汇总工作簿公式重算失败样例:{}", samples);
        return new int[]{ok, fail};
    }
 
    /**
     * 清掉「非 inlineStr 却残留 &lt;is&gt;」的非法单元格。
     *
     * 母版若由 openpyxl 写出,文本单元格用 &lt;is&gt;(inlineStr)存储;POI 的 setCellValue 只改写 t 与 &lt;v&gt;,
     * 不会清掉旧 &lt;is&gt;,于是产出 &lt;c t="n"&gt;&lt;v&gt;67.0&lt;/v&gt;&lt;is&gt;&lt;t&gt;67.00&lt;/t&gt;&lt;/is&gt;&lt;/c&gt; 这种非法格,
     * Excel 判定为「无法读取的内容」而直接拒绝打开(COM 表现为「不能取得类 Workbooks 的 Open 属性」)。
     * 2026-07 导出件即因此打不开(《公交》17 格 +《轨道、轮渡》2 格)。落盘前统一清理,幂等。
     */
    private int normalizeStaleInlineStrings(XSSFWorkbook wb) {
        int fixed = 0;
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet ctw = wb.getSheetAt(i).getCTWorksheet();
            if (ctw == null || ctw.getSheetData() == null) continue;
            for (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow row : ctw.getSheetData().getRowArray()) {
                for (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell c : row.getCArray()) {
                    if (!c.isSetIs()) continue;
                    STCellType.Enum t = c.getT();
                    if (t == null || !STCellType.INLINE_STR.equals(t)) {
                        c.unsetIs();
                        fixed++;
                    }
                }
            }
        }
        return fixed;
    }
 
    /**
     * 版式对齐:按 docs/生成汇总大表/《生成_道路运输量汇总表.xlsx》(用户提供的版式参照)逐列套用列宽与隐藏标记,
     * 使生成件与参照件“格式完全一致”,并让母版没定义宽度的“后续扩列”(如 8 月列)也有与 8 月一致的列宽。
     * 参照件缺失时静默跳过(返回 0),不影响原有导出。
     */
    private int applySummaryReferenceLayout(XSSFWorkbook wb) {
        File ref;
        try {
            ref = resolveSummaryLayoutRef();
        } catch (Exception e) {
            return 0;
        }
        if (ref == null) return 0;
        double savedZipRatio = ZipSecureFile.getMinInflateRatio();
        ZipSecureFile.setMinInflateRatio(0.0001);
        int applied = 0;
        try (InputStream in = new FileInputStream(ref); XSSFWorkbook rwb = new XSSFWorkbook(in)) {
            for (int i = 0; i < rwb.getNumberOfSheets(); i++) {
                org.apache.poi.xssf.usermodel.XSSFSheet rs = rwb.getSheetAt(i);
                if (rs == null) continue;
                org.apache.poi.xssf.usermodel.XSSFSheet ts = wb.getSheet(rs.getSheetName());
                if (ts == null) continue;
                int maxCol = Math.min(usedColumnCount(rs), usedColumnCount(ts) + 4);
                if (maxCol <= 0) continue;
                double[] width = new double[maxCol];
                boolean[] hasWidth = new boolean[maxCol];
                boolean[] hidden = new boolean[maxCol];
                for (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols cols : rs.getCTWorksheet().getColsArray()) {
                    for (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol col : cols.getColArray()) {
                        long min = col.getMin();
                        long max = col.getMax();
                        for (long c = min; c <= max; c++) {
                            int idx = (int) (c - 1);
                            if (idx < 0 || idx >= maxCol) {
                                if (idx >= maxCol) break;
                                continue;
                            }
                            if (col.isSetWidth()) {
                                width[idx] = col.getWidth();
                                hasWidth[idx] = true;
                            }
                            hidden[idx] = col.isSetHidden() && col.getHidden();
                        }
                    }
                }
                for (int c = 0; c < maxCol; c++) {
                    if (hasWidth[c]) {
                        int w = (int) Math.round(width[c] * 256d);
                        if (ts.getColumnWidth(c) != w) {
                            ts.setColumnWidth(c, w);
                            applied++;
                        }
                    }
                    if (hidden[c] != ts.isColumnHidden(c)) ts.setColumnHidden(c, hidden[c]);
                }
            }
            log.info("汇总工作簿版式对齐《生成_道路运输量汇总表》完成:套用列宽 {} 列(参照件:{})", applied, ref.getName());
        } catch (Exception e) {
            log.warn("汇总工作簿版式对齐失败(忽略,继续按母版版式输出):{}", e.getMessage());
            return 0;
        } finally {
            ZipSecureFile.setMinInflateRatio(savedZipRatio);
        }
        return applied;
    }
 
    /** 一页已用到的最大列数(0 基计数) */
    private int usedColumnCount(org.apache.poi.ss.usermodel.Sheet sh) {
        int max = 0;
        for (int r = sh.getFirstRowNum(); r <= sh.getLastRowNum(); r++) {
            org.apache.poi.ss.usermodel.Row row = sh.getRow(r);
            if (row == null) continue;
            if (row.getLastCellNum() > max) max = row.getLastCellNum();
        }
        return max;
    }
 
    // ==================== 汇总工作簿内的排名页刷新 ====================
 
    /**
     * 汇总工作簿里的「货运量排名 / 货运周转量排名 / 中口径排名」在母版里是上一报表期的静态值,
     * 导出时用独立排名表(《生成_货运量排名》《生成_周转量排名》《生成_中口径排名》,与用户日常核对的口径一致)
     * 的结果刷新数值,避免汇总表里的排名页停留在旧月份。
     * 数值格搬值、公式格按偏移平移公式后写入;目标格本身已是公式的(SUM/RANK/占比/补数)保留不动,打开时按新数据重算。
     */
    private int refreshSummaryRankSheets(XSSFWorkbook wb, String period, String mode) throws Exception {
        int n = 0;
        n += copyRankValues(wb, "货运量排名", exportFreightRank(period, mode), new int[][]{{0, 0}, {22, 0}});
        n += copyRankValues(wb, "货运周转量排名", exportTurnoverRank(period, mode), new int[][]{{0, 0}, {22, 0}});
        n += copyRankValues(wb, "中口径排名", exportPassengerMidRank(period, mode), new int[][]{{0, 0}, {0, 10}});
        return n;
    }
 
    /**
     * 把独立排名表的各个块按标题对齐搬进汇总大表对应页。
     * srcBlocks[i] = 源块 i 的起始 {行, 列}(0 基);目标块按标题顺序(累计块在前、当月块在后)对应。
     * 数值格直接搬值;源侧是公式的(排名/占比等)按目标块偏移整体平移后写入公式,打开时按汇总表自身数据重算。
     */
    private int copyRankValues(XSSFWorkbook dstWb, String dstSheetName, byte[] srcBytes, int[][] srcBlocks) throws Exception {
        XSSFSheet dst = dstWb.getSheet(dstSheetName);
        if (dst == null) return 0;
        List<XSSFCell> dstTitles = rankTitleCells(dst);
        if (dstTitles.isEmpty()) return 0;
        int copied = 0;
        FormulaEvaluator dstEv = dstWb.getCreationHelper().createFormulaEvaluator();
        try (InputStream sin = new ByteArrayInputStream(srcBytes);
             XSSFWorkbook srcWb = new XSSFWorkbook(sin)) {
            XSSFSheet src = srcWb.getSheetAt(0);
            FormulaEvaluator ev = srcWb.getCreationHelper().createFormulaEvaluator();
            int blocks = Math.min(srcBlocks.length, dstTitles.size());
            for (int b = 0; b < blocks; b++) {
                int sr0 = srcBlocks[b][0];
                int sc0 = srcBlocks[b][1];
                XSSFCell dstTitle = dstTitles.get(b);
                int rowOff = dstTitle.getRowIndex() - sr0;
                int colOff = dstTitle.getColumnIndex() - sc0;
                int h = rankBlockHeight(src, sr0, sc0);
                int w = rankBlockWidth(src, sr0, sc0);
                for (int r = 0; r < h; r++) {
                    Row srcRow = src.getRow(sr0 + r);
                    if (srcRow == null) continue;
                    Row dstRow = dst.getRow(sr0 + r + rowOff);
                    if (dstRow == null) dstRow = dst.createRow(sr0 + r + rowOff);
                    for (int c = 0; c < w; c++) {
                        Cell sc = srcRow.getCell(sc0 + c);
                        if (sc == null) continue;
                        int dc = sc0 + c + colOff;
                        if (r == 0) {
                            // 标题行:同步块标题文案。母版标题是 inlineStr,POI 的 setCellValue 只写 <v>、
                            // 不更新 <is>,Excel 会继续显示旧月份,必须先 setBlank 清掉再写。
                            Cell dt = dstRow.getCell(dc);
                            if (dt == null) dt = rankCell(dstRow, dc);
                            if (sc.getCellType() == CellType.STRING && dt.getCellType() != CellType.FORMULA) {
                                String text = sc.getStringCellValue();
                                String old = dt.getCellType() == CellType.STRING ? dt.getStringCellValue() : null;
                                if (text != null && !text.equals(old)) {
                                    dt.setBlank();
                                    dt.setCellValue(text);
                                }
                            }
                            continue;
                        }
                        Cell dcCell = dstRow.getCell(dc);
                        if (dcCell != null && dcCell.getCellType() == CellType.FORMULA) continue; // 保留母版自己的公式
                        if (sc.getCellType() == CellType.FORMULA) {
                            // 源侧公式(排名/占比):按目标块偏移平移后原样写入,再按汇总表自身数据求值缓存。
                            // 不直接搬 POI 对源表的求值结果(如 RANK 对手工留空的同比一律返回 1)。
                            String f = sc.getCellFormula();
                            if (f == null || f.isEmpty()) continue;
                            String shifted = shiftFormula(f, colOff, rowOff);
                            if (dcCell == null) dcCell = rankCell(dstRow, dc);
                            dcCell.setCellFormula(shifted);
                            try {
                                dstEv.evaluateFormulaCell(dcCell);
                            } catch (Exception ignore) {
                                // 求值失败不阻塞导出:打开时由 Excel/WPS 重算
                            }
                            copied++;
                            continue;
                        }
                        Double v = rankNumeric(sc, ev);
                        if (v == null) continue;
                        if (dcCell == null) dcCell = rankCell(dstRow, dc);
                        dcCell.setCellValue(v);
                        copied++;
                    }
                }
            }
        }
        return copied;
    }
 
    /** 目标块缺格时新建,并沿用同行左邻格样式(防新格丢边框/百分数格式) */
    private Cell rankCell(Row row, int col) {
        Cell c = row.createCell(col);
        Cell left = col > 0 ? row.getCell(col - 1) : null;
        if (left != null) c.setCellStyle(left.getCellStyle());
        return c;
    }
 
    /** 公式内所有 A1 引用整体平移:列 +dCol、行 +dRow(SUM/RANK 等函数名后接括号不会被匹配) */
    private String shiftFormula(String formula, int dCol, int dRow) {
        if (formula == null || (dCol == 0 && dRow == 0)) return formula;
        java.util.regex.Matcher m = java.util.regex.Pattern
                .compile("(?<![A-Za-z0-9_$])([$]?)([A-Z]{1,3})([$]?)([0-9]+)")
                .matcher(formula);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String col = m.group(2);
            int idx = 0;
            for (int i = 0; i < col.length(); i++) idx = idx * 26 + (col.charAt(i) - 'A' + 1);
            int nidx = Math.max(1, idx + dCol);
            StringBuilder nc = new StringBuilder();
            while (nidx > 0) {
                int rem = (nidx - 1) % 26;
                nc.insert(0, (char) ('A' + rem));
                nidx = (nidx - 1) / 26;
            }
            int nrow = Math.max(1, Integer.parseInt(m.group(4)) + dRow);
            m.appendReplacement(sb, java.util.regex.Matcher.quoteReplacement(m.group(1) + nc + m.group(3) + nrow));
        }
        m.appendTail(sb);
        return sb.toString();
    }
 
    /** 排名页里的“块标题”单元格(含“全省分市州”的说明文字),按行、列顺序返回 */
    private List<XSSFCell> rankTitleCells(Sheet sh) {
        List<XSSFCell> out = new ArrayList<>();
        for (int r = sh.getFirstRowNum(); r <= sh.getLastRowNum(); r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
                Cell cell = row.getCell(c);
                if (cell == null || cell.getCellType() != CellType.STRING) continue;
                String t = cell.getStringCellValue();
                if (t != null && t.contains("全省分市州")) out.add((XSSFCell) cell);
            }
        }
        return out;
    }
 
    /** 块高度:从起始行向下直到整行为空(在块列范围内) */
    private int rankBlockHeight(Sheet sh, int r0, int c0) {
        int h = 0;
        for (int r = r0; r <= sh.getLastRowNum(); r++) {
            Row row = sh.getRow(r);
            boolean any = false;
            if (row != null) {
                int last = row.getLastCellNum();
                for (int c = c0; c < last; c++) {
                    if (rankHasContent(row.getCell(c))) { any = true; break; }
                }
            }
            if (!any) break;
            h++;
        }
        return h;
    }
 
    /** 块宽度:从起始列向右直到整列为空(在块行范围内,扫描上限 40 列) */
    private int rankBlockWidth(Sheet sh, int r0, int c0) {
        int w = 0;
        for (int c = c0; c < c0 + 40; c++) {
            boolean any = false;
            for (int r = r0; r < r0 + 30; r++) {
                Row row = sh.getRow(r);
                if (row != null && rankHasContent(row.getCell(c))) { any = true; break; }
            }
            if (!any) break;
            w++;
        }
        return w;
    }
 
    private boolean rankHasContent(Cell c) {
        if (c == null) return false;
        CellType t = c.getCellType();
        if (t == CellType.BLANK) return false;
        if (t == CellType.STRING) {
            String v = c.getStringCellValue();
            return v != null && !v.trim().isEmpty();
        }
        return true;
    }
 
    /** 取单元格数值(公式取求值结果,纯数字文本按数值解析),取不到返回 null */
    private Double rankNumeric(Cell c, FormulaEvaluator ev) {
        try {
            CellType t = c.getCellType();
            if (t == CellType.NUMERIC) return c.getNumericCellValue();
            if (t == CellType.FORMULA) {
                org.apache.poi.ss.usermodel.CellValue cv = ev.evaluate(c);
                return cv != null && cv.getCellType() == CellType.NUMERIC ? cv.getNumberValue() : null;
            }
            if (t == CellType.STRING) {
                String v = c.getStringCellValue();
                if (v == null) return null;
                String x = v.replace(",", "").trim();
                if (x.isEmpty()) return null;
                try { return Double.parseDouble(x); } catch (NumberFormatException ignore) { return null; }
            }
        } catch (Exception ignore) {
            return null;
        }
        return null;
    }
 
    /** 定位版式参照件 docs/生成汇总大表/生成_道路运输量汇总表.xlsx(优先级同母版:配置目录 → user.dir 相对 → 上级) */
    // ==================== 汇总大表:四张“跨页合成”页随报表期刷新 ====================
    // 城市汇总 / 城市分市州 / 中口径分析 / 道路运输周转量 此前整页沿用母版静态值(标题停在 1-7 月)。
    // 本段按 2026-09-13 已验证口径(与 8 月母版逐格 0 差异)从母版台账页现算:
    //   城市客运量     = 城市内公交 + 城市内巡游出租 + 城市内网约车 + 轨道 + 轮渡
    //   道路客运周转量 = 公路班线 + 城际城乡公交 + 城际城乡巡游出租 + 城际城乡网约车
    //   道路货运周转量 = 货运页 市州 规上货物周转量 + 规下货物周转量
    // 2026 年 1..N 月取台账页 2026 列(C/E/G…),去年同期取母版缓存 2025 列(V/X/Z…)——库里没有 2025 年城市客运数据。
 
    /** 城市客运口径指标位(0..17 城市客运,18 货运周转量) */
    private static final int SM_BUS_PAX = 0, SM_BUS_TURN = 1;
    private static final int SM_TAXI_PAX = 2, SM_TAXI_TURN = 3;
    private static final int SM_WYC_PAX = 4, SM_WYC_TURN = 5;
    private static final int SM_RAIL_PAX = 6, SM_RAIL_TURN = 7;
    private static final int SM_FERRY_PAX = 8, SM_FERRY_TURN = 9;
    private static final int SM_BX_PAX = 10, SM_BX_TURN = 11;
    private static final int SM_BUS_CX_PAX = 12, SM_BUS_CX_TURN = 13;
    private static final int SM_TAXI_CX_PAX = 14, SM_TAXI_CX_TURN = 15;
    private static final int SM_WYC_CX_PAX = 16, SM_WYC_CX_TURN = 17;
    private static final int SM_FREIGHT_TURN = 18;
    private static final int SM_DIM = 19;
    /** 2026 年 1 月当月值列(0 基,C 列);2025 年 1 月当月值列(0 基,V 列) */
    private static final int SM_CUR_COL0 = 2;
    private static final int SM_LAST_COL0 = 21;
 
    /**
     * 刷新四张跨页合成页:当年累计(1..month 月)、当月(month 月)及对应同比;
     * 2026 与 2025 同期都取自母版台账页缓存,返回写入单元格数。
     */
    private int refreshSummaryCumulativeSheets(XSSFWorkbook wb, int year, int month) {
        if (wb == null || month <= 0) return 0;
        int[] cumMonths = new int[month];
        for (int m = 1; m <= month; m++) cumMonths[m - 1] = m;
        int[] oneMonth = new int[]{month};
        Map<String, double[]> curCum = smReadLedgerMetrics(wb, true, cumMonths);
        Map<String, double[]> lastCum = smReadLedgerMetrics(wb, false, cumMonths);
        Map<String, double[]> curMon = smReadLedgerMetrics(wb, true, oneMonth);
        Map<String, double[]> lastMon = smReadLedgerMetrics(wb, false, oneMonth);
        if (curCum.isEmpty()) {
            log.warn("汇总大表四页刷新:台账页未读到市州数据,跳过(保持母版原值)");
            return 0;
        }
        double[] pCurCum = smProvinceSum(curCum);
        double[] pLastCum = smProvinceSum(lastCum);
        double[] pCurMon = smProvinceSum(curMon);
        double[] pLastMon = smProvinceSum(lastMon);
        int n = 0;
        n += smWriteCitySummary(wb, year, month, pCurCum, pLastCum, pCurMon, pLastMon);
        n += smWriteCityByRegion(wb, month, curCum, lastCum, curMon, lastMon);
        n += smWriteMidAnalysis(wb, month, pCurCum, pLastCum, pCurMon, pLastMon);
        n += smWriteTurnoverSheets(wb, year, month, curCum, lastCum, curMon, lastMon);
        return n;
    }
 
    /** 17 市州合计(不含台账页“全省”公式行) */
    private double[] smProvinceSum(Map<String, double[]> m) {
        double[] p = new double[SM_DIM];
        for (String c : RegionUtil.cityList()) {
            double[] a = m.get(c);
            if (a == null) continue;
            for (int i = 0; i < SM_DIM; i++) p[i] += a[i];
        }
        return p;
    }
 
    /** 台账页累计取数:市州规范名 -> 19 维指标 */
    private Map<String, double[]> smReadLedgerMetrics(XSSFWorkbook wb, boolean curYear, int[] months) {
        Map<String, double[]> out = new LinkedHashMap<>();
        smReadCityPaxPage(wb, "公交", curYear, months, out, SM_BUS_PAX, SM_BUS_TURN, SM_BUS_CX_PAX, SM_BUS_CX_TURN);
        smReadCityPaxPage(wb, "出租", curYear, months, out, SM_TAXI_PAX, SM_TAXI_TURN, SM_TAXI_CX_PAX, SM_TAXI_CX_TURN);
        smReadCityPaxPage(wb, "网约车", curYear, months, out, SM_WYC_PAX, SM_WYC_TURN, SM_WYC_CX_PAX, SM_WYC_CX_TURN);
        smReadRailFerry(wb, curYear, months, out);
        smReadBanxian(wb, curYear, months, out);
        smReadFreightTurnover(wb, curYear, months, out);
        return out;
    }
 
    /** 公交/出租/网约车页:每市州 4 行(客运量/旅客周转量/其中城市内客运量/其中城市内旅客周转量) */
    private void smReadCityPaxPage(XSSFWorkbook wb, String sheetName, boolean curYear, int[] months,
                                   Map<String, double[]> out, int paxIdx, int turnIdx, int cxPaxIdx, int cxTurnIdx) {
        Sheet sh = wb.getSheet(sheetName);
        if (sh == null) return;
        int last = sh.getLastRowNum();
        for (int r = sh.getFirstRowNum(); r <= last; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            String city = smCityOf(row.getCell(0));
            if (city == null) continue;
            int paxRow = -1, turnRow = -1, cxPaxRow = -1, cxTurnRow = -1;
            for (int k = r; k <= Math.min(last, r + 4); k++) {
                Row kr = sh.getRow(k);
                if (kr == null) continue;
                String b = smText(kr.getCell(1));
                if (b == null || b.contains("个体")) continue;
                boolean inner = b.contains("城市内");
                boolean turn = b.contains("周转量");
                if (inner) {
                    if (turn) { if (cxTurnRow < 0) cxTurnRow = k; }
                    else if (cxPaxRow < 0) cxPaxRow = k;
                } else {
                    if (turn) { if (turnRow < 0) turnRow = k; }
                    else if (paxRow < 0) paxRow = k;
                }
            }
            double totalPax = smSumMonths(sh, paxRow, curYear, months);
            double totalTurn = smSumMonths(sh, turnRow, curYear, months);
            double cityPax = smSumMonths(sh, cxPaxRow, curYear, months);
            double cityTurn = smSumMonths(sh, cxTurnRow, curYear, months);
            double[] arr = out.computeIfAbsent(city, k -> new double[SM_DIM]);
            arr[paxIdx] += cityPax;                 // 城市客运口径取“城市内”
            arr[turnIdx] += cityTurn;
            arr[cxPaxIdx] += totalPax - cityPax;    // 城际城乡 = 总量 - 城市内
            arr[cxTurnIdx] += totalTurn - cityTurn;
        }
    }
 
    /** 轨道、轮渡页:轨道块(武汉/黄石)+ 轮渡块(武汉) */
    private void smReadRailFerry(XSSFWorkbook wb, boolean curYear, int[] months, Map<String, double[]> out) {
        Sheet sh = wb.getSheet("轨道、轮渡");
        if (sh == null) return;
        int last = sh.getLastRowNum();
        int ferryTitle = -1;
        for (int r = sh.getFirstRowNum(); r <= last; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            String a = smText(row.getCell(0));
            if (a != null && a.contains("轮渡客运量")) { ferryTitle = r; break; }
        }
        smReadRailFerryBlock(sh, curYear, months, out, sh.getFirstRowNum(),
                ferryTitle < 0 ? last : ferryTitle - 1, SM_RAIL_PAX);
        if (ferryTitle >= 0) smReadRailFerryBlock(sh, curYear, months, out, ferryTitle, last, SM_FERRY_PAX);
    }
 
    private void smReadRailFerryBlock(Sheet sh, boolean curYear, int[] months, Map<String, double[]> out,
                                      int start, int end, int baseIdx) {
        for (int r = start; r <= end; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            String city = smCityOf(row.getCell(0));
            if (city == null) continue;
            int paxRow = -1, turnRow = -1;
            for (int k = r; k <= Math.min(end, r + 3); k++) {
                Row kr = sh.getRow(k);
                if (kr == null) continue;
                String b = smText(kr.getCell(1));
                if (b == null) continue;
                if (b.contains("周转量")) { if (turnRow < 0) turnRow = k; }
                else if (paxRow < 0) paxRow = k;
            }
            double[] arr = out.computeIfAbsent(city, k -> new double[SM_DIM]);
            arr[baseIdx] += smSumMonths(sh, paxRow, curYear, months);
            arr[baseIdx + 1] += smSumMonths(sh, turnRow, curYear, months);
        }
    }
 
    /** 班线包车页:每市州 客运量 / 旅客周转量(“其中个体”行不重复计入) */
    private void smReadBanxian(XSSFWorkbook wb, boolean curYear, int[] months, Map<String, double[]> out) {
        Sheet sh = wb.getSheet("班线包车");
        if (sh == null) return;
        int last = sh.getLastRowNum();
        for (int r = sh.getFirstRowNum(); r <= last; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            String city = smCityOf(row.getCell(0));
            if (city == null) continue;
            int paxRow = -1, turnRow = -1;
            for (int k = r; k <= Math.min(last, r + 4); k++) {
                Row kr = sh.getRow(k);
                if (kr == null) continue;
                String b = smText(kr.getCell(1));
                if (b == null || b.contains("个体")) continue;
                if (b.contains("周转量")) { if (turnRow < 0) turnRow = k; }
                else if (b.contains("客运量") && paxRow < 0) paxRow = k;
            }
            double[] arr = out.computeIfAbsent(city, k -> new double[SM_DIM]);
            arr[SM_BX_PAX] += smSumMonths(sh, paxRow, curYear, months);
            arr[SM_BX_TURN] += smSumMonths(sh, turnRow, curYear, months);
        }
    }
 
    /** 货运页:每市州 规上货物周转量 + 规下货物周转量(“规上+规下”行本身是求和公式,不直接取) */
    private void smReadFreightTurnover(XSSFWorkbook wb, boolean curYear, int[] months, Map<String, double[]> out) {
        Sheet sh = wb.getSheet(" 货运");
        if (sh == null) sh = wb.getSheet("货运");
        if (sh == null) return;
        int last = sh.getLastRowNum();
        for (int r = sh.getFirstRowNum(); r <= last; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            String city = smCityOf(row.getCell(0));
            if (city == null) continue;
            int above = -1, below = -1;
            for (int k = r; k <= Math.min(last, r + 6); k++) {
                Row kr = sh.getRow(k);
                if (kr == null) continue;
                String b = smText(kr.getCell(1));
                if (b == null || b.contains("其中")) continue;
                if (b.contains("规上货物周转量")) { if (above < 0) above = k; }
                else if (b.contains("规下货物周转量")) { if (below < 0) below = k; }
            }
            double[] arr = out.computeIfAbsent(city, k -> new double[SM_DIM]);
            arr[SM_FREIGHT_TURN] += smSumMonths(sh, above, curYear, months) + smSumMonths(sh, below, curYear, months);
        }
    }
 
    private double smSumMonths(Sheet sh, int rowIdx, boolean curYear, int[] months) {
        if (sh == null || rowIdx < 0) return 0;
        Row row = sh.getRow(rowIdx);
        if (row == null) return 0;
        double s = 0;
        for (int m : months) {
            int col = (curYear ? SM_CUR_COL0 : SM_LAST_COL0) + 2 * (m - 1);
            s += smNum(row.getCell(col));
        }
        return s;
    }
 
    /** 台账页单元格取数(数字 / 数字文本 / 公式缓存) */
    private double smNum(Cell c) {
        if (c == null) return 0;
        try {
            CellType t = c.getCellType();
            if (t == CellType.NUMERIC) return c.getNumericCellValue();
            if (t == CellType.FORMULA) {
                return c.getCachedFormulaResultType() == CellType.NUMERIC ? c.getNumericCellValue() : 0;
            }
            if (t == CellType.STRING) {
                String s = c.getStringCellValue();
                if (s == null) return 0;
                s = s.replace(",", "").replace("%", "").trim();
                if (s.isEmpty()) return 0;
                return Double.parseDouble(s);
            }
        } catch (Exception ignore) {
            return 0;
        }
        return 0;
    }
 
    private String smText(Cell c) {
        if (c == null) return null;
        try {
            return c.getCellType() == CellType.STRING ? c.getStringCellValue() : null;
        } catch (Exception e) {
            return null;
        }
    }
 
    /** 列 A 文案 -> 规范市州名(“全省/湖北省”与非市州行返回 null) */
    private String smCityOf(Cell a) {
        String t = smText(a);
        if (t == null || t.trim().isEmpty()) return null;
        String norm = RegionUtil.normalizeCityName(t.trim());
        if (norm == null) return null;
        return RegionUtil.cityList().contains(norm) ? norm : null;
    }
 
    private double smYoy(double cur, double last) {
        return last == 0.0 ? 0.0 : (cur - last) / last;
    }
 
    /** 写值:公式格不动(保留母版公式,打开即重算);0 且原为空格则不新增 */
    private int smSetVal(Sheet sh, int r0, int c0, double v) {
        Row row = sh.getRow(r0);
        if (row == null) return 0;
        Cell c = row.getCell(c0);
        if (c != null && c.getCellType() == CellType.FORMULA) return 0;
        if (c == null) {
            if (v == 0.0) return 0;
            c = row.createCell(c0);
            Cell left = row.getCell(c0 - 1);
            if (left != null) c.setCellStyle(left.getCellStyle());
        } else if (v == 0.0 && c.getCellType() == CellType.BLANK) {
            return 0;
        }
        c.setBlank();
        c.setCellValue(v);
        return 1;
    }
 
    private void smSetTitle(Sheet sh, int r0, int c0, String text) {
        if (sh == null) return;
        Row row = sh.getRow(r0);
        if (row == null) return;
        Cell c = row.getCell(c0);
        if (c == null || c.getCellType() == CellType.FORMULA) return;
        c.setBlank();
        c.setCellValue(text);
    }
 
    /** 城市汇总页:块1(1..N 月累计)行 3-9,块2(当月)行 14-20;列 B/C/D/E = 客运量/同比/周转量/同比 */
    private int smWriteCitySummary(XSSFWorkbook wb, int year, int month,
                                   double[] pCurCum, double[] pLastCum, double[] pCurMon, double[] pLastMon) {
        Sheet sh = wb.getSheet("城市汇总");
        if (sh == null) return 0;
        smSetTitle(sh, 0, 0, year + "年1-" + month + "月全省累计完成城市客运量情况");
        smSetTitle(sh, 11, 0, year + "年" + month + "月全省完成城市客运量情况");
        int n = 0;
        int[] rows = {2, 3, 4, 5, 6, 7, 8};
        for (int i = 0; i < rows.length; i++) {
            double[] v = smCitySummaryRow(pCurCum, pLastCum, i);
            n += smSetVal(sh, rows[i], 1, v[0]);
            n += smSetVal(sh, rows[i], 2, v[2]);
            n += smSetVal(sh, rows[i], 3, v[1]);
            n += smSetVal(sh, rows[i], 4, v[3]);
        }
        for (int i = 0; i < rows.length; i++) {
            double[] v = smCitySummaryRow(pCurMon, pLastMon, i);
            n += smSetVal(sh, rows[i] + 11, 1, v[0]);
            n += smSetVal(sh, rows[i] + 11, 2, v[2]);
            n += smSetVal(sh, rows[i] + 11, 3, v[1]);
            n += smSetVal(sh, rows[i] + 11, 4, v[3]);
        }
        return n;
    }
 
    /** i=0 总客运量 1 公交 2 出租车 3 巡游出租 4 网约车 5 轨道 6 轮渡;返回 [客运量, 周转量, 客运量同比, 周转量同比] */
    private double[] smCitySummaryRow(double[] p, double[] pl, int i) {
        double pax, turn, lPax, lTurn;
        if (i == 0) {
            pax = p[SM_BUS_PAX] + p[SM_TAXI_PAX] + p[SM_WYC_PAX] + p[SM_RAIL_PAX] + p[SM_FERRY_PAX];
            turn = p[SM_BUS_TURN] + p[SM_TAXI_TURN] + p[SM_WYC_TURN] + p[SM_RAIL_TURN] + p[SM_FERRY_TURN];
            lPax = pl[SM_BUS_PAX] + pl[SM_TAXI_PAX] + pl[SM_WYC_PAX] + pl[SM_RAIL_PAX] + pl[SM_FERRY_PAX];
            lTurn = pl[SM_BUS_TURN] + pl[SM_TAXI_TURN] + pl[SM_WYC_TURN] + pl[SM_RAIL_TURN] + pl[SM_FERRY_TURN];
        } else if (i == 1) {
            pax = p[SM_BUS_PAX]; turn = p[SM_BUS_TURN]; lPax = pl[SM_BUS_PAX]; lTurn = pl[SM_BUS_TURN];
        } else if (i == 2) {
            pax = p[SM_TAXI_PAX] + p[SM_WYC_PAX]; turn = p[SM_TAXI_TURN] + p[SM_WYC_TURN];
            lPax = pl[SM_TAXI_PAX] + pl[SM_WYC_PAX]; lTurn = pl[SM_TAXI_TURN] + pl[SM_WYC_TURN];
        } else if (i == 3) {
            pax = p[SM_TAXI_PAX]; turn = p[SM_TAXI_TURN]; lPax = pl[SM_TAXI_PAX]; lTurn = pl[SM_TAXI_TURN];
        } else if (i == 4) {
            pax = p[SM_WYC_PAX]; turn = p[SM_WYC_TURN]; lPax = pl[SM_WYC_PAX]; lTurn = pl[SM_WYC_TURN];
        } else if (i == 5) {
            pax = p[SM_RAIL_PAX]; turn = p[SM_RAIL_TURN]; lPax = pl[SM_RAIL_PAX]; lTurn = pl[SM_RAIL_TURN];
        } else {
            pax = p[SM_FERRY_PAX]; turn = p[SM_FERRY_TURN]; lPax = pl[SM_FERRY_PAX]; lTurn = pl[SM_FERRY_TURN];
        }
        return new double[]{pax, turn, smYoy(pax, lPax), smYoy(turn, lTurn)};
    }
 
    /** 城市分市州页:4 块(累计客运量/累计周转量/当月客运量/当月周转量),每块 全省 1 行 + 17 市州 */
    private 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("城市分市州");
        if (sh == null) return 0;
        smSetTitle(sh, 0, 0, "城市客运客运量分市州1-" + month + "月累计情况");
        smSetTitle(sh, 22, 0, "城市客运客运周转量分市州1-" + month + "月累计情况");
        smSetTitle(sh, 45, 0, "城市客运客运量分市州" + month + "月情况");
        smSetTitle(sh, 67, 0, "城市客运客运周转量分市州" + month + "月情况");
        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);
        return n;
    }
 
    private int smWriteCityByRegionBlock(Sheet sh, int provRow, boolean pax,
                                         Map<String, double[]> cur, Map<String, double[]> last) {
        int n = 0;
        double[] provCur = smProvinceSum(cur);
        double[] provLast = smProvinceSum(last);
        List<String> cities = RegionUtil.cityList();
        for (int i = -1; i < cities.size(); i++) {
            int r = provRow + i + 1;
            double[] c = i < 0 ? provCur : cur.get(cities.get(i));
            double[] l = i < 0 ? provLast : last.get(cities.get(i));
            if (c == null) c = new double[SM_DIM];
            if (l == null) l = new double[SM_DIM];
            double[] g = smCityGroupValues(c, l, pax);
            n += smSetVal(sh, r, 1, g[0]);
            n += smSetVal(sh, r, 4, g[6]);
            n += smSetVal(sh, r, 6, g[1]);
            n += smSetVal(sh, r, 9, g[7]);
            n += smSetVal(sh, r, 11, g[2]);
            n += smSetVal(sh, r, 14, g[8]);
            n += smSetVal(sh, r, 16, g[3]);
            n += smSetVal(sh, r, 19, g[9]);
            n += smSetVal(sh, r, 21, g[4]);
            n += smSetVal(sh, r, 22, g[10]);
            n += smSetVal(sh, r, 23, g[5]);
            n += smSetVal(sh, r, 24, g[11]);
        }
        return n;
    }
 
    /** 6 组值 + 6 组增速:[总量, 公交, 出租, 网约车, 轨道, 轮渡, 总量增速, 公交…, 轮渡] */
    private double[] smCityGroupValues(double[] c, double[] l, boolean pax) {
        int busI = pax ? SM_BUS_PAX : SM_BUS_TURN;
        int taxiI = pax ? SM_TAXI_PAX : SM_TAXI_TURN;
        int wycI = pax ? SM_WYC_PAX : SM_WYC_TURN;
        int railI = pax ? SM_RAIL_PAX : SM_RAIL_TURN;
        int ferryI = pax ? SM_FERRY_PAX : SM_FERRY_TURN;
        double bus = c[busI], taxi = c[taxiI], wyc = c[wycI], rail = c[railI], ferry = c[ferryI];
        double lBus = l[busI], lTaxi = l[taxiI], lWyc = l[wycI], lRail = l[railI], lFerry = l[ferryI];
        double total = bus + taxi + wyc + rail + ferry;
        double lTotal = lBus + lTaxi + lWyc + lRail + lFerry;
        return new double[]{total, bus, taxi, wyc, rail, ferry,
                smYoy(total, lTotal), smYoy(bus, lBus), smYoy(taxi, lTaxi), smYoy(wyc, lWyc),
                smYoy(rail, lRail), smYoy(ferry, lFerry)};
    }
 
    /** 中口径分析页:块1(累计)行 3-7,块2(当月)行 12-16;列 B/C/F/G = 客运量/同比/周转量/同比 */
    private int smWriteMidAnalysis(XSSFWorkbook wb, int month,
                                   double[] pCurCum, double[] pLastCum, double[] pCurMon, double[] pLastMon) {
        Sheet sh = wb.getSheet("中口径分析");
        if (sh == null) return 0;
        smSetTitle(sh, 0, 0, "1-" + month + "月中口径客运量及周转量");
        smSetTitle(sh, 9, 0, month + "月中口径客运量及周转量");
        int n = 0;
        int[] rows = {2, 3, 4, 5, 6};
        for (int i = 0; i < rows.length; i++) {
            double[] v = smMidRow(pCurCum, pLastCum, i);
            n += smSetVal(sh, rows[i], 1, v[0]);
            n += smSetVal(sh, rows[i], 2, v[2]);
            n += smSetVal(sh, rows[i], 5, v[1]);
            n += smSetVal(sh, rows[i], 6, v[3]);
        }
        for (int i = 0; i < rows.length; i++) {
            double[] v = smMidRow(pCurMon, pLastMon, i);
            n += smSetVal(sh, rows[i] + 9, 1, v[0]);
            n += smSetVal(sh, rows[i] + 9, 2, v[2]);
            n += smSetVal(sh, rows[i] + 9, 5, v[1]);
            n += smSetVal(sh, rows[i] + 9, 6, v[3]);
        }
        return n;
    }
 
    /** i=0 总 1 公路班线 2 城际城乡公交 3 城际城乡巡游出租 4 城际城乡网约车;返回 [客运量, 周转量, 客运量同比, 周转量同比] */
    private double[] smMidRow(double[] p, double[] pl, int i) {
        int[] paxIdx = {SM_BX_PAX, SM_BX_PAX, SM_BUS_CX_PAX, SM_TAXI_CX_PAX, SM_WYC_CX_PAX};
        int[] turnIdx = {SM_BX_TURN, SM_BX_TURN, SM_BUS_CX_TURN, SM_TAXI_CX_TURN, SM_WYC_CX_TURN};
        double pax, turn, lPax, lTurn;
        if (i == 0) {
            pax = p[SM_BX_PAX] + p[SM_BUS_CX_PAX] + p[SM_TAXI_CX_PAX] + p[SM_WYC_CX_PAX];
            turn = p[SM_BX_TURN] + p[SM_BUS_CX_TURN] + p[SM_TAXI_CX_TURN] + p[SM_WYC_CX_TURN];
            lPax = pl[SM_BX_PAX] + pl[SM_BUS_CX_PAX] + pl[SM_TAXI_CX_PAX] + pl[SM_WYC_CX_PAX];
            lTurn = pl[SM_BX_TURN] + pl[SM_BUS_CX_TURN] + pl[SM_TAXI_CX_TURN] + pl[SM_WYC_CX_TURN];
        } else {
            pax = p[paxIdx[i]]; turn = p[turnIdx[i]];
            lPax = pl[paxIdx[i]]; lTurn = pl[turnIdx[i]];
        }
        return new double[]{pax, turn, smYoy(pax, lPax), smYoy(turn, lTurn)};
    }
 
    /** 道路运输周转量页:块1(累计)行 4-21,块2(当月)行 26-43;只写值列(B/E/V 等公式保留) */
    private int smWriteTurnoverSheets(XSSFWorkbook wb, int year, int month,
                                      Map<String, double[]> curCum, Map<String, double[]> lastCum,
                                      Map<String, double[]> curMon, Map<String, double[]> lastMon) {
        Sheet sh = wb.getSheet("道路运输周转量");
        if (sh == null) return 0;
        smSetTitle(sh, 0, 0, year + "年1-" + month + "月全省累计完成交通周转量情况");
        smSetTitle(sh, 22, 0, year + "年" + month + "月全省完成交通周转量情况");
        int n = 0;
        n += smWriteTurnoverBlock(sh, 3, curCum, lastCum);
        n += smWriteTurnoverBlock(sh, 25, curMon, lastMon);
        return n;
    }
 
    private int smWriteTurnoverBlock(Sheet sh, int provRow, Map<String, double[]> cur, Map<String, double[]> last) {
        int n = 0;
        double[] provCur = smProvinceSum(cur);
        double[] provLast = smProvinceSum(last);
        List<String> cities = RegionUtil.cityList();
        for (int i = -1; i < cities.size(); i++) {
            int r = provRow + i + 1;
            double[] c = i < 0 ? provCur : cur.get(cities.get(i));
            double[] l = i < 0 ? provLast : last.get(cities.get(i));
            if (c == null) c = new double[SM_DIM];
            if (l == null) l = new double[SM_DIM];
            double freight = c[SM_FREIGHT_TURN], lFreight = l[SM_FREIGHT_TURN];
            double pax = c[SM_BUS_PAX] + c[SM_TAXI_PAX] + c[SM_WYC_PAX] + c[SM_RAIL_PAX] + c[SM_FERRY_PAX];
            double lPax = l[SM_BUS_PAX] + l[SM_TAXI_PAX] + l[SM_WYC_PAX] + l[SM_RAIL_PAX] + l[SM_FERRY_PAX];
            double road = c[SM_BX_TURN] + c[SM_BUS_CX_TURN] + c[SM_TAXI_CX_TURN] + c[SM_WYC_CX_TURN];
            double lRoad = l[SM_BX_TURN] + l[SM_BUS_CX_TURN] + l[SM_TAXI_CX_TURN] + l[SM_WYC_CX_TURN];
            n += smSetVal(sh, r, 6, freight);
            n += smSetVal(sh, r, 9, smYoy(freight, lFreight));
            n += smSetVal(sh, r, 11, pax);
            n += smSetVal(sh, r, 14, smYoy(pax, lPax));
            n += smSetVal(sh, r, 16, road);
            n += smSetVal(sh, r, 19, smYoy(road, lRoad));
            n += smSetVal(sh, r, 22, lFreight);
            n += smSetVal(sh, r, 23, lPax);
            n += smSetVal(sh, r, 24, lRoad);
        }
        return n;
    }
 
 
    private File resolveSummaryLayoutRef() {
        String rel = summaryTemplateDir;
        while (rel != null && rel.startsWith("./")) rel = rel.substring(2);
        String[] roots = {
            summaryTemplateDir,
            System.getProperty("user.dir") + "/" + rel,
            System.getProperty("user.dir") + "/../" + rel
        };
        for (String root : roots) {
            if (root == null || root.trim().isEmpty()) continue;
            File f = new File(root, "生成_道路运输量汇总表.xlsx");
            if (f.isFile()) return f;
        }
        return null;
    }
 
    /** 汇总表母版定位:仅精确匹配 {year}年{month}月…(P0-P1 骨架期不允许跨月/跨年静默回退,
     *  目标期无对应母版时明确报错,月份扩展规则待 P0 母版定稿后再放开) */
    private File resolveSummaryMother(String period, String mode) throws Exception {
        String year = period != null && period.contains("-") ? period.split("-")[0] : (period == null ? "" : period);
        int month = period != null && period.contains("-") ? monthOf(period, mode) : 12;
        String name = year + "年" + month + "月道路运输量汇总表.xlsx";
        return resolveAnyTemplate(summaryTemplateDir, name);
    }
 
    // ==================== 网约车客运量拆分表(月度) ====================
 
    /** 网约车客运量拆分表:输入=部级订单(网约车订单及全省总量.xlsx G-W)+ 全省 pin(B-E)+ 巡游出租月报(DB);
     *  算法与《网约车拆分.xlsx》月页一致:客运量=订单占比×全省总量;城市内=巡游出租城市内占比代理、武汉保持自身其余按剩余分摊;
     *  周转量=客运量×巡游出租平均运距占比分摊全省周转量 pin,城市内周转量同法。 */
    public byte[] exportWycSplit(String period, String mode) throws Exception {
        if (period == null || !period.contains("-")) {
            throw new RuntimeException("网约车客运量拆分表仅支持按月生成,请选择具体月份");
        }
        int year = Integer.parseInt(period.split("-")[0]);
        int month = parseMonth(period);
        Map<String, Object> inp = loadWycMonthlyInput(period, mode);
        if (inp == null) {
            throw new RuntimeException("《网约车订单及全省总量.xlsx》缺少报表期 " + period + " 的数据行,请先维护当月订单与全省 pin");
        }
        double pinK = (Double) inp.get("pinK");
        double pinCK = (Double) inp.get("pinCK");
        double pinZ = (Double) inp.get("pinZ");
        double pinCZ = (Double) inp.get("pinCZ");
        double orderSum = (Double) inp.get("orderSum");
        double[] orderArr = (double[]) inp.get("orders");
        if (pinK <= 0 || pinCK <= 0 || pinZ <= 0 || pinCZ <= 0 || orderSum <= 0) {
            throw new RuntimeException("网约车输入 " + period + " 行不完整:全省 pin / 订单缺失,请核对《网约车订单及全省总量.xlsx》");
        }
        List<String> cities = RegionUtil.cityList();
        int n = cities.size();
        if (orderArr == null || orderArr.length < n) {
            throw new RuntimeException("网约车订单列数不足(应为 17 市州订单列)");
        }
        Map<String, double[]> taxi = taxiMonthlyOf(period);
        java.util.List<String> missingTaxi = new java.util.ArrayList<>();
        double[] Dtax = new double[n];
        double[] Etax = new double[n];
        double[] avgD = new double[n];
        double[] avgDc = new double[n];
        for (int i = 0; i < n; i++) {
            double[] a = taxi.get(cities.get(i));
            if (a == null || a[0] <= 0) {
                missingTaxi.add(cities.get(i));
                continue;
            }
            Dtax[i] = a[0];
            Etax[i] = a[2];
            avgD[i] = a[4];
            avgDc[i] = a[5];
        }
        if (!missingTaxi.isEmpty()) {
            throw new RuntimeException("巡游出租月报缺少以下市州数据:" + String.join("、", missingTaxi));
        }
        double[] C = new double[n];
        double[] H = new double[n];
        double[] I = new double[n];
        double[] Gl = new double[n];
        double[] Hl = new double[n];
        double[] Il = new double[n];
        double sumG = 0;
        double sumDl = 0;
        double sumEl = 0;
        for (int i = 0; i < n; i++) {
            C[i] = orderArr[i] / orderSum * pinK;
            double F = Etax[i] / Dtax[i];
            double G = C[i] * F;
            sumG += G;
            double Dl = C[i] * avgD[i];
            double El = H[i] * avgDc[i];
            sumDl += Dl;
            sumEl += El;
        }
        // 城市内:武汉保持自身,其余按 G 剩余占比分摊至全省城市内 pin
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                H[i] = C[i] * (Etax[i] / Dtax[i]);
            } else {
                H[i] = (C[i] * (Etax[i] / Dtax[i])) / (sumG - C[0] * (Etax[0] / Dtax[0])) * (pinCK - C[0] * (Etax[0] / Dtax[0]));
            }
            I[i] = C[i] - H[i];
        }
        // 周转量:按 客运量×巡游出租平均运距 占比分摊全省周转量 pin
        double whG = C[0] * (Etax[0] / Dtax[0]);
        double whDl = C[0] * avgD[0];
        double[] El2 = new double[n];
        double sumEl2 = 0;
        for (int i = 0; i < n; i++) {
            double Dl = C[i] * avgD[i];
            double El2v = H[i] * avgDc[i];
            El2[i] = El2v;
            sumEl2 += El2v;
            Gl[i] = Dl / sumDl * pinZ;
        }
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                Hl[i] = Gl[i];
            } else {
                Hl[i] = El2[i] / (sumEl2 - El2[0]) * (pinCZ - Gl[0]);
            }
            Il[i] = Gl[i] - Hl[i];
        }
        return writeWycSplitWorkbook(period, cities, orderArr, C, Dtax, Etax, H, I, avgD, avgDc, Gl, Hl, Il, pinK, pinCK, pinZ, pinCZ, orderSum);
    }
 
    private byte[] writeWycSplitWorkbook(String period, List<String> cities, double[] orders, double[] C, double[] Dtax,
                                         double[] Etax, double[] H, double[] I, double[] avgD, double[] avgDc,
                                         double[] Gl, double[] Hl, double[] Il,
                                         double pinK, double pinCK, double pinZ, double pinCZ, double orderSum) throws Exception {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sh = wb.createSheet(period + "拆分");
        int n = cities.size();
        // 客运量块
        Row t = sh.createRow(0);
        t.createCell(0).setCellValue("网约车客运量拆分(万人)  报表期:" + period);
        String[] head1 = {"市州", "订单总数(单)", "网约车客运量(万人)", "巡游出租客运量(万人)", "巡游出租城市内客运量(万人)", "城市内占比", "网约车城市内客运量(万人)", "网约车城际城乡客运量(万人)"};
        Row h1 = sh.createRow(1);
        for (int j = 0; j < head1.length; j++) h1.createCell(j).setCellValue(head1[j]);
        int r = 2;
        writeWycRow(sh, r++, "全省", orderSum, pinK, sum(Dtax), sum(Etax), pinK > 0 ? pinCK / pinK : 0, pinCK, pinK - pinCK);
        for (int i = 0; i < n; i++) {
            writeWycRow(sh, r++, cities.get(i), orders[i], C[i], Dtax[i], Etax[i], Dtax[i] > 0 ? Etax[i] / Dtax[i] : 0, H[i], I[i]);
        }
        r += 1;
        Row t2 = sh.createRow(r++);
        t2.createCell(0).setCellValue("旅客周转量拆分(万人公里)");
        String[] head2 = {"市州", "出租车平均运距(公里)", "出租车城市内平均运距(公里)", "网约车旅客周转量(万人公里)", "网约车城市内旅客周转量(万人公里)", "网约车城际城乡旅客周转量(万人公里)"};
        Row h2 = sh.createRow(r++);
        for (int j = 0; j < head2.length; j++) h2.createCell(j).setCellValue(head2[j]);
        writeWycRow2(sh, r++, "全省", 0, 0, pinZ, pinCZ, pinZ - pinCZ);
        for (int i = 0; i < n; i++) {
            writeWycRow2(sh, r++, cities.get(i), avgD[i], avgDc[i], Gl[i], Hl[i], Il[i]);
        }
        r += 1;
        String[] notes = {
            "注:1) 客运量 C = 订单占比 × 全省网约车客运量;城市内 H:武汉=自身,其余按 G 剩余占比分摊至全省城市内 pin;城际城乡 = 总量 - 城市内。",
            "    2) 周转量 G = 客运量 × 巡游出租平均运距 的占比 × 全省网约车周转量 pin;城市内周转量同法(用城市内平均运距)。",
            "    3) 输入:订单 = 当月分析报告附件1(17 市州订单数);全省 pin = 网约车总数.xlsx;巡游出租 = 巡游出租月报(系统导入,含平均运距)。",
            "    4) 武汉无城际城乡;数值保留全精度,界面/文件显示两位小数。"
        };
        for (String nt : notes) {
            Row nr = sh.createRow(r++);
            nr.createCell(0).setCellValue(nt);
        }
        for (int j = 0; j < 8; j++) sh.setColumnWidth(j, 18 * 256);
        return toBytes(wb);
    }
 
    private void writeWycRow(Sheet sh, int r, String name, double ord, double k, double dt, double et, double f, double h, double i) {
        Row row = sh.createRow(r);
        row.createCell(0).setCellValue(name);
        row.createCell(1).setCellValue(ord);
        row.createCell(2).setCellValue(k);
        row.createCell(3).setCellValue(dt);
        row.createCell(4).setCellValue(et);
        row.createCell(5).setCellValue(f);
        row.createCell(6).setCellValue(h);
        row.createCell(7).setCellValue(i);
    }
 
    private void writeWycRow2(Sheet sh, int r, String name, double ad, double adc, double g, double h, double i) {
        Row row = sh.createRow(r);
        row.createCell(0).setCellValue(name);
        row.createCell(1).setCellValue(ad);
        row.createCell(2).setCellValue(adc);
        row.createCell(3).setCellValue(g);
        row.createCell(4).setCellValue(h);
        row.createCell(5).setCellValue(i);
    }
 
    private double sum(double[] a) {
        double s = 0;
        for (double v : a) s += v;
        return s;
    }
 
    /** 读取 docs/城市客运/网约车订单及全省总量.xlsx:按 period(yyyy-MM) 行取 pin(B-E) 与 17 市州订单(G-W) */
    private Map<String, Object> loadWycMonthlyInput(String period, String mode) throws Exception {
        String target = period;
        File f;
        try {
            f = resolveAnyTemplate(templateDir, "网约车订单及全省总量.xlsx");
        } catch (Exception e) {
            throw new RuntimeException("未找到《网约车订单及全省总量.xlsx》,请放到 docs/城市客运 目录");
        }
        try (InputStream in = new FileInputStream(f);
             org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(in)) {
            org.apache.poi.ss.usermodel.Sheet sh = wb.getSheetAt(0);
            org.apache.poi.ss.usermodel.DataFormatter df = new org.apache.poi.ss.usermodel.DataFormatter();
            List<String> cities = RegionUtil.cityList();
            int n = cities.size();
            for (int i = 1; i <= sh.getLastRowNum(); i++) {
                Row row = sh.getRow(i);
                if (row == null) continue;
                Cell c0 = row.getCell(0);
                if (c0 == null) continue;
                String m = df.formatCellValue(c0).trim();
                if (m == null || m.isEmpty()) continue;
                if (m.length() > 7) m = m.substring(0, 7);
                if (!target.equals(m)) continue;
                Map<String, Object> map = new HashMap<>();
                map.put("pinK", cellNum(row, 1, df, 0.0));
                map.put("pinCK", cellNum(row, 2, df, 0.0));
                map.put("pinZ", cellNum(row, 3, df, 0.0));
                map.put("pinCZ", cellNum(row, 4, df, 0.0));
                double[] orders = new double[n];
                double orderSum = 0;
                for (int k = 0; k < n; k++) {
                    orders[k] = cellNum(row, 6 + k, df, 0.0);
                    orderSum += orders[k];
                }
                if (orderSum <= 0) {
                    orderSum = cellNum(row, 5, df, 0.0); // F 列合计兜底
                }
                map.put("orders", orders);
                map.put("orderSum", orderSum);
                return map;
            }
            return null;
        }
    }
 
    private double cellNum(Row row, int idx, org.apache.poi.ss.usermodel.DataFormatter df, double def) {
        Cell c = row.getCell(idx);
        if (c == null) return def;
        if (c.getCellType() == CellType.NUMERIC) return c.getNumericCellValue();
        String s = df.formatCellValue(c).replace(",", "").trim();
        if (s == null || s.isEmpty()) return def;
        try {
            return Double.parseDouble(s);
        } catch (Exception e) {
            return def;
        }
    }
 
    /** 巡游出租月报(DB)按月加载:市州 -> [客运量, 周转量, 城市内客运量, 城市内周转量, 平均运距, 城市内平均运距] */
    private Map<String, double[]> taxiMonthlyOf(String period) {
        Map<String, double[]> map = new java.util.LinkedHashMap<>();
        List<CityTaxiMonthly> rows = cityTaxiMapper.selectList(
            new LambdaQueryWrapper<CityTaxiMonthly>().eq(CityTaxiMonthly::getReportPeriod, period));
        for (CityTaxiMonthly r : rows) {
            String city = RegionUtil.normalizeCityName(r.getCity());
            if (city == null || city.isEmpty()) city = "未知";
            double[] a = map.computeIfAbsent(city, k -> new double[6]);
            a[0] += nz(r.getPassengerVolume());
            a[1] += nz(r.getTurnover());
            a[2] += nz(r.getPassengerCity());
            a[3] += nz(r.getTurnoverCity());
            if (r.getAvgDistance() != null && r.getAvgDistance() > 0) a[4] = r.getAvgDistance();
            if (r.getAvgDistanceCity() != null && r.getAvgDistanceCity() > 0) a[5] = r.getAvgDistanceCity();
        }
        for (double[] a : map.values()) {
            if (a[4] <= 0 && a[0] > 0) a[4] = round(a[1] / a[0], 2);
            if (a[5] <= 0 && a[2] > 0) a[5] = round(a[3] / a[2], 2);
        }
        return map;
    }
 
    /** 定位同期备份母版(清理前的完整月母版),用于把骨架母版缺失的 5-7 月月度块按备份还原;
     *  与 resolveAnyTemplate 相同策略:在配置目录、user.dir 相对目录、上级目录三个候选根中扫描 */
    private File resolveSummaryDonor(int year, int month) {
        String rel = summaryTemplateDir;
        while (rel.startsWith("./")) rel = rel.substring(2);
        String[] roots = {
            summaryTemplateDir,
            System.getProperty("user.dir") + "/" + rel,
            System.getProperty("user.dir") + "/../" + rel
        };
        for (String root : roots) {
            if (root == null || root.trim().isEmpty()) continue;
            File dir = new File(root);
            File[] files = dir.listFiles((d, name) ->
                name.startsWith("_备份_") && name.contains(year + "年" + month + "月") && name.contains("道路运输量汇总表"));
            if (files != null && files.length > 0) return files[0];
        }
        return null;
    }
 
    /** 按同期备份母版还原月度块:凡备份有内容而母版同格缺失/内容不同(清理时被裁掉的 5-7 月值与累计公式)的单元格,整格回填并覆盖 */
    private int restoreSummaryMonthlyBlocks(XSSFWorkbook wb, XSSFWorkbook donor) {
        int restored = 0;
        for (int i = 0; i < donor.getNumberOfSheets(); i++) {
            Sheet ds = donor.getSheetAt(i);
            if (ds == null) continue;
            Sheet bs = wb.getSheet(ds.getSheetName());
            if (bs == null) continue;
            // 中口径排名页 A..H 陈旧左块已于 2026-09-09 拍板整块删除:该区域永不从备份母版回填,避免“生成又复活”
            boolean skipMidRankLeftBlock = "中口径排名".equals(ds.getSheetName());
            // 《 货运》页 2025..2021 年度块(T 列起,含各年累计/累计同比列)在母版里是「市州派生公式」,
            // 备份母版同区仍是旧的写死常量(全省规下等),回填会让 2021/2022/2023 累计又对不上(“生成又复活”),故该区不回填。
            boolean skipFreightHistoryBlocks = " 货运".equals(ds.getSheetName());
            for (int r = ds.getFirstRowNum(); r <= ds.getLastRowNum(); r++) {
                Row dr = ds.getRow(r);
                if (dr == null) continue;
                Row br = bs.getRow(r);
                for (Cell dc : dr) {
                    if (dc == null) continue;
                    if (skipMidRankLeftBlock && dc.getColumnIndex() < 8) continue;
                    if (skipFreightHistoryBlocks && dc.getColumnIndex() >= 19) continue; // T 列及以后 = 2025..2021 年度块
                    CellType t = dc.getCellType();
                    if (t == CellType.BLANK) continue;
                    String dtx = summaryCellText(dc);
                    if (dtx == null || dtx.isEmpty()) continue;
                    Cell bc = (br == null) ? null : br.getCell(dc.getColumnIndex());
                    if (bc != null && dtx.equals(summaryCellText(bc))) continue;
                    Row tr = (bc != null) ? br : bs.getRow(r);
                    if (tr == null) tr = bs.createRow(r);
                    Cell tc = (bc != null) ? bc : tr.getCell(dc.getColumnIndex());
                    if (tc == null) tc = tr.createCell(dc.getColumnIndex());
                    tc.setBlank();
                    applyDonorNumberFormat(wb, tc, dc);
                    if (t == CellType.FORMULA) {
                        String f = dc.getCellFormula();
                        if (f != null) tc.setCellFormula(f);
                    } else if (t == CellType.STRING) {
                        tc.setCellValue(dc.getStringCellValue());
                    } else if (t == CellType.NUMERIC) {
                        tc.setCellValue(dc.getNumericCellValue());
                    } else if (t == CellType.BOOLEAN) {
                        tc.setCellValue(dc.getBooleanCellValue());
                    }
                    restored++;
                }
            }
        }
        return restored;
    }
 
    /** 还原回填时同步 donor(已做版式统一)的数字格式,保证同比/数值/排名格显示格式与母版一致 */
    private void applyDonorNumberFormat(XSSFWorkbook wb, Cell dst, Cell src) {
        if (wb == null || dst == null || src == null) return;
        try {
            String fmt = src.getCellStyle().getDataFormatString();
            if (fmt == null || fmt.isEmpty() || "General".equalsIgnoreCase(fmt)) return;
            org.apache.poi.ss.usermodel.DataFormat df = wb.createDataFormat();
            short idx = df.getFormat(fmt);
            CellStyle cur = dst.getCellStyle();
            if (cur != null && idx == cur.getDataFormat()) return;
            CellStyle ns = wb.createCellStyle();
            if (cur != null) ns.cloneStyleFrom(cur);
            ns.setDataFormat(idx);
            dst.setCellStyle(ns);
        } catch (Exception ignore) {
            // 个别格式异常不阻塞还原
        }
    }
    /** 单元格内容文本化(公式带 = 前缀),用于跨簿比对 */
    private String summaryCellText(Cell c) {
        if (c == null) return null;
        switch (c.getCellType()) {
            case FORMULA: {
                String f = c.getCellFormula();
                return f == null ? "" : "=" + f;
            }
            case STRING:
                return c.getStringCellValue();
            case NUMERIC:
                return Double.toString(c.getNumericCellValue());
            case BOOLEAN:
                return Boolean.toString(c.getBooleanCellValue());
            default:
                return "";
        }
    }
 
    /** 把 8 个台账/合成页裁剪到 keepMonths 月:
     *  月度值列 2026 年 m 月 = 第 3+2*(m-1) 列(C/E/G/I/K/M/O),同比列为其后一列;
     *  累计 Q 列 = C+E+...+O,累计同比 R 列分母 = 2025 年同月列 T/V/X/Z/AB/AD/AF。
     *  裁剪 = 数据行(含累计公式且任一 2026 月度列有内容)中 keepMonths+1..7 月的当月值/同比单元格清空(表头保留),
     *  并把该行累计公式去掉对应 +列,累计同比分母去掉对应 2025 列。 */
    private int trimSummaryMonthlySheets(XSSFWorkbook wb, int keepMonths) {
        String[] scope = {" 货运", "班线包车", "城市客运", "公交", "出租", "网约车", "轨道、轮渡", "中口径明细"};
        java.util.Set<String> scopeSet = new java.util.HashSet<>();
        for (String s : scope) scopeSet.add(s);
        int touched = 0;
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sh = wb.getSheetAt(i);
            if (sh == null || !scopeSet.contains(sh.getSheetName())) continue;
            for (int r = sh.getFirstRowNum(); r <= sh.getLastRowNum(); r++) {
                Row row = sh.getRow(r);
                if (row == null) continue;
                Cell q = row.getCell(16); // Q 列 = 累计
                if (q == null || q.getCellType() != CellType.FORMULA) continue;
                boolean hasMonthly = false;
                for (int m = 1; m <= 7 && !hasMonthly; m++) {
                    Cell v = row.getCell(2 + 2 * (m - 1)); // C,E,G,I,K,M,O
                    if (v != null && v.getCellType() != CellType.BLANK) hasMonthly = true;
                }
                if (!hasMonthly) continue; // 排除页首/页中跨页签校验公式行(无月度值)
                int excelRow = r + 1; // 公式内引用为 Excel 1-based 行号
                for (int m = keepMonths + 1; m <= 7; m++) {
                    int valueCol = 2 + 2 * (m - 1);     // 2026 当月值列
                    int yoyCol = valueCol + 1;          // 当月同比列
                    String vLetter = colLetter(valueCol);
                    Cell vc = row.getCell(valueCol);
                    if (vc != null) {
                        vc.setBlank();
                        touched++;
                    }
                    Cell yc = row.getCell(yoyCol);
                    if (yc != null) {
                        yc.setBlank();
                        touched++;
                    }
                    String fq = q.getCellFormula();
                    if (fq != null && fq.contains("+" + vLetter + excelRow)) {
                        q.setCellFormula(fq.replace("+" + vLetter + excelRow, ""));
                        touched++;
                    }
                    Cell rc = row.getCell(17); // R 列 = 累计同比
                    if (rc != null && rc.getCellType() == CellType.FORMULA) {
                        String fr = rc.getCellFormula();
                        int lastCol = 19 + 2 * (m - 1); // 2025 同月列 T..AF(0-based:T=19,AF=31)
                        String lLetter = colLetter(lastCol);
                        String txt = fr;
                        if (txt != null && txt.contains("," + lLetter + excelRow)) {
                            txt = txt.replace("," + lLetter + excelRow, "");
                            touched++;
                        }
                        if (txt != null && txt.contains("+" + lLetter + excelRow)) {
                            txt = txt.replace("+" + lLetter + excelRow, "");
                            touched++;
                        }
                        if (txt != null && !txt.equals(fr)) rc.setCellFormula(txt);
                    }
                }
            }
        }
        return touched;
    }
 
    /** 汇总工作簿年份动态化:仅把 2026年 前缀替换为目标年(2025/2024 参考列不动) */
    private void dynamicSummaryYear(XSSFWorkbook wb, int year) {
        String oldPrefix = "2026年";
        String newPrefix = year + "年";
        if (oldPrefix.equals(newPrefix)) return;
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sh = wb.getSheetAt(i);
            if (sh == null) continue;
            for (Row row : sh) {
                if (row == null) continue;
                for (Cell c : row) {
                    if (c == null || c.getCellType() != CellType.STRING) continue;
                    String v = c.getStringCellValue();
                    if (v != null && v.contains(oldPrefix)) {
                        c.setCellValue(v.replace(oldPrefix, newPrefix));
                    }
                }
            }
        }
    }
 
    /** 生成_投资报表系列.zip:预安排 + 十五五 + 亿元 + 经济强县 */
    public byte[] exportInvestSeries(String period, String mode) throws Exception {
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        try (java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(bos)) {
            addZipEntry(zos, "生成_全省预安排计划进度情况汇总.xls", investPlan(period, mode));
            addZipEntry(zos, "生成_十五五规划物流项目进展情况.xls", investFiveYearLogistics(period, mode));
            addZipEntry(zos, "生成_湖北省(客货站场)亿元投资项目.xls", investBillion(period, mode));
            addZipEntry(zos, "生成_经济强县交通物流基础设施投资统计报表.xls", investCounty(period, mode));
        }
        return bos.toByteArray();
    }
 
    /** 生成_所选报表.zip:按 type 列表打包(前端"生成选中"多选时用) */
    public byte[] exportSelectedSeries(String period, String mode, List<String> types) throws Exception {
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        try (java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(bos)) {
            if (types != null) {
                for (String t : types) {
                    if (t == null || t.trim().isEmpty()) continue;
                    byte[] data = selectedReportData(period, mode, t);
                    if (data != null) {
                        String name = REPORT_FILE_NAMES.get(t);
                        if (name == null) name = "生成_" + t + ".xlsx";
                        addZipEntry(zos, name, data);
                    }
                }
            }
        }
        return bos.toByteArray();
    }
 
    private byte[] selectedReportData(String period, String mode, String type) throws Exception {
        switch (type) {
            case "cityDetail": return exportCityDetail(period, mode);
            case "freightRank": return exportFreightRank(period, mode);
            case "turnoverRank": return exportTurnoverRank(period, mode);
            case "passengerCityDetail": return exportPassengerCityDetail(period, mode);
            case "passengerMidDetail": return exportPassengerMidDetail(period, mode);
            case "passengerMidRank": return exportPassengerMidRank(period, mode);
            case "passengerMidAnalysis": return exportPassengerMidAnalysis(period, mode);
            case "summaryWorkbook": return exportSummaryWorkbook(period, mode);
            case "energySummary": return exportEnergySummary(period);
            case "investPlan": return investPlan(period, mode);
            case "investFiveYearLogistics": return investFiveYearLogistics(period, mode);
            case "investBillion": return investBillion(period, mode);
            case "investCounty": return investCounty(period, mode);
            case "cityBusDetail": return exportCityBusDetail(period, mode);
            case "cityTaxiDetail": return exportCityTaxiDetail(period, mode);
            case "cityRailFerryDetail": return exportCityRailFerryDetail(period, mode);
            case "cityPassengerCitySum": return exportCityPassengerCitySum("生成_城市客运客运量分市州累计.xlsx", period, mode);
            case "cityPassengerDetailSum": return exportCityPassengerCitySum("生成_城市客运各市州明细表.xlsx", period, mode);
            case "cityPassengerSummary": return exportCityPassengerSummary(period, mode);
            case "wycSplit": return exportWycSplit(period, mode);
            default: return null;
        }
    }
 
    private static final Map<String, String> REPORT_FILE_NAMES = new HashMap<>();
    static {
        REPORT_FILE_NAMES.put("cityDetail", "生成_货运量分市州明细.xlsx");
        REPORT_FILE_NAMES.put("freightRank", "生成_货运量排名.xlsx");
        REPORT_FILE_NAMES.put("turnoverRank", "生成_周转量排名.xlsx");
        REPORT_FILE_NAMES.put("passengerCityDetail", "生成_公路旅客分市州明细.xlsx");
        REPORT_FILE_NAMES.put("passengerMidDetail", "生成_公路旅客中口径明细.xlsx");
        REPORT_FILE_NAMES.put("passengerMidRank", "生成_公路旅客中口径排名.xlsx");
        REPORT_FILE_NAMES.put("passengerMidAnalysis", "生成_公路旅客中口径分析.xlsx");
        REPORT_FILE_NAMES.put("energySummary", "生成_能运汇总表.xlsx");
        REPORT_FILE_NAMES.put("investPlan", "生成_全省预安排计划进度情况汇总.xls");
        REPORT_FILE_NAMES.put("investFiveYearLogistics", "生成_十五五规划物流项目进展情况.xls");
        REPORT_FILE_NAMES.put("investBillion", "生成_湖北省(客货站场)亿元投资项目.xls");
        REPORT_FILE_NAMES.put("investCounty", "生成_经济强县交通物流基础设施投资统计报表.xls");
        REPORT_FILE_NAMES.put("cityBusDetail", "生成_城市公交客运量分市州明细.xlsx");
        REPORT_FILE_NAMES.put("cityTaxiDetail", "生成_巡游出租客运量分市州明细.xlsx");
        REPORT_FILE_NAMES.put("cityRailFerryDetail", "生成_轨道轮渡客运量分市州明细.xlsx");
        REPORT_FILE_NAMES.put("cityPassengerCitySum", "生成_城市客运客运量分市州累计.xlsx");
        REPORT_FILE_NAMES.put("cityPassengerDetailSum", "生成_城市客运各市州明细表.xlsx");
        REPORT_FILE_NAMES.put("cityPassengerSummary", "生成_城市客运汇总.xlsx");
        REPORT_FILE_NAMES.put("summaryWorkbook", "生成_道路运输量汇总表.xlsx");
        REPORT_FILE_NAMES.put("wycSplit", "生成_网约车客运量拆分表.xlsx");
    }
 
    private void addZipEntry(java.util.zip.ZipOutputStream zos, String name, byte[] data) throws Exception {
        zos.putNextEntry(new java.util.zip.ZipEntry(name));
        zos.write(data);
        zos.closeEntry();
    }
 
    /** 城市客运模板填充:以目录下模板为底稿,保留标题/表头/合并/公式/样式,仅替换数据区市州月度值 */
    public byte[] exportCityByTemplate(String templateFileName, String period, String mode,
                                       Map<Integer, Map<String, double[]>> monthCity,
                                       Map<Integer, Map<String, double[]>> lastYearMonthCity) throws Exception {
        int month = monthOf(period, mode);
        int year = Integer.parseInt(period.split("-")[0]);
        File template = resolveTemplate(templateFileName);
        try (InputStream in = new FileInputStream(template);
             XSSFWorkbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheetAt(0);
            dynamicTitle(sheet, year, month); // 标题按报表期动态化(不写死年份/月份)
            // 结构守卫:数据区应为 全省+17市州 × 4 指标 = 72 行(1-based 第5行起)
            if (sheet.getLastRowNum() < 75) {
                throw new RuntimeException("城市客运模板数据区行数不足,请确认模板未改版: " + templateFileName);
            }
            List<String> areas = new java.util.ArrayList<>();
            areas.add("全省");
            areas.addAll(RegionUtil.cityList());
            // 模板数据区:1-based 第5行起,每地区4行(客运量/周转量/城市内客运量/城市内周转量)
            int rowIdx = 4;
            for (int a = 0; a < areas.size(); a++) {
                String area = areas.get(a);
                for (int k = 0; k < 4; k++) {
                    Row row = sheet.getRow(rowIdx);
                    if (row == null) row = sheet.createRow(rowIdx);
                    if (a > 0) {
                        // 2026年 1..N 月:库内有值则填(2位小数),无值清空模板样例
                        for (int m = 1; m <= month; m++) {
                            double v = cityVal(monthCity, area, m, k);
                            Cell c = row.getCell(2 + (m - 1) * 2);
                            if (c == null) c = row.createCell(2 + (m - 1) * 2);
                            if (v == 0) c.setBlank(); else c.setCellValue(round(v, 2));
                        }
                        // 2025年 1..12 月:库内有去年数据则填,否则清空模板样例
                        for (int m = 1; m <= 12; m++) {
                            double v = cityVal(lastYearMonthCity, area, m, k);
                            Cell c = row.getCell(19 + (m - 1) * 2);
                            if (c == null) c = row.createCell(19 + (m - 1) * 2);
                            if (v == 0) c.setBlank(); else c.setCellValue(round(v, 2));
                        }
                    }
                    rowIdx++;
                }
            }
            // 重算公式缓存值(全省求和/同比/累计,保留公式;求值异常忽略)
            try {
                wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
            } catch (Exception e) {
                log.warn("城市客运模板公式求值失败: {}", e.getMessage());
            }
            clearFormulaErrorCells(sheet, 4, sheet.getLastRowNum(), 3, 39);
            fixCityMonthHeaderText(sheet);
            return toBytes(wb);
        }
    }
 
    /** 清空求值结果为错误的公式单元格(模板跨簿同比公式残留 #REF! 时移除公式,避免导出显示错误) */
    private void clearFormulaErrorCells(Sheet sheet, int startRow, int endRow, int startCol, int endCol) {
        for (int r = startRow; r <= endRow; r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            for (int c = startCol; c <= endCol; c++) {
                Cell cell = row.getCell(c);
                if (cell == null || cell.getCellType() != CellType.FORMULA) continue;
                try {
                    if (cell.getCachedFormulaResultType() == CellType.ERROR) cell.setBlank();
                } catch (Exception ignore) {
                    // 缓存类型读取失败时忽略
                }
            }
        }
    }
 
    /** 清空工作簿全部工作表内求值结果为错误的公式单元格(跨簿/跨表 #REF!、缺去年数据 #DIV/0! 等) */
    private void clearFormulaErrorsAll(XSSFWorkbook wb) {
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet s = wb.getSheetAt(i);
            clearFormulaErrorCells(s, 2, s.getLastRowNum(), 1, 60);
        }
    }
 
    /** 城市客运类模板表头月份列若为未格式化日期序列号(如 46082),改写为「YYYY年M月」文本 */
    private void fixCityMonthHeaderText(Sheet sheet) {
        for (int r = 0; r <= Math.min(sheet.getLastRowNum(), 20); r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            Cell nameCell = row.getCell(0);
            String nameText = nameCell == null ? null : cellText(nameCell);
            if (nameText == null || !nameText.contains("地区")) continue;
            for (int c = 2; c <= 40; c += 2) {
                Cell cell = row.getCell(c);
                if (cell == null || cell.getCellType() != CellType.NUMERIC) continue;
                double v = cell.getNumericCellValue();
                if (v < 40000 || v > 60000) continue;
                java.util.Date d = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(v);
                cell.setCellValue(new java.text.SimpleDateFormat("yyyy年M月").format(d));
            }
        }
    }
 
    /** 模板文件定位:配置目录 → user.dir → user.dir/.. 逐级回退 */
    private File resolveTemplate(String fileName) throws Exception {
        return resolveAnyTemplate(templateDir, fileName);
    }
 
    /** 公路旅客输出模板定位(docs/公路旅客+能耗/输出) */
    private File resolvePassengerTemplate(String fileName) throws Exception {
        return resolveAnyTemplate(passengerTemplateDir, fileName);
    }
 
    /** 通用模板文件定位(用于城市客运/能耗等不同模板目录) */
    private File resolveAnyTemplate(String dir, String fileName) throws Exception {
        String rel = dir;
        while (rel.startsWith("./")) rel = rel.substring(2);
        String[] roots = {
            dir,
            System.getProperty("user.dir") + "/" + rel,
            System.getProperty("user.dir") + "/../" + rel
        };
        for (String root : roots) {
            if (root == null || root.trim().isEmpty()) continue;
            File f = new File(root, fileName);
            if (f.exists() && f.isFile()) return f;
        }
        throw new RuntimeException("未找到模板文件: " + fileName);
    }
 
    /** 城市客运模板标题动态化:扫描全部文本单元格,替换写死的年份与累计区间(如 "2026年1-6月" → "2027年1-8月"),列标题 "2026年1月" 只换年份;"1-12月" 整年对比块(2024/2025)标题保持原样 */
    private void dynamicTitle(Sheet sheet, int year, int month) {
        java.util.regex.Pattern yearP = java.util.regex.Pattern.compile("\\d{4}年");
        java.util.regex.Pattern rangeP = java.util.regex.Pattern.compile("(\\d+)-(\\d+)月");
        String cum = cumRange(month);
        for (int r = 0; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;
            for (Cell cell : row) {
                if (cell.getCellType() != CellType.STRING) continue;
                String v = cell.getStringCellValue();
                if (v == null || v.isEmpty() || v.contains("1-12月")) continue; // 整年对比块标题保持原样
                String nv = yearP.matcher(v).replaceAll(year + "年");
                nv = rangeP.matcher(nv).replaceAll(java.util.regex.Matcher.quoteReplacement(cum));
                if (!nv.equals(v)) cell.setCellValue(nv);
            }
        }
    }
 
    /** 城市客运分市州取值(月 → 市州 → 指标数组) */
    private double cityVal(Map<Integer, Map<String, double[]>> monthCity, String area, int m, int idx) {
        Map<String, double[]> cityMap = monthCity.get(m);
        if (cityMap == null) return 0;
        double[] arr = cityMap.get(area);
        if (arr == null) return 0;
        return idx < arr.length ? arr[idx] : 0;
    }
 
    // ==================== 投资模块 4 张领导报表(模板底稿生成,输出 .xls) ====================
 
    /** 加载投资 .xls 模板(HSSFWorkbook,保留列宽/表头/样式/合并) */
    private HSSFWorkbook loadInvestTemplate(String fileName) throws Exception {
        File f = resolveAnyTemplate(investTemplateDir, fileName);
        try (InputStream in = new FileInputStream(f)) {
            return new HSSFWorkbook(in);
        }
    }
 
    /** 模板单元格写文本:先清空(保留样式/合并)再写,null/空白留空 */
    private void hssfText(Row row, int idx, String v) {
        Cell c = row.getCell(idx);
        if (c == null) c = row.createCell(idx);
        c.setBlank();
        if (v != null && !v.trim().isEmpty()) c.setCellValue(v.trim());
    }
 
    /** 模板单元格写数值:先清空(保留样式/合并)再写 */
    private void hssfNum(Row row, int idx, Double v) {
        Cell c = row.getCell(idx);
        if (c == null) c = row.createCell(idx);
        c.setBlank();
        if (v != null) c.setCellValue(v);
    }
 
    /** 删除数据区多余行(fromRow..lastKeepRow-1):先移除合并单元格再删行记录;表头合并不受影响 */
    private void trimHssfTail(HSSFSheet sheet, int fromRow, int lastKeepRow) {
        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--) {
            CellRangeAddress m = sheet.getMergedRegion(i);
            if (m.getFirstRow() >= fromRow && m.getFirstRow() < lastKeepRow) {
                sheet.removeMergedRegion(i);
            }
        }
        for (int r = lastKeepRow - 1; r >= fromRow; r--) {
            HSSFRow row = sheet.getRow(r);
            if (row != null) sheet.removeRow(row);
        }
    }
 
    private void trimHssfTail(HSSFSheet sheet, int fromRow) {
        trimHssfTail(sheet, fromRow, sheet.getLastRowNum() + 1);
    }
 
    /** 新建行并复制模板行样式(含行高),保证与模板数据区外观一致 */
    private HSSFRow createRowLike(HSSFSheet sheet, int rowIdx, HSSFRow styleRow) {
        HSSFRow row = sheet.createRow(rowIdx);
        if (styleRow != null) {
            row.setHeight(styleRow.getHeight());
            for (int c = 0; c < styleRow.getLastCellNum(); c++) {
                HSSFCell sc = styleRow.getCell(c);
                if (sc == null) continue;
                row.createCell(c).setCellStyle(sc.getCellStyle());
            }
        }
        return row;
    }
 
    /** 添加单行合并单元格(与模板数据区合并规则一致) */
    private void addMerge(HSSFSheet sheet, int r, int firstCol, int lastCol) {
        sheet.addMergedRegion(new CellRangeAddress(r, r, firstCol, lastCol));
    }
 
    /** 解析 4 位年份为数值(写模板年份列),无法解析返回 null */
    private Double parseYearNum(String t) {
        if (t == null || t.trim().isEmpty()) return null;
        String s = t.trim().substring(0, Math.min(4, t.trim().length()));
        try {
            return Double.parseDouble(s);
        } catch (Exception e) {
            return null;
        }
    }
 
    /** 解析 6 位 YYYYMM 为数值(写模板开工时间列) */
    private Double parseTimeYMNum(String t) {
        if (t == null || t.trim().isEmpty()) return null;
        String x = padTimeYM(t.trim()).replaceAll("[^0-9]", "");
        if (x.length() < 6) return null;
        try {
            return Double.parseDouble(x.substring(0, 6));
        } catch (Exception e) {
            return null;
        }
    }
 
    private byte[] toBytesHssf(HSSFWorkbook wb) throws Exception {
        applyTwoDecimalFormat(wb);
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            wb.write(out);
            wb.close();
            return out.toByteArray();
        }
    }
 
    /** 投资模板 市州小节结构(表1/表2 动态插行用) */
    private static class InvestSection {
        String blockCategory;
        String city;
        int startRow;
        int styleRowIdx = -1; // 该小节首个项目行(复制样式用)
        List<Integer> projectRows = new ArrayList<>();
        Set<Long> matched = new HashSet<>();
    }
 
    /** 在 pos 行之后插入 n 行:下方行下移(合并区先摘除再按新行号恢复),新行复制 styleRow 样式/行高/单行合并;返回首个新行索引 */
    private int insertRowsDown(HSSFSheet sheet, int pos, int n, HSSFRow styleRow) {
        if (n <= 0) return -1;
        List<CellRangeAddress> below = new ArrayList<>();
        for (int i = sheet.getNumMergedRegions() - 1; i >= 0; i--) {
            CellRangeAddress m = sheet.getMergedRegion(i);
            if (m.getFirstRow() > pos) {
                below.add(m);
                sheet.removeMergedRegion(i);
            }
        }
        int last = sheet.getLastRowNum();
        if (pos < last) {
            sheet.shiftRows(pos + 1, last, n, true, false);
        }
        for (CellRangeAddress m : below) {
            sheet.addMergedRegion(new CellRangeAddress(m.getFirstRow() + n, m.getLastRow() + n, m.getFirstColumn(), m.getLastColumn()));
        }
        int first = pos + 1;
        for (int i = 0; i < n; i++) {
            HSSFRow row = createRowLike(sheet, first + i, styleRow);
            if (styleRow != null) copyRowMerges(sheet, styleRow.getRowNum(), first + i);
        }
        return first;
    }
 
    /** 复制 styleRow 所在行的单行合并区到 toRow(插入行外观与模板行一致) */
    private void copyRowMerges(HSSFSheet sheet, int fromRow, int toRow) {
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress m = sheet.getMergedRegion(i);
            if (m.getFirstRow() == fromRow && m.getLastRow() == fromRow) {
                sheet.addMergedRegion(new CellRangeAddress(toRow, toRow, m.getFirstColumn(), m.getLastColumn()));
            }
        }
    }
 
    /** 投资表 序号重排:表1 按块重置、表2 按市州小节重置、表4 全表连续(模板/插入行统一排序) */
    private void renumberInvestSeq(HSSFSheet sheet, String kind) {
        int seq = 0;
        int lastRow = sheet.getLastRowNum();
        for (int r = 4; r <= lastRow; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;
            String a = nvl(cellText(row.getCell(0)));
            if ("investPlan".equals(kind)) {
                if ("一、客运站场基础设施建设".equals(a) || "二、交通物流基础设施建设".equals(a) || "三、老旧营运货车报废更新".equals(a)) {
                    seq = 0;
                    continue;
                }
            } else if ("investFiveYear".equals(kind)) {
                if (isCityLabel(a)) {
                    seq = 0;
                    continue;
                }
            }
            String b = nvl(cellText(row.getCell(1)));
            if (b.isEmpty()) continue;
            hssfNum(row, 0, (double) (++seq));
        }
    }
 
    /** 表1 项目行填充(模板行/插入行共用;cityText 与模板 B 列格式一致) */
    private void fillInvestPlanProjectRow(HSSFRow row, InvestmentProject p, InvestmentMonthly m, int seq, String cityText, String displayName) {
        hssfNum(row, 0, (double) seq);
        hssfText(row, 1, cityText);
        hssfText(row, 2, valueOf(p.getCounty()));
        hssfText(row, 3, displayName != null && !displayName.trim().isEmpty() ? displayName.trim() : p.getProjectName());
        hssfText(row, 4, valueOf(p.getConstructNature()));
        hssfNum(row, 5, parseYearNum(p.getStartTime()));
        hssfNum(row, 6, parseYearNum(p.getEndTime()));
        hssfNum(row, 7, p.getTotalInvestment());
        hssfText(row, 20, p.getApprovalGk());
        hssfText(row, 21, p.getApprovalCs());
        if (m != null) {
            hssfNum(row, 8, m.getStartCum());
            hssfNum(row, 9, m.getYearPlan());
            hssfText(row, 13, m.getProgressDesc());
            hssfNum(row, 22, m.getYearCum());
            hssfText(row, 23, valueOf(m.getProgressStage()));
        }
    }
 
    /** 表2 项目行填充(模板行/插入行共用) */
    private void fillFiveYearProjectRow(HSSFRow row, InvestmentProject p, InvestmentMonthly m, int seq) {
        hssfNum(row, 0, (double) seq);
        hssfText(row, 1, p.getProjectName());
        hssfText(row, 2, valueOf(p.getBuilderName()));
        hssfNum(row, 3, parseYearNum(p.getStartTime()));
        hssfNum(row, 4, parseYearNum(p.getEndTime()));
        hssfNum(row, 5, p.getTotalInvestment());
        if (m != null) {
            hssfNum(row, 6, m.getStartCum());
            hssfNum(row, 7, m.getYearCum());
            hssfText(row, 8, valueOf(m.getProgressStage()));
            hssfText(row, 9, m.getProgressDesc());
        }
    }
 
    /** 表4 项目行填充(模板行/插入行共用) */
    private void fillCountyProjectRow(HSSFRow row, InvestmentProject p, InvestmentMonthly m, int seq) {
        hssfNum(row, 0, (double) seq);
        hssfText(row, 1, p.getProjectName());
        hssfText(row, 2, valueOf(p.getBuilderName()));
        hssfNum(row, 3, p.getTotalInvestment());
        if (m != null) {
            hssfNum(row, 4, m.getStartCum());
            hssfNum(row, 5, m.getYearPlan());
            hssfNum(row, 6, m.getYearCum());
            hssfNum(row, 7, m.getMonthDone());
            hssfText(row, 8, valueOf(m.getProgressStage()));
            hssfText(row, 9, m.getProgressDesc());
            if (m.getBuildingArea() != null && m.getBuildingArea() > 0) hssfNum(row, 12, m.getBuildingArea());
        }
        hssfText(row, 15, p.getApprovalGk());
        hssfText(row, 16, p.getApprovalCs());
    }
 
    /** 表1:2026年全省预安排计划进度情况汇总(模板底稿原位替换:保留全部行/合并/列宽,仅替换数据;DB 项目多于模板预留行时动态插行) */
    public byte[] investPlan(String period, String mode) throws Exception {
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
        List<InvestmentMonthly> monthlies = investMonthlyMapper.selectList(
            new LambdaQueryWrapper<InvestmentMonthly>()
                .eq(InvestmentMonthly::getReportPeriod, period));
        Map<Long, InvestmentProject> pmap = new HashMap<>();
        List<InvestmentProject> projects = investProjectMapper.selectList(null);
        for (InvestmentProject p : projects) pmap.put(p.getId(), p);
        Map<Long, InvestmentMonthly> mByProject = new HashMap<>();
        for (InvestmentMonthly m : monthlies) mByProject.put(m.getProjectId(), m);
 
        HSSFWorkbook wb = loadInvestTemplate("模板_全省预安排计划进度情况汇总.xls");
        HSSFSheet sheet = wb.getSheetAt(0);
        // 标题/表头年份与累计月份
        hssfText(sheet.getRow(1), 0, year + "年全省客货运站场建设预安排计划表");
        hssfText(sheet.getRow(2), 0, "蓝色项目为" + year + "年市州拟申报资金项目        单位:万元");
        hssfText(sheet.getRow(3), 22, cumRange(month) + "累计完成投资");
        // 结构扫描:块(客运/物流/老旧) -> 市州小节 -> 项目行
        List<InvestSection> sections = new ArrayList<>();
        String blockCategory = null;
        InvestSection cur = null;
        int firstProjectRow = -1;
        int lastRow = sheet.getLastRowNum();
        for (int r = 5; r <= lastRow; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;
            String a = nvl(cellText(row.getCell(0)));
            String b = nvl(cellText(row.getCell(1)));
            String d = nvl(cellText(row.getCell(3)));
            if (r == 5) continue; // 全省合计行单独处理
            if ("一、客运站场基础设施建设".equals(a) || "二、交通物流基础设施建设".equals(a) || "三、老旧营运货车报废更新".equals(a)) {
                blockCategory = a.startsWith("一、") ? "客运站场" : (a.startsWith("二、") ? "物流园区" : null);
                cur = null;
                continue;
            }
            if (a.startsWith("一、") || a.startsWith("二、")) continue; // 预安排计划内/外 分组标题:归当前小节
            if (b.isEmpty() && d.isEmpty()) {
                cur = new InvestSection();
                cur.blockCategory = blockCategory;
                cur.city = templateCityFull(a);
                cur.startRow = r;
                sections.add(cur);
                continue;
            }
            if (cur != null) {
                cur.projectRows.add(r);
                if (cur.styleRowIdx < 0) cur.styleRowIdx = r;
                if (firstProjectRow < 0) firstProjectRow = r;
            }
        }
        HSSFRow globalStyle = firstProjectRow >= 0 ? sheet.getRow(firstProjectRow) : null;
        // 原位逐行替换:合计/块标题/市州小节/分组/项目行;未匹配的模板行清空样例数据(结构/合并/列宽不变)
        int seq = 0;
        int si = -1;
        java.util.Set<Long> usedProjectIds = new HashSet<>();
        for (int r = 5; r <= lastRow; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;
            String a = nvl(cellText(row.getCell(0)));
            String b = nvl(cellText(row.getCell(1)));
            String d = nvl(cellText(row.getCell(3)));
            if (si + 1 < sections.size() && r >= sections.get(si + 1).startRow) si++;
            InvestSection sec = si >= 0 && si < sections.size() ? sections.get(si) : null;
            if (r == 5) { // 全省合计行
                blankInvestRow(row, 25);
                hssfText(row, 0, "合计");
                InvestAgg prov = new InvestAgg();
                for (InvestmentMonthly m : monthlies) prov.add(m, pmap.get(m.getProjectId()));
                writeInvestAggHssf(row, prov);
                continue;
            }
            if ("一、客运站场基础设施建设".equals(a) || "二、交通物流基础设施建设".equals(a)) {
                blockCategory = a.startsWith("一、") ? "客运站场" : "物流园区";
                blankInvestRow(row, 25);
                hssfText(row, 0, a);
                InvestAgg agg = new InvestAgg();
                for (InvestmentMonthly m : monthlies) {
                    InvestmentProject p = pmap.get(m.getProjectId());
                    if (p != null && blockCategory.equals(p.getCategory())) agg.add(m, p);
                }
                writeInvestAggHssf(row, agg);
                seq = 0;
                continue;
            }
            if ("三、老旧营运货车报废更新".equals(a)) {
                blockCategory = null; // 暂无数据源:整块清空
                blankInvestRow(row, 25);
                hssfText(row, 0, a);
                seq = 0;
                continue;
            }
            if (a.startsWith("一、") || a.startsWith("二、")) {
                // 预安排计划内/外 分组标题行:无数据,仅保留标签
                blankInvestRow(row, 25);
                hssfText(row, 0, a);
                continue;
            }
            if (b.isEmpty() && d.isEmpty()) {
                // 市州小节行:重算该市州(当前块类别)合计
                String city = templateCityFull(a);
                blankInvestRow(row, 25);
                hssfText(row, 0, a);
                InvestAgg agg = new InvestAgg();
                for (InvestmentMonthly m : monthlies) {
                    InvestmentProject p = pmap.get(m.getProjectId());
                    if (p != null && city.equals(p.getCity())
                        && (blockCategory == null || blockCategory.equals(p.getCategory()))) agg.add(m, p);
                }
                writeInvestAggHssf(row, agg);
                continue;
            }
            // 项目行:按 模板名称+市州+块类别 匹配 DB 项目,未匹配整行清空
            blankInvestRow(row, 25);
            if (blockCategory == null) continue; // 老旧货车段无数据源
            InvestmentProject p = matchInvest(projects, d, b, blockCategory, usedProjectIds);
            if (p == null) continue;
            usedProjectIds.add(p.getId());
            if (sec != null) sec.matched.add(p.getId());
            InvestmentMonthly m = mByProject.get(p.getId());
            fillInvestPlanProjectRow(row, p, m, ++seq, b, d);
        }
        // 动态插行:DB 项目数 > 模板预留行数时,在市州小节末尾补齐(自底向上插行,避免行号错位)
        for (int i = sections.size() - 1; i >= 0; i--) {
            InvestSection s = sections.get(i);
            if (s.blockCategory == null || s.city == null) continue; // 老旧货车段无数据源
            List<InvestmentProject> extra = new ArrayList<>();
            for (InvestmentProject p : projects) {
                if (s.blockCategory.equals(p.getCategory())
                    && s.city.equals(RegionUtil.normalizeCityName(p.getCity()))
                    && !s.matched.contains(p.getId())) extra.add(p);
            }
            if (extra.isEmpty()) continue;
            HSSFRow styleRow = s.styleRowIdx >= 0 ? sheet.getRow(s.styleRowIdx) : globalStyle;
            int pos = s.projectRows.isEmpty() ? s.startRow : s.projectRows.get(s.projectRows.size() - 1);
            int first = insertRowsDown(sheet, pos, extra.size(), styleRow);
            for (int j = 0; j < extra.size(); j++) {
                HSSFRow row = sheet.getRow(first + j);
                if (row == null) continue;
                InvestmentProject p = extra.get(j);
                fillInvestPlanProjectRow(row, p, mByProject.get(p.getId()), 0, p.getCity(), p.getProjectName());
            }
        }
        renumberInvestSeq(sheet, "investPlan");
        return toBytesHssf(wb);
    }
 
    /** 市州小节/合计行数值:7总投资 8自开始累计 9本年计划 22自年初累计 23实际进度(部/省资金与自筹无数据源,留空) */
    private void writeInvestAggHssf(HSSFRow row, InvestAgg agg) {
        hssfNum(row, 7, agg.total == 0 ? null : agg.total);
        hssfNum(row, 8, agg.startCum == 0 ? null : agg.startCum);
        hssfNum(row, 9, agg.yearPlan == 0 ? null : agg.yearPlan);
        hssfNum(row, 22, agg.yearCum == 0 ? null : agg.yearCum);
        if (agg.yearPlan > 0) hssfNum(row, 23, round(agg.yearCum / agg.yearPlan, 4));
    }
 
 
    private static class InvestAgg {
        double total, startCum, yearPlan, yearCum;
 
        void add(InvestmentMonthly m, InvestmentProject p) {
            if (p != null) total += nz(p.getTotalInvestment());
            startCum += nz(m.getStartCum());
            yearPlan += nz(m.getYearPlan());
            yearCum += nz(m.getYearCum());
        }
 
        private double nz(Double v) {
            return v == null ? 0.0 : v;
        }
    }
 
    /** 表2:"十五五"规划物流项目进展情况(模板底稿原位替换:保留全部行/合并/列宽,仅替换数据;DB 项目多于模板预留行时动态插行) */
    public byte[] investFiveYearLogistics(String period, String mode) throws Exception {
        List<InvestmentMonthly> monthlies = investMonthlyMapper.selectList(
            new LambdaQueryWrapper<InvestmentMonthly>()
                .eq(InvestmentMonthly::getReportPeriod, period));
        Map<Long, InvestmentProject> pmap = new HashMap<>();
        List<InvestmentProject> fiveProjects = new java.util.ArrayList<>();
        for (InvestmentProject p : investProjectMapper.selectList(null)) {
            pmap.put(p.getId(), p);
            if (Integer.valueOf(1).equals(p.getIsFiveYear())) fiveProjects.add(p);
        }
        Map<Long, InvestmentMonthly> mByProject = new HashMap<>();
        for (InvestmentMonthly m : monthlies) mByProject.put(m.getProjectId(), m);
        List<InvestmentMonthly> mine = new java.util.ArrayList<>();
        for (InvestmentMonthly m : monthlies) {
            InvestmentProject p = pmap.get(m.getProjectId());
            if (p != null && Integer.valueOf(1).equals(p.getIsFiveYear())) mine.add(m);
        }
 
        HSSFWorkbook wb = loadInvestTemplate("模板_十五五规划物流项目进展情况.xls");
        HSSFSheet sheet = wb.getSheetAt(1); // 「 分项目投资完成情况」
        int lastRow = sheet.getLastRowNum();
        // 结构扫描:市州小节 -> 项目行
        List<InvestSection> sections = new ArrayList<>();
        InvestSection cur = null;
        int firstProjectRow = -1;
        for (int r = 4; r <= lastRow; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;
            String a = nvl(cellText(row.getCell(0)));
            String b = nvl(cellText(row.getCell(1)));
            if (r == 4) continue; // 全省合计行单独处理
            if (b.isEmpty() && isCityLabel(a)) {
                cur = new InvestSection();
                cur.city = templateCityFull(a);
                cur.startRow = r;
                sections.add(cur);
                continue;
            }
            if (cur != null) {
                cur.projectRows.add(r);
                if (cur.styleRowIdx < 0) cur.styleRowIdx = r;
                if (firstProjectRow < 0) firstProjectRow = r;
            }
        }
        HSSFRow globalStyle = firstProjectRow >= 0 ? sheet.getRow(firstProjectRow) : null;
        String currentCity = null;
        int seq = 0;
        int si = -1;
        for (int r = 4; r <= lastRow; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;
            String a = nvl(cellText(row.getCell(0)));
            String b = nvl(cellText(row.getCell(1)));
            if (si + 1 < sections.size() && r >= sections.get(si + 1).startRow) si++;
            InvestSection sec = si >= 0 && si < sections.size() ? sections.get(si) : null;
            if (r == 4) { // 全省合计行
                blankInvestRow(row, 12);
                hssfText(row, 0, "合计");
                InvestAgg total = new InvestAgg();
                for (InvestmentMonthly m : mine) total.add(m, pmap.get(m.getProjectId()));
                hssfNum(row, 5, total.total == 0 ? null : total.total);
                hssfNum(row, 6, total.startCum == 0 ? null : total.startCum);
                hssfNum(row, 7, total.yearCum == 0 ? null : total.yearCum);
                continue;
            }
            if (b.isEmpty() && isCityLabel(a)) {
                currentCity = templateCityFull(a);
                seq = 0;
                blankInvestRow(row, 12);
                hssfText(row, 0, a);
                InvestAgg agg = new InvestAgg();
                for (InvestmentMonthly m : mine) {
                    InvestmentProject p = pmap.get(m.getProjectId());
                    if (p != null && currentCity.equals(p.getCity())) agg.add(m, p);
                }
                hssfNum(row, 5, agg.total == 0 ? null : agg.total);
                hssfNum(row, 6, agg.startCum == 0 ? null : agg.startCum);
                hssfNum(row, 7, agg.yearCum == 0 ? null : agg.yearCum);
                continue;
            }
            // 项目行:按 模板名称+市州 匹配 DB 十五五项目,未匹配整行清空
            blankInvestRow(row, 12);
            InvestmentProject p = matchInvest(fiveProjects, b, currentCity, null);
            if (p == null) continue;
            if (sec != null) sec.matched.add(p.getId());
            InvestmentMonthly m = mByProject.get(p.getId());
            fillFiveYearProjectRow(row, p, m, ++seq);
        }
        // 动态插行:DB 十五五项目数 > 模板预留行数时,在市州小节末尾补齐
        for (int i = sections.size() - 1; i >= 0; i--) {
            InvestSection s = sections.get(i);
            if (s.city == null) continue;
            List<InvestmentProject> extra = new ArrayList<>();
            for (InvestmentProject p : fiveProjects) {
                if (s.city.equals(RegionUtil.normalizeCityName(p.getCity()))
                    && !s.matched.contains(p.getId())) extra.add(p);
            }
            if (extra.isEmpty()) continue;
            HSSFRow styleRow = s.styleRowIdx >= 0 ? sheet.getRow(s.styleRowIdx) : globalStyle;
            int pos = s.projectRows.isEmpty() ? s.startRow : s.projectRows.get(s.projectRows.size() - 1);
            int first = insertRowsDown(sheet, pos, extra.size(), styleRow);
            for (int j = 0; j < extra.size(); j++) {
                HSSFRow row = sheet.getRow(first + j);
                if (row == null) continue;
                InvestmentProject p = extra.get(j);
                fillFiveYearProjectRow(row, p, mByProject.get(p.getId()), 0);
            }
        }
        renumberInvestSeq(sheet, "investFiveYear");
        return toBytesHssf(wb);
    }
 
    /** 表4:(经济强县项目)湖北省交通物流基础设施投资统计报表(模板底稿原位替换:保留全部行/合并/列宽,仅替换数据;DB 项目多于模板预留行时动态插行) */
    public byte[] investCounty(String period, String mode) throws Exception {
        List<InvestmentMonthly> monthlies = investMonthlyMapper.selectList(
            new LambdaQueryWrapper<InvestmentMonthly>()
                .eq(InvestmentMonthly::getReportPeriod, period));
        Map<Long, InvestmentProject> pmap = new HashMap<>();
        List<InvestmentProject> countyProjects = new java.util.ArrayList<>();
        for (InvestmentProject p : investProjectMapper.selectList(null)) {
            pmap.put(p.getId(), p);
            if (Integer.valueOf(1).equals(p.getIsCounty())) countyProjects.add(p);
        }
        Map<Long, InvestmentMonthly> mByProject = new HashMap<>();
        for (InvestmentMonthly m : monthlies) mByProject.put(m.getProjectId(), m);
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
 
        HSSFWorkbook wb = loadInvestTemplate("模板_经济强县投资统计报表.xls");
        HSSFSheet sheet = wb.getSheetAt(0);
        hssfText(sheet.getRow(0), 0, "(经济强县项目)湖北省交通物流基础设施投资统计报表(" + month + "月月报)");
        hssfText(sheet.getRow(1), 0, "全省货运(物流)基础设施建设投资月报(" + year + "年" + month + "月)");
        int lastRow = Math.min(sheet.getLastRowNum(), 65533);
        int seq = 0;
        int lastProjectRow = -1;
        Set<Long> matched = new HashSet<>();
        for (int r = 4; r <= lastRow; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row == null) continue;
            String name = nvl(cellText(row.getCell(1)));
            if (name.isEmpty()) continue; // 模板尾部空行不处理
            lastProjectRow = r;
            blankInvestRow(row, 20);
            InvestmentProject p = matchInvest(countyProjects, name, null, null);
            if (p == null) continue;
            matched.add(p.getId());
            InvestmentMonthly m = mByProject.get(p.getId());
            fillCountyProjectRow(row, p, m, ++seq);
        }
        // 动态插行:DB 经济强县项目数 > 模板预留行数时,在项目区末尾补齐
        List<InvestmentProject> extra = new ArrayList<>();
        for (InvestmentProject p : countyProjects) {
            if (!matched.contains(p.getId())) extra.add(p);
        }
        if (!extra.isEmpty()) {
            HSSFRow styleRow = sheet.getRow(4);
            int pos = lastProjectRow >= 4 ? lastProjectRow : 3;
            int first = insertRowsDown(sheet, pos, extra.size(), styleRow);
            for (int j = 0; j < extra.size(); j++) {
                HSSFRow row = sheet.getRow(first + j);
                if (row == null) continue;
                InvestmentProject p = extra.get(j);
                fillCountyProjectRow(row, p, mByProject.get(p.getId()), 0);
            }
        }
        renumberInvestSeq(sheet, "investCounty");
        return toBytesHssf(wb);
    }
 
 
    /** 表3:湖北省(客货站场)亿元投资项目(模板底稿 .xls,投资总表 + 规上项目表 两 sheet) */
    public byte[] investBillion(String period, String mode) throws Exception {
        List<InvestmentMonthly> monthlies = investMonthlyMapper.selectList(
            new LambdaQueryWrapper<InvestmentMonthly>()
                .eq(InvestmentMonthly::getReportPeriod, period));
        Map<Long, InvestmentProject> pmap = new HashMap<>();
        for (InvestmentProject p : investProjectMapper.selectList(null)) pmap.put(p.getId(), p);
        List<InvestmentMonthly> mine = new java.util.ArrayList<>();
        for (InvestmentMonthly m : monthlies) {
            InvestmentProject p = pmap.get(m.getProjectId());
            if (p != null && Integer.valueOf(1).equals(p.getIsBillion())) mine.add(m);
        }
        int year = Integer.parseInt(period.split("-")[0]);
        int month = monthOf(period, mode);
 
        HSSFWorkbook wb = loadInvestTemplate("模板_亿元投资项目.xls");
        // ---- sheet1 投资总表:固定骨架,仅填(五)其他 全年预计(全省本年计划折亿) ----
        HSSFSheet s1 = wb.getSheetAt(0);
        hssfText(s1.getRow(0), 0, year + "年固定资产投资预计完成情况");
        double provinceYearPlan = 0;
        for (InvestmentMonthly m : monthlies) provinceYearPlan += nz(m.getYearPlan());
        HSSFRow rowOther = s1.getRow(9);
        for (int c = 1; c <= 5; c++) hssfNum(rowOther, c, null);
        hssfNum(rowOther, 5, provinceYearPlan == 0 ? null : round(provinceYearPlan / 10000, 2));
        // ---- sheet2 规上项目表:表头年份 + 合计 + 项目行 ----
        HSSFSheet s2 = wb.getSheetAt(1);
        hssfText(s2.getRow(0), 0, year + "年交通建设项目储备情况(规模以上项目)");
        hssfText(s2.getRow(2), 5, "截止到" + year + "年" + month + "月底建设状态");
        hssfText(s2.getRow(2), 9, "截至" + (year - 1) + "年年底实际完成投资(万元)");
        hssfText(s2.getRow(2), 10, year + "年1-3月实际完成投资(万元)");
        hssfText(s2.getRow(2), 11, year + "年4月计划完成投资(万元)");
        hssfText(s2.getRow(2), 12, year + "年第二季度计划完成投资(万元)");
        hssfText(s2.getRow(2), 13, year + "年全年计划完成投资(万元)");
        hssfText(s2.getRow(2), 16, year + "年1-4月计划完成投资(万元)");
        hssfText(s2.getRow(2), 17, year + "年4月实际完成投资(万元)");
        hssfText(s2.getRow(2), 18, year + "年1-4月累计完成投资(万元)");
        hssfText(s2.getRow(2), 19, year + "年5月实际完成投资(万元)");
        hssfText(s2.getRow(2), 21, year + "年6月实际完成投资(万元)");
        hssfText(s2.getRow(2), 22, year + "年第二季度实际完成投资(万元)");
        hssfText(s2.getRow(2), 23, year + "年" + month + "月实际完成投资");
        // 合计行 R4(col0-1 属于表头合并区,保留;清空并重写数据列)
        HSSFRow totalRow = s2.getRow(4);
        for (int c = 2; c <= 23; c++) hssfNum(totalRow, c, null);
        InvestAgg total = new InvestAgg();
        for (InvestmentMonthly m : mine) total.add(m, pmap.get(m.getProjectId()));
        hssfNum(totalRow, 8, total.total == 0 ? null : total.total);
        hssfNum(totalRow, 13, total.yearPlan == 0 ? null : total.yearPlan);
        hssfNum(totalRow, 23, sumMonth(mine) == 0 ? null : sumMonth(mine));
        // 项目行:保留模板 R79 注释行
        HSSFRow styleProject = s2.getRow(5);
        trimHssfTail(s2, 5, 79);
        int rowIdx = 5;
        for (InvestmentMonthly m : mine) {
            InvestmentProject p = pmap.get(m.getProjectId());
            HSSFRow row = createRowLike(s2, rowIdx++, styleProject);
            hssfText(row, 0, "湖北省");
            hssfText(row, 1, RegionUtil.shortName(p.getCity()));
            hssfText(row, 2, p.getProjectName());
            hssfText(row, 3, "客运站场".equals(p.getCategory()) ? "综合客运枢纽" : "综合货运枢纽");
            hssfText(row, 5, valueOf(m.getProgressStage()));
            hssfText(row, 6, valueOf(p.getConstructNature()));
            hssfNum(row, 7, parseTimeYMNum(p.getStartTime()));
            hssfNum(row, 8, p.getTotalInvestment());
            hssfNum(row, 13, m.getYearPlan());
            hssfNum(row, 23, m.getMonthDone());
        }
        trimHssfTail(s2, rowIdx, 79);
        return toBytesHssf(wb);
    }
 
    /** 空串归一(cellText 可能返回 null) */
    private String nvl(String v) {
        return v == null ? "" : v.trim();
    }
 
    /** 清空模板数据行指定范围单元格(保留样式与合并单元格) */
    private void blankInvestRow(HSSFRow row, int lastCol) {
        if (row == null) return;
        for (int c = 0; c <= lastCol; c++) {
            Cell cell = row.getCell(c);
            if (cell != null) cell.setBlank();
        }
    }
 
    /** 模板市州短名 -> 规范市州名(武汉->武汉市、林区->神农架林区、恩施州->恩施州) */
    private String templateCityFull(String label) {
        if (label == null) return null;
        String t = label.trim();
        String canon = RegionUtil.normalizeCityName(t);
        for (String city : RegionUtil.cityList()) {
            if (city.equals(canon)) return city;
        }
        for (String city : RegionUtil.cityList()) {
            if (RegionUtil.shortName(city).equals(canon) || RegionUtil.shortName(city).equals(t)) return city;
        }
        return canon;
    }
 
    /** 判断模板行 A 列是否为市州小节标签 */
    private boolean isCityLabel(String label) {
        if (label == null || label.isEmpty()) return false;
        String full = templateCityFull(label);
        return full != null && RegionUtil.cityList().contains(full);
    }
 
    /** 在候选项目池中按名称匹配 DB 项目(可选 市州/类别 过滤 + 县区前缀消歧),未命中返回 null */
    private InvestmentProject matchInvest(List<InvestmentProject> pool, String name, String cityFull, String category) {
        return matchInvest(pool, name, cityFull, category, null);
    }
 
    /** 带已占用排除的匹配:同一 DB 项目只允许填充一行(避免模板一期/二期同名行重复输出) */
    private InvestmentProject matchInvest(List<InvestmentProject> pool, String name, String cityFull, String category, Set<Long> excludeIds) {
        if (name == null || name.trim().isEmpty()) return null;
        String norm = normInvestName(name);
        if (norm.isEmpty()) return null;
        String cityCanon = cityFull == null || cityFull.trim().isEmpty() ? null : RegionUtil.normalizeCityName(cityFull);
        String county = extractInvestCounty(name);
        List<InvestmentProject> cands = new java.util.ArrayList<>();
        for (InvestmentProject p : pool) {
            if (excludeIds != null && excludeIds.contains(p.getId())) continue;
            if (category != null && !category.equals(p.getCategory())) continue;
            if (cityCanon != null && !cityCanon.equals(RegionUtil.normalizeCityName(p.getCity()))) continue;
            cands.add(p);
        }
        if (cands.isEmpty()) return null;
        // 归一化同名多候选:县区前缀消歧(如 罗田县综合物流园 vs 咸丰县综合物流园)
        if (!county.isEmpty()) {
            for (InvestmentProject p : cands) {
                if (normInvestName(p.getProjectName()).equals(norm)
                    && p.getProjectName() != null && p.getProjectName().contains(county)) {
                    return p;
                }
            }
        }
        InvestmentProject best = null;
        int bestScore = Integer.MIN_VALUE;
        for (InvestmentProject p : cands) {
            String pn = normInvestName(p.getProjectName());
            if (pn.isEmpty()) continue;
            int s = investNameScore(norm, pn, county, p.getProjectName());
            if (s > bestScore) {
                bestScore = s;
                best = p;
            }
        }
        return bestScore > 0 ? best : null;
    }
 
    /** 项目名归一化:去括号字符、连接符,罗马数字统一,去省/市/县/区前缀与空白(与审核引擎一致) */
    private String normInvestName(String name) {
        if (name == null) return "";
        String s = name.trim();
        s = s.replaceAll("[((]", "").replaceAll("[))]", "");
        s = s.replaceAll("[·•—-_\\-]", "");
        s = s.replaceAll("[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ]", "I");
        s = s.replaceAll("^(湖北省|武汉市|黄石市|十堰市|宜昌市|襄阳市|鄂州市|荆门市|孝感市|荆州市|黄冈市|咸宁市|随州市|恩施州|仙桃市|潜江市|天门市|神农架林区)", "");
        s = s.replaceAll("^(?:[\\u4e00-\\u9fa5]{2,4}(?:县|市|区))", "");
        s = s.replaceAll("\\s+", "");
        return s;
    }
 
    /** 名称相似度评分:精确>前缀包含>中后部包含>5字>子序列>最长公共子串;县区前缀不一致拒绝/大扣分 */
    private int investNameScore(String norm, String key, String county, String rawB) {
        if (norm.equals(key)) {
            // 归一化后同名:若模板带县区前缀而候选原始名不含该县区 → 拒绝(防 宜都市 vs 当阳市 误配)
            if (county != null && !county.isEmpty() && (rawB == null || !rawB.contains(county))) return -1;
            return 2000;
        }
        if (key.length() < 5 || norm.length() < 5) return -1;
        String shortSide = key.length() <= norm.length() ? key : norm;
        String longSide = key.length() > norm.length() ? key : norm;
        int idx = longSide.indexOf(shortSide);
        int lenDiff = Math.abs(key.length() - norm.length());
        int base;
        if (idx == 0) {
            base = 1000 - lenDiff * 2;
        } else if (idx > 0 && shortSide.length() >= 6) {
            base = 500 - idx * 5 - lenDiff * 2 + shortSide.length();
        } else if (idx > 0 && shortSide.length() == 5 && lenDiff <= 5) {
            String diff = longSide.substring(0, idx) + longSide.substring(idx + shortSide.length());
            if (diff.matches(".*(县|市|州).*")) return -1;
            base = 400 - idx * 5 - lenDiff;
        } else if (isInvestSubsequence(shortSide, longSide) && lenDiff <= 8) {
            base = 300 - lenDiff * 3;
        } else {
            int l = investLcs(norm, key);
            if (l >= 6) {
                base = 200 + l * 5 - lenDiff;
            } else if (l == 5 && lenDiff <= 4 && county != null && !county.isEmpty()
                       && rawB != null && rawB.contains(county)) {
                base = 150 + l * 5 - lenDiff;
            } else {
                return -1;
            }
        }
        if (county != null && !county.isEmpty() && (rawB == null || !rawB.contains(county))) {
            base -= 400;
        }
        return base;
    }
 
    /** 判断短名是否为长名的字符子序列 */
    private boolean isInvestSubsequence(String shortSide, String longSide) {
        int i = 0;
        for (int j = 0; i < shortSide.length() && j < longSide.length(); j++) {
            if (shortSide.charAt(i) == longSide.charAt(j)) i++;
        }
        return i == shortSide.length();
    }
 
    /** 最长公共子串长度 */
    private int investLcs(String a, String b) {
        int n = a.length(), m = b.length();
        if (n == 0 || m == 0) return 0;
        int[][] dp = new int[n + 1][m + 1];
        int max = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (a.charAt(i - 1) == b.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    if (dp[i][j] > max) max = dp[i][j];
                }
            }
        }
        return max;
    }
 
    /** 提取名称中的 县/市/区 前缀(如 咸丰县、宜都市) */
    private String extractInvestCounty(String name) {
        if (name == null) return "";
        java.util.regex.Matcher mm = java.util.regex.Pattern.compile("([\\u4e00-\\u9fa5]{2,4}(?:县|市|区))").matcher(name);
        if (mm.find()) return mm.group(1);
        return "";
    }
 
 
    private double sumMonth(List<InvestmentMonthly> list) {
        double s = 0;
        for (InvestmentMonthly m : list) s += nz(m.getMonthDone());
        return s;
    }
 
    private String padTimeYM(String t) {
        if (t == null) return null;
        String x = t.trim();
        if (x.matches("\\d{4}")) return x + "01";
        return x;
    }
 
    private String valueOf(String v) {
        return v == null || v.trim().isEmpty() ? "" : v.trim();
    }
    private double nz(Double v) {
        return v == null ? 0.0 : v;
    }
    /** 生成表数值列统一样式:保留全精度数值,显示两位小数;整数值与 %/日期/科学计数等既有样式不改变 */
    private void applyTwoDecimalFormat(org.apache.poi.ss.usermodel.Workbook wb) {
        if (wb == null) return;
        org.apache.poi.ss.usermodel.DataFormat df = wb.createDataFormat();
        short fmtTwo = df.getFormat("0.00");
        for (int s = 0; s < wb.getNumberOfSheets(); s++) {
            org.apache.poi.ss.usermodel.Sheet sh = wb.getSheetAt(s);
            if (sh == null) continue;
            for (org.apache.poi.ss.usermodel.Row row : sh) {
                if (row == null) continue;
                for (org.apache.poi.ss.usermodel.Cell cell : row) {
                    if (cell == null) continue;
                    try {
                        CellType ct = cell.getCellType();
                        if (ct != CellType.NUMERIC && ct != CellType.FORMULA) continue;
                        if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) continue;
                        boolean integral = false;
                        if (ct == CellType.NUMERIC) {
                            integral = Math.rint(cell.getNumericCellValue()) == cell.getNumericCellValue();
                        } else if (cell.getCachedFormulaResultType() == CellType.NUMERIC) {
                            integral = Math.rint(cell.getNumericCellValue()) == cell.getNumericCellValue();
                        }
                        if (integral) continue; // 整数不显示 .00(含公式结果为整数的格)
                        org.apache.poi.ss.usermodel.CellStyle cs = cell.getCellStyle();
                        if (cs == null) continue;
                        String f = cs.getDataFormatString();
                        if (f == null) f = "";
                        if (f.contains("%") || f.contains("E") || f.contains("0.00")) continue;
                        boolean plain = f.isEmpty() || "General".equals(f) || "general".equals(f)
                            || "0".equals(f) || "#,##0".equals(f) || "0.0".equals(f) || "#,##0.0".equals(f);
                        if (!plain) continue;
                        org.apache.poi.ss.usermodel.CellStyle ns = wb.createCellStyle();
                        ns.cloneStyleFrom(cs);
                        ns.setDataFormat(fmtTwo);
                        cell.setCellStyle(ns);
                    } catch (Exception ignore) {
                        // 个别单元格格式异常不阻塞导出
                    }
                }
            }
        }
    }
 
    /** 汇总表导出后按内容自动加宽列宽(只加宽不缩窄;跳过合并单元格标题,防长标题把单列撑爆) */
    /** 汇总大表输出前清理母版残留下来的筛选与行隐藏:筛选箭头/隐藏行会让整页看起来“缺数据” */
    private void cleanSummarySheetPresentation(org.apache.poi.ss.usermodel.Workbook wb) {
        if (wb == null) return;
        for (int s = 0; s < wb.getNumberOfSheets(); s++) {
            org.apache.poi.ss.usermodel.Sheet sh = wb.getSheetAt(s);
            if (sh == null) continue;
            if (sh instanceof org.apache.poi.xssf.usermodel.XSSFSheet) {
                org.apache.poi.xssf.usermodel.XSSFSheet xs = (org.apache.poi.xssf.usermodel.XSSFSheet) sh;
                if (xs.getCTWorksheet().isSetAutoFilter()) xs.getCTWorksheet().unsetAutoFilter();
                for (org.apache.poi.ss.usermodel.Row row : xs) {
                    if (row == null) continue;
                    org.apache.poi.xssf.usermodel.XSSFRow xr = (org.apache.poi.xssf.usermodel.XSSFRow) row;
                    if (xr.getCTRow().isSetHidden()) xr.getCTRow().unsetHidden();
                }
            } else {
                sh.setAutoFilter(null);
            }
        }
    }
 
 
    private byte[] toBytes(XSSFWorkbook wb) throws Exception {
        applyTwoDecimalFormat(wb);
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            wb.write(out);
            wb.close();
            return out.toByteArray();
        }
    }
}