#
Junjie
9 小时以前 522aaae547187ed4abc737ead587f646ed0147e2
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
package com.zy.core.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.domain.enums.NotifyMsgType;
import com.zy.asrs.domain.path.StationPathResolvedPolicy;
import com.zy.asrs.domain.vo.StationCycleCapacityVo;
import com.zy.asrs.domain.vo.StationCycleLoopVo;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.NotifyUtils;
import com.zy.common.entity.FindCrnNoResult;
import com.zy.common.model.NavigateNode;
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
import com.zy.common.utils.NavigateUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.move.StationMoveCoordinator;
import com.zy.core.move.StationMoveDispatchMode;
import com.zy.core.move.StationMoveSession;
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.Task;
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.model.protocol.StationTaskBufferItem;
import com.zy.core.service.StationTaskLoopService;
import com.zy.core.thread.StationThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
@Component
public class StationOperateProcessUtils {
    private static final int LOOP_LOAD_RESERVE_EXPIRE_SECONDS = 120;
    private static final int OUT_ORDER_DISPATCH_LIMIT_SECONDS = 2;
    private static final int STATION_COMMAND_DISPATCH_DEDUP_SECONDS = 10;
    private static final int STATION_IDLE_RECOVER_SECONDS = 10;
    private static final int STATION_IDLE_RECOVER_LIMIT_SECONDS = 30;
    private static final int STATION_IDLE_TRACK_EXPIRE_SECONDS = 60 * 60;
    private static final long STATION_MOVE_RESET_WAIT_MS = 1000L;
    private static final String IDLE_RECOVER_CLEARED_MEMO = "idleRecoverRerouteCleared";
 
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private WmsOperateUtils wmsOperateUtils;
    @Autowired
    private NotifyUtils notifyUtils;
    @Autowired
    private NavigateUtils navigateUtils;
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private StationCycleCapacityService stationCycleCapacityService;
    @Autowired
    private StationPathPolicyService stationPathPolicyService;
    @Autowired
    private BasStationOptService basStationOptService;
    @Autowired
    private StationTaskLoopService stationTaskLoopService;
    @Autowired
    private WrkAnalysisService wrkAnalysisService;
    @Autowired
    private StationMoveCoordinator stationMoveCoordinator;
 
    //执行输送站点入库任务
    public synchronized void stationInExecute() {
        try {
            DispatchLimitConfig baseLimitConfig = getDispatchLimitConfig(null, null);
            int[] currentStationTaskCountRef = new int[]{countCurrentStationTask()};
            LoadGuardState loadGuardState = buildLoadGuardState(baseLimitConfig);
 
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
 
                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
 
                List<StationObjModel> list = basDevp.getBarcodeStationList$();
                for (StationObjModel entity : list) {
                    Integer stationId = entity.getStationId();
                    if (!stationMap.containsKey(stationId)) {
                        continue;
                    }
 
                    StationProtocol stationProtocol = stationMap.get(stationId);
                    if (stationProtocol == null) {
                        continue;
                    }
 
                    Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
                    if (lock != null) {
                        continue;
                    }
 
                    //满足自动、有物、有工作号
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.getTaskNo() > 0
                    ) {
                        //检测任务是否生成
                        WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
                        if (wrkMast == null) {
                            continue;
                        }
 
                        if (!Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
                            continue;
                        }
 
                        String locNo = wrkMast.getLocNo();
                        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
                        if (findCrnNoResult == null) {
                            News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
                            continue;
                        }
 
                        Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
                        if (targetStationId == null) {
                            News.taskInfo(wrkMast.getWrkNo(), "{}站点,搜索入库站点失败", stationId);
                            continue;
                        }
 
                        DispatchLimitConfig limitConfig = getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
                        LoopHitResult loopHitResult = findPathLoopHit(limitConfig, stationProtocol.getStationId(), targetStationId, loadGuardState);
 
                        if (isDispatchBlocked(limitConfig, currentStationTaskCountRef[0], loadGuardState, loopHitResult.isThroughLoop())) {
                            return;
                        }
 
                        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
                        if (command == null) {
                            News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
                            continue;
                        }
 
                        Date now = new Date();
                        wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
                        wrkMast.setSourceStaNo(stationProtocol.getStationId());
                        wrkMast.setStaNo(targetStationId);
                        wrkMast.setSystemMsg("");
                        wrkMast.setIoTime(now);
                        wrkMast.setModiTime(now);
                        if (wrkMastService.updateById(wrkMast)) {
                            wrkAnalysisService.markInboundStationStart(wrkMast, now);
                            boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
                            if (offered && stationMoveCoordinator != null) {
                                // 初始入库命令也纳入 session 跟踪,后续停留恢复/绕圈/堵塞重算才能基于同一条路线状态判断。
                                stationMoveCoordinator.recordDispatch(
                                        wrkMast.getWrkNo(),
                                        stationProtocol.getStationId(),
                                        "stationInExecute",
                                        command,
                                        false
                                );
                            }
                            News.info("输送站点入库命令下发成功,站点号={},工作号={},命令数据={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
                            redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
                            loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
                            saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //执行堆垛机输送站点出库任务
    public synchronized void crnStationOutExecute() {
        try {
            DispatchLimitConfig baseLimitConfig = getDispatchLimitConfig(null, null);
            int[] currentStationTaskCountRef = new int[]{countCurrentStationTask()};
            LoadGuardState loadGuardState = buildLoadGuardState(baseLimitConfig);
 
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
                    .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
                    .isNotNull("crn_no")
            );
            List<Integer> outOrderList = getAllOutOrderList();
 
            for (WrkMast wrkMast : wrkMasts) {
                Object infoObj = redisUtil.get(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo());
                if (infoObj == null) {
                    News.info("出库任务{}数据缓存不存在", wrkMast.getWrkNo());
                    continue;
                }
 
                StationObjModel stationObjModel = JSON.parseObject(infoObj.toString(), StationObjModel.class);
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
                if (stationThread == null) {
                    continue;
                }
 
                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
                StationProtocol stationProtocol = stationMap.get(stationObjModel.getStationId());
                if (stationProtocol == null) {
                    continue;
                }
 
                Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId());
                if (lock != null) {
                    continue;
                }
 
                //满足自动、有物、工作号0
                if (stationProtocol.isAutoing()
                        && stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 0
                ) {
                    // 先算当前任务在批次出库中的路径倾向系数,再带着这个系数去决策目标站,
                    // 这样同一批次不同序号任务在排序点、绕圈点和堵塞重算时会得到一致的目标裁决。
                    Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
                    OutOrderDispatchDecision dispatchDecision = resolveOutboundDispatchDecision(
                            stationProtocol.getStationId(),
                            wrkMast,
                            outOrderList,
                            pathLenFactor
                    );
                    Integer moveStaNo = dispatchDecision == null ? null : dispatchDecision.getTargetStationId();
                    if (moveStaNo == null) {
                        continue;
                    }
 
                    DispatchLimitConfig limitConfig = getDispatchLimitConfig(stationProtocol.getStationId(), moveStaNo);
                    LoopHitResult loopHitResult = findPathLoopHit(limitConfig, stationProtocol.getStationId(), moveStaNo, loadGuardState, wrkMast, pathLenFactor);
 
                    if (isDispatchBlocked(limitConfig, currentStationTaskCountRef[0], loadGuardState, loopHitResult.isThroughLoop())) {
                        return;
                    }
 
                    StationCommand command = buildOutboundMoveCommand(
                            stationThread,
                            wrkMast,
                            stationProtocol.getStationId(),
                            moveStaNo,
                            pathLenFactor
                    );
                    if (command == null) {
                        News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
                        continue;
                    }
 
                    Date now = new Date();
                    wrkMast.setWrkSts(WrkStsType.STATION_RUN.sts);
                    wrkMast.setSystemMsg("");
                    wrkMast.setIoTime(now);
                    wrkMast.setModiTime(now);
                    if (wrkMastService.updateById(wrkMast)) {
                        wrkAnalysisService.markOutboundStationStart(wrkMast, now);
                        boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "crnStationOutExecute");
                        if (offered && stationMoveCoordinator != null) {
                            stationMoveCoordinator.recordDispatch(
                                    wrkMast.getWrkNo(),
                                    stationProtocol.getStationId(),
                                    "crnStationOutExecute",
                                    command,
                                    false
                            );
                        }
                        News.info("输送站点出库命令下发成功,站点号={},工作号={},命令数据={}", stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
                        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5);
                        redisUtil.del(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo());
                        currentStationTaskCountRef[0]++;
                        loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
                        saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //执行双工位堆垛机输送站点出库任务
    public synchronized void dualCrnStationOutExecute() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
                    .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
                    .isNotNull("dual_crn_no")
            );
            for (WrkMast wrkMast : wrkMasts) {
                Object infoObj = redisUtil.get(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
                if (infoObj == null) {
                    News.info("出库任务{}数据缓存不存在", wrkMast.getWrkNo());
                    continue;
                }
 
                StationObjModel stationObjModel = JSON.parseObject(infoObj.toString(), StationObjModel.class);
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
                if (stationThread == null) {
                    continue;
                }
 
                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
                StationProtocol stationProtocol = stationMap.get(stationObjModel.getStationId());
                if (stationProtocol == null) {
                    continue;
                }
 
                Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId());
                if (lock != null) {
                    continue;
                }
 
                //满足自动、有物、工作号0
                if (stationProtocol.isAutoing()
                        && stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 0
                ) {
                    Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
                    StationCommand command = buildOutboundMoveCommand(
                            stationThread,
                            wrkMast,
                            stationProtocol.getStationId(),
                            wrkMast.getStaNo(),
                            pathLenFactor
                    );
                    if (command == null) {
                        News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
                        continue;
                    }
 
                    wrkMast.setWrkSts(WrkStsType.STATION_RUN.sts);
                    wrkMast.setSystemMsg("");
                    wrkMast.setIoTime(new Date());
                    if (wrkMastService.updateById(wrkMast)) {
                        boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "dualCrnStationOutExecute");
                        if (offered && stationMoveCoordinator != null) {
                            // 双工位堆垛机转入输送线后同样要登记 session,否则后续重算只能看到 PLC 命令,看不到路线语义。
                            stationMoveCoordinator.recordDispatch(
                                    wrkMast.getWrkNo(),
                                    stationProtocol.getStationId(),
                                    "dualCrnStationOutExecute",
                                    command,
                                    false
                            );
                        }
                        notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN, null);
                        News.info("输送站点出库命令下发成功,站点号={},工作号={},命令数据={}", stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
                        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5);
                        redisUtil.del(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //检测输送站点出库任务执行完成
    public synchronized void stationOutExecuteFinish() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN.sts));
            for (WrkMast wrkMast : wrkMasts) {
                Integer wrkNo = wrkMast.getWrkNo();
                Integer targetStaNo = wrkMast.getStaNo();
                if (wrkNo == null || targetStaNo == null) {
                    continue;
                }
 
                boolean complete = false;
                Integer targetDeviceNo = null;
                BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
                if (basStation != null) {
                    targetDeviceNo = basStation.getDeviceNo();
                    StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
                    if (stationThread != null) {
                        Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
                        StationProtocol stationProtocol = statusMap.get(basStation.getStationId());
                        if (stationProtocol != null && wrkNo.equals(stationProtocol.getTaskNo())) {
                            complete = true;
                        }
                    }
                }
 
                if (complete) {
                    completeStationRunTask(wrkMast, targetDeviceNo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void completeStationRunTask(WrkMast wrkMast, Integer deviceNo) {
        if (wrkMast == null || wrkMast.getWrkNo() == null) {
            return;
        }
        if (stationMoveCoordinator != null) {
            stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
        }
        Date now = new Date();
        wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
        wrkMast.setIoTime(now);
        wrkMast.setModiTime(now);
        wrkMastService.updateById(wrkMast);
        wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
        if (deviceNo != null) {
            notifyUtils.notify(String.valueOf(SlaveType.Devp), deviceNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
        }
        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
    }
 
    // 检测任务转完成
    public synchronized void checkTaskToComplete() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
            for (WrkMast wrkMast : wrkMasts) {
                Integer wrkNo = wrkMast.getWrkNo();
                Integer targetStaNo = wrkMast.getStaNo();
 
                Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
                if (lock != null) {
                    continue;
                }
 
                boolean complete = false;
                BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
                if (basStation == null) {
                    continue;
                }
 
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
                if (stationThread == null) {
                    continue;
                }
 
                Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
                StationProtocol stationProtocol = statusMap.get(basStation.getStationId());
                if (stationProtocol == null) {
                    continue;
                }
 
                if (!stationProtocol.getTaskNo().equals(wrkNo)) {
                    complete = true;
                }
 
                if (complete) {
                    if (stationMoveCoordinator != null) {
                        stationMoveCoordinator.finishSession(wrkNo);
                    }
                    wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                    wrkMast.setIoTime(new Date());
                    wrkMastService.updateById(wrkMast);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //检测输送站点是否运行堵塞
    public synchronized void checkStationRunBlock() {
        try {
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
 
                List<Integer> runBlockReassignLocStationList = new ArrayList<>();
                for (StationObjModel stationObjModel : basDevp.getRunBlockReassignLocStationList$()) {
                    runBlockReassignLocStationList.add(stationObjModel.getStationId());
                }
                List<Integer> outOrderStationIds = basDevp.getOutOrderIntList();
 
                List<StationProtocol> list = stationThread.getStatus();
                for (StationProtocol stationProtocol : list) {
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.getTaskNo() > 0
                            && stationProtocol.isRunBlock()
                    ) {
                        WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                        if (wrkMast == null) {
                            News.info("输送站点号={} 运行阻塞,但无法找到对应任务,工作号={}", stationProtocol.getStationId(), stationProtocol.getTaskNo());
                            continue;
                        }
 
                        Object lock = redisUtil.get(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo());
                        if (lock != null) {
                            continue;
                        }
                        redisUtil.set(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo(), "lock", 15);
 
                        if (shouldUseRunBlockDirectReassign(wrkMast, stationProtocol.getStationId(), runBlockReassignLocStationList)) {
                            executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
                            continue;
                        }
 
                        Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
                        // 运行堵塞不单独决定业务目标站,仍然复用出库排序/绕圈的目标裁决,
                        // 这里只是要求用 run-block 专用算路,并在重发前清掉旧 session/segment 状态。
                        RerouteContext context = RerouteContext.create(
                                RerouteSceneType.RUN_BLOCK_REROUTE,
                                basDevp,
                                stationThread,
                                stationProtocol,
                                wrkMast,
                                outOrderStationIds,
                                pathLenFactor,
                                "checkStationRunBlock_reroute"
                        ).withRunBlockCommand()
                                .withSuppressDispatchGuard()
                                .withCancelSessionBeforeDispatch()
                                .withResetSegmentCommandsBeforeDispatch();
                        executeSharedReroute(context);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //检测输送站点任务停留超时后重新计算路径
    public synchronized void checkStationIdleRecover() {
        try {
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
 
                List<StationProtocol> list = stationThread.getStatus();
                for (StationProtocol stationProtocol : list) {
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.getTaskNo() > 0
                            && !stationProtocol.isRunBlock()
                    ) {
                        checkStationIdleRecover(basDevp, stationThread, stationProtocol, basDevp.getOutOrderIntList());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //获取输送线任务数量
    public synchronized int getCurrentStationTaskCount() {
        return countCurrentStationTask();
    }
 
    public synchronized int getCurrentOutboundTaskCountByTargetStation(Integer stationId) {
        if (stationId == null) {
            return 0;
        }
        return (int) wrkMastService.count(new QueryWrapper<WrkMast>()
                .eq("io_type", WrkIoType.OUT.id)
                .eq("sta_no", stationId)
                .in("wrk_sts",
                        WrkStsType.OUTBOUND_RUN.sts,
                        WrkStsType.OUTBOUND_RUN_COMPLETE.sts,
                        WrkStsType.STATION_RUN.sts));
    }
 
    // 检测出库排序
    public synchronized void checkStationOutOrder() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            List<StationObjModel> orderList = basDevp.getOutOrderList$();
            List<Integer> outOrderStationIds = basDevp.getOutOrderIntList();
            for (StationObjModel stationObjModel : orderList) {
                StationProtocol stationProtocol = statusMap.get(stationObjModel.getStationId());
                if (stationProtocol == null) {
                    continue;
                }
 
                if (!stationProtocol.isAutoing()) {
                    continue;
                }
 
                if (!stationProtocol.isLoading()) {
                    continue;
                }
 
                if (stationProtocol.getTaskNo() <= 0) {
                    continue;
                }
 
                // 排序点本身已经堵塞时,不在 out-order 里做二次决策,统一交给 run-block 重规划处理。
                if (stationProtocol.isRunBlock()) {
                    continue;
                }
 
                if (!stationProtocol.getStationId().equals(stationProtocol.getTargetStaNo())) {
                    continue;
                }
 
                WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                if (wrkMast == null) {
                    continue;
                }
                if (!Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
                    continue;
                }
                if (Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
                    continue;
                }
                // 只有活动中的现有路线才会压制 out-order;BLOCKED 路线要允许排序点重新启动。
                if (shouldSkipOutOrderDispatchForExistingRoute(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
                    continue;
                }
 
                Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
                RerouteContext context = RerouteContext.create(
                        RerouteSceneType.OUT_ORDER,
                        basDevp,
                        stationThread,
                        stationProtocol,
                        wrkMast,
                        outOrderStationIds,
                        pathLenFactor,
                        "checkStationOutOrder"
                ).withDispatchDeviceNo(stationObjModel.getDeviceNo())
                        .withSuppressDispatchGuard()
                        .withOutOrderDispatchLock()
                        .withResetSegmentCommandsBeforeDispatch();
                executeSharedReroute(context);
            }
        }
    }
 
    // 监控绕圈站点
    public synchronized void watchCircleStation() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
 
            List<Integer> outOrderList = basDevp.getOutOrderIntList();
 
            for (StationProtocol stationProtocol : stationThread.getStatus()) {
                if (!stationProtocol.isAutoing()) {
                    continue;
                }
 
                if (!stationProtocol.isLoading()) {
                    continue;
                }
 
                if (stationProtocol.getTaskNo() <= 0) {
                    continue;
                }
 
                // 绕圈触发优先读 session 的下一决策站,legacy WATCH_CIRCLE key 只做兼容回退。
                if (!isWatchingCircleArrival(stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
                    continue;
                }
 
                WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                if (wrkMast == null) {
                    continue;
                }
                if (!Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
                    continue;
                }
                if (Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
                    continue;
                }
                Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
                RerouteContext context = RerouteContext.create(
                        RerouteSceneType.WATCH_CIRCLE,
                        basDevp,
                        stationThread,
                        stationProtocol,
                        wrkMast,
                        outOrderList,
                        pathLenFactor,
                        "watchCircleStation"
                ).withSuppressDispatchGuard()
                        .withOutOrderDispatchLock()
                        .withResetSegmentCommandsBeforeDispatch();
                executeSharedReroute(context);
            }
        }
    }
 
    private StationCommand buildOutboundMoveCommand(StationThread stationThread,
                                                    WrkMast wrkMast,
                                                    Integer stationId,
                                                    Integer targetStationId,
                                                    Double pathLenFactor) {
        if (stationThread == null || wrkMast == null) {
            return null;
        }
        return stationThread.getCommand(
                StationCommandType.MOVE,
                wrkMast.getWrkNo(),
                stationId,
                targetStationId,
                0,
                normalizePathLenFactor(pathLenFactor)
        );
    }
 
    RerouteCommandPlan buildRerouteCommandPlan(RerouteContext context,
                                               RerouteDecision decision) {
        if (context == null) {
            return RerouteCommandPlan.skip("missing-context");
        }
        if (decision == null) {
            return RerouteCommandPlan.skip("missing-decision");
        }
        if (decision.skip()) {
            return RerouteCommandPlan.skip(decision.skipReason());
        }
        if (context.stationThread() == null || context.stationProtocol() == null || context.wrkMast() == null) {
            return RerouteCommandPlan.skip("missing-runtime-dependency");
        }
        Integer currentStationId = context.stationProtocol().getStationId();
        Integer targetStationId = decision.targetStationId();
        if (currentStationId == null || targetStationId == null) {
            return RerouteCommandPlan.skip("missing-target-station");
        }
        if (Objects.equals(currentStationId, targetStationId)) {
            return RerouteCommandPlan.skip("same-station");
        }
 
        StationCommand command = context.useRunBlockCommand()
                ? context.stationThread().getRunBlockRerouteCommand(
                context.wrkMast().getWrkNo(),
                currentStationId,
                targetStationId,
                0,
                context.pathLenFactor()
        )
                : buildOutboundMoveCommand(
                context.stationThread(),
                context.wrkMast(),
                currentStationId,
                targetStationId,
                context.pathLenFactor()
        );
        if (command == null) {
            if (context.sceneType() == RerouteSceneType.RUN_BLOCK_REROUTE) {
                News.taskInfo(context.wrkMast().getWrkNo(),
                        "输送站点堵塞重规划未找到可下发路线,当前站点={},目标站点={}",
                        currentStationId,
                        targetStationId);
            } else if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
                News.taskInfo(context.wrkMast().getWrkNo(),
                        "站点任务停留超时后重算路径失败,当前站点={},目标站点={}",
                        currentStationId,
                        targetStationId);
            } else {
                News.taskInfo(context.wrkMast().getWrkNo(), "获取输送线命令失败");
            }
            return RerouteCommandPlan.skip("missing-command");
        }
        return RerouteCommandPlan.dispatch(command, decision, context.dispatchScene());
    }
 
    RerouteExecutionResult executeReroutePlan(RerouteContext context,
                                              RerouteCommandPlan plan) {
        if (context == null) {
            return RerouteExecutionResult.skip("missing-context");
        }
        if (plan == null) {
            return RerouteExecutionResult.skip("missing-plan");
        }
        if (plan.skip()) {
            return RerouteExecutionResult.skip(plan.skipReason());
        }
        StationProtocol stationProtocol = context.stationProtocol();
        if (stationProtocol == null) {
            return RerouteExecutionResult.skip("missing-station-protocol");
        }
        Integer taskNo = stationProtocol.getTaskNo();
        Integer stationId = stationProtocol.getStationId();
        if (taskNo == null || taskNo <= 0 || stationId == null) {
            return RerouteExecutionResult.skip("invalid-station-task");
        }
        boolean runBlockReroute = context.sceneType() == RerouteSceneType.RUN_BLOCK_REROUTE;
        if (runBlockReroute) {
            // 站点进入堵塞后,设备侧可能已经把之前预下发的分段命令清掉了。
            // 先作废本地 session/segment 状态,再按新路线重发,避免被旧状态反向卡住。
            if (context.cancelSessionBeforeDispatch() && stationMoveCoordinator != null) {
                stationMoveCoordinator.cancelSession(taskNo);
            }
            if (context.resetSegmentCommandsBeforeDispatch()) {
                resetSegmentMoveCommandsBeforeReroute(taskNo);
            }
        }
        if (context.checkRecentDispatch()
                && shouldSkipIdleRecoverForRecentDispatch(taskNo, stationId)) {
            return RerouteExecutionResult.skip("recent-dispatch");
        }
        int currentTaskBufferCommandCount = countCurrentTaskBufferCommands(stationProtocol.getTaskBufferItems(), taskNo);
        if (currentTaskBufferCommandCount > 0 && !runBlockReroute) {
            if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
                News.info("输送站点任务停留超时,但缓存区仍存在当前任务命令,已跳过重算。站点号={},工作号={},当前任务命令数={}",
                        stationId,
                        taskNo,
                        currentTaskBufferCommandCount);
            }
            return RerouteExecutionResult.skip("buffer-has-current-task");
        }
        if (currentTaskBufferCommandCount > 0 && runBlockReroute) {
            // 堵塞重规划要替换的正是这些旧分段命令,不能再把残留 buffer 当成新的拦截条件。
            News.info("输送站点运行堵塞重规划检测到旧分段命令残留,已先清理本地状态后继续重发。站点号={},工作号={},当前任务命令数={}",
                    stationId,
                    taskNo,
                    currentTaskBufferCommandCount);
        }
        if (!runBlockReroute
                && context.checkSuppressDispatch()
                && stationMoveCoordinator != null
                && stationMoveCoordinator.shouldSuppressDispatch(taskNo, stationId, plan.command())) {
            return RerouteExecutionResult.skip("dispatch-suppressed");
        }
        // 进入堵塞重规划后,旧路线已经被显式取消,本轮命令不再参与 active-session suppress 判定。
        if (context.requireOutOrderDispatchLock()
                && !tryAcquireOutOrderDispatchLock(taskNo, stationId)) {
            return RerouteExecutionResult.skip("out-order-lock");
        }
 
        if (!runBlockReroute
                && context.cancelSessionBeforeDispatch() && stationMoveCoordinator != null) {
            stationMoveCoordinator.cancelSession(taskNo);
        }
        if (!isBlank(context.executionLockKey())) {
            Object lock = redisUtil.get(context.executionLockKey());
            if (lock != null) {
                return RerouteExecutionResult.skip("scene-lock");
            }
            redisUtil.set(context.executionLockKey(), "lock", context.executionLockSeconds());
        }
        if (!runBlockReroute && context.resetSegmentCommandsBeforeDispatch()) {
            resetSegmentMoveCommandsBeforeReroute(taskNo);
        }
 
        int clearedCommandCount = 0;
        if (context.clearIdleIssuedCommands()) {
            clearedCommandCount = clearIssuedMoveCommandsDuringIdleStay(context.idleTrack(), taskNo, stationId);
        }
 
        boolean offered = offerDevpCommandWithDedup(context.dispatchDeviceNo(), plan.command(), plan.dispatchScene());
        if (!offered) {
            return RerouteExecutionResult.skip("dispatch-dedup");
        }
 
        applyRerouteDispatchEffects(context, plan, clearedCommandCount);
        return RerouteExecutionResult.dispatched(plan.command(), clearedCommandCount);
    }
 
    RerouteDecision resolveSharedRerouteDecision(RerouteContext context) {
        if (context == null || context.wrkMast() == null || context.stationProtocol() == null) {
            return RerouteDecision.skip("missing-runtime-dependency");
        }
        Integer currentStationId = context.stationProtocol().getStationId();
        if (currentStationId == null) {
            return RerouteDecision.skip("missing-current-station");
        }
 
        if (context.sceneType() == RerouteSceneType.IDLE_RECOVER
                && !Objects.equals(context.wrkMast().getWrkSts(), WrkStsType.STATION_RUN.sts)) {
            Integer targetStationId = context.wrkMast().getStaNo();
            return targetStationId == null || Objects.equals(targetStationId, currentStationId)
                    ? RerouteDecision.skip("same-station")
                    : RerouteDecision.proceed(targetStationId);
        }
 
        OutOrderDispatchDecision dispatchDecision = resolveOutboundDispatchDecision(
                currentStationId,
                context.wrkMast(),
                context.outOrderStationIds(),
                context.pathLenFactor()
        );
        Integer targetStationId = dispatchDecision == null ? null : dispatchDecision.getTargetStationId();
        if (targetStationId == null || Objects.equals(targetStationId, currentStationId)) {
            return RerouteDecision.skip("same-station");
        }
        return RerouteDecision.proceed(targetStationId, dispatchDecision);
    }
 
    private RerouteExecutionResult executeSharedReroute(RerouteContext context) {
        RerouteDecision decision = resolveSharedRerouteDecision(context);
        if (decision.skip()) {
            return RerouteExecutionResult.skip(decision.skipReason());
        }
        RerouteCommandPlan plan = buildRerouteCommandPlan(context, decision);
        return executeReroutePlan(context, plan);
    }
 
    private void applyRerouteDispatchEffects(RerouteContext context,
                                             RerouteCommandPlan plan,
                                             int clearedCommandCount) {
        if (context == null || plan == null || plan.command() == null || context.wrkMast() == null || context.stationProtocol() == null) {
            return;
        }
        WrkMast wrkMast = context.wrkMast();
        StationProtocol stationProtocol = context.stationProtocol();
        OutOrderDispatchDecision dispatchDecision = plan.decision() == null ? null : plan.decision().dispatchDecision();
 
        syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), context.outOrderStationIds(), dispatchDecision, plan.command());
        if (stationMoveCoordinator != null) {
            stationMoveCoordinator.recordDispatch(
                    wrkMast.getWrkNo(),
                    stationProtocol.getStationId(),
                    plan.dispatchScene(),
                    plan.command(),
                    dispatchDecision != null && dispatchDecision.isCircle()
            );
        }
        if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
            saveStationTaskIdleTrack(new StationTaskIdleTrack(wrkMast.getWrkNo(), stationProtocol.getStationId(), System.currentTimeMillis()));
            News.info("输送站点任务停留{}秒未运行,已重新计算路径并重启运行,站点号={},目标站={},工作号={},清理旧分段命令数={},命令数据={}",
                    STATION_IDLE_RECOVER_SECONDS,
                    stationProtocol.getStationId(),
                    plan.command().getTargetStaNo(),
                    wrkMast.getWrkNo(),
                    clearedCommandCount,
                    JSON.toJSONString(plan.command()));
            return;
        }
        if (context.sceneType() == RerouteSceneType.RUN_BLOCK_REROUTE) {
            News.info("输送站点堵塞后重新计算路径命令下发成功,站点号={},工作号={},命令数据={}",
                    stationProtocol.getStationId(),
                    wrkMast.getWrkNo(),
                    JSON.toJSONString(plan.command()));
            return;
        }
        if (context.sceneType() == RerouteSceneType.OUT_ORDER) {
            News.info(dispatchDecision != null && dispatchDecision.isCircle() ? "{}任务进行绕圈" : "{}任务直接去目标点", wrkMast.getWrkNo());
        }
    }
 
    private List<NavigateNode> calcOutboundNavigatePath(WrkMast wrkMast,
                                                        Integer sourceStationId,
                                                        Integer targetStationId,
                                                        Double pathLenFactor) {
        Double normalizedFactor = normalizePathLenFactor(pathLenFactor);
        Integer currentTaskNo = wrkMast == null ? null : wrkMast.getWrkNo();
        if (currentTaskNo == null) {
            return navigateUtils.calcByStationId(sourceStationId, targetStationId, normalizedFactor);
        }
        return navigateUtils.calcByStationId(sourceStationId, targetStationId, currentTaskNo, normalizedFactor);
    }
 
    /**
     * 计算当前出库任务的路径倾向系数。
     *
     * <p>这个系数不是业务目标站本身,而是“在多条可行路线之间更偏向哪一条”的辅助输入,
     * 目的是让同一批次、不同序号的任务在共享环线里尽量形成稳定、可重复的路径分布。
     *
     * <p>返回值范围固定在 {@code [0, 1]}:
     * 1. 非批次出库任务,直接返回 {@code 0.0},表示不引入额外路径偏置。
     * 2. 当前批次只有 1 个有效活动任务,返回 {@code 0.0},因为没有“前后顺序”可比较。
     * 3. 否则按“当前任务前面还有多少个有效前序任务”占“有效活动任务总数”的比例来算。
     *
     * <p>这里的“有效任务”只统计:
     * 1. ioType=OUT 的出库任务;
     * 2. 仍处于活动状态,未完成/未结算;
     * 3. 有 batchSeq;
     * 4. mk != taskCancel。
     *
     * <p>结果含义可以直观理解为:
     * 任务批次序号越靠后,前面已经存在的有效任务越多,得到的系数越大;
     * 后续算路时就更容易和前序任务形成稳定的路径分流,而不是所有任务都走同一条默认短路。
     *
     * <p>注意:
     * 这个方法不直接决定目标站,不负责排序放行,只提供“路径偏好”输入。
     * 真正的目标站仍由 {@link #resolveOutboundDispatchDecision(Integer, WrkMast, List, Double)} 决定。
     */
    private Double resolveOutboundPathLenFactor(WrkMast wrkMast) {
        if (!isBatchOutboundTaskWithSeq(wrkMast)) {
            return 0.0d;
        }
        List<WrkMast> activeBatchTaskList = loadActiveBatchTaskList(wrkMast.getBatch());
        if (activeBatchTaskList.size() <= 1) {
            return 0.0d;
        }
 
        int activeTaskCount = 0;
        int predecessorCount = 0;
        for (WrkMast item : activeBatchTaskList) {
            if (!isFactorCandidateTask(item)) {
                continue;
            }
            activeTaskCount++;
            if (item.getBatchSeq() < wrkMast.getBatchSeq()) {
                predecessorCount++;
            }
        }
        if (activeTaskCount <= 1 || predecessorCount <= 0) {
            return 0.0d;
        }
        return normalizePathLenFactor((double) predecessorCount / (double) (activeTaskCount - 1));
    }
 
    /**
     * 判断当前任务是否具备“按批次出库规则参与排序/路径偏好计算”的基础条件。
     *
     * <p>这里只做最基础的资格过滤,不关心当前是否真的需要排序点介入。
     * 只要不是批次出库任务,后面的路径偏好系数与排序目标决策都应该直接退化为默认行为。
     */
    private boolean isBatchOutboundTaskWithSeq(WrkMast wrkMast) {
        return wrkMast != null
                && Objects.equals(wrkMast.getIoType(), WrkIoType.OUT.id)
                && !Cools.isEmpty(wrkMast.getBatch())
                && wrkMast.getBatchSeq() != null
                && wrkMast.getWrkNo() != null;
    }
 
    /**
     * 加载同一批次下仍处于活动中的出库任务。
     *
     * <p>这里用于两类计算:
     * 1. 计算路径偏好系数时,统计当前任务前面还有多少个有效前序任务。
     * 2. 当前排序点重新决策时,找出这一批“首个未完成任务”的实际批次序号。
     *
     * <p>已经完成/结算的任务不再参与当前批次的排序与偏好计算。
     */
    private List<WrkMast> loadActiveBatchTaskList(String batch) {
        if (Cools.isEmpty(batch)) {
            return Collections.emptyList();
        }
        return wrkMastService.list(new QueryWrapper<WrkMast>()
                .eq("io_type", WrkIoType.OUT.id)
                .eq("batch", batch)
                .notIn("wrk_sts",
                        WrkStsType.STATION_RUN_COMPLETE.sts,
                        WrkStsType.COMPLETE_OUTBOUND.sts,
                        WrkStsType.SETTLE_OUTBOUND.sts));
    }
 
    /**
     * 判断某条批次任务是否应该计入路径偏好系数的分母/分子统计。
     *
     * <p>这里排除没有 batchSeq 的任务以及被显式标记为 taskCancel 的任务,
     * 避免无效任务把同批次的路径偏好计算拉偏。
     */
    private boolean isFactorCandidateTask(WrkMast wrkMast) {
        return wrkMast != null
                && Objects.equals(wrkMast.getIoType(), WrkIoType.OUT.id)
                && wrkMast.getBatchSeq() != null
                && !"taskCancel".equals(wrkMast.getMk());
    }
 
    public List<Integer> getAllOutOrderList() {
        List<Integer> list = new ArrayList<>();
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            List<Integer> orderList = basDevp.getOutOrderIntList();
            list.addAll(orderList);
        }
        return list;
    }
 
    /**
     * 统一计算当前任务“此刻应该朝哪个目标站继续运行”。
     *
     * <p>这是出库排序、绕圈、堵塞重规划共用的目标裁决入口。
     * 不管触发来源是 OUT_ORDER、WATCH_CIRCLE 还是 RUN_BLOCK_REROUTE,
     * 只要业务语义还是“当前这票出库任务下一步该往哪里走”,都从这里得出目标站。
     *
     * <p>它做三层判断:
     * 1. 如果当前任务根本不适用出库排序,直接返回任务业务目标站 {@code wrkMast.staNo}。
     * 2. 如果适用出库排序,先算出“当前批次规则下,此刻允许前往的 dispatchStationId”。
     * 3. 如果当前站点正好就是排序决策点,再进一步判断是:
     *    直接去目标点,还是先进入绕圈目标点,或者因为严格窗口限制而暂不放行。
     *
     * <p>返回的 {@link OutOrderDispatchDecision} 不只是一个目标站,
     * 还携带了这次决策是否属于绕圈、是否来自当前排序点重新裁决等语义信息,
     * 供后续日志、session 记录和 watch-circle 判定使用。
     *
     * <p>参数含义:
     * 1. {@code currentStationId}:任务当前所在站点。用于判断当前是不是排序决策点、
     *    当前是不是已经到达 watch-circle 的下一决策站。
     * 2. {@code wrkMast}:当前任务主状态,至少要提供业务目标站、来源站、批次、序号等信息。
     * 3. {@code outOrderStationIds}:当前设备配置的所有出库排序站点列表。
     * 4. {@code pathLenFactor}:由 {@link #resolveOutboundPathLenFactor(WrkMast)} 得到的路径偏好系数,
     *    用来让同一批次任务在选择 dispatch target 时保持稳定的路径倾向。
     *
     * <p>返回值语义:
     * 1. 返回 {@code null}:当前无法得到合法目标站,调用方应跳过本次派发。
     * 2. 返回 {@code targetStationId=wrkMast.staNo, circle=false}:
     *    当前不需要出库排序干预,或已允许直接去业务目标站。
     * 3. 返回 {@code targetStationId!=wrkMast.staNo}:
     *    当前应该先去一个中间 dispatch 目标站,后续再由排序点/绕圈点继续决策。
     * 4. 返回 {@code circle=true}:
     *    当前属于绕圈决策结果,后续 watch-circle 逻辑会据此接管。
     *
     * <p>注意:
     * 这个方法只决定“目标站”,不直接生成输送命令。
     * 真正的路径由普通算路或 run-block 专用算路在后续步骤生成。
     */
    private OutOrderDispatchDecision resolveOutboundDispatchDecision(Integer currentStationId,
                                                                     WrkMast wrkMast,
                                                                     List<Integer> outOrderStationIds,
                                                                     Double pathLenFactor) {
        if (wrkMast == null || wrkMast.getStaNo() == null) {
            return null;
        }
        if (!shouldApplyOutOrder(wrkMast, outOrderStationIds)) {
            return new OutOrderDispatchDecision(wrkMast.getStaNo(), false);
        }
        Integer dispatchStationId = resolveDispatchOutOrderTarget(
                wrkMast,
                wrkMast.getSourceStaNo(),
                wrkMast.getStaNo(),
                outOrderStationIds,
                pathLenFactor
        );
        if (dispatchStationId == null) {
            return null;
        }
        if (isCurrentOutOrderDispatchStation(currentStationId, wrkMast, outOrderStationIds, pathLenFactor)) {
            return resolveCurrentOutOrderDispatchDecision(currentStationId, wrkMast, outOrderStationIds, pathLenFactor);
        }
        if (!Objects.equals(dispatchStationId, wrkMast.getStaNo())
                && isCurrentOutOrderStation(currentStationId, outOrderStationIds)
                && isWatchingCircleArrival(wrkMast.getWrkNo(), currentStationId)) {
            return new OutOrderDispatchDecision(dispatchStationId, true, null, false);
        }
        return new OutOrderDispatchDecision(dispatchStationId, false);
    }
 
    /**
     * 在“当前站点就是本次排序决策点”时,计算这里到底该直接放行还是进入绕圈。
     *
     * <p>这是出库排序里最核心的局部决策方法。进入这里之前,已经满足:
     * 1. 当前任务适用 out-order;
     * 2. 当前站点就是这票任务此刻对应的 dispatch 排序点。
     *
     * <p>内部决策顺序:
     * 1. 先取出同批次仍未完成的任务,找出这批当前“应当被优先放行”的序号位置。
     * 2. 再结合当前任务在初始路径上的排序窗口位置,判断自己此刻能否直接去业务目标站。
     * 3. 如果理论上该自己放行,还要额外检查目标方向上是否存在可进入的 release slot。
     * 4. 如果不能直达,或者直达方向当前全部阻塞,就转成 circle 决策,寻找下一排序检测点。
     *
     * <p>返回值:
     * 1. {@code circle=false} 表示当前排序点已经允许直接去业务目标站。
     * 2. {@code circle=true} 表示当前只能先去下一绕圈目标站,后续由 watch-circle/排序点继续接力决策。
     * 3. {@code null} 表示当前既不能直达,也没找到合法的下一绕圈点,调用方应跳过本次派发。
     */
    private OutOrderDispatchDecision resolveCurrentOutOrderDispatchDecision(Integer currentStationId,
                                                                            WrkMast wrkMast,
                                                                            List<Integer> outOrderStationIds,
                                                                            Double pathLenFactor) {
        if (!isCurrentOutOrderDispatchStation(currentStationId, wrkMast, outOrderStationIds, pathLenFactor)) {
            return null;
        }
 
        List<WrkMast> batchWrkList = wrkMastService.list(new QueryWrapper<WrkMast>()
                .eq("io_type", WrkIoType.OUT.id)
                .notIn("wrk_sts",
                        WrkStsType.STATION_RUN_COMPLETE.sts,
                        WrkStsType.COMPLETE_OUTBOUND.sts,
                        WrkStsType.SETTLE_OUTBOUND.sts)
                .eq("batch", wrkMast.getBatch())
                .orderByAsc("batch_seq")
                .orderByAsc("wrk_no"));
        if (batchWrkList.isEmpty()) {
            return new OutOrderDispatchDecision(wrkMast.getStaNo(), false);
        }
 
        WrkMast firstWrkMast = batchWrkList.get(0);
        Integer currentBatchSeq = firstWrkMast.getBatchSeq();
        if (currentBatchSeq == null) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 首个未完成任务缺少批次序号,当前任务暂不放行", wrkMast.getBatch());
            return null;
        }
 
        List<NavigateNode> initPath;
        try {
            initPath = calcOutboundNavigatePath(wrkMast, wrkMast.getSourceStaNo(), wrkMast.getStaNo(), pathLenFactor);
        } catch (Exception e) {
            News.taskInfo(wrkMast.getWrkNo(), "批次:{} 计算排序路径失败,当前站点={}", wrkMast.getBatch(), currentStationId);
            return null;
        }
 
        Integer seq = getOutStationBatchSeq(initPath, currentStationId, wrkMast.getBatch());
        boolean toTarget;
        if (seq == null) {
            toTarget = currentBatchSeq.equals(wrkMast.getBatchSeq());
        } else {
            toTarget = Integer.valueOf(seq + 1).equals(wrkMast.getBatchSeq());
        }
        if (toTarget) {
            if (hasReachableOutReleaseSlot(wrkMast, currentStationId, wrkMast.getStaNo(), pathLenFactor)) {
                return new OutOrderDispatchDecision(wrkMast.getStaNo(), false);
            }
            StationTaskLoopService.LoopEvaluation loopEvaluation = evaluateOutOrderLoop(
                    wrkMast.getWrkNo(),
                    currentStationId,
                    outOrderStationIds
            );
            Integer circleTarget = resolveNextCircleOrderTarget(
                    wrkMast,
                    currentStationId,
                    outOrderStationIds,
                    loopEvaluation.getExpectedLoopIssueCount(),
                    pathLenFactor
            );
            if (circleTarget == null) {
                News.taskInfo(wrkMast.getWrkNo(), "目标站当前不可进,且未找到可执行的下一排序检测点,当前站点={}", currentStationId);
                return null;
            }
            return new OutOrderDispatchDecision(circleTarget, true, loopEvaluation, true);
        }
 
        StationTaskLoopService.LoopEvaluation loopEvaluation = evaluateOutOrderLoop(
                wrkMast.getWrkNo(),
                currentStationId,
                outOrderStationIds
        );
        Integer circleTarget = resolveNextCircleOrderTarget(
                wrkMast,
                currentStationId,
                outOrderStationIds,
                loopEvaluation.getExpectedLoopIssueCount(),
                pathLenFactor
        );
        if (circleTarget == null) {
            News.taskInfo(wrkMast.getWrkNo(), "未找到可执行的下一排序检测点,当前站点={}", currentStationId);
            return null;
        }
        return new OutOrderDispatchDecision(circleTarget, true, loopEvaluation, true);
    }
 
    /**
     * 判断这票任务在当前设备上是否应该启用出库排序逻辑。
     *
     * <p>只要缺少任何一个排序前提,例如不是出库任务、没有批次、没有序号、
     * 当前设备也没有配置排序点,就应该直接退回“普通目标站决策”。
     */
    private boolean shouldApplyOutOrder(WrkMast wrkMast, List<Integer> outOrderStationIds) {
        return wrkMast != null
                && wrkMast.getStaNo() != null
                && Objects.equals(wrkMast.getIoType(), WrkIoType.OUT.id)
                && !Cools.isEmpty(wrkMast.getBatch())
                && wrkMast.getBatchSeq() != null
                && outOrderStationIds != null
                && !outOrderStationIds.isEmpty();
    }
 
    /**
     * 判断当前所在站点是否就是“这票任务此刻应该触发排序决策的 dispatch 站点”。
     *
     * <p>注意它不是简单判断“当前站点是否属于排序点列表”,
     * 而是先根据完整路径反推出当前任务对应的 dispatch 排序点,
     * 再判断当前位置是否正好等于这个 dispatch 点。
     */
    private boolean isCurrentOutOrderDispatchStation(Integer currentStationId,
                                                     WrkMast wrkMast,
                                                     List<Integer> outOrderStationIds,
                                                     Double pathLenFactor) {
        if (!shouldApplyOutOrder(wrkMast, outOrderStationIds) || currentStationId == null) {
            return false;
        }
        Integer dispatchStationId = resolveDispatchOutOrderTarget(
                wrkMast,
                wrkMast.getSourceStaNo(),
                wrkMast.getStaNo(),
                outOrderStationIds,
                pathLenFactor
        );
        return dispatchStationId != null
                && !Objects.equals(dispatchStationId, wrkMast.getStaNo())
                && Objects.equals(currentStationId, dispatchStationId);
    }
 
    /**
     * 判断当前位置是否属于设备配置里的任意一个排序点。
     *
     * <p>这个判断比 {@link #isCurrentOutOrderDispatchStation(Integer, WrkMast, List, Double)} 更宽,
     * 只回答“当前位置是不是排序点”,不回答“是不是这票任务当前应该命中的排序点”。
     */
    private boolean isCurrentOutOrderStation(Integer currentStationId,
                                             List<Integer> outOrderStationIds) {
        return currentStationId != null
                && outOrderStationIds != null
                && outOrderStationIds.contains(currentStationId);
    }
 
    private void syncOutOrderWatchState(WrkMast wrkMast,
                                        Integer currentStationId,
                                        List<Integer> outOrderStationIds,
                                        OutOrderDispatchDecision dispatchDecision,
                                        StationCommand command) {
        if (dispatchDecision == null || command == null || !shouldApplyOutOrder(wrkMast, outOrderStationIds)) {
            return;
        }
        if (dispatchDecision.isCircle()) {
            saveWatchCircleCommand(wrkMast.getWrkNo(), command);
            if (dispatchDecision.shouldCountLoopIssue()
                    && stationTaskLoopService != null
                    && dispatchDecision.getLoopEvaluation() != null) {
                stationTaskLoopService.recordLoopIssue(dispatchDecision.getLoopEvaluation(), "OUT_ORDER_CIRCLE");
            }
        } else {
            clearWatchCircleCommand(wrkMast.getWrkNo());
        }
    }
 
    /**
     * 为当前排序点决策预先做一次环线评估。
     *
     * <p>当本次决策最终进入绕圈时,评估结果会被带进 {@link OutOrderDispatchDecision},
     * 后续用于记录 loop issue 统计,而不是在真正下发后再重复评估一次。
     */
    private StationTaskLoopService.LoopEvaluation evaluateOutOrderLoop(Integer taskNo,
                                                                       Integer currentStationId,
                                                                       List<Integer> outOrderStationIds) {
        if (stationTaskLoopService == null) {
            return new StationTaskLoopService.LoopEvaluation(
                    taskNo,
                    currentStationId,
                    StationTaskLoopService.LoopIdentitySnapshot.empty(),
                    0,
                    0,
                    false
            );
        }
        return stationTaskLoopService.evaluateLoop(
                taskNo,
                currentStationId,
                true,
                outOrderStationIds,
                "outOrderCircle"
        );
    }
 
    /**
     * 从“源站到业务目标站”的完整路径里,反推出当前任务应当先命中的 dispatch 排序点。
     *
     * <p>做法是:
     * 1. 先计算从 sourceStationId 到 finalTargetStationId 的完整导航路径;
     * 2. 再从路径尾部向前扫描;
     * 3. 找到离最终目标最近的那个排序点,作为当前 dispatch 目标。
     *
     * <p>如果路径上根本没有排序点,或者缺少源站/排序点配置,
     * 就直接把业务目标站本身当成 dispatch 目标。
     */
    private Integer resolveDispatchOutOrderTarget(WrkMast wrkMast,
                                                  Integer sourceStationId,
                                                  Integer finalTargetStationId,
                                                  List<Integer> outOrderList,
                                                  Double pathLenFactor) {
        if (finalTargetStationId == null) {
            return null;
        }
        if (sourceStationId == null || outOrderList == null || outOrderList.isEmpty()) {
            return finalTargetStationId;
        }
 
        try {
            List<NavigateNode> nodes = calcOutboundNavigatePath(wrkMast, sourceStationId, finalTargetStationId, pathLenFactor);
            for (int i = nodes.size() - 1; i >= 0; i--) {
                Integer stationId = getStationIdFromNode(nodes.get(i));
                if (stationId == null) {
                    continue;
                }
                if (Objects.equals(stationId, finalTargetStationId)) {
                    continue;
                }
                if (outOrderList.contains(stationId)) {
                    return stationId;
                }
            }
        } catch (Exception ignore) {}
        return finalTargetStationId;
    }
 
    /**
     * 判断从当前排序点继续前往最终业务目标站时,路径上是否至少存在一个可进入的后续站点。
     *
     * <p>这里不是要求整条路径完全畅通,而是判断“当前是否有释放口可走”。
     * 只要后续路径上存在一个未阻塞站点,就认为当前仍可尝试直达;
     * 只有整段后续路径看起来都被阻塞时,才会强制转入绕圈。
     */
    private boolean hasReachableOutReleaseSlot(WrkMast wrkMast,
                                               Integer currentStationId,
                                               Integer finalTargetStationId,
                                               Double pathLenFactor) {
        if (currentStationId == null || finalTargetStationId == null) {
            return true;
        }
 
        try {
            List<NavigateNode> nodes = calcOutboundNavigatePath(wrkMast, currentStationId, finalTargetStationId, pathLenFactor);
            if (nodes == null || nodes.isEmpty()) {
                return true;
            }
 
            for (NavigateNode node : nodes) {
                Integer stationId = getStationIdFromNode(node);
                if (stationId == null || Objects.equals(stationId, currentStationId)) {
                    continue;
                }
 
                if (!isPathStationBlocked(stationId)) {
                    return true;
                }
            }
            return false;
        } catch (Exception ignore) {
            return true;
        }
    }
 
    private boolean isPathStationBlocked(Integer stationId) {
        if (stationId == null) {
            return true;
        }
 
        BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", stationId));
        if (basStation == null) {
            return true;
        }
 
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
        if (stationThread == null) {
            return true;
        }
 
        StationProtocol stationProtocol = stationThread.getStatusMap().get(stationId);
        if (stationProtocol == null) {
            return true;
        }
 
        return !stationProtocol.isAutoing()
                || stationProtocol.isLoading()
                || (stationProtocol.getTaskNo() != null && stationProtocol.getTaskNo() > 0);
    }
 
    /**
     * 为“排序点需要绕圈”的场景,从出库排序站点列表里挑出下一跳绕圈目标。
     * <p>
     * 这里的输入不是整张地图,而是已经按业务顺序排好的出库站点序列。方法会从当前站点在该序列中的后继开始,
     * 依次尝试每个候选站点,并计算“当前站点 -> 候选站点”的可行路径:
     * <p>
     * 1. 只要能算出路径,就把候选站点记录为一个可选绕圈目标;
     * 2. 记录两类排序信息:
     *    - pathLength:到该候选点的路径长度,越短说明越适合先拿来做临时缓冲;
     *    - offset:该站点在排序序列中距离当前站点有多远,用来在路径长度相同的时候保留既有顺序感;
     * 3. 最后把候选列表交给 {@link #resolveGradualCircleTargetByPathLength(Integer, List, Double)},
     *    再根据“当前已经绕圈多少次”与“路径长度偏好系数”从不同长度层级里挑最终目标。
     * <p>
     * 这个方法本身不判断“是否应该绕圈”,只负责在已经决定绕圈后,从排序站点链路里找一个下一跳缓冲点。
     *
     * @param wrkMast 当前任务,主要用于算路
     * @param currentStationId 当前站点,即本次准备从哪里发出绕圈命令
     * @param orderedOutStationList 已按业务顺序排好的出库站点列表
     * @param expectedLoopIssueCount 预计已发生的绕圈/堵塞轮次,用于决定是否逐步放大绕圈半径
     * @param pathLenFactor 当前任务对应的路径偏好系数,影响 calcOutboundNavigatePath 的选路结果
     * @return 下一跳绕圈目标站;如果没有任何可到达候选则返回 null
     */
    private Integer resolveNextCircleOrderTarget(WrkMast wrkMast,
                                                 Integer currentStationId,
                                                 List<Integer> orderedOutStationList,
                                                 Integer expectedLoopIssueCount,
                                                 Double pathLenFactor) {
        if (currentStationId == null || orderedOutStationList == null || orderedOutStationList.size() <= 1) {
            return null;
        }
 
        int startIndex = orderedOutStationList.indexOf(currentStationId);
        int total = orderedOutStationList.size();
        List<CircleTargetCandidate> candidateList = new ArrayList<>();
        for (int offset = 1; offset < total; offset++) {
            int candidateIndex = (startIndex + offset + total) % total;
            Integer candidateStationId = orderedOutStationList.get(candidateIndex);
            if (candidateStationId == null || currentStationId.equals(candidateStationId)) {
                continue;
            }
            try {
                List<NavigateNode> path = calcOutboundNavigatePath(wrkMast, currentStationId, candidateStationId, pathLenFactor);
                if (path != null && !path.isEmpty()) {
                    candidateList.add(new CircleTargetCandidate(candidateStationId, path.size(), offset));
                }
            } catch (Exception ignore) {}
        }
        if (candidateList.isEmpty()) {
            return null;
        }
        candidateList.sort(new Comparator<CircleTargetCandidate>() {
            @Override
            public int compare(CircleTargetCandidate left, CircleTargetCandidate right) {
                if (left == right) {
                    return 0;
                }
                if (left == null) {
                    return 1;
                }
                if (right == null) {
                    return -1;
                }
                int pathCompare = Integer.compare(left.getPathLength(), right.getPathLength());
                if (pathCompare != 0) {
                    return pathCompare;
                }
                return Integer.compare(left.getOffset(), right.getOffset());
            }
        });
        return resolveGradualCircleTargetByPathLength(expectedLoopIssueCount, candidateList, pathLenFactor);
    }
 
    /**
     * 在“已按路径长度升序排好”的绕圈候选列表中,按层级渐进地挑选目标站。
     * <p>
     * candidateList 里可能存在多个候选点拥有相同的 pathLength。对绕圈决策来说,
     * 同一长度层里的候选点都属于“同一绕圈半径”,真正需要控制的是:
     * <p>
     * 1. 初次/前几次堵塞时,优先选择最短可达层,尽量用最小绕行距离恢复流转;
     * 2. 如果任务已经连续多次在同一区域绕圈,说明短半径候选大概率已经试过或恢复效果差,
     *    就需要逐步放大到更远一层;
     * 3. pathLenFactor 代表当前任务对“短路径/长路径”的偏好,允许在相同堵塞轮次下适度往更远层偏移。
     * <p>
     * 因此这里先把 candidateList 压缩成“按 pathLength 去重后的 tierList”,每个 tier 只保留该长度层的首个候选。
     * 然后同时计算两个层级索引:
     * <p>
     * - defaultTierIndex:基于 expectedLoopIssueCount 的默认放大层级;
     * - factorTierIndex:基于 pathLenFactor 的偏好层级;
     * <p>
     * 最终取两者较大值,含义是“至少满足当前堵塞轮次需要的放大半径,同时允许路径偏好把目标推向更远层级”。
     *
     * @param expectedLoopIssueCount 预计已发生的绕圈/堵塞轮次
     * @param candidateList 已按 pathLength、offset 排序的候选列表
     * @param pathLenFactor 当前任务的路径偏好系数
     * @return 最终选中的绕圈目标站;若没有候选则返回 null
     */
    private Integer resolveGradualCircleTargetByPathLength(Integer expectedLoopIssueCount,
                                                           List<CircleTargetCandidate> candidateList,
                                                           Double pathLenFactor) {
        if (candidateList == null || candidateList.isEmpty()) {
            return null;
        }
 
        List<CircleTargetCandidate> tierList = new ArrayList<>();
        Integer lastPathLength = null;
        for (CircleTargetCandidate candidate : candidateList) {
            if (candidate == null) {
                continue;
            }
            if (lastPathLength == null || !Objects.equals(lastPathLength, candidate.getPathLength())) {
                tierList.add(candidate);
                lastPathLength = candidate.getPathLength();
            }
        }
        if (tierList.isEmpty()) {
            return candidateList.get(0).getStationId();
        }
        int defaultTierIndex = expectedLoopIssueCount == null || expectedLoopIssueCount <= 2
                ? 0
                : Math.min(expectedLoopIssueCount - 2, tierList.size() - 1);
        int factorTierIndex = (int) Math.round(normalizePathLenFactor(pathLenFactor) * (tierList.size() - 1));
        int tierIndex = Math.max(defaultTierIndex, factorTierIndex);
        return tierList.get(tierIndex).getStationId();
    }
 
    private boolean tryAcquireOutOrderDispatchLock(Integer wrkNo, Integer stationId) {
        if (wrkNo == null || wrkNo <= 0 || stationId == null) {
            return true;
        }
        String key = RedisKeyType.STATION_OUT_ORDER_DISPATCH_LIMIT_.key + wrkNo + "_" + stationId;
        Object lock = redisUtil.get(key);
        if (lock != null) {
            return false;
        }
        redisUtil.set(key, "lock", OUT_ORDER_DISPATCH_LIMIT_SECONDS);
        return true;
    }
 
    private boolean shouldSkipOutOrderDispatchForExistingRoute(Integer wrkNo, Integer stationId) {
        if (stationMoveCoordinator == null || wrkNo == null || wrkNo <= 0 || stationId == null) {
            return false;
        }
        StationMoveSession session = stationMoveCoordinator.loadSession(wrkNo);
        if (session == null) {
            return false;
        }
        if (!session.isActive() || !session.containsStation(stationId)) {
            return false;
        }
        // 绕圈路线在当前站点尚未走完时,排序点不应再次插手。
        if (StationMoveDispatchMode.CIRCLE == session.getDispatchMode()) {
            return true;
        }
        // 直接路线只在“当前站点已经被别的活动路线占住且目标不同”时才拦截。
        return !Objects.equals(stationId, session.getCurrentRouteTargetStationId());
    }
 
    private boolean isWatchingCircleArrival(Integer wrkNo, Integer stationId) {
        if (stationMoveCoordinator != null) {
            StationMoveSession session = stationMoveCoordinator.loadSession(wrkNo);
            if (session != null && session.isActive() && stationId != null) {
                // nextDecisionStationId 表示这条路线真正等待重新决策的站点,到站才触发 watch-circle。
                if (stationId.equals(session.getNextDecisionStationId())) {
                    return true;
                }
                // 还在 session 路径中间站运行时不应误触发。
                if (session.containsStation(stationId)) {
                    return false;
                }
            }
        }
        StationCommand command = getWatchCircleCommand(wrkNo);
        return command != null && stationId != null && stationId.equals(command.getTargetStaNo());
    }
 
    private boolean isWatchingCircleTransit(Integer wrkNo, Integer stationId) {
        if (stationMoveCoordinator != null) {
            StationMoveSession session = stationMoveCoordinator.loadSession(wrkNo);
            if (session != null && session.isActive() && stationId != null) {
                if (stationId.equals(session.getNextDecisionStationId())) {
                    return false;
                }
                if (session.containsStation(stationId)) {
                    return true;
                }
            }
        }
        StationCommand command = getWatchCircleCommand(wrkNo);
        if (command == null || stationId == null || Objects.equals(stationId, command.getTargetStaNo())) {
            return false;
        }
        List<Integer> navigatePath = command.getNavigatePath();
        return navigatePath != null && navigatePath.contains(stationId);
    }
 
    private StationCommand getWatchCircleCommand(Integer wrkNo) {
        if (wrkNo == null || wrkNo <= 0) {
            return null;
        }
        Object circleObj = redisUtil.get(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo);
        if (circleObj == null) {
            return null;
        }
        try {
            return JSON.parseObject(circleObj.toString(), StationCommand.class);
        } catch (Exception ignore) {
            return null;
        }
    }
 
    private void saveWatchCircleCommand(Integer wrkNo, StationCommand command) {
        if (wrkNo == null || wrkNo <= 0 || command == null) {
            return;
        }
        redisUtil.set(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo,
                JSON.toJSONString(command, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
    }
 
    private void clearWatchCircleCommand(Integer wrkNo) {
        if (wrkNo == null || wrkNo <= 0) {
            return;
        }
        redisUtil.del(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo);
    }
 
    private void checkStationIdleRecover(BasDevp basDevp,
                                         StationThread stationThread,
                                         StationProtocol stationProtocol,
                                         List<Integer> outOrderList) {
        if (stationProtocol == null || stationProtocol.getTaskNo() == null || stationProtocol.getTaskNo() <= 0) {
            return;
        }
        if (!Objects.equals(stationProtocol.getStationId(), stationProtocol.getTargetStaNo())) {
            return;
        }
 
        StationTaskIdleTrack idleTrack = touchStationTaskIdleTrack(stationProtocol.getTaskNo(), stationProtocol.getStationId());
        if (shouldSkipIdleRecoverForRecentDispatch(stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
            return;
        }
        if (idleTrack == null || !idleTrack.isTimeout(STATION_IDLE_RECOVER_SECONDS)) {
            return;
        }
 
        WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
        if (!canRecoverIdleStationTask(wrkMast, stationProtocol.getStationId())) {
            return;
        }
 
        Object lock = redisUtil.get(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo());
        if (lock != null) {
            return;
        }
        Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
        RerouteContext context = RerouteContext.create(
                RerouteSceneType.IDLE_RECOVER,
                basDevp,
                stationThread,
                stationProtocol,
                wrkMast,
                outOrderList,
                pathLenFactor,
                "checkStationIdleRecover"
        ).withCancelSessionBeforeDispatch()
                .withExecutionLock(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), STATION_IDLE_RECOVER_LIMIT_SECONDS)
                .withResetSegmentCommandsBeforeDispatch()
                .clearIdleIssuedCommands(idleTrack);
        executeSharedReroute(context);
    }
 
    boolean shouldUseRunBlockDirectReassign(WrkMast wrkMast,
                                            Integer stationId,
                                            List<Integer> runBlockReassignLocStationList) {
        return wrkMast != null
                && Objects.equals(wrkMast.getIoType(), WrkIoType.IN.id)
                && stationId != null
                && runBlockReassignLocStationList != null
                && runBlockReassignLocStationList.contains(stationId);
    }
 
    private void executeRunBlockDirectReassign(BasDevp basDevp,
                                               StationThread stationThread,
                                               StationProtocol stationProtocol,
                                               WrkMast wrkMast) {
        if (basDevp == null || stationThread == null || stationProtocol == null || wrkMast == null) {
            return;
        }
        int currentTaskBufferCommandCount = countCurrentTaskBufferCommands(
                stationProtocol.getTaskBufferItems(),
                stationProtocol.getTaskNo()
        );
        if (currentTaskBufferCommandCount > 0) {
            News.info("输送站点运行堵塞重分配已跳过,缓存区仍存在当前任务命令。站点号={},工作号={},当前任务命令数={}",
                    stationProtocol.getStationId(),
                    stationProtocol.getTaskNo(),
                    currentTaskBufferCommandCount);
            return;
        }
        if (stationMoveCoordinator != null) {
            stationMoveCoordinator.cancelSession(wrkMast.getWrkNo());
        }
        String response = wmsOperateUtils.applyReassignTaskLocNo(wrkMast.getWrkNo(), stationProtocol.getStationId());
        if (Cools.isEmpty(response)) {
            News.taskError(wrkMast.getWrkNo(), "请求WMS重新分配库位接口失败,接口未响应!!!response:{}", response);
            return;
        }
        JSONObject jsonObject = JSON.parseObject(response);
        if (!jsonObject.getInteger("code").equals(200)) {
            News.error("请求WMS接口失败!!!response:{}", response);
            return;
        }
 
        StartupDto dto = jsonObject.getObject("data", StartupDto.class);
        String sourceLocNo = wrkMast.getLocNo();
        String locNo = dto.getLocNo();
 
        LocMast sourceLocMast = locMastService.queryByLoc(sourceLocNo);
        if (sourceLocMast == null) {
            News.taskInfo(wrkMast.getWrkNo(), "库位号:{} 源库位信息不存在", sourceLocNo);
            return;
        }
        if (!sourceLocMast.getLocSts().equals("S")) {
            News.taskInfo(wrkMast.getWrkNo(), "库位号:{} 源库位状态不处于入库预约", sourceLocNo);
            return;
        }
 
        LocMast locMast = locMastService.queryByLoc(locNo);
        if (locMast == null) {
            News.taskInfo(wrkMast.getWrkNo(), "库位号:{} 目标库位信息不存在", locNo);
            return;
        }
        if (!locMast.getLocSts().equals("O")) {
            News.taskInfo(wrkMast.getWrkNo(), "库位号:{} 目标库位状态不处于空库位", locNo);
            return;
        }
 
        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
        if (findCrnNoResult == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
            return;
        }
        Integer crnNo = findCrnNoResult.getCrnNo();
 
        Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationProtocol.getStationId());
        if (targetStationId == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}站点,搜索入库站点失败", stationProtocol.getStationId());
            return;
        }
 
        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationProtocol.getStationId(), targetStationId, 0);
        if (command == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
            return;
        }
 
        sourceLocMast.setLocSts("O");
        sourceLocMast.setModiTime(new Date());
        locMastService.updateById(sourceLocMast);
 
        locMast.setLocSts("S");
        locMast.setModiTime(new Date());
        locMastService.updateById(locMast);
 
        wrkMast.setLocNo(locNo);
        wrkMast.setStaNo(targetStationId);
 
        if (findCrnNoResult.getCrnType().equals(SlaveType.Crn)) {
            wrkMast.setCrnNo(crnNo);
        } else if (findCrnNoResult.getCrnType().equals(SlaveType.DualCrn)) {
            wrkMast.setDualCrnNo(crnNo);
        } else {
            throw new CoolException("未知设备类型");
        }
 
        if (!wrkMastService.updateById(wrkMast)) {
            return;
        }
        boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_direct");
        if (!offered) {
            return;
        }
        if (stationMoveCoordinator != null) {
            stationMoveCoordinator.recordDispatch(
                    wrkMast.getWrkNo(),
                    stationProtocol.getStationId(),
                    "checkStationRunBlock_direct",
                    command,
                    false
            );
        }
    }
 
    private boolean canRecoverIdleStationTask(WrkMast wrkMast, Integer currentStationId) {
        if (wrkMast == null || currentStationId == null || wrkMast.getStaNo() == null) {
            return false;
        }
        if (Objects.equals(currentStationId, wrkMast.getStaNo())) {
            return false;
        }
        return Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN.sts)
                || Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts);
    }
 
    private boolean shouldSkipIdleRecoverForRecentDispatch(Integer taskNo, Integer stationId) {
        if (stationMoveCoordinator == null || taskNo == null || taskNo <= 0 || stationId == null) {
            return false;
        }
        StationMoveSession session = stationMoveCoordinator.loadSession(taskNo);
        if (session == null || !session.isActive() || session.getLastIssuedAt() == null) {
            return false;
        }
        if (!Objects.equals(stationId, session.getCurrentStationId())
                && !Objects.equals(stationId, session.getDispatchStationId())) {
            return false;
        }
        long elapsedMs = System.currentTimeMillis() - session.getLastIssuedAt();
        long thresholdMs = STATION_IDLE_RECOVER_SECONDS * 1000L;
        if (elapsedMs >= thresholdMs) {
            return false;
        }
        saveStationTaskIdleTrack(new StationTaskIdleTrack(taskNo, stationId, System.currentTimeMillis()));
        News.info("输送站点任务刚完成命令下发,已跳过停留重算。站点号={},工作号={},距上次下发={}ms,routeVersion={}",
                stationId, taskNo, elapsedMs, session.getRouteVersion());
        return true;
    }
 
    private void resetSegmentMoveCommandsBeforeReroute(Integer taskNo) {
        if (redisUtil == null || taskNo == null || taskNo <= 0) {
            return;
        }
        String key = RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo;
        redisUtil.set(key, "cancel", 3);
        try {
            Thread.sleep(STATION_MOVE_RESET_WAIT_MS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (Exception ignore) {
        }
        redisUtil.del(key);
    }
 
    private int countCurrentTaskBufferCommands(List<StationTaskBufferItem> taskBufferItems, Integer currentTaskNo) {
        if (taskBufferItems == null || taskBufferItems.isEmpty() || currentTaskNo == null || currentTaskNo <= 0) {
            return 0;
        }
        int count = 0;
        for (StationTaskBufferItem item : taskBufferItems) {
            if (item == null || item.getTaskNo() == null) {
                continue;
            }
            if (currentTaskNo.equals(item.getTaskNo())) {
                count++;
            }
        }
        return count;
    }
 
    private boolean offerDevpCommandWithDedup(Integer deviceNo, StationCommand command, String scene) {
        if (deviceNo == null || command == null) {
            return false;
        }
        String dedupKey = buildStationCommandDispatchDedupKey(deviceNo, command);
        if (redisUtil != null) {
            Object lock = redisUtil.get(dedupKey);
            if (lock != null) {
                News.info("输送站点命令短时重复派发,已跳过。scene={},deviceNo={},taskNo={},stationId={},targetStaNo={},commandType={}",
                        scene,
                        deviceNo,
                        command.getTaskNo(),
                        command.getStationId(),
                        command.getTargetStaNo(),
                        command.getCommandType());
                return false;
            }
            redisUtil.set(dedupKey, "lock", STATION_COMMAND_DISPATCH_DEDUP_SECONDS);
        }
        boolean offered = MessageQueue.offer(SlaveType.Devp, deviceNo, new Task(2, command));
        if (!offered && redisUtil != null) {
            redisUtil.del(dedupKey);
        }
        return offered;
    }
 
    private String buildStationCommandDispatchDedupKey(Integer deviceNo, StationCommand command) {
        return RedisKeyType.STATION_COMMAND_DISPATCH_DEDUP_.key
                + deviceNo + "_"
                + command.getTaskNo() + "_"
                + command.getStationId() + "_"
                + (stationMoveCoordinator == null ? Integer.toHexString(buildFallbackPathSignature(command).hashCode())
                : stationMoveCoordinator.buildPathSignatureHash(command));
    }
 
    private String buildFallbackPathSignature(StationCommand command) {
        if (command == null) {
            return "";
        }
        return String.valueOf(command.getCommandType())
                + "_" + command.getStationId()
                + "_" + command.getTargetStaNo()
                + "_" + command.getNavigatePath()
                + "_" + command.getLiftTransferPath()
                + "_" + command.getOriginalNavigatePath();
    }
 
    private int clearIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
                                                      Integer taskNo,
                                                      Integer stationId) {
        if (basStationOptService == null) {
            return 0;
        }
        List<BasStationOpt> optList;
        try {
            optList = listIssuedMoveCommandsDuringIdleStay(idleTrack, taskNo);
        } catch (Exception e) {
            return 0;
        }
        if (optList == null || optList.isEmpty()) {
            return 0;
        }
 
        Date now = new Date();
        String cleanupMemo = buildIdleRecoverClearedMemo(stationId);
        int clearedCount = 0;
        for (BasStationOpt opt : optList) {
            if (opt == null || opt.getId() == null) {
                continue;
            }
            opt.setSend(0);
            opt.setUpdateTime(now);
            opt.setMemo(appendCleanupMemo(opt.getMemo(), cleanupMemo));
            clearedCount++;
        }
        if (clearedCount > 0) {
            basStationOptService.updateBatchById(optList);
        }
        return clearedCount;
    }
 
    private List<BasStationOpt> listIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
                                                                     Integer taskNo) {
        if (idleTrack == null || taskNo == null || taskNo <= 0 || idleTrack.firstSeenTime == null || basStationOptService == null) {
            return Collections.emptyList();
        }
        List<BasStationOpt> optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
                .select("id", "task_no", "send_time", "target_station_id", "memo", "send")
                .eq("task_no", taskNo)
                .eq("mode", String.valueOf(StationCommandType.MOVE))
                .eq("send", 1)
                .ge("send_time", new Date(idleTrack.firstSeenTime))
                .orderByAsc("send_time"));
        if (optList == null || optList.isEmpty()) {
            return Collections.emptyList();
        }
        return optList;
    }
 
    private String buildIdleRecoverClearedMemo(Integer stationId) {
        if (stationId == null) {
            return IDLE_RECOVER_CLEARED_MEMO;
        }
        return IDLE_RECOVER_CLEARED_MEMO + "(stationId=" + stationId + ")";
    }
 
    private String appendCleanupMemo(String memo, String cleanupMemo) {
        if (Cools.isEmpty(cleanupMemo)) {
            return memo;
        }
        if (Cools.isEmpty(memo)) {
            return cleanupMemo;
        }
        if (memo.contains(cleanupMemo)) {
            return memo;
        }
        return memo + " | " + cleanupMemo;
    }
 
    private StationTaskIdleTrack touchStationTaskIdleTrack(Integer taskNo, Integer stationId) {
        if (taskNo == null || taskNo <= 0 || stationId == null) {
            return null;
        }
        long now = System.currentTimeMillis();
        StationTaskIdleTrack idleTrack = getStationTaskIdleTrack(taskNo);
        if (idleTrack == null || !Objects.equals(idleTrack.stationId, stationId)) {
            idleTrack = new StationTaskIdleTrack(taskNo, stationId, now);
            saveStationTaskIdleTrack(idleTrack);
        }
        return idleTrack;
    }
 
    private StationTaskIdleTrack getStationTaskIdleTrack(Integer taskNo) {
        if (taskNo == null || taskNo <= 0) {
            return null;
        }
        Object obj = redisUtil.get(RedisKeyType.STATION_TASK_IDLE_TRACK_.key + taskNo);
        if (obj == null) {
            return null;
        }
        try {
            return JSON.parseObject(obj.toString(), StationTaskIdleTrack.class);
        } catch (Exception e) {
            return null;
        }
    }
 
    private void saveStationTaskIdleTrack(StationTaskIdleTrack idleTrack) {
        if (idleTrack == null || idleTrack.taskNo == null || idleTrack.taskNo <= 0) {
            return;
        }
        redisUtil.set(
                RedisKeyType.STATION_TASK_IDLE_TRACK_.key + idleTrack.taskNo,
                JSON.toJSONString(idleTrack, SerializerFeature.DisableCircularReferenceDetect),
                STATION_IDLE_TRACK_EXPIRE_SECONDS
        );
    }
 
    /**
     * 沿“源站 -> 目标站”的理论路径,从当前站点往下游回看,找出同批次任务在后续站点上的已知序号。
     * <p>
     * 严格窗口控制要回答的问题不是“当前任务自己的 batchSeq 是多少”,而是:
     * “在当前站点后面,沿这条出库链路已经排着的同批次任务,最靠近目标端的序号是多少?”
     * 只有拿到这个序号,排序点才能判断当前任务是否应该紧跟其后放行。
     * <p>
     * 具体做法:
     * <p>
     * 1. 按 pathList 从尾到头回扫,截出 searchStationId 之后的全部下游站点;
     * 2. 读取这些站点当前正在执行的任务号;
     * 3. 如果某站点上的任务属于 searchBatch,就记录它的 batchSeq;
     * 4. 返回该批次在下游已知的序号。
     * <p>
     * 这里返回的是“路径下游现场已出现的批次序号”,不是批次理论最小/最大值。
     * 如果下游没有同批次任务,返回 null,调用方需要退回到“当前批次首个未完成任务是否就是自己”的判定。
     *
     * @param pathList 从任务源站到目标站的理论出库路径
     * @param searchStationId 当前正在做排序决策的站点
     * @param searchBatch 当前任务所属批次
     * @return 下游同批次任务的已知序号;若路径下游尚未出现该批次则返回 null
     */
    public Integer getOutStationBatchSeq(List<NavigateNode> pathList, Integer searchStationId, String searchBatch) {
        if (pathList == null || pathList.isEmpty() || searchStationId == null || Cools.isEmpty(searchBatch)) {
            return null;
        }
        // 只关心当前站点之后的下游站点,当前站点之前的节点不会影响“谁应该排在我前面”。
        List<Integer> checkList = new ArrayList<>();
        for (int i = pathList.size() - 1; i >= 0; i--) {
            NavigateNode node = pathList.get(i);
            JSONObject v = JSONObject.parseObject(node.getNodeValue());
            if (v != null) {
                Integer stationId = v.getInteger("stationId");
                if (searchStationId.equals(stationId)) {
                    break;
                } else {
                    checkList.add(stationId);
                }
            }
        }
 
        // 下游站点可能同时挂着多个不同批次任务;这里只抽取与当前批次相关的现场序号快照。
        HashMap<String, Integer> batchMap = new HashMap<>();
        for (Integer station : checkList) {
            BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", station));
            if (basStation == null) {
                continue;
            }
 
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol checkStationProtocol = statusMap.get(station);
            if (checkStationProtocol == null) {
                continue;
            }
            if (checkStationProtocol.getTaskNo() > 0) {
                WrkMast checkWrkMast = wrkMastService.selectByWorkNo(checkStationProtocol.getTaskNo());
                if (checkWrkMast == null) {
                    continue;
                }
 
                if (!Cools.isEmpty(checkWrkMast.getBatch())) {
                    batchMap.put(checkWrkMast.getBatch(), checkWrkMast.getBatchSeq());
                }
            }
        }
 
        Integer seq = batchMap.get(searchBatch);
        return seq;
    }
 
    private int countCurrentStationTask() {
        int currentStationTaskCount = 0;
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
 
            for (StationProtocol stationProtocol : stationThread.getStatus()) {
                if (stationProtocol.getTaskNo() > 0) {
                    currentStationTaskCount++;
                }
            }
        }
        return currentStationTaskCount;
    }
 
    private boolean isDispatchBlocked(DispatchLimitConfig config,
                                      int currentStationTaskCount,
                                      LoadGuardState loadGuardState,
                                      boolean needReserveLoopLoad) {
        if (config.loopModeEnable) {
            double currentLoad = loadGuardState.currentLoad();
            if (currentLoad >= config.circleMaxLoadLimit) {
                News.warn("当前承载量达到上限,已停止站点任务下发。当前承载量={},上限={}", formatPercent(currentLoad), formatPercent(config.circleMaxLoadLimit));
                return true;
            }
 
            if (needReserveLoopLoad) {
                double reserveLoad = loadGuardState.loadAfterReserve();
                if (reserveLoad >= config.circleMaxLoadLimit) {
                    News.warn("预占后承载量达到上限,已停止站点任务下发。预占后承载量={},上限={}", formatPercent(reserveLoad), formatPercent(config.circleMaxLoadLimit));
                    return true;
                }
            }
        }
        return false;
    }
 
    private LoadGuardState buildLoadGuardState(DispatchLimitConfig config) {
        LoadGuardState state = new LoadGuardState();
        if (!config.loopModeEnable) {
            return state;
        }
 
        StationCycleCapacityVo capacityVo = stationCycleCapacityService.getLatestSnapshot();
        if (capacityVo == null) {
            return state;
        }
 
        state.totalStationCount = toNonNegative(capacityVo.getTotalStationCount());
        Integer occupiedStationCount = capacityVo.getOccupiedStationCount();
        state.projectedTaskStationCount = toNonNegative(occupiedStationCount != null ? occupiedStationCount : capacityVo.getTaskStationCount());
 
        List<StationCycleLoopVo> loopList = capacityVo.getLoopList();
        if (loopList != null) {
            for (StationCycleLoopVo loopVo : loopList) {
                if (loopVo == null || loopVo.getStationIdList() == null) {
                    continue;
                }
                Integer loopNo = loopVo.getLoopNo();
                for (Integer stationId : loopVo.getStationIdList()) {
                    if (stationId != null) {
                        if (loopNo != null) {
                            state.stationLoopNoMap.put(stationId, loopNo);
                        }
                    }
                }
            }
        }
        return state;
    }
 
    private LoopHitResult findPathLoopHit(DispatchLimitConfig config,
                                          Integer sourceStationId,
                                          Integer targetStationId,
                                          LoadGuardState loadGuardState) {
        return findPathLoopHit(config, sourceStationId, targetStationId, loadGuardState, null, null);
    }
 
    private LoopHitResult findPathLoopHit(DispatchLimitConfig config,
                                          Integer sourceStationId,
                                          Integer targetStationId,
                                          LoadGuardState loadGuardState,
                                          WrkMast wrkMast,
                                          Double pathLenFactor) {
        if (!config.loopModeEnable) {
            return LoopHitResult.NO_HIT;
        }
        if (sourceStationId == null || targetStationId == null) {
            return LoopHitResult.NO_HIT;
        }
        if (loadGuardState.stationLoopNoMap.isEmpty()) {
            return LoopHitResult.NO_HIT;
        }
 
        try {
            List<NavigateNode> nodes = wrkMast == null
                    ? navigateUtils.calcByStationId(sourceStationId, targetStationId)
                    : calcOutboundNavigatePath(wrkMast, sourceStationId, targetStationId, pathLenFactor);
            if (nodes == null || nodes.isEmpty()) {
                return LoopHitResult.NO_HIT;
            }
 
            for (NavigateNode node : nodes) {
                Integer stationId = getStationIdFromNode(node);
                if (stationId == null) {
                    continue;
                }
                Integer loopNo = loadGuardState.stationLoopNoMap.get(stationId);
                if (loopNo != null) {
                    return new LoopHitResult(true, loopNo, stationId);
                }
            }
        } catch (Exception e) {
            return LoopHitResult.NO_HIT;
        }
 
        return LoopHitResult.NO_HIT;
    }
 
    private Integer getStationIdFromNode(NavigateNode node) {
        if (node == null || isBlank(node.getNodeValue())) {
            return null;
        }
        try {
            JSONObject v = JSONObject.parseObject(node.getNodeValue());
            if (v == null) {
                return null;
            }
            return v.getInteger("stationId");
        } catch (Exception e) {
            return null;
        }
    }
 
    private int toNonNegative(Integer value) {
        if (value == null || value < 0) {
            return 0;
        }
        return value;
    }
 
    private Double normalizePathLenFactor(Double pathLenFactor) {
        if (pathLenFactor == null || pathLenFactor < 0.0d) {
            return 0.0d;
        }
        if (pathLenFactor > 1.0d) {
            return 1.0d;
        }
        return pathLenFactor;
    }
 
    enum RerouteSceneType {
        RUN_BLOCK_REROUTE,
        IDLE_RECOVER,
        OUT_ORDER,
        WATCH_CIRCLE
    }
 
    static final class RerouteDecision {
        private final boolean skip;
        private final String skipReason;
        private final Integer targetStationId;
        private final OutOrderDispatchDecision dispatchDecision;
 
        private RerouteDecision(boolean skip,
                                String skipReason,
                                Integer targetStationId,
                                OutOrderDispatchDecision dispatchDecision) {
            this.skip = skip;
            this.skipReason = skipReason;
            this.targetStationId = targetStationId;
            this.dispatchDecision = dispatchDecision;
        }
 
        static RerouteDecision skip(String reason) {
            return new RerouteDecision(true, reason, null, null);
        }
 
        static RerouteDecision proceed(Integer targetStationId) {
            return new RerouteDecision(false, null, targetStationId, null);
        }
 
        static RerouteDecision proceed(Integer targetStationId,
                                       OutOrderDispatchDecision dispatchDecision) {
            return new RerouteDecision(false, null, targetStationId, dispatchDecision);
        }
 
        boolean skip() {
            return skip;
        }
 
        String skipReason() {
            return skipReason;
        }
 
        Integer targetStationId() {
            return targetStationId;
        }
 
        OutOrderDispatchDecision dispatchDecision() {
            return dispatchDecision;
        }
    }
 
    /**
     * 重算/重发链路的一次性执行上下文。
     *
     * <p>这个对象只在一次 reroute 执行过程中存在,不会落库,也不会长期缓存。
     * 它的职责不是表达业务状态,而是把“这次为什么进来、当前用哪套运行时对象、
     * 下发前后要不要启用额外控制逻辑”集中打包,供统一执行链路使用。
     *
     * <p>统一执行链路大致分三段:
     * 1. {@code resolveSharedRerouteDecision} 根据当前任务和场景先决策目标站。
     * 2. {@code buildRerouteCommandPlan} 决定用普通出库算路还是 run-block 专用算路。
     * 3. {@code executeReroutePlan} 按上下文里的开关决定是否做 suppress、是否加锁、
     *    是否先 cancel 旧 session、是否先清旧分段命令,然后真正下发。
     *
     * <p>字段可以分成四组理解:
     * 1. 场景与运行时对象:
     *    {@code sceneType} / {@code basDevp} / {@code stationThread} /
     *    {@code stationProtocol} / {@code wrkMast}
     *    表示“谁在什么场景下触发了这次重算”。
     * 2. 目标决策输入:
     *    {@code outOrderStationIds} / {@code pathLenFactor}
     *    表示“目标站如何算、路径倾向系数是多少”。
     * 3. 下发目标信息:
     *    {@code dispatchScene} / {@code dispatchDeviceNo}
     *    表示“这次命令最终往哪个输送设备队列发,以及日志/去重场景名是什么”。
     * 4. 执行控制开关:
     *    {@code useRunBlockCommand} / {@code checkSuppressDispatch} /
     *    {@code requireOutOrderDispatchLock} / {@code cancelSessionBeforeDispatch} /
     *    {@code resetSegmentCommandsBeforeDispatch} / {@code clearIdleIssuedCommands} /
     *    {@code checkRecentDispatch} / {@code executionLockKey} / {@code executionLockSeconds}
     *    表示“真正执行前后要打开哪些保护动作”。
     *
     * <p>它本质上是一个参数对象加布尔开关集合:
     * {@code create(...)} 先把这次 reroute 的基础现场填进来,
     * 再通过 {@code withXxx(...)} 逐项声明这次执行需要附加哪些控制语义。
     *
     * <p>例如:
     * RUN_BLOCK_REROUTE 会打开 {@code useRunBlockCommand},
     * 并要求在下发前先 {@code cancelSession}、先清旧分段命令;
     * OUT_ORDER 会打开 suppress guard 和 out-order lock;
     * IDLE_RECOVER 则会打开 recent dispatch guard,并记录/清理停留期间已下发命令。
     */
    static final class RerouteContext {
        // 本次 reroute 的触发来源。决定后面走哪类目标裁决、日志文案和特殊保护分支。
        private final RerouteSceneType sceneType;
        // 当前输送设备配置,主要用于拿默认下发设备号和相关站点配置。
        private final BasDevp basDevp;
        // 当前站点线程,后面构造输送命令时直接依赖它取 command。
        private final StationThread stationThread;
        // 触发这次 reroute 的现场站点状态快照,包含 stationId/taskNo/runBlock/targetStaNo 等运行时信息。
        private final StationProtocol stationProtocol;
        // 当前工作主表记录,表示这次重算对应的任务主状态。
        private final WrkMast wrkMast;
        // 当前设备的出库排序点列表。目标站决策时需要知道哪些站点属于 out-order 节点。
        private final List<Integer> outOrderStationIds;
        // 路径长度倾向系数。批次出库时用于让不同任务对路径选择有稳定偏好。
        private final Double pathLenFactor;
        // 这次派发在日志、去重、session 记录中使用的场景名,例如 checkStationRunBlock_reroute。
        private final String dispatchScene;
        // 实际投递命令的设备号。默认取 basDevp.getDevpNo(),某些场景可显式覆盖。
        private Integer dispatchDeviceNo;
        // true 表示命令构造阶段改走 stationThread.getRunBlockRerouteCommand,而不是普通出库算路。
        private boolean useRunBlockCommand;
        // true 表示执行前要先做 active-session suppress,避免旧活动路线被重复插入新命令。
        private boolean checkSuppressDispatch;
        // true 表示执行前要加 out-order 专用短锁,防止同一排序点短时间重复计算/下发。
        private boolean requireOutOrderDispatchLock;
        // true 表示真正下发前先取消旧 session。通常用于 reroute 替换旧路线。
        private boolean cancelSessionBeforeDispatch;
        // true 表示真正下发前先清理旧分段输送命令,避免 segment executor 还持有旧路线。
        private boolean resetSegmentCommandsBeforeDispatch;
        // true 表示要清理 idle recover 期间已经下发过但未实际生效的旧命令痕迹。
        private boolean clearIdleIssuedCommands;
        // true 表示执行前要检查“最近刚下发过”,用于 idle recover 避免刚发完就重算。
        private boolean checkRecentDispatch;
        // 可选执行锁 key。用于给某个 reroute 场景加短时间互斥。
        private String executionLockKey;
        // executionLockKey 对应的锁秒数。
        private int executionLockSeconds;
        // 仅 idle recover 需要,记录停留跟踪上下文,供清理旧命令与更新时间使用。
        private StationTaskIdleTrack idleTrack;
 
        private RerouteContext(RerouteSceneType sceneType,
                               BasDevp basDevp,
                               StationThread stationThread,
                               StationProtocol stationProtocol,
                               WrkMast wrkMast,
                               List<Integer> outOrderStationIds,
                               Double pathLenFactor,
                               String dispatchScene) {
            this.sceneType = sceneType;
            this.basDevp = basDevp;
            this.stationThread = stationThread;
            this.stationProtocol = stationProtocol;
            this.wrkMast = wrkMast;
            this.outOrderStationIds = outOrderStationIds == null ? Collections.emptyList() : outOrderStationIds;
            this.pathLenFactor = pathLenFactor;
            this.dispatchScene = dispatchScene;
            this.dispatchDeviceNo = basDevp == null ? null : basDevp.getDevpNo();
        }
 
        static RerouteContext create(RerouteSceneType sceneType,
                                     BasDevp basDevp,
                                     StationThread stationThread,
                                     StationProtocol stationProtocol,
                                     WrkMast wrkMast,
                                     List<Integer> outOrderStationIds,
                                     Double pathLenFactor,
                                     String dispatchScene) {
            // create 只负责填基础现场,不默认打开任何执行开关。
            // 每个场景后面通过 withXxx 明确声明自己需要哪些附加控制。
            return new RerouteContext(sceneType, basDevp, stationThread, stationProtocol, wrkMast, outOrderStationIds, pathLenFactor, dispatchScene);
        }
 
        RerouteContext withDispatchDeviceNo(Integer dispatchDeviceNo) {
            // 覆盖默认下发设备号。典型场景是 out-order 站点配置的 deviceNo 与 basDevp 默认值不同。
            this.dispatchDeviceNo = dispatchDeviceNo;
            return this;
        }
 
        RerouteContext withRunBlockCommand() {
            // 命令构造阶段切换到 run-block 专用算路器。
            // 目标站仍由统一决策逻辑决定,只是“去目标站的路径”改为堵塞重规划算法生成。
            this.useRunBlockCommand = true;
            return this;
        }
 
        RerouteContext withSuppressDispatchGuard() {
            // 执行前启用 session suppress:
            // 如果当前 task 在当前位置已经有一条活动中的同路径/同覆盖范围路线,则本次不再重复派发。
            this.checkSuppressDispatch = true;
            return this;
        }
 
        RerouteContext withOutOrderDispatchLock() {
            // 执行前启用排序点短锁。
            // 主要防止同一个 out-order/watch-circle 触发点在极短时间内被并发重复重算。
            this.requireOutOrderDispatchLock = true;
            return this;
        }
 
        RerouteContext withCancelSessionBeforeDispatch() {
            // 执行前显式取消旧 session。
            // 语义是“本次命令准备替换旧路线”,旧 routeVersion 之后不应再继续推进。
            this.cancelSessionBeforeDispatch = true;
            return this;
        }
 
        RerouteContext withResetSegmentCommandsBeforeDispatch() {
            // 执行前清掉 segment executor 侧旧分段命令。
            // 这对 run-block/idle recover 很关键,否则系统可能还拿着旧 segment 状态阻断新路线。
            this.resetSegmentCommandsBeforeDispatch = true;
            return this;
        }
 
        RerouteContext clearIdleIssuedCommands(StationTaskIdleTrack idleTrack) {
            // 仅 idle recover 使用:
            // 表示重启前要把“停留期间已经发过但可能未真正执行的命令痕迹”清理掉。
            this.clearIdleIssuedCommands = true;
            this.idleTrack = idleTrack;
            return this;
        }
 
        RerouteContext withRecentDispatchGuard() {
            // 执行前检查最近是否刚下发过。
            // 避免 idle recover 在“刚重发完”的窗口内又马上触发一次。
            this.checkRecentDispatch = true;
            return this;
        }
 
        RerouteContext withExecutionLock(String executionLockKey, int executionLockSeconds) {
            // 为某个场景挂一个独立执行锁。
            // 和 out-order lock 不同,这里是泛化锁,谁传 key 谁负责定义锁语义。
            this.executionLockKey = executionLockKey;
            this.executionLockSeconds = executionLockSeconds;
            return this;
        }
 
        RerouteSceneType sceneType() {
            return sceneType;
        }
 
        BasDevp basDevp() {
            return basDevp;
        }
 
        StationThread stationThread() {
            return stationThread;
        }
 
        StationProtocol stationProtocol() {
            return stationProtocol;
        }
 
        WrkMast wrkMast() {
            return wrkMast;
        }
 
        List<Integer> outOrderStationIds() {
            return outOrderStationIds;
        }
 
        Double pathLenFactor() {
            return pathLenFactor;
        }
 
        String dispatchScene() {
            return dispatchScene;
        }
 
        Integer dispatchDeviceNo() {
            return dispatchDeviceNo;
        }
 
        boolean useRunBlockCommand() {
            return useRunBlockCommand;
        }
 
        boolean checkSuppressDispatch() {
            return checkSuppressDispatch;
        }
 
        boolean requireOutOrderDispatchLock() {
            return requireOutOrderDispatchLock;
        }
 
        boolean cancelSessionBeforeDispatch() {
            return cancelSessionBeforeDispatch;
        }
 
        boolean resetSegmentCommandsBeforeDispatch() {
            return resetSegmentCommandsBeforeDispatch;
        }
 
        boolean clearIdleIssuedCommands() {
            return clearIdleIssuedCommands;
        }
 
        boolean checkRecentDispatch() {
            return checkRecentDispatch;
        }
 
        String executionLockKey() {
            return executionLockKey;
        }
 
        int executionLockSeconds() {
            return executionLockSeconds;
        }
 
        StationTaskIdleTrack idleTrack() {
            return idleTrack;
        }
    }
 
    static final class RerouteCommandPlan {
        private final boolean skip;
        private final String skipReason;
        private final StationCommand command;
        private final RerouteDecision decision;
        private final String dispatchScene;
 
        private RerouteCommandPlan(boolean skip,
                                   String skipReason,
                                   StationCommand command,
                                   RerouteDecision decision,
                                   String dispatchScene) {
            this.skip = skip;
            this.skipReason = skipReason;
            this.command = command;
            this.decision = decision;
            this.dispatchScene = dispatchScene;
        }
 
        static RerouteCommandPlan skip(String reason) {
            return new RerouteCommandPlan(true, reason, null, null, null);
        }
 
        static RerouteCommandPlan dispatch(StationCommand command,
                                           RerouteDecision decision,
                                           String dispatchScene) {
            return new RerouteCommandPlan(false, null, command, decision, dispatchScene);
        }
 
        boolean skip() {
            return skip;
        }
 
        String skipReason() {
            return skipReason;
        }
 
        StationCommand command() {
            return command;
        }
 
        RerouteDecision decision() {
            return decision;
        }
 
        String dispatchScene() {
            return dispatchScene;
        }
    }
 
    static final class RerouteExecutionResult {
        private final boolean skipped;
        private final String skipReason;
        private final boolean dispatched;
        private final StationCommand command;
        private final int clearedCommandCount;
 
        private RerouteExecutionResult(boolean skipped,
                                       String skipReason,
                                       boolean dispatched,
                                       StationCommand command,
                                       int clearedCommandCount) {
            this.skipped = skipped;
            this.skipReason = skipReason;
            this.dispatched = dispatched;
            this.command = command;
            this.clearedCommandCount = clearedCommandCount;
        }
 
        static RerouteExecutionResult skip(String reason) {
            return new RerouteExecutionResult(true, reason, false, null, 0);
        }
 
        static RerouteExecutionResult dispatched(StationCommand command,
                                                 int clearedCommandCount) {
            return new RerouteExecutionResult(false, null, true, command, clearedCommandCount);
        }
 
        boolean skipped() {
            return skipped;
        }
 
        String skipReason() {
            return skipReason;
        }
 
        boolean dispatched() {
            return dispatched;
        }
 
        StationCommand command() {
            return command;
        }
 
        int clearedCommandCount() {
            return clearedCommandCount;
        }
    }
 
    private static class OutOrderDispatchDecision {
        private final Integer targetStationId;
        private final boolean circle;
        private final StationTaskLoopService.LoopEvaluation loopEvaluation;
        private final boolean countLoopIssue;
 
        private OutOrderDispatchDecision(Integer targetStationId, boolean circle) {
            this(targetStationId, circle, null, false);
        }
 
        private OutOrderDispatchDecision(Integer targetStationId,
                                         boolean circle,
                                         StationTaskLoopService.LoopEvaluation loopEvaluation,
                                         boolean countLoopIssue) {
            this.targetStationId = targetStationId;
            this.circle = circle;
            this.loopEvaluation = loopEvaluation;
            this.countLoopIssue = countLoopIssue;
        }
 
        private Integer getTargetStationId() {
            return targetStationId;
        }
 
        private boolean isCircle() {
            return circle;
        }
 
        private StationTaskLoopService.LoopEvaluation getLoopEvaluation() {
            return loopEvaluation;
        }
 
        private boolean shouldCountLoopIssue() {
            return countLoopIssue;
        }
    }
 
    private static class CircleTargetCandidate {
        private final Integer stationId;
        private final Integer pathLength;
        private final Integer offset;
 
        private CircleTargetCandidate(Integer stationId, Integer pathLength, Integer offset) {
            this.stationId = stationId;
            this.pathLength = pathLength == null ? 0 : pathLength;
            this.offset = offset == null ? 0 : offset;
        }
 
        private Integer getStationId() {
            return stationId;
        }
 
        private Integer getPathLength() {
            return pathLength;
        }
 
        private Integer getOffset() {
            return offset;
        }
    }
 
    private void saveLoopLoadReserve(Integer wrkNo, LoopHitResult loopHitResult) {
        if (wrkNo == null || wrkNo <= 0 || loopHitResult == null || !loopHitResult.isThroughLoop()) {
            return;
        }
        JSONObject reserveJson = new JSONObject();
        reserveJson.put("wrkNo", wrkNo);
        reserveJson.put("loopNo", loopHitResult.getLoopNo());
        reserveJson.put("hitStationId", loopHitResult.getHitStationId());
        reserveJson.put("createTime", System.currentTimeMillis());
        redisUtil.hset(RedisKeyType.STATION_CYCLE_LOAD_RESERVE.key, String.valueOf(wrkNo), reserveJson.toJSONString());
        redisUtil.expire(RedisKeyType.STATION_CYCLE_LOAD_RESERVE.key, LOOP_LOAD_RESERVE_EXPIRE_SECONDS);
    }
 
    private DispatchLimitConfig getDispatchLimitConfig(Integer startStationId, Integer endStationId) {
        DispatchLimitConfig config = new DispatchLimitConfig();
        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
        if (systemConfigMapObj instanceof Map) {
            Map<?, ?> systemConfigMap = (Map<?, ?>) systemConfigMapObj;
            config.circleMaxLoadLimit = parseLoadLimit(getConfigValue(systemConfigMap, "circleMaxLoadLimit"), config.circleMaxLoadLimit);
            String loopModeValue = getConfigValue(systemConfigMap, "circleLoopModeEnable");
            if (isBlank(loopModeValue)) {
                loopModeValue = getConfigValue(systemConfigMap, "circleModeEnable");
            }
            if (isBlank(loopModeValue)) {
                loopModeValue = getConfigValue(systemConfigMap, "isCircleMode");
            }
            config.loopModeEnable = parseBoolean(loopModeValue, config.loopModeEnable);
        }
 
        if (stationPathPolicyService != null && startStationId != null && endStationId != null) {
            try {
                StationPathResolvedPolicy resolvedPolicy = stationPathPolicyService.resolvePolicy(startStationId, endStationId);
                if (resolvedPolicy != null && resolvedPolicy.getProfileConfig() != null) {
                    config.circleMaxLoadLimit = parseLoadLimit(String.valueOf(resolvedPolicy.getProfileConfig().getCircleMaxLoadLimit()), config.circleMaxLoadLimit);
                }
            } catch (Exception ignore) {
            }
        }
 
        return config;
    }
 
    private String getConfigValue(Map<?, ?> configMap, String key) {
        Object value = configMap.get(key);
        if (value == null) {
            return null;
        }
        return String.valueOf(value).trim();
    }
 
    private boolean parseBoolean(String value, boolean defaultValue) {
        if (isBlank(value)) {
            return defaultValue;
        }
        String lowValue = value.toLowerCase(Locale.ROOT);
        if ("y".equals(lowValue) || "yes".equals(lowValue) || "true".equals(lowValue)
                || "1".equals(lowValue) || "on".equals(lowValue)) {
            return true;
        }
        if ("n".equals(lowValue) || "no".equals(lowValue) || "false".equals(lowValue)
                || "0".equals(lowValue) || "off".equals(lowValue)) {
            return false;
        }
        return defaultValue;
    }
 
    private double parseLoadLimit(String value, double defaultValue) {
        if (isBlank(value)) {
            return defaultValue;
        }
        try {
            String normalized = value.replace("%", "").trim();
            double parsed = Double.parseDouble(normalized);
            if (parsed > 1.0) {
                parsed = parsed / 100.0;
            }
            if (parsed < 0.0) {
                return 0.0;
            }
            if (parsed > 1.0) {
                return 1.0;
            }
            return parsed;
        } catch (Exception e) {
            return defaultValue;
        }
    }
 
    private int parseInt(String value, int defaultValue) {
        if (isBlank(value)) {
            return defaultValue;
        }
        try {
            int parsed = Integer.parseInt(value.trim());
            return parsed < 0 ? defaultValue : parsed;
        } catch (Exception e) {
            return defaultValue;
        }
    }
 
    private String formatPercent(double value) {
        return String.format(Locale.ROOT, "%.1f%%", value * 100.0);
    }
 
    private boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }
 
    private static class DispatchLimitConfig {
        // 圈最大承载能力,默认80%
        private double circleMaxLoadLimit = 0.8d;
        // 是否启用绕圈模式(仅启用时才生效承载限制)
        private boolean loopModeEnable = false;
    }
 
    private static class LoadGuardState {
        private int totalStationCount = 0;
        private int projectedTaskStationCount = 0;
        private final Map<Integer, Integer> stationLoopNoMap = new HashMap<>();
 
        private double currentLoad() {
            return calcLoad(this.projectedTaskStationCount, this.totalStationCount);
        }
 
        private double loadAfterReserve() {
            return calcLoad(this.projectedTaskStationCount + 1, this.totalStationCount);
        }
 
        private void reserveLoopTask(Integer loopNo) {
            if (loopNo == null || loopNo <= 0) {
                return;
            }
            if (this.totalStationCount <= 0) {
                return;
            }
            this.projectedTaskStationCount++;
        }
 
        private double calcLoad(int taskCount, int stationCount) {
            if (stationCount <= 0 || taskCount <= 0) {
                return 0.0;
            }
            double load = (double) taskCount / (double) stationCount;
            if (load < 0.0) {
                return 0.0;
            }
            if (load > 1.0) {
                return 1.0;
            }
            return load;
        }
    }
 
    private static class LoopHitResult {
        private static final LoopHitResult NO_HIT = new LoopHitResult(false, null, null);
        private final boolean throughLoop;
        private final Integer loopNo;
        private final Integer hitStationId;
 
        private LoopHitResult(boolean throughLoop, Integer loopNo, Integer hitStationId) {
            this.throughLoop = throughLoop;
            this.loopNo = loopNo;
            this.hitStationId = hitStationId;
        }
 
        private boolean isThroughLoop() {
            return throughLoop;
        }
 
        private Integer getLoopNo() {
            return loopNo;
        }
 
        private Integer getHitStationId() {
            return hitStationId;
        }
    }
 
    private static class StationTaskIdleTrack {
        private Integer taskNo;
        private Integer stationId;
        private Long firstSeenTime;
 
        private StationTaskIdleTrack() {}
 
        private StationTaskIdleTrack(Integer taskNo, Integer stationId, Long firstSeenTime) {
            this.taskNo = taskNo;
            this.stationId = stationId;
            this.firstSeenTime = firstSeenTime;
        }
 
        private boolean isTimeout(int seconds) {
            if (firstSeenTime == null) {
                return false;
            }
            return System.currentTimeMillis() - firstSeenTime >= seconds * 1000L;
        }
 
        public Integer getTaskNo() {
            return taskNo;
        }
 
        public void setTaskNo(Integer taskNo) {
            this.taskNo = taskNo;
        }
 
        public Integer getStationId() {
            return stationId;
        }
 
        public void setStationId(Integer stationId) {
            this.stationId = stationId;
        }
 
        public Long getFirstSeenTime() {
            return firstSeenTime;
        }
 
        public void setFirstSeenTime(Long firstSeenTime) {
            this.firstSeenTime = firstSeenTime;
        }
    }
 
}