自动化立体仓库 - WCS系统
Junjie
2024-12-21 2074f1e03c9b2e2ff1d0b57de05becb7f6e0ffb8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.*;
import com.zy.common.model.enums.NavigationMapType;
import com.zy.common.service.CommonService;
import com.zy.common.service.erp.ErpService;
import com.zy.common.utils.*;
import com.zy.core.DevpThread;
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.*;
import com.zy.core.model.command.*;
import com.zy.core.model.protocol.*;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.*;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 立体仓库WCS系统主流程业务
 * Created by vincent on 2020/8/6
 */
@Slf4j
@Service("mainService")
public class MainServiceImpl {
 
    @Value("${wms.url}")
    private String wmsUrl;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private WrkMastMapper wrkMastMapper;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private BasErrLogService basErrLogService;
    @Autowired
    private BasSteErrLogService basSteErrLogService;
    @Autowired
    private BasShuttleService basShuttleService;
    @Autowired
    private BasShuttleErrLogService basShuttleErrLogService;
    @Autowired
    private BasLiftErrLogService basLiftErrLogService;
    @Autowired
    private BasShuttleErrService basShuttleErrService;
    @Autowired
    private BasCrnErrorMapper basCrnErrorMapper;
    @Autowired
    private BasSteService basSteService;
    @Autowired
    private WrkChargeService wrkChargeService;
    @Autowired
    private BasSteErrService basSteErrService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private WrkChargeMapper wrkChargeMapper;
    @Autowired
    private BasMapService basMapService;
    @Autowired
    private ErpService erpService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetlMapper orderDetlMapper;
    @Autowired
    private BasLiftService basLiftService;
    @Autowired
    private ShuttleDispatchUtils shuttleDispatchUtils;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WrkMastLogMapper wrkMastLogMapper;
    @Autowired
    private BasLiftOptService basLiftOptService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private NavigateMapUtils navigateMapUtils;
    @Autowired
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private MatService matService;
    @Autowired
    private NavigateMapData navigateMapData;
 
 
    /**
     * 组托
     * 入库站,根据条码扫描生成入库工作档,工作状态 2
     */
    public void generateStoreWrkFile() {
        try {
            // 根据输送线plc遍历
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历入库口
                for (DevpSlave.Sta inSta : devp.getInSta()) {
                    // 获取入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    Short workNo = staProtocol.getWorkNo();
                    // 尺寸检测异常
                    boolean back = false;
                    String errMsg = "异常:";
                    if (staProtocol.isFrontErr()) {
                        errMsg = errMsg + "前超限;";
                        back = true;
                    }
                    if (staProtocol.isBackErr()) {
                        errMsg = errMsg + "后超限";
                        back = true;
                    }
                    if (staProtocol.isHighErr()) {
                        errMsg = errMsg + "高超限";
                        back = true;
                    }
                    if (staProtocol.isLeftErr()) {
                        errMsg = errMsg + "左超限";
                        back = true;
                    }
                    if (staProtocol.isRightErr()) {
                        errMsg = errMsg + "右超限";
                        back = true;
                    }
                    if (staProtocol.isWeightErr()) {
                        errMsg = errMsg + "超重";
                        back = true;
                    }
                    if (staProtocol.isBarcodeErr()) {
                        errMsg = errMsg + "扫码失败";
                        back = true;
                    }
                    // 退回
                    if (back) {
                        // led 异常显示
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        if (ledThread != null) {
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                        }
                        continue;
                    }
 
                    // 判断是否满足入库条件
                    if (staProtocol.isAutoing() && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                    ) {
 
                        // 获取条码扫描仪信息
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                        if (barcodeThread == null) {
                            continue;
                        }
                        String barcode = barcodeThread.getBarcode();
                        if (!Cools.isEmpty(barcode)) {
//                        News.info("{}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                            if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode) || "00000000".equals(barcode)) {
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                                // led 异常显示
                                LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                                if (ledThread != null) {
                                    String errorMsg = "扫码失败,请重试";
                                    MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                                }
                                continue;
                            }
                        } else {
//                        staProtocol.setWorkNo((short) 32002);
//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
//                        // led 异常显示
//                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
//                        if (ledThread != null) {
//                            String errorMsg = "扫码失败,请重试";
//                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
//                        }
                            continue;
                        }
 
                        // 过滤盘点/拣料/并板任务
                        WrkMast wrkMast1 = wrkMastMapper.selectPickStepByBarcode(barcode);
                        if (null != wrkMast1) {
                            continue;
                        }
 
                        // 判断重复工作档
                        WrkMast wrkMast2 = wrkMastMapper.selectPakInStep1(inSta.getStaNo(), barcode);
                        if (wrkMast2 != null) {
                            News.error("工作档中已存在该站状态为( 2.设备上走 )的数据,工作号={}", wrkMast2.getWrkNo());
                            continue;
                        }
 
                        try {
                            LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
                            SearchLocParam param = new SearchLocParam();
                            param.setBarcode(barcode);
                            param.setIoType(1);
                            param.setSourceStaNo(inSta.getStaNo());
                            param.setLocType1(locTypeDto.getLocType1());
                            String response = new HttpHandler.Builder()
                                    .setUri(wmsUrl)
                                    .setPath("/rpc/pakin/loc/v2")
                                    .setJson(JSON.toJSONString(param))
                                    .build()
                                    .doPost();
                            JSONObject jsonObject = JSON.parseObject(response);
                            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                            Integer code = jsonObject.getInteger("code");
                            if (code.equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                                // plc 处理
                                barcodeThread.setBarcode("");
//                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
//                            staProtocol.setStaNo(dto.getStaNo().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                throw new CoolException("更新plc站点信息失败");
//                            }
 
                                // 判断重复工作档
                                WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
                                if (wrkMast == null) {
                                    continue;
                                }
 
                                // 更新工作主档
                                wrkMast.setWrkSts(2L); // 工作状态:2.设备上走
                                wrkMast.setModiTime(new Date());
                                if (wrkMastMapper.updateById(wrkMast) == 0) {
                                    News.error("更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                                }
 
                            } else if (code == 500) {
                                if (ledThread != null) {
                                    String errorMsg = jsonObject.getString("msg");
                                    if (!Cools.isEmpty(errorMsg)) {
                                        MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                                        ledThread.setLedMk(false);
                                    }
                                }
                                News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v2", JSON.toJSONString(param), response);
                            } else if (code == 700) {
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setRollback102(1);//102站回退信号
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(5, staProtocol));
 
                                // led 异常显示
                                if (ledThread != null) {
                                    String errorMsg = barcode + "托盘识别异常,请先进行组托!";
                                    MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                                    ledThread.setLedMk(false);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * wms入库
     * 入库站,根据条码扫描生成入库工作档,工作状态 1 ==>> 2
     */
    @Deprecated
    public void generateStoreWrkFile0() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 判断是否满足入库条件
                if (staProtocol.isAutoing() && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                        && staProtocol.isPakMk()) {
 
                    // 尺寸检测异常
                    boolean back = false;
                    String errMsg = "";
                    if (staProtocol.isFrontErr()) {
                        errMsg = "前超限";
                        back = true;
                    }
                    if (!back && staProtocol.isBackErr()) {
                        errMsg = "后超限";
                        back = true;
                    }
                    if (!back && staProtocol.isHighErr()) {
                        errMsg = "高超限";
                        back = true;
                    }
                    if (!back && staProtocol.isLeftErr()) {
                        errMsg = "左超限";
                        back = true;
                    }
                    if (!back && staProtocol.isRightErr()) {
                        errMsg = "右超限";
                        back = true;
                    }
                    if (!back && staProtocol.isWeightErr()) {
                        errMsg = "超重";
                        back = true;
                    }
                    if (!back && staProtocol.isBarcodeErr()) {
                        errMsg = "扫码失败";
                        back = true;
                    }
 
                    // 退回
                    if (back) {
                        News.warn("扫码入库失败,{}入库站因{}异常,托盘已被退回", inSta.getStaNo(), errMsg);
                        staProtocol.setWorkNo((short) 32002);
                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
                        devpThread.setPakMk(staProtocol.getSiteId(), false);
                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                        // led 异常显示
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        if (ledThread != null) {
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                        }
                        continue;
                    }
 
                    // 判断重复工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
                    if (wrkMast == null) { continue; }
 
                    // 命令下发区 --------------------------------------------------------------------------
 
                    // 更新站点信息 且 下发plc命令
                    staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                    staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                    devpThread.setPakMk(staProtocol.getSiteId(), false);
                    boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                    if (result) {
                        // 更新工作主档
                        wrkMast.setWrkSts(2L); // 工作状态:2.设备上走
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            News.error("更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
                    } else {
                        News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                    }
                }
            }
        }
    }
 
    /**
     * 拣料、并板再入库
     */
    @Transactional
    public synchronized void stnToCrnStnPick(){
        try {
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历拣料入库口
                for (DevpSlave.Sta pickSta : devp.getPickInSta()) {
                    // 获取拣料入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    if (staProtocol.isAutoing()
                            && staProtocol.isLoading()
                            && staProtocol.isInEnable()
//                        && (staProtocol.getWorkNo() > 0)
                            && staProtocol.isPakMk()) {
 
                        // 获取条码扫描仪信息
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, pickSta.getBarcode());
                        if (barcodeThread == null) {
                            continue;
                        }
                        String barcode = barcodeThread.getBarcode();
                        if (Cools.isEmpty(barcode)) {
                            continue;
                        }
                        if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
                            continue;
                        }
 
                        WrkMast wrkMast = wrkMastMapper.selectPickStepByBarcode(barcode);
                        if (wrkMast == null) {//找不到工作档
                            continue;
                        }
 
                        if ((wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104)
                                || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo())) {
                            continue;
                        }
 
//                        //*********************同库位组校验*********************
//                        boolean flag = false;
//                        String th = "";
//                        List<String> innerLoc = Utils.getGroupInnerLoc(wrkMast.getSourceLocNo());
//                        for (String loc : innerLoc) {
//                            LocMast locMast = locMastService.selectById(loc);
//                            if (locMast == null) {
//                                continue;
//                            }
//
//                            if (!locMast.getLocSts().equals("F")) {
//                                flag = true;
//                                th = loc + "库位存在未回库任务";
//                                break;
//                            }
//                        }
//                        if (flag) {
//                            News.info(th);
//                            continue;
//                        }
//                        //*********************同库位组校验*********************
 
//                        // 保存工作主档历史档
//                        if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
//                            News.info(wrkMast.getWrkNo() + "保存工作主档历史档失败");
//                            continue;
//                        }
 
                        try {
                            LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());//源库位
                            SearchLocParam param = new SearchLocParam();
                            param.setWrkNo(wrkMast.getWrkNo());
                            param.setBarcode(wrkMast.getBarcode());
                            param.setIoType(wrkMast.getIoType());
                            param.setSourceStaNo(pickSta.getStaNo());
                            param.setLiftNo(pickSta.getLiftNo());
                            param.setLocType1(locMast.getLocType1());
                            String response = new HttpHandler.Builder()
                                    .setUri(wmsUrl)
                                    .setPath("/rpc/pakin/loc/v2")
                                    .setJson(JSON.toJSONString(param))
                                    .build()
                                    .doPost();
                            JSONObject jsonObject = JSON.parseObject(response);
                            Integer code = jsonObject.getInteger("code");
                            if (code.equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
 
//                                //获取回库提升机目标站
//                                LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(pickSta.getLiftNo(), Utils.getLev(dto.getLocNo()));
//                                if (liftStaProtocol == null) {
//                                    News.info(wrkMast.getWrkNo() + "获取回库提升机目标站失败");
//                                    continue;
//                                }
//
//                                // 更新工作档数据状态
//                                wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 103->53,104->54
//                                wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
//                                wrkMast.setSourceStaNo(dto.getSourceStaNo()); // 源站
//                                wrkMast.setStaNo(liftStaProtocol.getStaNo());//目标站
//                                wrkMast.setLocNo(dto.getLocNo()); // 目标库位
//                                wrkMast.setShuttleNo(null); // 穿梭车清空
//                                wrkMast.setLiftNo(null);// 提升机清空
//                                wrkMast.setModiTime(new Date());
//                                if (wrkMastMapper.updateById(wrkMast) == 0) {
//                                    News.info(wrkMast.getWrkNo() + "更新工作档数据状态失败");
//                                    continue;
//                                }
                            } else if (code == 500) {
                                News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v2", JSON.toJSONString(param), response);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
                    }
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 拣料、并板再入库(325、331、333、339)
     */
    @Transactional
    public synchronized void stnToCrnStnPick2(){
        try {
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历拣料入库口
                for (DevpSlave.Sta pickSta : devp.getPickInSta2()) {
                    // 获取拣料入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    if (staProtocol.isAutoing()
                            && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && (staProtocol.getWorkNo() > 0)
                            && staProtocol.isPakMk()) {
 
                        WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol.getWorkNo().intValue());
                        if (wrkMast == null) {
                            continue;
                        }
                        if (wrkMast.getSteNo() == null) {
                            wrkMast.setSteNo(1);
                            wrkMastMapper.updateById(wrkMast);
                            staProtocol.setStaNo((short) 341);//写入目标站
                            staProtocol.setPakMk(false);
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                        }
                    }
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 盘点再入库
     */
    @Transactional
    public synchronized void stnToCrnStnPlate(){
        try {
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历拣料入库口
                for (DevpSlave.Sta pickSta : devp.getPickInSta()) {
                    // 获取拣料入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    if (staProtocol.isAutoing()
                            && staProtocol.isLoading()
                            && staProtocol.isInEnable()
//                            && (staProtocol.getWorkNo() > 0)
                            && staProtocol.isPakMk()) {
 
                        // 获取条码扫描仪信息
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, pickSta.getBarcode());
                        if (barcodeThread == null) {
                            continue;
                        }
                        String barcode = barcodeThread.getBarcode();
                        if (Cools.isEmpty(barcode)) {
                            continue;
                        }
                        if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
                            continue;
                        }
                        WrkMast wrkMast = wrkMastMapper.selectByBarcode(barcode);
                        if (wrkMast == null) {//找不到工作档
                            continue;
                        }
 
                        if ((wrkMast.getIoType() != 107)
                                || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo())) {
                            continue;
                        }
 
//                        // 保存工作主档历史档
//                        if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
//                            News.info(wrkMast.getWrkNo() + "保存工作主档历史档失败");
//                            continue;
//                        }
 
                        //盘点找新库位
                        try {
                            LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());//源库位
                            SearchLocParam param = new SearchLocParam();
                            param.setWrkNo(wrkMast.getWrkNo());
                            param.setBarcode(wrkMast.getBarcode());
                            param.setIoType(107);//盘点
                            param.setSourceStaNo(pickSta.getStaNo());
                            param.setLiftNo(pickSta.getLiftNo());
                            param.setLocType1(locMast.getLocType1());
                            String response = new HttpHandler.Builder()
                                    .setUri(wmsUrl)
                                    .setPath("/rpc/pakin/loc/v2")
                                    .setJson(JSON.toJSONString(param))
                                    .build()
                                    .doPost();
                            JSONObject jsonObject = JSON.parseObject(response);
                            Integer code = jsonObject.getInteger("code");
                            if (code.equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
 
//                                //获取回库提升机目标站
//                                LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(pickSta.getLiftNo(), Utils.getLev(dto.getLocNo()));
//                                if (liftStaProtocol == null) {
//                                    News.info(wrkMast.getWrkNo() + "获取回库提升机目标站失败");
//                                    continue;
//                                }
//
//                                // 更新工作档数据状态
//                                wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 107->57
//                                wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
//                                wrkMast.setSourceStaNo(dto.getSourceStaNo()); // 源站
//                                wrkMast.setStaNo(liftStaProtocol.getStaNo());//目标站
//                                wrkMast.setLocNo(dto.getLocNo()); // 目标库位
//                                wrkMast.setShuttleNo(null); // 穿梭车清空
//                                wrkMast.setLiftNo(null);// 提升机清空
//                                wrkMast.setModiTime(new Date());
//                                if (wrkMastMapper.updateById(wrkMast) == 0) {
//                                    News.info(wrkMast.getWrkNo() + "更新工作档数据状态失败");
//                                    continue;
//                                }
 
//                                staProtocol.setStaNo(dto.getSourceStaNo().shortValue());//写入目标站
//                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                            } else if (code == 500) {
                                News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v2", JSON.toJSONString(param), response);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
 
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
//    /**
//     * 拣料、并板、盘点再入库
//     */
//    @Transactional
//    public synchronized void stnToCrnStnPick(){
//        for (DevpSlave devp : slaveProperties.getDevp()) {
//            // 遍历拣料入库口
//            for (DevpSlave.Sta pickSta : devp.getPickInSta()) {
//
//                // 获取拣料入库站信息
//                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
//                StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
//                if (staProtocol == null) {
//                    continue;
//                } else {
//                    staProtocol = staProtocol.clone();
//                }
//                if (staProtocol.isAutoing()
//                        && staProtocol.isLoading()
////                        && (staProtocol.getWorkNo() > 0 && staProtocol.getWorkNo() < 9999)
////                        && staProtocol.getStaNo().equals(staProtocol.getSiteId().shortValue())
//                        && staProtocol.isPakMk()){
//
//                    // 获取条码扫描仪信息
//                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, pickSta.getBarcode());
//                    if (barcodeThread == null) {
//                        continue;
//                    }
//                    String barcode = barcodeThread.getBarcode();
//
//                    WrkMast wrkMast = null;
//                    wrkMast = wrkMastMapper.selectPickStepByBarcode(barcode);
//                    if (wrkMast == null) {
//                        continue;
//                    }
////                    if (staProtocol.getWorkNo() == 9996) {
////                        String barcode = barcodeThread.getBarcode();
////                        if(!Cools.isEmpty(barcode)) {
////                            News.info("{}号条码扫描器检测条码信息:{}", pickSta.getBarcode(), barcode);
////                            if("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
////                                staProtocol.setWorkNo((short) 32002);
////                                staProtocol.setStaNo(pickSta.getBackSta().shortValue());
////                                devpThread.setPakMk(staProtocol.getSiteId(), false);
////                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
////
////                                // led 异常显示
////                                LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
////                                if (ledThread != null) {
////                                    String errorMsg = "扫码失败,请重试";
////                                    MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
////                                }
////                                continue;
////                            }
////                        } else {
////                            staProtocol.setWorkNo((short) 32002);
////                            staProtocol.setStaNo(pickSta.getBackSta().shortValue());
////                            devpThread.setPakMk(staProtocol.getSiteId(), false);
////                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
////
////                            // led 异常显示
////                            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
////                            if (ledThread != null) {
////                                String errorMsg = "扫码失败,请重试";
////                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
////                            }
////                            continue;
////                        }
////                        wrkMast = wrkMastMapper.selectPickStepByBarcode(barcode);
////                        if (null == wrkMast) {
////                            News.error("{}条码错误,暂无拣料任务!", barcode);
////                        }
////                    }
//
//                    if ((wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 107)
//                        || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo()) ) {
//                        continue;
//                    }
//
//                    // 保存工作主档历史档
//                    if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
//                        throw new CoolException(wrkMast.getWrkNo() + "保存工作主档历史档失败");
//                    }
//
//                    String sourceLocNo = wrkMast.getSourceLocNo().trim();
//                    LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(pickSta.getLiftNo(), Utils.getLev(sourceLocNo));//获取回库提升机目标站
//                    if (liftStaProtocol == null) {
//                        continue;
//                    }
//
//                    // 更新工作档数据状态
//                    wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 103->53,104->54,107->57
//                    wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
//                    wrkMast.setSourceStaNo(pickSta.getStaNo()); // 源站
////                            wrkMast.setStaNo(dto.getStaNo()); // 目标站
////                            wrkMast.setCrnNo(dto.getCrnNo());
//                    wrkMast.setStaNo(liftStaProtocol.getStaNo());//目标站
//                    wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
//                    wrkMast.setShuttleNo(null); // 穿梭车清空
//                    wrkMast.setLiftNo(null);// 提升机清空
//                    wrkMast.setModiTime(new Date());
//                    if (wrkMastMapper.updateById(wrkMast) == 0) {
//                        throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
//                    }
//                    if (wrkMastMapper.setSteEmpty(wrkMast.getWrkNo()) == 0) {
//                        throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
//                    }
//
//                    // 修改库位状态 Q.拣料/盘点/并板再入库
//                    LocMast locMast = locMastService.selectById(sourceLocNo);
//                    locMast.setLocSts("Q");
//                    locMast.setModiTime(new Date());
//                    if (!locMastService.updateById(locMast)) {
//                        throw new CoolException("修改库位状态失败");
//                    }
//                    barcodeThread.setBarcode("");//清理条码
//
////                    try {
////                        // 访问 WMS 获取入库库位
////                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
////                        SearchLocParam param = new SearchLocParam();
////                        param.setWrkNo(wrkMast.getWrkNo());
////                        param.setIoType(wrkMast.getIoType());
////                        param.setSourceStaNo(pickSta.getStaNo());
//////                        param.setLocType1(locTypeDto.getLocType1());
////                        String response = new HttpHandler.Builder()
////                                .setUri(wmsUrl)
////                                .setPath("/rpc/pakin/loc/v1")
////                                .setJson(JSON.toJSONString(param))
////                                .build()
////                                .doPost();
////                        JSONObject jsonObject = JSON.parseObject(response);
////                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
////                        Integer code = jsonObject.getInteger("code");
////                        if (code.equals(200)) {
////                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
////
//////                            // 获取目标站
//////                            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
//////                                    .eq("type_no", wrkMast.getIoType() - 50)
//////                                    .eq("stn_no", pickSta.getStaNo()) // 作业站点 = 拣料出库的目标站
//////                                    .eq("crn_no", dto.getCrnNo()); // 堆垛机号
//////                            StaDesc staDesc = staDescService.selectOne(wrapper);
//////                            if (Cools.isEmpty(staDesc)) {
//////                                News.error("入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
//////                                continue;
//////                            }
//////                            // 堆垛机站点(目标站)
//////                            Integer staNo = staDesc.getCrnStn();
////
////                            // 保存工作明细档历史档
//////                        if (wrkMastMapper.saveWrkDetlLog(wrkMast.getWrkNo()) == 0) {
//////                            throw new CoolException("保存工作明细档历史档失败");
//////                        }
////                            // 保存工作主档历史档
////                            if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
////                                throw new CoolException(wrkMast.getWrkNo() + "保存工作主档历史档失败");
////                            }
////
////                            String sourceLocNo = wrkMast.getSourceLocNo().trim();
////                            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(pickSta.getLiftNo(), Utils.getLev(sourceLocNo));//获取回库提升机目标站
////                            if (liftStaProtocol == null) {
////                                continue;
////                            }
////
////                            // 更新工作档数据状态
////                            wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 103->53,104->54,107->57
////                            wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
////                            wrkMast.setSourceStaNo(pickSta.getStaNo()); // 源站
//////                            wrkMast.setStaNo(dto.getStaNo()); // 目标站
//////                            wrkMast.setCrnNo(dto.getCrnNo());
////                            wrkMast.setStaNo(liftStaProtocol.getStaNo());//目标站
////                            wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
////                            wrkMast.setShuttleNo(null); // 穿梭车清空
////                            wrkMast.setLiftNo(null);// 提升机清空
////                            wrkMast.setModiTime(new Date());
////                            if (wrkMastMapper.updateById(wrkMast) == 0) {
////                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
////                            }
////                            if (wrkMastMapper.setSteEmpty(wrkMast.getWrkNo()) == 0) {
////                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
////                            }
////
////                            // 修改库位状态 Q.拣料/盘点/并板再入库
////                            LocMast locMast = locMastService.selectById(sourceLocNo);
////                            locMast.setLocSts("Q");
////                            locMast.setModiTime(new Date());
////                            if (!locMastService.updateById(locMast)) {
////                                throw new CoolException("修改库位状态失败");
////                            }
////
//////                            // 更新站点信息 且 下发plc命令
//////                            staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
//////                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
//////                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//////                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//////                            if (!result) {
//////                                News.error("发布命令至输送线队列失败!!! [plc编号{}]", devp.getId());
//////                            }
////
////                            barcodeThread.setBarcode("");//清理条码
////
////                        } else if (code == 500){
////                            if (ledThread != null) {
////                                String errorMsg = jsonObject.getString("msg");
////                                if (!Cools.isEmpty(errorMsg)) {
////                                    MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
////                                }
////                            }
////                            News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
////                        } else {
//////                            staProtocol.setWorkNo((short) 32002);
//////                            staProtocol.setStaNo(pickSta.getBackSta().shortValue());
//////                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//////                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
////
////                            // led 异常显示
////                            if (ledThread != null) {
////                                String errorMsg = jsonObject.getString("msg");
//////                                String errorMsg = barcode + "托盘识别异常,请先进行组托!";
////                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
////                            }
////                        }
////
////                    } catch (Exception e) {
////                        e.printStackTrace();
////                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
////                    }
//
//                }
//            }
//
//        }
//    }
 
    /**
     * 初始化实时地图
     */
    public synchronized void initRealtimeBasMap() {
        try {
            for (int i = 1; i <= 10; i++) {//总共四层楼
                Object data = redisUtil.get(RedisKeyType.MAP.key + i);
                if (data == null) {//redis地图数据为空,从数据库中获取
                    BasMap basMap = basMapService.selectLatestMap(i);
                    if (basMap == null) {
                        //数据库中也不存在地图数据,从地图文件中获取
                        //载入地图
                        List<List<MapNode>> lists = navigateMapData.getJsonData(i, -1, null, null);//获取完整地图(包括入库出库)
 
                        //存入数据库
                        basMap = new BasMap();
                        basMap.setData(JSON.toJSONString(lists));
                        basMap.setCreateTime(new Date());
                        basMap.setUpdateTime(new Date());
                        basMap.setLev(i);
 
                        if (!basMapService.insert(basMap)) {
                            log.info("地图数据存储失败");
                        }
                    }
 
                    //将数据库地图数据存入redis
                    redisUtil.set(RedisKeyType.MAP.key + i, JSON.toJSONString(basMap));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 从redis中重启任务
     */
    public synchronized void restartTaskFromRedis() {
        HashMap<Object, Object> map = redisUtil.getRedis();
        for (Object key : map.keySet()) {
            if (key.toString().contains(RedisKeyType.LIFT.key)) {//提升机任务
                LiftRedisCommand redisCommand = JSON.parseObject(map.get(key).toString(), LiftRedisCommand.class);
                if (redisCommand == null) {
                    continue;
                }
 
                Short liftNo = redisCommand.getLiftNo();
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo.intValue());
                if (liftThread == null) {
                    continue;
                }
                LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                if (liftProtocol == null) {
                    continue;
                }
                if (!liftProtocol.isIdle()) {
                    continue;
                }
 
                //提升机处于空闲状态,进行任务的恢复
                liftProtocol.setTaskNo(redisCommand.getWrkNo());//将提升机线程分配任务号
                liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//工作状态
 
            }else if(key.toString().contains(RedisKeyType.SHUTTLE.key)){//四向穿梭车任务
                ShuttleRedisCommand redisCommand = JSON.parseObject(map.get(key).toString(), ShuttleRedisCommand.class);
                if (redisCommand == null) {
                    continue;
                }
 
                Short shuttleNo = redisCommand.getShuttleNo();
                NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo.intValue());
                if (shuttleThread == null) {
                    continue;
                }
                NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
                if (!shuttleProtocol.isIdle()) {
                    continue;
                }
 
                //四向穿梭车处于空闲状态,进行任务的恢复
                shuttleProtocol.setTaskNo(redisCommand.getWrkNo().intValue());//将四向穿梭车线程分配任务号
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WORKING);//工作状态
            }
        }
 
    }
 
    /**
     * 入库  ===>>  四向穿梭车入库作业下发
     */
    public synchronized void shuttleInExecute() {
        try {
            List<WrkMast> wrkMasts = wrkMastMapper.selectInStep4();
            for (WrkMast wrkMast : wrkMasts) {
                //获取源站
                BasDevp sourceBasDevp = basDevpService.selectById(wrkMast.getSourceStaNo());
                if (sourceBasDevp == null) {
                    continue;//站点不存在
                }
 
                //获取目标站
                LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(sourceBasDevp.getLiftNo(), Utils.getLev(wrkMast.getLocNo()));
                if (liftStaProtocol == null) {
                    continue;//站点不存在
                }
 
                // 判断是否满足入库条件,自动、空闲、有物
                if (!(liftStaProtocol.getModel() && !liftStaProtocol.getBusy() && liftStaProtocol.getHasTray())) {
                    News.info("{}任务,输送站点状态不满足入库。输送站点:{}", wrkMast.getWrkNo(), JSON.toJSONString(liftStaProtocol));
                    continue;
                }
 
                boolean step1 = this.shuttleInExecuteStep1(wrkMast, liftStaProtocol);//小车搬入库中
                if (!step1) {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
//    /**
//     * 入库  ===>>  四向穿梭车入库作业下发
//     */
//    public synchronized void shuttleInExecute() {
//        // 根据输送线plc遍历
//        for (DevpSlave devp : slaveProperties.getDevp()) {
//            // 遍历入库站
//            for (DevpSlave.StaRack staRack : devp.getRackInStn()) {
//                // 获取入库站信息
//                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
//                StaProtocol staProtocol = devpThread.getStation().get(staRack.getStaNo());
//                if (staProtocol == null) {
//                    continue;
//                } else {
//                    staProtocol = staProtocol.clone();
//                }
//                Short workNo = staProtocol.getWorkNo();
//                BasDevp basDevp = basDevpService.selectById(staProtocol.getSiteId());
//                if (basDevp == null) {
//                    continue;//站点信息不存在
//                }
//
//                // 判断是否满足入库条件,自动、有物、四向穿梭车可取信号
//                if (!(staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isShuttleTakeEnable())) {
//                    continue;
//                }
//
//                WrkMast wrkMast = wrkMastMapper.selectRackInStep4(workNo, staProtocol.getSiteId());
//                if (wrkMast == null) {
//                    continue;
//                }
//
//                boolean step1 = this.shuttleInExecuteStep1(wrkMast, basDevp);//小车搬入库中
//                if (!step1) {
//                    continue;
//                }
//
//            }
//        }
//
//    }
 
    /**
     * 入库-小车搬入库中
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    public boolean shuttleInExecuteStep1(WrkMast wrkMast, LiftStaProtocol liftStaProtocol) {
        if (wrkMast.getWrkSts() == 4) {
            if (wrkMast.getShuttleNo() == null) {//没有绑定小车,进行调度
                boolean result = shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), liftStaProtocol.getLocNo());//调度小车到货物所在输送站点进行取货
                News.info("{}任务,调度小车{}系统等待中", wrkMast.getWrkNo(), result ? "成功" : "失败");
                return false;
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
            if (!shuttleProtocol.isIdle()) {
                News.info("{}任务,{}小车忙碌中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
            //判断小车令牌是否未被占领
            if (shuttleProtocol.getToken() != 0) {
                News.info("{}任务,{}小车,令牌已被独占,禁止派发任务", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//小车已被独占,禁止再派发任务
            }
 
            //判断小车是否存在移动任务
            WrkMast hasMoveWorking = wrkMastMapper.selectShuttleHasMoveWorking(wrkMast.getShuttleNo());
            if (hasMoveWorking != null) {
                News.info("{}任务,{}小车,存在移动任务,禁止派发任务", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//存在移动任务,禁止执行入库任务
            }
 
            //判断小车是否到达输送站点库位
            if (!shuttleProtocol.getCurrentLocNo().equals(liftStaProtocol.getLocNo())) {
                //小车不在输送站点位置
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), liftStaProtocol.getLocNo(), wrkMast.getShuttleNo());//调度小车到货物所在输送站点进行取货
                News.info("{}任务,{}小车,未到达输送站点,系统等待中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //小车已抵达输送站点位置,进行搬运货物
            NyShuttleOperaResult result = NyShuttleOperaUtils.getShuttleTransportCommands(wrkMast.getShuttleNo(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo());
            if (result == null) {//路径计算失败
                News.info("{}任务,{}小车,路径计算失败,系统等待中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //创建分配命令
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());//入库模式
            assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
            assignCommand.setCommands(result.getCommands());//运行命令
            assignCommand.setNodes(result.getNodes());//路径节点
 
            wrkMast.setWrkSts(5L);//4.提升机搬运完成 => 5.小车搬运中
            wrkMast.setModiTime(new Date());
            shuttleProtocol.setToken(wrkMast.getWrkNo());//独占小车令牌
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
 
            return false;
        }
        return true;
    }
 
    /**
     * 出库  ===>>  四向穿梭车出库作业下发
     */
    public synchronized void shuttleOutExecute() {
        try {
            for (WrkMast wrkMast : wrkMastMapper.selectBy2125()) {
                boolean step1 = this.shuttleOutExecuteStep1(wrkMast);//小车搬出库中
                if (!step1) {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 出库-小车搬出库中
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    public boolean shuttleOutExecuteStep1(WrkMast wrkMast) {
        //21.生成出库任务 => 22.小车搬运中
        if (wrkMast.getWrkSts() == 21) {
            EntityWrapper<StaDesc> wrapper = new EntityWrapper<>();
            wrapper.eq("type_no", wrkMast.getIoType());//路径类型
            wrapper.eq("stn_no", wrkMast.getStaNo());//出库站点编号
            StaDesc staDesc = staDescService.selectOne(wrapper);
            if (staDesc == null) {
                News.info("{}任务,出库路径不存在", wrkMast.getWrkNo());
                return false;//出库路径不存在
            }
 
            //获取出库站点
            BasDevp basDevp = basDevpService.selectById(wrkMast.getStaNo());
            if (basDevp == null) {
                return false;//出库站点不存在
            }
 
//            if (!basDevp.getAutoing().equals("Y")) {
//                News.info("{}任务,{}站点,不是自动状态",wrkMast.getWrkNo(),basDevp.getDevNo());
//                return false;//不是自动状态
//            }
//
//            if (!basDevp.getOutEnable().equals("Y")) {
//                News.info("{}任务,{}站点,没有可出信号", wrkMast.getWrkNo(), basDevp.getDevNo());
//                return false;//出库站点不可出
//            }
 
            Integer liftNo = basDevp.getLiftNo();//搜索出库提升机是否存在入库任务,如存在禁止出库
            List<WrkMast> liftWrkMasts = wrkMastMapper.selectInWrkMastByLiftNo(liftNo);
            if (!liftWrkMasts.isEmpty()) {
                News.info("{}任务,{}号提升机,存在入库任务,系统禁止出库", wrkMast.getWrkNo(), liftNo);
                return false;//存在入库任务,禁止出库
            }
 
            //同库位组校验
            List<String> outerLoc = Utils.getGroupOuterLoc(wrkMast.getSourceLocNo());
            List<LocMast> outerLocMasts = locMastService.selectNotEmptyLocNos(outerLoc);
            if (!outerLocMasts.isEmpty()) {
                News.info("{}任务,浅库位存在货物,系统等待中", wrkMast.getWrkNo());
                return false;//浅库位存在未执行任务
            }
 
            //获取源站
            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(basDevp.getLiftNo(), Utils.getLev(wrkMast.getSourceLocNo()));
            if (liftStaProtocol == null) {
                return false;//找不到站点
            }
 
            if (!(liftStaProtocol.getModel() && !liftStaProtocol.getBusy() && !liftStaProtocol.getHasTray())) {
                News.info("{}任务,{}内部输送站,不满足自动、空闲、无托盘状态", wrkMast.getWrkNo(), liftStaProtocol.getStaNo());
                return false;//站点必须自动、空闲、没有托盘
            }
 
            if (wrkMast.getStaNo() == 300) {
                //出300站,检测300站任务数量
                List<WrkMast> wrkMasts = wrkMastMapper.select300Wrk();
                if (wrkMasts.size() >= 20) {
                    News.info("{}任务,输送线任务过载{}", wrkMast.getWrkNo(), wrkMasts.size());
                    return false;
                }
            }
 
            if (wrkMast.getShuttleNo() == null) {//没有绑定小车,进行调度
                //强制预留一台小车给入库任务
                int lev = Utils.getLev(wrkMast.getSourceLocNo());
                //获取当前楼层有几台可用小车
                int shuttleCount = shuttleDispatchUtils.getShuttleCountByLev(lev);
                if (shuttleCount >= 2) {//只有可用小车数量大于2,才进行入库任务预留小车
                    int shuttleWrkInObligateCount = 1;//预留小车数量
                    Config config = configService.selectOne(new EntityWrapper<Config>().eq("code", "shuttleWrkInObligateCount").eq("status", 1));
                    if (config != null) {
                        shuttleWrkInObligateCount = Integer.parseInt(config.getValue());
                    }
                    //可用出库小车数量(给入库任务预留一台车)
                    int useShuttleCount = shuttleCount - shuttleWrkInObligateCount;
                    //查询楼层已分配车辆的出库任务数量
                    List<WrkMast> wrkMasts = wrkMastService.selectShuttleOutWrkByLev(lev);
                    if (wrkMasts.size() >= useShuttleCount) {
                        News.info("{}任务,当前楼层可用小车{}台,出库任务已分配{}台,系统等待中。", wrkMast.getWrkNo(), useShuttleCount, wrkMasts.size());
                        return false;
                    }
                }
                boolean result = shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), wrkMast.getSourceLocNo());//调度小车到货物所在库位进行取货
                News.info("{}任务,调度小车{}系统等待中。", wrkMast.getWrkNo(), result ? "成功" : "失败");
                return false;
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
            if (!shuttleProtocol.isIdle()) {
                News.info("{}任务,{}小车,忙碌中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
            //判断小车令牌是否未被占领
            if (shuttleProtocol.getToken() != 0) {
                News.info("{}任务,{}小车,令牌已被独占,禁止派发任务", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//小车已被独占,禁止再派发任务
            }
 
            //判断小车是否到达货物库位
            if (!shuttleProtocol.getCurrentLocNo().equals(wrkMast.getSourceLocNo())) {
                //小车不在输送站点位置
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), wrkMast.getShuttleNo());//调度小车到货物所在库位进行取货
                News.info("{}任务,{}小车,未到达输送站点,系统等待中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //小车已抵达货物位置,进行搬运货物
            NyShuttleOperaResult result = NyShuttleOperaUtils.getShuttleTransportCommands(wrkMast.getShuttleNo(), wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), liftStaProtocol.getLocNo());//将货物搬运至提升机输送站点
            if (result == null) {//出库路径计算失败
                News.info("{}任务,{}小车,路径计算失败,系统等待中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //创建分配命令
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_OUT.id.shortValue());//出库模式
            assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
            assignCommand.setCommands(result.getCommands());//运行命令
            assignCommand.setNodes(result.getNodes());//路径节点
 
            wrkMast.setWrkSts(22L);//21.生成出库任务 => 22.小车搬运中
            wrkMast.setModiTime(new Date());
            shuttleProtocol.setToken(wrkMast.getWrkNo());//独占小车令牌
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
            return false;
        }
        return true;
    }
 
    /**
     * 四向穿梭车任务完成
     */
    public synchronized void shuttleFinished() {
        try {
            for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
                //获取四向穿梭车信息
                NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
                NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
 
                //四向穿梭车状态为等待确认、小车处于空闲状态
                if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.WAITING.id  //任务完成等待确认
                        && shuttleProtocol.getTaskNo() != 0
                        && shuttleProtocol.getFree() == ShuttleStatusType.IDLE.id
                ) {
                    //将任务档标记为完成
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(shuttleProtocol.getTaskNo());
                    if (wrkMast != null) {
                        switch (wrkMast.getWrkSts().intValue()) {
                            case 5://5.小车搬运中 ==> 9.入库完成
                                wrkMast.setWrkSts(9L);
                                shuttleProtocol.setTaskNo(0);
                                if (shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                                    //释放小车令牌
                                    shuttleProtocol.setToken(0);
                                }
                                break;
                            case 22://22.小车搬运中 ==> 23.小车搬运完成
                                wrkMast.setWrkSts(23L);
                                shuttleProtocol.setTaskNo(0);
                                if (shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                                    //释放小车令牌
                                    shuttleProtocol.setToken(0);
                                }
                                break;
                            case 102://102.小车移动至站点 ==> 103.小车移动至站点完成
                                wrkMast.setWrkSts(103L);
                                break;
                            case 104://104.小车迁入提升机中 ==> 105.小车迁入提升机完成
                                wrkMast.setWrkSts(105L);
                                shuttleProtocol.setTaskNo(0);//清理工作号
                                break;
                            case 108://108.小车迁出提升机中 ==> 109.小车迁出提升机完成
                                wrkMast.setWrkSts(109L);
                                break;
                            case 110://110.小车移动中 ==> 111.小车移动完成
                                wrkMast.setWrkSts(111L);
                                shuttleProtocol.setTaskNo(0);
                                if (shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                                    //释放小车令牌
                                    shuttleProtocol.setToken(0);
                                }
                                break;
                            default:
                        }
 
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            if (wrkMast.getWrkSts() == 111) {
                                // 保存工作主档历史档
                                if (wrkMastLogMapper.save(wrkMast.getWrkNo()) <= 0) {
                                    log.info("保存工作历史档[workNo={0}]失败", wrkMast.getWrkNo());
                                }
                                // 删除工作主档
                                if (!wrkMastService.deleteById(wrkMast)) {
                                    log.info("删除工作主档[workNo={0}]失败", wrkMast.getWrkNo());
                                }
                            }
 
                            //设置四向穿梭车为空闲状态
                            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                            //源库位清零
                            shuttleProtocol.setSourceLocNo(null);
                            //目标库位清零
                            shuttleProtocol.setLocNo(null);
                            //任务指令清零
                            shuttleProtocol.setAssignCommand(null);
                            News.info("四向穿梭车已确认且任务完成状态,复位。四向穿梭车号={}", shuttleProtocol.getShuttleNo());
                        } else {
                            News.error("四向穿梭车已确认且任务完成状态,复位失败,但未找到工作档。四向穿梭车号={},工作号={}", shuttleProtocol.getShuttleNo(), shuttleProtocol.getTaskNo());
                        }
                    } else {
                        ShuttleAssignCommand assignCommand = shuttleProtocol.getAssignCommand();
                        if (!assignCommand.getAuto()) {
                            //手动模式
                            //工作号清零
                            shuttleProtocol.setTaskNo(0);
                            //设置四向穿梭车为空闲状态
                            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                            //源库位清零
                            shuttleProtocol.setSourceLocNo(null);
                            //目标库位清零
                            shuttleProtocol.setLocNo(null);
                            //任务指令清零
                            shuttleProtocol.setAssignCommand(null);
                            //清零令牌
                            shuttleProtocol.setToken(0);
                            News.info("四向穿梭车已确认且任务完成状态,复位。四向穿梭车号={}", shuttleProtocol.getShuttleNo());
                        }
                    }
 
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 提升机任务
     */
    public synchronized void liftIoExecute() {
        try {
            for (LiftSlave liftSlave : slaveProperties.getLift()) {
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
                if (liftThread == null) {
                    continue;
                }
                LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                if (liftProtocol == null) {
                    continue;
                }
 
                if (!liftProtocol.isIdle()) {
                    News.info("{}号提升机,忙碌中", liftSlave.getId());
                    continue;
                }
 
                //判断提升机令牌是否被占用
                if (liftProtocol.getToken() != 0) {
                    News.info("{}号提升机,令牌已被独占", liftSlave.getId());
                    continue;
                }
 
                //搜索是否有待处理的任务
                List<WrkMast> wrkMasts = wrkMastMapper.selectLiftStep223103();
                if (wrkMasts.isEmpty()) {
                    continue;
                }
 
                for (WrkMast wrkMast : wrkMasts) {
                    //搜索是否有其他任务占用了提升机,如果占用提升机的任务和当前任务相同,则运行执行
                    WrkMast wrkMast1 = wrkMastMapper.selectLiftWrkMast(liftProtocol.getLiftNo().intValue());
                    if (wrkMast1 != null && wrkMast1.getWrkNo().intValue() != wrkMast.getWrkNo().intValue()) {
                        News.info("{}号提升机,被其他任务{}占用且和当前任务{}不相同,禁止派发", liftSlave.getId(), wrkMast1.getWrkNo(), wrkMast.getWrkNo());
                        continue;
                    }
 
                    boolean stepIn = this.liftIoExecuteStepIn(wrkMast);//提升机入库
                    if (!stepIn) {
                        continue;
                    }
 
                    boolean stepOut = this.liftIoExecuteStepOut(wrkMast);//提升机出库
                    if (!stepOut) {
                        continue;
                    }
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 提升机入库
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean liftIoExecuteStepIn(WrkMast wrkMast) {
        //--------------------------------------提升机入库-----------------------------------------//
        Date now = new Date();
 
        //2.设备上走 ==> 3.提升机搬运中
        if (wrkMast.getWrkSts() == 2) {
            //获取源站
            BasDevp sourceBasDevp = basDevpService.selectById(wrkMast.getSourceStaNo());
            if (sourceBasDevp == null) {
                return false;//找不到站点
            }
 
            if (!sourceBasDevp.getInEnable().equals("Y")) {
                News.info("{}任务,{}源站,没有可入信号", wrkMast.getWrkNo(), sourceBasDevp.getDevNo());
                return false;//站点不可入
            }
 
            Integer barcodeId = Utils.getBarcodeIdByStaNo(wrkMast.getSourceStaNo());
            if (barcodeId == null) {
                News.info("{}任务,{}源站,找不到可用条码器ID", wrkMast.getWrkNo(), sourceBasDevp.getDevNo());
                return false;//站点不可入
            }
 
            BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId);
            if (barcodeThread == null) {
                News.info("{}任务,{}源站,条码器{}线程为空", wrkMast.getWrkNo(), sourceBasDevp.getDevNo(), barcodeId);
                return false;//站点不可入
            }
            String barcode = barcodeThread.getBarcode();
            if(!Cools.isEmpty(barcode)) {
                if(!("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode))) {
                    //存在条码值,判断是否和当前工作档一致
                    if (!barcode.equals(wrkMast.getBarcode())) {
                        News.info("{}任务,{}站点,条码器{}值{}与工作档条码值{}不一致,系统跳过执行", wrkMast.getWrkNo(), sourceBasDevp.getDevNo(), barcodeId, barcode,wrkMast.getBarcode());
                        return false;//站点不可入
                    }
                }
            }else {
                return false;
            }
 
            //判断提升机整个三楼是否都处于入库模式
            Integer outInModel1 = Utils.getOutInModelByLift(sourceBasDevp.getLiftNo(), 1);
            Integer outInModel2 = Utils.getOutInModelByLift(sourceBasDevp.getLiftNo(), 5);
            Integer outInModel3 = Utils.getOutInModelByLift(sourceBasDevp.getLiftNo(), 8);
            if (outInModel1 == null || outInModel2 == null || outInModel3 == null) {
                News.info("{}任务,没有出入库模式", wrkMast.getWrkNo());
                return false;//不存在出入库模式
            }
 
            if (outInModel1 == 2 || outInModel2 == 2 && outInModel3 == 2) {
                News.info("{}任务,有站点处于出库模式,禁止入库", wrkMast.getWrkNo());
                return false;//只要有一个处于出库模式,禁止入库
            }
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, sourceBasDevp.getLiftNo());
            if (liftThread == null) {
                return false;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return false;
            }
            if (!liftProtocol.isIdle()) {
                News.info("{}任务,{}号提升机,忙碌中", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;
            }
            //判断提升机令牌是否未被占领
            if (liftProtocol.getToken() != 0) {
                News.info("{}任务,{}号提升机,令牌已被独占,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//提升机已被独占,禁止再派发任务
            }
 
            //判断提升机内是否有小车
            if (liftProtocol.getHasCar()) {
                News.info("{}任务,{}号提升机,提升机内部有小车,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//有小车,禁止派发
            }
 
            //判断提升机内是否有托盘
            if (liftProtocol.getHasTray()) {
                News.info("{}任务,{}号提升机,提升机内部有托盘,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//有托盘,禁止派发
            }
 
            //判断提升机是否有其他任务
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftThread.getSlave().getId());
            if (liftWrkMast != null) {
                News.info("{}任务,{}号提升机,当前提升机存在未完成任务,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//当前提升机存在未完成任务,等待下一次轮询
            }
 
            //获取源站对应的牛眼提升机站点编号(起点编号)
            Integer startSta = Integer.parseInt(sourceBasDevp.getQrCodeValue());
            //获取提升机站点
            LiftStaProtocol targetStaProtocol = NyLiftUtils.getLiftStaByLev(liftThread.getSlave().getId(), Utils.getLev(wrkMast.getLocNo()));
            if (targetStaProtocol == null) {
                return false;//站点不存在
            }
 
            if (targetStaProtocol.getHasTray()) {
                News.info("{}任务,{}号提升机,{}站点,提升机站点有托盘,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo(), targetStaProtocol.getStaNo());
                return false;//提升机站点有托盘,禁止派发
            }
 
            Integer targetSta = targetStaProtocol.getStaNo();
 
            //获取提升机命令
            NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_TRAY.id, startSta, targetSta, wrkMast.getWrkNo());
            if (wrkMast.getIoType() == 53 || wrkMast.getIoType() == 57) {
                //拣料再回库,重新分配设备工作号
                int deviceWrk = commonService.getWorkNo(8);//生成提升机设备工作号
                BasLiftOpt basLiftOpt = basLiftOptService.selectByDeviceWrk(String.valueOf(deviceWrk), liftThread.getSlave().getId());
                if (basLiftOpt != null) {
                    News.info("{}任务,{}号提升机,设备工作号出现重复情况,请联系技术人员支持。", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                    return false;
                }
                liftCommand.setTaskNo((short) deviceWrk);
            }
            ArrayList<NyLiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
 
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
            assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_TRAY.id.shortValue());
 
            wrkMast.setWrkSts(3L);//2.设备上走 ==> 3.提升机搬运中
            wrkMast.setLiftNo(liftThread.getSlave().getId());//任务档绑定提升机号
            wrkMast.setModiTime(now);
            liftProtocol.setToken(wrkMast.getWrkNo());//独占提升机令牌
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
            }
 
            return false;
        }
        return true;
    }
 
    /**
     * 提升机出库
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean liftIoExecuteStepOut(WrkMast wrkMast) {
        //--------------------------------------提升机出库-----------------------------------------//
        Date now = new Date();
 
        //23.小车搬运完成 ==> 24.提升机搬运中
        if (wrkMast.getWrkSts() == 23) {
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
 
            //获取出库站点(目标站)
            BasDevp basDevp = basDevpService.selectById(wrkMast.getStaNo());
            if (basDevp == null) {
                News.info("{}任务,出库站点不存在,禁止派发", wrkMast.getWrkNo());
                return false;//出库站点不存在
            }
 
            //获取源站对应的牛眼提升机站点编号(起点编号)
            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(basDevp.getLiftNo(), Utils.getLev(wrkMast.getSourceLocNo()));
            if (liftStaProtocol == null) {
                News.info("{}任务,找不到站点,禁止派发", wrkMast.getWrkNo());
                return false;//找不到站点
            }
            Integer startSta = liftStaProtocol.getStaNo();
 
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol.getCurrentLocNo().equals(liftStaProtocol.getLocNo())) {
                //小车还在输送站点
                //调度小车避让
                boolean result = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(wrkMast.getSourceLocNo()), wrkMast.getShuttleNo(), shuttleThread);
                if (!result) {
                    News.info("{}任务,{}小车,小车在输送站点调度小车避让失败", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                    return false;
                }
            }
 
            if (!basDevp.getAutoing().equals("Y")) {
                News.info("{}任务,{}站点,没有自动信号,禁止派发", wrkMast.getWrkNo(), basDevp.getDevNo());
                return false;//出库站点不可出
            }
 
            if (basDevp.getLoading().equals("Y")) {
                News.info("{}任务,{}站点,存在有物信号,禁止派发", wrkMast.getWrkNo(), basDevp.getDevNo());
                return false;//出库站点不可出
            }
 
            if (!basDevp.getOutEnable().equals("Y")) {
                News.info("{}任务,{}站点,没有可出信号,禁止派发", wrkMast.getWrkNo(), basDevp.getDevNo());
                return false;//出库站点不可出
            }
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, basDevp.getLiftNo());
            if (liftThread == null) {
                return false;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return false;
            }
 
            if (!liftProtocol.isIdle()) {
                News.info("{}任务,{}号提升机,忙碌中", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;
            }
            //判断提升机令牌是否未被占领
            if (liftProtocol.getToken() != 0) {
                News.info("{}任务,{}号提升机,令牌已被独占,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//提升机已被独占,禁止再派发任务
            }
            //判断提升机是否有其他任务
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftThread.getSlave().getId());
            if (liftWrkMast != null) {
                News.info("{}任务,{}号提升机,当前提升机存在未完成任务,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//当前提升机存在未完成任务,等待下一次轮询
            }
 
            //获目标站源站对应的输送站点
            BasDevp targetBasDevp = basDevpService.selectById(wrkMast.getStaNo());
            if (targetBasDevp == null) {
                News.info("{}任务,{}站点,站点不存在,禁止派发", wrkMast.getWrkNo(), wrkMast.getStaNo());
                return false;//站点不存在
            }
            //获取牛眼提升机站点编号(目标编号)
            Integer targetSta = Integer.parseInt(targetBasDevp.getQrCodeValue());
 
            //获取提升机命令
            NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftThread.getSlave().getId(), NyLiftTaskModelType.MOVE_TRAY.id, startSta, targetSta, wrkMast.getWrkNo());
 
            ArrayList<NyLiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
 
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
            assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_TRAY.id.shortValue());
 
            wrkMast.setWrkSts(24L);//23.小车搬运完成 ==> 24.提升机搬运中
            wrkMast.setLiftNo(liftThread.getSlave().getId());//任务档绑定提升机号
            wrkMast.setModiTime(now);
            liftProtocol.setToken(wrkMast.getWrkNo());//独占提升机令牌
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
            }
 
            return false;
        }
        return true;
    }
 
    /**
     * 提升机任务完成
     */
    public synchronized void liftFinished() {
        try {
            for (LiftSlave liftSlave : slaveProperties.getLift()) {
                //获取提升机信息
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
                LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                if (liftProtocol == null) {
                    continue;
                }
 
                //提升机为等待确认且空闲
                if (liftProtocol.getProtocolStatus() == LiftProtocolStatusType.WAITING.id
                        && liftProtocol.getTaskNo() != 0
                        && !liftProtocol.getBusy()
                ) {
                    int taskNo = liftProtocol.getTaskNo().intValue();
                    if (taskNo >= 20000 && taskNo <= 30000) {
                        //提升机设备工作号,需要查询对应任务号
                        BasLiftOpt basLiftOpt = basLiftOptService.selectByDeviceWrk(liftProtocol.getTaskNo().toString(), liftSlave.getId());
                        if (basLiftOpt != null) {
                            taskNo = basLiftOpt.getWrkNo();
                        }
                    }
 
                    //将任务档标记为完成
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo324104(taskNo);
                    if (wrkMast != null) {
                        switch (wrkMast.getWrkSts().intValue()) {
                            case 3://3.提升机搬运中 ==> 4.提升机搬运完成
                                wrkMast.setWrkSts(4L);
                                wrkMast.setLiftNo(null);//释放提升机
                                if (liftProtocol.getToken().equals(wrkMast.getWrkNo())) {
                                    //释放提升机令牌
                                    liftProtocol.setToken(0);
                                }
                                break;
                            case 24://24.提升机搬运中 ==> 25.提升机搬运完成
                                wrkMast.setWrkSts(25L);
                                if (wrkMast.getIoType() == 11) {//库位移转
                                    wrkMast.setWrkSts(4L);//4.提升机搬运完成
                                } else {
                                    if (wrkMast.getMk() == null || !wrkMast.getMk().equals("Y")) {
                                        //不需要用到机械臂,直接转29.出库完成
                                        wrkMast.setWrkSts(29L);
                                        wrkMast.setShuttleNo(null);//释放小车
                                        wrkMast.setLiftNo(null);//释放提升机
                                        wrkMast.setModiTime(new Date());
                                    }
                                }
 
                                if (liftProtocol.getToken().equals(wrkMast.getWrkNo())) {
                                    //释放提升机令牌
                                    liftProtocol.setToken(0);
                                }
 
                                break;
                            case 106://106.提升机搬运中 ==> 107.提升机搬运完成
                                wrkMast.setWrkSts(107L);
                                break;
                            default:
                        }
 
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            //设置提升机为空闲状态
                            liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                            //任务指令清零
                            liftProtocol.setAssignCommand(null);
                            News.info("提升机已确认且任务完成状态。提升机号={}", liftProtocol.getLiftNo());
                        } else {
                            News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                        }
                    } else {
                        LiftAssignCommand assignCommand = liftProtocol.getAssignCommand();
                        if (assignCommand != null) {
                            if (!assignCommand.getAuto()) {
                                //手动操作
                                //设置提升机为空闲状态
                                liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                                //任务指令清零
                                liftProtocol.setAssignCommand(null);
                                News.info("提升机已确认手动操作。提升机号={}", liftProtocol.getLiftNo());
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 库位移转
     */
    public synchronized void locToLocExecute() {
        try {
            //获取出入库工作档
//        List<WrkMast> wrkMasts = wrkMastMapper.selectInOutWrkMast();
//        if (wrkMasts.size() > 0) {
//            //有出入库任务,必须等待任务执行完毕再执行库位移转
//            return;
//        }
 
            //查询库位移转工作档
            List<WrkMast> wrkMasts1 = wrkMastMapper.selectLocToLocWrkMast();
            for (WrkMast wrkMast : wrkMasts1) {
                if (Utils.getLev(wrkMast.getSourceLocNo()) == Utils.getLev(wrkMast.getLocNo())) {
                    boolean step1 = this.locToLocExecuteStep1(wrkMast);//同楼层库位移转
                    if (!step1) {
                        continue;
                    }
                } else {
                    //跨楼层库位移转
                    boolean step2 = this.locToLocExecuteStep2(wrkMast);//调度车辆取货并运送到出库口
                    if (!step2) {
                        continue;
                    }
 
                    boolean step3 = this.locToLocExecuteStep3(wrkMast);//提升机搬运货物
                    if (!step3) {
                        continue;
                    }
 
                    boolean step4 = this.locToLocExecuteStep4(wrkMast);//调度车辆取货并运送到目标库位
                    if (!step4) {
                        continue;
                    }
 
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 同楼层库位移转
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep1(WrkMast wrkMast) {
        if (wrkMast.getWrkSts() == 21) {//21.生成出库任务
            if (wrkMast.getShuttleNo() == null) {
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), wrkMast.getSourceLocNo());//调度小车到源库位进行取货
                return false;
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
            if (!shuttleProtocol.isIdle()) {
                return false;
            }
 
            //判断小车是否到达源库位
            if (!shuttleProtocol.getCurrentLocNo().equals(wrkMast.getSourceLocNo())) {
                //小车不在源库位位置
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), wrkMast.getShuttleNo());//调度小车到源库位进行取货
                return false;
            }
 
            //小车已抵达源库位,进行搬运货物
            NyShuttleOperaResult result = NyShuttleOperaUtils.getShuttleTransportCommands(wrkMast.getShuttleNo(), wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), wrkMast.getLocNo());
            if (result == null) {//路径计算失败
                return false;
            }
 
            //创建分配命令
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());//入库模式
            assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
            assignCommand.setCommands(result.getCommands());//运行命令
            assignCommand.setNodes(result.getNodes());//路径节点
 
            wrkMast.setWrkSts(5L);//21.生成出库任务 => 5.小车搬运中
            wrkMast.setModiTime(new Date());
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
 
            return false;
        }
        return true;
    }
 
    /**
     * 跨楼层库位移转-调度车辆取货并运送到出库口
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep2(WrkMast wrkMast) {
        if (wrkMast.getWrkSts() == 21) {//21.生成出库任务
            if (wrkMast.getShuttleNo() == null) {
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), wrkMast.getSourceLocNo());//调度小车到源库位进行取货
                return false;
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
            if (!shuttleProtocol.isIdle()) {
                return false;
            }
 
            //判断小车是否到达源库位
            if (!shuttleProtocol.getCurrentLocNo().equals(wrkMast.getSourceLocNo())) {
                //小车不在源库位位置
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), wrkMast.getShuttleNo());//调度小车到源库位进行取货
                return false;
            }
 
            //获取穿梭车最近且空闲的提升机输送站点
            LiftStaProtocol liftSta = shuttleDispatchUtils.getRecentLiftSta(shuttleThread.getSlave().getId(), Utils.getLev(wrkMast.getLocNo()));
            if (liftSta == null) {
                return false;//没有可用且空闲的输送站点
            }
            //源站
            Integer sourceStaNo = liftSta.getStaNo();
            //提升机号*100+目标楼层=目标站点
            Integer staNo = liftSta.getLiftNo() * 100 + Utils.getLev(wrkMast.getLocNo());//目标站
 
            //小车已抵达源库位,将货物搬运到输送站点
            NyShuttleOperaResult result = NyShuttleOperaUtils.getShuttleTransportCommands(wrkMast.getShuttleNo(), wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), liftSta.getLocNo());
            if (result == null) {//路径计算失败
                return false;
            }
 
            //创建分配命令
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setTaskMode(ShuttleTaskModeType.SHUTTLE_LOC_TO_LOC.id.shortValue());//库位移转模式
            assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
            assignCommand.setCommands(result.getCommands());//运行命令
            assignCommand.setNodes(result.getNodes());//路径节点
 
            wrkMast.setWrkSts(22L);//21.生成出库任务 => 22.小车搬运中
            wrkMast.setSourceStaNo(sourceStaNo);//源站
            wrkMast.setStaNo(staNo);//目标站
            wrkMast.setModiTime(new Date());
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
 
            return false;
        }
        return true;
    }
 
    /**
     * 跨楼层库位移转-提升机搬运货物
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep3(WrkMast wrkMast) {
        Date now = new Date();
        if (wrkMast.getWrkSts() == 23) {//23.小车搬运完成
            //源站
            Integer sourceStaNo = wrkMast.getSourceStaNo();
            //目标站
            Integer staNo = wrkMast.getStaNo();
            if (sourceStaNo == null || staNo == null) {
                return false;//源站或目标站为空
            }
 
            int liftNo = staNo / 100;//获取提升机号
 
            //获取提升机线程
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo);
            if (liftThread == null) {
                return false;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return false;
            }
 
            //获取源站对应的输送站点
            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(liftNo, Utils.getLev(wrkMast.getSourceLocNo()));
            if (liftStaProtocol == null) {
                return false;//找不到站点
            }
 
            if (!(liftStaProtocol.getModel() && !liftStaProtocol.getBusy() && liftStaProtocol.getHasTray())) {
                return false;//站点必须自动、空闲、有托盘
            }
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol.getCurrentLocNo().equals(liftStaProtocol.getLocNo())) {
                //小车还在输送站点
                //调度小车避让
                boolean result = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(wrkMast.getSourceLocNo()), wrkMast.getShuttleNo(), shuttleThread);
                if (!result) {
                    return false;
                }
            }
 
            if (!liftProtocol.isIdle()) {
                return false;
            }
 
            //搜索是否有其他任务占用了提升机,如果占用提升机的任务和当前任务相同,则运行执行
            WrkMast wrkMast1 = wrkMastMapper.selectLiftWrkMast(liftProtocol.getLiftNo().intValue());
            if (wrkMast1 != null && wrkMast1.getWrkNo().intValue() != wrkMast.getWrkNo().intValue()) {
                return false;
            }
 
            //获取提升机命令
            NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_TRAY.id, sourceStaNo, staNo, wrkMast.getWrkNo());
 
            ArrayList<NyLiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
 
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
            assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_TRAY.id.shortValue());
 
            wrkMast.setWrkSts(24L);//23.小车搬运完成 ==> 24.提升机搬运中
            wrkMast.setLiftNo(liftThread.getSlave().getId());//任务档绑定提升机号
            wrkMast.setShuttleNo(null);//清空小车号,等货物搬运完成后,到目标楼层重新搜索小车
            wrkMast.setModiTime(now);
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
            }
            return false;
        }
        return true;
    }
 
    /**
     * 跨楼层库位移转-调度车辆取货并运送到目标库位
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep4(WrkMast wrkMast) {
        if (wrkMast.getWrkSts() == 4) {//4.提升机搬运完成
            //获取目标站对应的输送站点
            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(wrkMast.getLiftNo(), Utils.getLev(wrkMast.getLocNo()));
            if (liftStaProtocol == null) {
                return false;//找不到站点
            }
 
            if (!(liftStaProtocol.getModel() && !liftStaProtocol.getBusy() && liftStaProtocol.getHasTray())) {
                return false;//站点必须自动、空闲、有托盘
            }
 
            if (wrkMast.getShuttleNo() == null) {
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), liftStaProtocol.getLocNo());//调度小车到目标输送站点进行取货
                return false;
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
            if (!shuttleProtocol.isIdle()) {
                return false;
            }
 
            //判断小车是否到达目标输送站点
            if (!shuttleProtocol.getCurrentLocNo().equals(liftStaProtocol.getLocNo())) {
                //小车不在目标输送站点
                shuttleDispatchUtils.dispatchShuttle(wrkMast.getWrkNo(), liftStaProtocol.getLocNo(), wrkMast.getShuttleNo());//调度小车到目标输送站点进行取货
                return false;
            }
 
            //小车已抵达目标输送站点,将货物搬运到目标库位
            NyShuttleOperaResult result = NyShuttleOperaUtils.getShuttleTransportCommands(wrkMast.getShuttleNo(), wrkMast.getWrkNo(), liftStaProtocol.getLocNo(), wrkMast.getLocNo());
            if (result == null) {//路径计算失败
                return false;
            }
 
            //创建分配命令
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setTaskMode(ShuttleTaskModeType.SHUTTLE_LOC_TO_LOC.id.shortValue());//库位移转模式
            assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
            assignCommand.setCommands(result.getCommands());//运行命令
            assignCommand.setNodes(result.getNodes());//路径节点
 
            wrkMast.setWrkSts(5L);//4.提升机搬运完成 => 5.小车搬运中
            wrkMast.setLiftNo(null);//释放提升机
            wrkMast.setModiTime(new Date());
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
 
            return false;
        }
        return true;
    }
 
    /**
     * 异常信息记录
     */
    public void recErr() {
        try {
            this.recShuttleErr();
            this.recLiftErr();
        } catch (Exception e) {
            News.error("recErr fail", e);
        }
    }
 
    /**
     * 四向穿梭车异常信息记录
     */
    private void recShuttleErr() {
        Date now = new Date();
        for (ShuttleSlave shuttleSlave : slaveProperties.getShuttle()) {
            // 获取四向穿梭车信息
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleSlave.getId());
            if (shuttleThread == null) {
                continue;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
 
            if (shuttleProtocol.getTaskNo() != 0) {
                //有任务
                BasShuttleErrLog latest = basShuttleErrLogService.findLatestByTaskNo(shuttleSlave.getId(), shuttleProtocol.getTaskNo());
                // 有异常
                if (latest == null) {
                    if (shuttleProtocol.getErrState() != null && shuttleProtocol.getErrState() == 1) {
                        WrkMast wrkMast = wrkMastMapper.selectById(shuttleProtocol.getTaskNo());
                        if (wrkMast == null) {
                            continue;
                        }
                        BasShuttleErr basShuttleErr = basShuttleErrService.queryByCode(shuttleProtocol.getErrCode());
                        String errName = basShuttleErr==null? "未知异常":basShuttleErr.getErrName();
                        BasShuttleErrLog basShuttleErrLog = new BasShuttleErrLog(
                                null,    // 编号
                                wrkMast.getWrkNo(),    // 工作号
                                now,    // 发生时间
                                null,    // 结束时间
                                wrkMast.getWrkSts(),    // 工作状态
                                wrkMast.getIoType(),    // 入出库类型
                                shuttleSlave.getId(),    // 四向穿梭车
                                null,    // plc
                                wrkMast.getLocNo(),    // 目标库位
                                wrkMast.getStaNo(),    // 目标站
                                wrkMast.getSourceStaNo(),    // 源站
                                wrkMast.getSourceLocNo(),    // 源库位
                                wrkMast.getBarcode(),    // 条码
                                shuttleProtocol.getErrCode(),    // 异常码
                                errName,    // 异常
                                1,    // 异常情况
                                now,    // 添加时间
                                null,    // 添加人员
                                now,    // 修改时间
                                null,    // 修改人员
                                "任务中异常",    // 备注
                                JSON.toJSONString(shuttleProtocol)    // 系统状态数据
                        );
                        if (!basShuttleErrLogService.insert(basShuttleErrLog)) {
                            News.error("四向穿梭车plc异常记录失败 ===>> [id:{}] [error:{}]", shuttleSlave.getId(), errName);
                        }
                    }
                } else {
                    // 异常修复
                    if (shuttleProtocol.getErrState() == null || shuttleProtocol.getErrState() == 0) {
                        latest.setEndTime(now);
                        latest.setUpdateTime(now);
                        latest.setStatus(2);
                        if (!basShuttleErrLogService.updateById(latest)) {
                            News.error("四向穿梭车plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", shuttleSlave.getId(), latest.getId());
                        }
                    }
                }
            }else {
//                //无任务
//                BasShuttleErrLog latest = basShuttleErrLogService.findLatest(shuttleSlave.getId());
//                // 有异常
//                if (shuttleProtocol.getStatusErrorCode() != null && shuttleProtocol.getStatusErrorCode() > 0) {
//                    // 记录新异常
//                    if (latest == null || (latest.getErrCode() != shuttleProtocol.getStatusErrorCode().intValue())) {
//                        BasShuttleErr basShuttleErr = basShuttleErrService.queryByCode(shuttleProtocol.getStatusErrorCode().intValue());
//                        String errName = basShuttleErr==null? "未知异常":basShuttleErr.getErrName();
//                        BasShuttleErrLog basShuttleErrLog = new BasShuttleErrLog(
//                                null,    // 编号
//                                null,    // 工作号
//                                now,    // 发生时间
//                                null,    // 结束时间
//                                null,    // 工作状态
//                                null,    // 入出库类型
//                                shuttleSlave.getId(),    // 四向穿梭车
//                                null,    // plc
//                                null,    // 目标库位
//                                null,    // 目标站
//                                null,    // 源站
//                                null,    // 源库位
//                                null,    // 条码
//                                (int)shuttleProtocol.getStatusErrorCode(),    // 异常码
//                                errName,    // 异常
//                                1,    // 异常情况
//                                now,    // 添加时间
//                                null,    // 添加人员
//                                now,    // 修改时间
//                                null,    // 修改人员
//                                "无任务异常"    // 备注
//                        );
//                        if (!basShuttleErrLogService.insert(basShuttleErrLog)) {
//                            News.error("四向穿梭车plc异常记录失败 ===>> [id:{}] [error:{}]", shuttleSlave.getId(), errName);
//                        }
//                    }
//                    // 无异常
//                } else {
//                    // 异常修复
//                    if (latest != null && latest.getStatus() == 1) {
//                        latest.setEndTime(now);
//                        latest.setUpdateTime(now);
//                        latest.setStatus(2);
//                        if (!basShuttleErrLogService.updateById(latest)) {
//                            News.error("四向穿梭车plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", shuttleSlave.getId(), latest.getId());
//                        }
//                    }
//                }
            }
        }
    }
 
    /**
     * 提升机异常信息记录
     */
    private void recLiftErr() {
        Date now = new Date();
        for (LiftSlave liftSlave : slaveProperties.getLift()) {
            // 获取提升机信息
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
 
            if (liftProtocol.getTaskNo() != 0) {
                //有任务
                BasLiftErrLog latest = basLiftErrLogService.findLatestByTaskNo(liftSlave.getId(), liftProtocol.getTaskNo().intValue());
                // 有异常
                if (latest == null) {
                    if (liftProtocol.getDeviceError() != null && liftProtocol.getDeviceError()) {
                        WrkMast wrkMast = wrkMastMapper.selectById(liftProtocol.getTaskNo());
                        if (wrkMast == null) {
                            continue;
                        }
 
                        String errName = "";
                        if (liftProtocol.getFrontOverrun()) {
                            errName = "前超限";
                        } else if (liftProtocol.getBackOverrun()) {
                            errName = "后超限";
                        } else if (liftProtocol.getLeftOverrun()) {
                            errName = "左超限";
                        } else if (liftProtocol.getRightOverrun()) {
                            errName = "右超限";
                        } else if (liftProtocol.getOverHeight()) {
                            errName = "超高";
                        } else if (liftProtocol.getOverWeight()) {
                            errName = "超重";
                        }
 
                        BasLiftErrLog basLiftErrLog = new BasLiftErrLog(
                                null,    // 编号
                                wrkMast.getWrkNo(),    // 工作号
                                now,    // 发生时间
                                null,    // 结束时间
                                wrkMast.getWrkSts(),    // 工作状态
                                wrkMast.getIoType(),    // 入出库类型
                                liftSlave.getId(),    // 提升机
                                null,    // plc
                                wrkMast.getLocNo(),    // 目标库位
                                wrkMast.getStaNo(),    // 目标站
                                wrkMast.getSourceStaNo(),    // 源站
                                wrkMast.getSourceLocNo(),    // 源库位
                                wrkMast.getBarcode(),    // 条码
                                null,    // 异常码
                                errName,    // 异常
                                1,    // 异常情况
                                now,    // 添加时间
                                null,    // 添加人员
                                now,    // 修改时间
                                null,    // 修改人员
                                "任务中异常",    // 备注
                                JSON.toJSONString(liftProtocol)    // 系统状态数据
                        );
                        if (!basLiftErrLogService.insert(basLiftErrLog)) {
                            News.error("提升机plc异常记录失败 ===>> [id:{}] [error:{}]", liftSlave.getId(), errName);
                        }
                    }
                } else {
                    // 异常修复
                    if (liftProtocol.getDeviceError() == null || !liftProtocol.getDeviceError()) {
                        latest.setEndTime(now);
                        latest.setUpdateTime(now);
                        latest.setStatus(2);
                        if (!basLiftErrLogService.updateById(latest)) {
                            News.error("提升机plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", liftSlave.getId(), latest.getId());
                        }
                    }
                }
            }
        }
    }
 
    // -------------------------------------------------------------------------------
 
    /**
     * 空栈板初始化入库,叉车入库站放货
     */
    public void storeEmptyPlt(){
        try {
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历空板入库口
                for (DevpSlave.Sta emptyInSta : devp.getEmptyInSta()) {
                    // 获取空板入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(emptyInSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    // 站点条件判断
                    if (staProtocol.isAutoing()
                            && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && staProtocol.isEmptyMk()
                            && (staProtocol.getWorkNo() == 0 || (staProtocol.getWorkNo() >= 9990 || staProtocol.getWorkNo() <= 9999))
                            && staProtocol.isPakMk()) {
 
                        // 获取条码扫描仪信息
                        String barcode = null;
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, emptyInSta.getBarcode());
                        if (barcodeThread != null) {
                            String barcode0 = barcodeThread.getBarcode();
                            if (!Cools.isEmpty(barcode0)) {
//                            News.info("{}号条码扫描器检测条码信息:{}", emptyInSta.getBarcode(), barcode0);
                                if (!"NG".endsWith(barcode0) && !"NoRead".equals(barcode0) && !"empty".equals(barcode0)) {
                                    barcode = barcode0;
                                }
                            }
                        }
 
                        if (!Cools.isEmpty(barcode)) {
                            WrkMast wrkMast = wrkMastMapper.selectByBarcode(barcode);//条码存在工作档
                            if (wrkMast != null) {
                                continue;
                            }
                        }
 
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, emptyInSta.getLed());
 
                        try {
                            LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
 
                            SearchLocParam param = new SearchLocParam();
                            param.setIoType(10);
                            if (!Cools.isEmpty(barcode)) {
                                param.setBarcode(barcode);
                            }
                            param.setSourceStaNo(emptyInSta.getStaNo());
//                        param.setLocType1(locTypeDto.getLocType1());
                            String response = new HttpHandler.Builder()
                                    .setUri(wmsUrl)
                                    .setPath("/rpc/pakin/loc/v2")
                                    .setJson(JSON.toJSONString(param))
                                    .build()
                                    .doPost();
                            JSONObject jsonObject = JSON.parseObject(response);
                            Integer code = jsonObject.getInteger("code");
                            if (code.equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                                barcodeThread.setBarcode("");
//                            // 更新站点信息 且 下发plc命令
//                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
//                            staProtocol.setStaNo(dto.getStaNo().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                throw new CoolException("更新plc站点信息失败");
//                            }
                            } else {
                                if (ledThread != null) {
                                    String errorMsg = jsonObject.getString("msg");
                                    if (!Cools.isEmpty(errorMsg)) {
                                        MessageQueue.offer(SlaveType.Led, emptyInSta.getLed(), new Task(3, errorMsg));
                                    }
                                }
                                News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v2", JSON.toJSONString(param), response);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
 
                    }
 
 
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * AGV补货 => 生成入库通知档
     */
    public synchronized void robotGenerateAgvTask() {
        try {
            //检测300站是否自动、有物、工作号
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(300);
                if (staProtocol == null) {
                    continue;
                }
                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() != 0) {
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
 
                    if (wrkMast.getWrkSts() != 25) {//25.提升机搬运完成
                        continue;
                    }
 
                    if (wrkMast.getMk() != null && wrkMast.getMk().equals("Y")) {//标记为Y表示需要用到机械臂拣料
                        Short targetSta = null;//目标站
                        //判断机械臂拣料站是否空闲
                        StaProtocol staProtocol303 = devpThread.getStation().get(303);
                        StaProtocol staProtocol317 = devpThread.getStation().get(317);
 
                        List<WrkMast> wrkMasts303 = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 25).eq("sta_no", 303));
                        List<WrkMast> wrkMasts317 = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 25).eq("sta_no", 317));
                        if (staProtocol303.isAutoing() && !staProtocol303.isLoading() && wrkMasts303.isEmpty()) {
                            //自动、无物
                            targetSta = (short) 303;
                        } else if (staProtocol317.isAutoing() && !staProtocol317.isLoading() && wrkMasts317.isEmpty()) {
                            //自动、无物
                            targetSta = (short) 317;
                        } else {
                            continue;//没有空闲站点
                        }
 
                        if (wrkMast.getStaNo() != 303 && wrkMast.getStaNo() != 317) {
                            //向AGV发起组托请求
                            try {
                                HashMap<String, Object> param = new HashMap<>();
                                param.put("wrkNo", wrkMast.getWrkNo());
                                String response = new HttpHandler.Builder()
                                        .setUri(wmsUrl)
                                        .setPath("/rpc/replenishment")
                                        .setJson(JSON.toJSONString(param))
                                        .build()
                                        .doPost();
                                JSONObject jsonObject = JSON.parseObject(response);
                                Integer code = jsonObject.getInteger("code");
                                if (code.equals(200)) {//AGV组托成功
                                    //覆盖工作档目标站
                                    wrkMast.setStaNo(targetSta.intValue());
                                    wrkMast.setShuttleNo(null);//释放小车
                                    wrkMast.setLiftNo(null);//释放提升机
                                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                                        //向300站写入目标站
                                        staProtocol = staProtocol.clone();
                                        staProtocol.setStaNo(targetSta);//移动到目标站
                                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                                        try {
                                            Thread.sleep(500);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
//                                e.printStackTrace();
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            }
 
//                        //覆盖工作档目标站
//                        wrkMast.setStaNo(targetSta.intValue());
//                        wrkMast.setShuttleNo(null);//释放小车
//                        wrkMast.setLiftNo(null);//释放提升机
//                        if (wrkMastMapper.updateById(wrkMast) > 0) {
//                            //向300站写入目标站
//                            staProtocol = staProtocol.clone();
//                            staProtocol.setStaNo(targetSta);//移动到目标站
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            try {
//                                Thread.sleep(500);
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                            continue;
//                        }
                        }
                    }
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }
 
    /**
     * AGV补货 => 机械臂拣料
     */
    public synchronized void agvRestockByRobot() {
        try {
            //检测300站是否自动、有物、工作号
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol303 = devpThread.getStation().get(303);
                StaProtocol staProtocol317 = devpThread.getStation().get(317);
                if (staProtocol303 == null || staProtocol317 == null) {
                    continue;
                }
 
                if (staProtocol303.isAutoing() && staProtocol303.isLoading() && staProtocol303.getWorkNo() != 0) {
                    //调度机械臂
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol303.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    if (wrkDetls.isEmpty()) {
                        continue;
                    }
 
                    if (wrkMast.getInvWh() == null) {
                        boolean result = RobotUtils.sendTask(staProtocol303.getWorkNo().toString(), wrkDetls.size(), "303");
                        if (result) {
                            wrkMast.setInvWh("Y");//标记已经下发机械臂任务
                            wrkMastMapper.updateById(wrkMast);
                        }
                    }
                }
 
                if (staProtocol317.isAutoing() && staProtocol317.isLoading() && staProtocol317.getWorkNo() != 0) {
                    //调度机械臂
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol317.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    if (wrkDetls.isEmpty()) {
                        continue;
                    }
                    if (wrkMast.getInvWh() == null) {
                        boolean result = RobotUtils.sendTask(staProtocol317.getWorkNo().toString(), wrkDetls.size(), "317");
                        if (result) {
                            wrkMast.setInvWh("Y");//标记已经下发机械臂任务
                            wrkMastMapper.updateById(wrkMast);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * AGV补货(通知AGV取货)
     */
    public synchronized void agvRestockInto() {
        try {
            //检测309和312站是否自动、有物
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol309 = devpThread.getStation().get(309);
                StaProtocol staProtocol312 = devpThread.getStation().get(312);
                if (staProtocol309 == null || staProtocol312 == null) {
                    continue;
                }
 
                if (staProtocol309.isAutoing() && staProtocol309.isLoading() && staProtocol309.isInEnable()) {
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 9);//308站条码器
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if (!Cools.isEmpty(barcode)) {
                        //通知AGV取货
                        boolean result = agvRestockCall("301-1", barcode);
                        log.info("机械臂通知AGV取货,条码号:" + barcode);
                        if (result) {
                            barcodeThread.setBarcode("");
                        }
                    }
                }
 
                if (staProtocol312.isAutoing() && staProtocol312.isLoading() && staProtocol312.isInEnable()) {
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 10);//311站条码器
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if (!Cools.isEmpty(barcode)) {
                        //通知AGV取货
                        boolean result = agvRestockCall("302-1", barcode);
                        log.info("机械臂通知AGV取货,条码号:" + barcode);
                        if (result) {
                            barcodeThread.setBarcode("");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //通知AGV取货
    private boolean agvRestockCall(String staNo, String barcode) {
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("devNo", staNo);
            param.put("containerCode", barcode);
            String response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath("/rpc/start")
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            News.info("悬挂线,WMS返回结果:" + jsonObject);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {//呼叫AGV
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
 
    //通知WMS当前拣料站点信息
    private boolean agvCureentCall(String staNo, String barcode) {
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("devNo", staNo);
            param.put("containerCode", barcode);
            String response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath("/rpc/current/containerCode")
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            News.info("悬挂线,WMS返回结果:" + jsonObject);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {//呼叫AGV
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
 
    /**
     * AGV补货(悬挂线通知AGV取货)
     */
    public synchronized void agvRestockIntoByHangingWire() {
        try {
            //检测350和351扫码器
            int[] barcodeStaNo = {11, 12,14,16,18,20};//11 => 350站扫码器,12 => 351站扫码器
            for (int staNo : barcodeStaNo) {
                // 获取条码扫描仪信息
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, staNo);
                if (barcodeThread == null) {
                    continue;
                }
                String barcode = barcodeThread.getBarcode();
                if (!Cools.isEmpty(barcode)) {
                    if (barcode.contains("NoRead")) {
                        continue;
                    }
 
                    String agvStaNo = null;
                    if (staNo == 11) {
                        agvStaNo = "303-1";
                    } else if(staNo == 12){
                        agvStaNo = "304-1";
                    } else if(staNo == 14){
                        agvStaNo = "311-1";
                    } else if(staNo == 16){
                        agvStaNo = "313-1";
                    } else if(staNo == 18){
                        agvStaNo = "315-1";
                    } else if(staNo == 20){
                        agvStaNo = "317-1";
                    }
                    //通知AGV取货
                    boolean result = agvRestockCall(agvStaNo, barcode);
                    if (result) {
                        barcodeThread.setBarcode("");
                    }
                    log.info(barcodeThread.getSlave().getId() + "号扫码器,通知AGV取货,条码号:" + barcode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * AGV拣料站点信息
     */
    public synchronized void agvCurrentContainerCodeInfoWire() {
        try {
            //检测350和351扫码器
            int[] barcodeStaNo = {13, 15,17,19};//11 => 350站扫码器,12 => 351站扫码器
            for (int staNo : barcodeStaNo) {
                // 获取条码扫描仪信息
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, staNo);
                if (barcodeThread == null) {
                    continue;
                }
                String barcode = barcodeThread.getBarcode();
                if (!Cools.isEmpty(barcode)) {
                    if (barcode.contains("NoRead")) {
                        continue;
                    }
 
                    String agvStaNo = null;
                    if (staNo == 13) {
                        agvStaNo = "CS-310";
                    } else if(staNo == 15){
                        agvStaNo = "CS-311";
                    } else if(staNo == 17){
                        agvStaNo = "CS-312";
                    } else if(staNo == 19){
                        agvStaNo = "CS-313";
                    }
                    //通知WMS当前容器码
                    boolean result = agvCureentCall(agvStaNo, barcode);
                    if (result) {
                        barcodeThread.setBarcode("");
                    }
                    log.info(barcodeThread.getSlave().getId() + "号扫码器,通知AGV取货,条码号:" + barcode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    // 300站拣料
    public void pick300() {
        try {
            //检测300站是否自动、有物、工作号
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(300);
                if (staProtocol == null) {
                    continue;
                }else {
                    staProtocol = staProtocol.clone();
                }
 
                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() != 0) {
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
 
                    if (wrkMast.getWrkSts() != 29) {//29.出库完成
                        continue;
                    }
 
                    if (wrkMast.getMk() == null) {
                        Integer sourceStaNo = wrkMast.getSourceStaNo();//源站
                        Integer staNo = wrkMast.getStaNo();//目标站
                        //覆盖工作档目标站
                        wrkMast.setStaNo(sourceStaNo);
                        wrkMast.setSourceStaNo(staNo);
                        wrkMast.setMk("N");
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            //向300站写入目标站
                            staProtocol = staProtocol.clone();
                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());//移动到目标站
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            try {
                                Thread.sleep(500);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 出库  ===>> 工作档信息写入led显示器
     */
    public void ledExecute() {
        try {
            for (LedSlave led : slaveProperties.getLed()) {
                // 获取输送线plc线程
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
                // 命令集合
                List<LedCommand> commands = new ArrayList<>();
                // 工作档集合
                List<WrkMast> wrkMasts = new ArrayList<>();
                List<WrkMastLog> wrkMastLogs = new ArrayList<>();
                for (Integer staNo : led.getStaArr()) {
                    // 获取叉车站点
                    StaProtocol staProtocol = devpThread.getStation().get(staNo);
                    if (null == staProtocol || null == staProtocol.getWorkNo() || 0 == staProtocol.getWorkNo() || !staProtocol.isLoading()) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    // 获取工作档数据
                    WrkMast wrkMast = wrkMastMapper.selectById(staProtocol.getWorkNo());
                    Integer wrkNo = staProtocol.getWorkNo().intValue();
                    Integer ioType = null;
                    String sourceLocNo = null;
                    String locNo = null;
                    Integer wrkStaNo = null;
                    String barcode = null;
                    if (wrkMast == null) {
                        //查询历史档
                        WrkMastLog wrkMastLog = wrkMastLogMapper.selectLatestByWorkNo(staProtocol.getWorkNo().intValue());
                        if (wrkMastLog == null) {
                            continue;
                        }
                        ioType = wrkMastLog.getIoType();
                        sourceLocNo = wrkMastLog.getSourceLocNo();
                        locNo = wrkMastLog.getLocNo();
                        wrkStaNo = wrkMastLog.getStaNo();
                        barcode = wrkMastLog.getBarcode();
                        wrkMastLogs.add(wrkMastLog);
                    }else {
                        if (wrkMast.getWrkSts() < 14 || wrkMast.getIoType() < 100) {
                            continue;
                        }
                        ioType = wrkMast.getIoType();
                        sourceLocNo = wrkMast.getSourceLocNo();
                        locNo = wrkMast.getLocNo();
                        wrkStaNo = wrkMast.getStaNo();
                        barcode = wrkMast.getBarcode();
                        wrkMasts.add(wrkMast);
                    }
                    // 组装命令
                    LedCommand ledCommand = new LedCommand();
                    ledCommand.setWorkNo(wrkNo);
                    ledCommand.setIoType(ioType);
                    // 出库模式
                    switch (ioType) {
                        case 101:
                            ledCommand.setTitle("全板出库");
                            break;
                        case 103:
                            ledCommand.setTitle("拣料出库");
                            break;
                        case 104:
                            ledCommand.setTitle("并板出库");
                            break;
                        case 107:
                            ledCommand.setTitle("盘点出库");
                            break;
                        case 110:
                            ledCommand.setTitle("空板出库");
                            ledCommand.setEmptyMk(true);
                            break;
                        default:
                            News.error("任务入出库类型错误!!![工作号:{}] [入出库类型:{}]", wrkNo, ioType);
                            break;
                    }
                    ledCommand.setSourceLocNo(sourceLocNo);
                    ledCommand.setLocNo(locNo);
                    ledCommand.setStaNo(wrkStaNo);
                    ledCommand.setBarcode(barcode);
                    if (ioType != 110 && ioType != 10) {
                        List<WrkDetl> wrkDetls = wrkDetlService.selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo));
                        if (!wrkDetls.isEmpty()) {
                            wrkDetls.forEach(wrkDetl -> {
                                double remainNum = wrkDetl.getStock() - wrkDetl.getAnfme();//剩余数量
                                if (remainNum < 0) {
                                    remainNum = 0;
                                }
                                String matnr = wrkDetl.getMatnr();
                                Mat mat = matService.selectByMatnr(wrkDetl.getMatnr());
                                if (mat != null) {
                                    if (!mat.getMatnr().equals(mat.getMatnr2())) {
                                        matnr += " - " + mat.getMatnr2();
                                    }
                                }
                                ledCommand.getMatDtos().add(new MatDto(matnr, wrkDetl.getMaktx(), wrkDetl.getAnfme(), remainNum, wrkDetl.getSpecs(), wrkDetl.getSuppCode(), wrkDetl.getOrderNo()));
                            });
                        }else {
                            List<WrkDetlLog> wrkDetlLogs = wrkDetlLogService.selectLatestByWorkNo(wrkNo, barcode);
                            for (WrkDetlLog wrkDetlLog : wrkDetlLogs) {
                                double remainNum = wrkDetlLog.getStock() - wrkDetlLog.getAnfme();//剩余数量
                                if (remainNum < 0) {
                                    remainNum = 0;
                                }
                                String matnr = wrkDetlLog.getMatnr();
                                Mat mat = matService.selectByMatnr(wrkDetlLog.getMatnr());
                                if (mat != null) {
                                    if (!mat.getMatnr().equals(mat.getMatnr2())) {
                                        matnr += " - " + mat.getMatnr2();
                                    }
                                }
                                ledCommand.getMatDtos().add(new MatDto(matnr, wrkDetlLog.getMaktx(), wrkDetlLog.getAnfme(), remainNum, wrkDetlLog.getSpecs(), wrkDetlLog.getSuppCode()));
                            }
                        }
 
//                        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
//                        if (ioType == 101) {
//                            List<WrkDetl> wrkDetls = wrkDetlService.selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo));
//                            wrkDetls.forEach(wrkDetl -> {
//                                ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme(), 0D, wrkDetl.getSpecs(), wrkDetl.getSuppCode()));
//                            });
////                            locDetls.forEach(locDetl -> {
////                                Wrapper<WrkDetl> wrapper = new EntityWrapper<WrkDetl>().eq("matnr", locDetl.getMatnr()).eq("wrk_no", wrkNo);
////                                Utils.wapperSetCondition(wrapper, "batch", locDetl.getBatch());
////                                Utils.wapperSetCondition(wrapper, "three_code", locDetl.getThreeCode());
////                                Utils.wapperSetCondition(wrapper, "dead_time", locDetl.getDeadTime());
////                                Utils.wapperSetCondition(wrapper, "supp_code", locDetl.getSuppCode());//料箱码
////                                List<WrkDetl> detl = wrkDetlService.selectList(wrapper);
////                                if (Cools.isEmpty(detl)) {
////                                    String suppCode = "";
////                                    if (locDetl.getSuppCode() != null) {
////                                        suppCode = locDetl.getSuppCode();
////                                    }
////                                    ledCommand.getMatDtos().add(new MatDto(locDetl.getMatnr(), locDetl.getMaktx(), 0D, locDetl.getAnfme(), locDetl.getSpecs(), suppCode));
////                                } else {
////                                    //出库数量累计
////                                    Double conut = 0.0;
////                                    for (WrkDetl wrkDetl : detl) {
////                                        conut = conut + wrkDetl.getAnfme();
////                                    }
////
////                                    for (WrkDetl wrkDetl : detl) {
////                                        String suppCode = "";
////                                        if (wrkDetl.getSuppCode() != null) {
////                                            suppCode = wrkDetl.getSuppCode();
////                                        }
////                                        ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme(), (locDetl.getAnfme() - conut), wrkDetl.getSpecs(), suppCode, wrkDetl.getOrderNo()));
////                                    }
////                                }
////
////                            });
//                        } else {
//                            locDetls.forEach(locDetl -> {
//                                Wrapper<WrkDetl> wrapper = new EntityWrapper<WrkDetl>().eq("matnr", locDetl.getMatnr()).eq("wrk_no", wrkNo);
//                                Utils.wapperSetCondition(wrapper, "batch", locDetl.getBatch());
//                                Utils.wapperSetCondition(wrapper, "three_code", locDetl.getThreeCode());
//                                Utils.wapperSetCondition(wrapper, "dead_time", locDetl.getDeadTime());
//                                Utils.wapperSetCondition(wrapper, "supp_code", locDetl.getSuppCode());//料箱码
//                                List<WrkDetl> detl = wrkDetlService.selectList(wrapper);
//                                if (Cools.isEmpty(detl)) {
//                                    String suppCode = "";
//                                    if (locDetl.getSuppCode() != null) {
//                                        suppCode = locDetl.getSuppCode();
//                                    }
//                                    ledCommand.getMatDtos().add(new MatDto(locDetl.getMatnr(), locDetl.getMaktx(), 0D, locDetl.getAnfme(), locDetl.getSpecs(), suppCode));
//                                } else {
//                                    //出库数量累计
//                                    Double conut = 0.0;
//                                    for (WrkDetl wrkDetl : detl) {
//                                        conut = conut + wrkDetl.getAnfme();
//                                    }
//
//                                    for (WrkDetl wrkDetl : detl) {
//                                        String suppCode = "";
//                                        if (wrkDetl.getSuppCode() != null) {
//                                            suppCode = wrkDetl.getSuppCode();
//                                        }
//                                        ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme(), (wrkDetl.getStock() - conut), wrkDetl.getSpecs(), suppCode, wrkDetl.getOrderNo()));
//                                    }
//                                }
//
//                            });
//                        }
//
//
//                        if (ioType == 107) {
//                            locDetls = new ArrayList<>();
//                            ledCommand.setMatDtos(new ArrayList<>());
//                        }
//
//                        if (locDetls.isEmpty() && ioType != 101) {
//                            List<WrkDetl> wrkDetls = wrkDetlService.selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo));
//                            wrkDetls.forEach(wrkDetl -> {
//                                if (wrkDetl.getAnfme() > 0D) {
//                                    ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme(), wrkDetl.getAnfme(), wrkDetl.getSpecs(), wrkDetl.getSuppCode()));
//                                }
//                            });
//
//                            if (wrkDetls.isEmpty()) {//从历史档查询
//                                List<WrkDetlLog> wrkDetlLogs = wrkDetlLogService.selectLatestByWorkNo(wrkNo, barcode);
//                                for (WrkDetlLog wrkDetlLog : wrkDetlLogs) {
//                                    if (wrkDetlLog.getAnfme() > 0D) {
//                                        ledCommand.getMatDtos().add(new MatDto(wrkDetlLog.getMatnr(), wrkDetlLog.getMaktx(), wrkDetlLog.getAnfme(), wrkDetlLog.getAnfme(), wrkDetlLog.getSpecs(), wrkDetlLog.getSuppCode()));
//                                    }
//                                }
//                            }
//                        }
                    }
 
                    commands.add(ledCommand);
                }
                Set<Integer> workNos = null;
                if (!wrkMasts.isEmpty()) {
                    workNos = wrkMasts.stream().map(WrkMast::getWrkNo).collect(Collectors.toSet());
                }else {
                    workNos = wrkMastLogs.stream().map(WrkMastLog::getWrkNo).collect(Collectors.toSet());
                }
                // 获取LED线程
                LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
                // 相同工作号集合则过滤
                if (CollectionUtils.equals(ledThread.getWorkNos(), workNos)) {
                    continue;
                }
                // 命令下发 -------------------------------------------------------------------------------
                if (!commands.isEmpty()) {
                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                        continue;
                    } else {
                        ledThread.setLedMk(false);
                    }
                }
 
                try {
                    // 修改主档led标记
                    for (WrkMast wrkMast : wrkMasts) {
                        wrkMast.setOveMk("Y");
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            throw new CoolException("更新工作档失败");
                        }
                    }
 
                    // 更新线程当前工作号集合
                    ledThread.setWorkNos(workNos);
 
                } catch (Exception e) {
                    e.printStackTrace();
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 其他  ===>> LED显示器复位,显示默认信息
     */
    public void ledReset() {
        try {
            //        for (LedSlave led : slaveProperties.getLed()) {
//
//            // 获取输送线plc线程
//            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
//            // 命令集合
//            boolean reset = true;
//            for (Integer staNo : led.getStaArr()) {
//                // 获取叉车站点
//                StaProtocol staProtocol = devpThread.getStation().get(staNo);
//                if (staProtocol == null) {
//                    continue;
//                }
//                if (staProtocol.getWorkNo() != 0 && staProtocol.isLoading()) {
//                    reset = false;
//                    break;
//                }
//            }
//            // 获取led线程
//            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
//            // led显示默认内容
//            if (reset && !ledThread.isLedMk()) {
//                ledThread.setLedMk(true);
//                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(4, new ArrayList<>()))) {
//                    News.error(" - {}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
//                } else {
//                    News.error("{}号LED命令下发成功!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
//                    ledThread.setLedMk(false);
//                }
//            }
//        }
            for (LedSlave led : slaveProperties.getLed()) {
                // 获取输送线plc线程
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
                // 命令集合
                boolean reset = true;
                for (Integer staNo : led.getStaArr()) {
                    // 获取叉车站点
                    StaProtocol staProtocol = devpThread.getStation().get(staNo);
                    if (staProtocol == null) {
                        continue;
                    }
                    if (staProtocol.isLoading()) {
                        reset = false;
                        break;
                    }
                }
                // 获取led线程
                LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
                // led显示默认内容
                if (reset) {
                    ledThread.setLedMk(true);
                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(2, new ArrayList<>()))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 四向穿梭车电量检测 ===>> 发起充电
     */
    public synchronized void loopShuttleCharge() {
        try {
            for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
                //获取四向穿梭车线程
                NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
                NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
 
                //判断当前小车是否满足需要充电要求
                if (!shuttleProtocol.isRequireCharge()) {
                    continue;
                }
 
                WrkCharge wrkCharge = wrkChargeService.selectWorking(shuttleProtocol.getShuttleNo().intValue());
                if (wrkCharge != null) {//已有充电任务
                    continue;
                }
 
                //小车所在楼层
                int lev = Utils.getLev(shuttleProtocol.getCurrentLocNo());
                ShuttleChargeType shuttleCharge = null;
 
                //搜索小车所在楼层有没有充电桩
                for (ShuttleChargeType chargeType : ShuttleChargeType.values()) {
                    if (lev != Utils.getLev(chargeType.locNo)) {
                        continue;//小车和充电桩不在同一层
                    }
 
                    //小车和充电桩在同一层
                    if (wrkChargeService.selectWorkingOfCharge(chargeType.id) == null) {
                        shuttleCharge = chargeType;
                        break;
                    }
                }
 
                if (shuttleCharge == null) {
                    //同楼层没有找到充电桩,找可用充电桩
                    //小车同楼层没有充电桩,只要充电桩可用就生成充电任务
                    for (ShuttleChargeType chargeType : ShuttleChargeType.values()) {
                        if (wrkChargeService.selectWorkingOfCharge(chargeType.id) == null) {
                            //判断当前充电桩楼层是否有小车,如有小车,不分配该充电桩
                            int chargeLev = Utils.getLev(chargeType.locNo);//充电桩楼层
                            boolean checkLevHasShuttle = Utils.checkLevHasShuttle(chargeLev);//检测楼层是否有穿梭车
                            if (checkLevHasShuttle) {
                                //当前充电桩楼层有穿梭车,不分配该充电桩
                                continue;
                            }
 
                            shuttleCharge = chargeType;
                            break;
                        }
                    }
                }
 
                if (shuttleCharge == null) {
                    continue;
                }
 
                String chargeLocNo = shuttleCharge.locNo;
                wrkCharge = new WrkCharge();
                wrkCharge.setShuttleNo(shuttle.getId());
                wrkCharge.setCharge(shuttleCharge.id);
                wrkCharge.setWrkNo(commonService.getChargeWorkNo(4));
                wrkCharge.setWrkSts(51L);   // 51.准备充电
                wrkCharge.setIoPri((double) 10);
                wrkCharge.setLocNo(chargeLocNo);
                wrkCharge.setMemo("charge");
                wrkCharge.setAppeTime(new Date());
                if (!wrkChargeService.insert(wrkCharge)) {
                    News.error("保存{}号四向穿梭车充电任务失败!!!", shuttle.getId());
                    continue;
                }
 
                News.info("保存{}号四向穿梭车充电任务成功!!!", shuttle.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 执行四向穿梭车充电任务
     */
    public synchronized void executeShuttleCharge() {
        try {
            for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
                WrkCharge wrkCharge = wrkChargeService.selectWorking(shuttle.getId());
                if (wrkCharge == null) {
                    continue;
                }
 
                NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkCharge.getShuttleNo());
                if (shuttleThread == null) {
                    continue;
                }
                NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
 
                if (wrkCharge.getWrkSts() == 51) {
                    if (!shuttleProtocol.isIdleNoCharge()) {
                        continue;
                    }
 
                    if (shuttleProtocol.getSuspendState() == 1) {
                        continue;//管制中
                    }
 
                    if (!shuttleProtocol.getCurrentLocNo().equals(wrkCharge.getLocNo())) {
                        //小车不在充电桩位置
                        shuttleDispatchUtils.dispatchShuttle(wrkCharge.getWrkNo(), wrkCharge.getLocNo(), shuttle.getId());//调度小车去充电桩
                        continue;
                    }
 
                    //小车已经在充电桩位置,下发充电命令
                    NyShuttleHttpCommand chargeCommand = NyHttpUtils.getChargeCommand(shuttle.getId(), wrkCharge.getWrkNo(), true);
                    ArrayList<NyShuttleHttpCommand> commands = new ArrayList<>();
                    commands.add(chargeCommand);
                    //创建分配命令
                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
                    assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());//任务号
                    assignCommand.setTaskMode(ShuttleTaskModeType.CHARGE.id.shortValue());//出库模式
                    assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
                    assignCommand.setCommands(commands);//运行命令
 
                    wrkCharge.setWrkSts(52L);//51.生成充电任务 => 52.小车去充电中
                    wrkCharge.setModiTime(new Date());
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        //下发任务
                        MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                    }
                } else if (wrkCharge.getWrkSts() == 52) {
                    //检测小车是否满电
 
                    //获取满电阈值
                    int maxPower = 95;
                    EntityWrapper<Config> wrapper = new EntityWrapper<>();
                    wrapper.eq("code", "chargeMaxValue");
                    Config config = configService.selectOne(wrapper);
                    if (config != null) {
                        maxPower = Integer.parseInt(config.getValue());
                    }
 
                    if (shuttleProtocol.getPowerPercent() < maxPower) {
                        continue;
                    }
 
                    //***************判断是否满充校准***************
                    EntityWrapper<Config> wrapper1 = new EntityWrapper<>();
                    wrapper.eq("code", "shuttleMaxPowerVerify");
                    Config config1 = configService.selectOne(wrapper1);
                    if (config1 != null) {
                        if (config1.getValue().equals("true")) {
                            if (shuttleProtocol.getVoltage() < 5630) {
                                continue;//电压不够继续充电
                            }
                        }
                    }
                    //***************判断是否满充校准***************
 
                    //小车满电,结束充电任务
                    NyShuttleHttpCommand chargeCommand = NyHttpUtils.getChargeCommand(shuttle.getId(), wrkCharge.getWrkNo(), false);
                    ArrayList<NyShuttleHttpCommand> commands = new ArrayList<>();
                    commands.add(chargeCommand);
                    //创建分配命令
                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
                    assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());//任务号
                    assignCommand.setTaskMode(ShuttleTaskModeType.CHARGE.id.shortValue());//出库模式
                    assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
                    assignCommand.setCommands(commands);//运行命令
 
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                    try {
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
 
                    wrkCharge.setWrkSts(53L);//52.小车去充电中 => 53.小车充电完成
                    wrkCharge.setModiTime(new Date());
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE.id);
                        shuttleProtocol.setTaskNo(0);
                        shuttleProtocol.setPakMk(false);
                    }
                } else if (wrkCharge.getWrkSts() == 53) {
                    if (shuttleProtocol.getChargState() == 0) {//小车处于未充电状态
                        boolean result = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(wrkCharge.getLocNo()), shuttleThread.getSlave().getId(), shuttleThread);
                        if (!result) {
                            continue;
                        }
 
                        wrkCharge.setWrkSts(60L);//53.小车充电完成 => 60.充电任务完成
                        wrkCharge.setModiTime(new Date());
                        if (wrkChargeMapper.updateById(wrkCharge) > 0) {
 
                        }
                    }
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 执行小车移库任务
     */
    public synchronized void shuttleMoveExecute() {
        try {
            //查询小车移库任务
            List<WrkMast> wrkMasts = wrkMastMapper.selectShuttleMoveWrk();
            for (WrkMast wrkMast : wrkMasts) {
                boolean stepMoveSta = this.shuttleMoveExecuteStepMoveSta(wrkMast);//小车移动到站点
                if (!stepMoveSta) {
                    continue;
                }
 
                boolean stepIntoLift = this.shuttleMoveExecuteStepIntoLift(wrkMast);//小车迁入提升机
                if (!stepIntoLift) {
                    continue;
                }
 
                boolean stepLiftMove = this.shuttleMoveExecuteStepLiftMove(wrkMast);//提升机搬运中
                if (!stepLiftMove) {
                    continue;
                }
 
                boolean stepOutLift = this.shuttleMoveExecuteStepOutLift(wrkMast);//小车迁出提升机
                if (!stepOutLift) {
                    continue;
                }
 
                boolean stepMoveLoc = this.shuttleMoveExecuteStepMoveLoc(wrkMast);//小车移动到目标库位中
                if (!stepMoveLoc) {
                    continue;
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 小车迁移-小车移动到站点
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean shuttleMoveExecuteStepMoveSta(WrkMast wrkMast) {
        //--------------------------------------小车移动至站点-----------------------------------------//
        Date now = new Date();
 
        //小车移动至站点  101.生成小车移库任务 ==> 102.小车移动至站点中
        if (wrkMast.getWrkSts() == 101) {
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
 
            //小车处于空闲状态
            if (!shuttleProtocol.isIdleNoCharge(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车忙碌中", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //判断小车令牌是否未被占领
            if (shuttleProtocol.getToken() != 0) {
                News.info("{}任务,{}小车,令牌已被独占,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//小车已被独占,禁止再派发任务
            }
 
            if (Utils.getLev(wrkMast.getLocNo()) == shuttleProtocol.getPoint().getZ()) {
                //目标库位和小车库位处于同一楼层,不需要通过提升机调度
                wrkMast.setWrkSts(109L);// 109.小车迁出提升机完成 ==> 110.小车移动中
                wrkMast.setModiTime(now);
                shuttleProtocol.setToken(wrkMast.getWrkNo());//独占该小车令牌
                if (wrkMastMapper.updateById(wrkMast) > 0) {
                    //下发任务
                    return true;//直接进入109.小车迁出提升机完成 ==> 110.小车移动中
                }
                return false;
            }
 
            //获取源输送站
            LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getSourceStaNo());
            if (liftSta == null) {
                return false;//找不到站点
            }
 
            //判断提升机是否有其他任务(该任务需要换层必须提前独占提升机)
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftSta.getLiftNo());
            if (liftWrkMast != null) {
                News.info("{}任务,{}号提升机,提升机存在未完成任务,禁止派发", wrkMast.getWrkNo(), liftSta.getLiftNo());
                return false;//当前提升机存在未完成任务,等待下一次轮询
            }
 
            //*************尝试锁定目标站路径***************
            List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(wrkMast.getStaNo());
            if (targetNodes == null) {
                return false;//未获取到节点
            }
            boolean checkPathIsAvailable = NavigateUtils.checkPathIsAvailable(targetNodes, shuttleProtocol.getShuttleNo().intValue(), Utils.getLev(wrkMast.getLocNo()), null);
            if (!checkPathIsAvailable) {
                News.info("{}任务,{}小车,目标站点路径被占用,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//检测目标站点路径是否未被占用
            }
            //尝试锁定目标站路径
            boolean result2 = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(wrkMast.getLocNo()), shuttleProtocol.getShuttleNo().intValue(), targetNodes, true);//所使用的路径进行锁定禁用
            if (!result2) {
                News.info("{}任务,{}小车,路径锁定失败,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//路径锁定失败
            }
            //*************尝试锁定目标站路径***************
 
            //获取小车到输送站点行走命令
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), liftSta.getLocNo(), NavigationMapType.NORMAL.id);
            if (result == null) {
                //路径获取失败,需要解锁上面锁定的路径
                //尝试解锁目标站路径
                boolean result3 = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(wrkMast.getLocNo()), shuttleProtocol.getShuttleNo().intValue(), targetNodes, false);//所使用的路径进行解锁
                if (!result3) {
                    News.info("{}任务,{}小车,路径解锁失败", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                    return false;//路径解锁失败
                }
                News.info("{}任务,{}小车,路径计算失败", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;//路径解锁失败
            }
            List<NyShuttleHttpCommand> commands = result.getCommands();
 
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo()); // 四向穿梭车编号
            assignCommand.setTaskMode(ShuttleTaskModeType.SHUTTLE_MOVE_LOC_NO.id.shortValue());//小车移库任务
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setAuto(true);//自动模式
            assignCommand.setCommands(commands);
            assignCommand.setNodes(result.getNodes());
 
            wrkMast.setWrkSts(102L);//小车移动到提升机中  101.生成小车移库任务 ==> 102.小车移动至站点
            wrkMast.setModiTime(now);
            wrkMast.setLiftNo(liftSta.getLiftNo());//提前锁定提升机
            shuttleProtocol.setToken(wrkMast.getWrkNo());//独占该小车令牌
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                return false;
            }
            return false;
        }
        return true;
    }
 
    /**
     * 小车迁移-小车迁入提升机
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean shuttleMoveExecuteStepIntoLift(WrkMast wrkMast) {
        //--------------------------------------小车迁入提升机-----------------------------------------//
        Date now = new Date();
 
        //小车移动到提升机中  103.小车移动至站点完成 ==> 104.小车迁入提升机中
        if (wrkMast.getWrkSts() == 103) {
            //获取源站
            LiftStaProtocol sourceLiftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getSourceStaNo());
            if (sourceLiftSta == null) {
                return false;//找不到站点
            }
 
            //获取目标输送站
            LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
            if (liftSta == null) {
                return false;//找不到站点
            }
 
            //获取提升机数据
            BasLift basLift = basLiftService.selectById(liftSta.getLiftNo());
            if (basLift == null) {
                return false;//没有提升机数据
            }
 
            if (basLift.getPoint() == null) {
                News.info("{}任务,{}号提升机,没有提升机点位坐标,禁止派发", wrkMast.getWrkNo(), basLift.getLiftNo());
                return false;//没有设置提升机点位坐标
            }
 
            //判断提升机是否有其他任务
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(basLift.getLiftNo());
            if (liftWrkMast != null) {
                if (!liftWrkMast.getWrkNo().equals(wrkMast.getWrkNo())) {//提升机任务和当前任务不相同
                    News.info("{}任务,{}号提升机,提升机存在未完成任务,禁止派发", wrkMast.getWrkNo(), basLift.getLiftNo());
                    return false;//当前提升机存在未完成任务,等待下一次轮询
                }
            }
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSta.getLiftNo());
            if (liftThread == null) {
                return false;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return false;
            }
 
            if (!liftProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                News.info("{}任务,{}号提升机,提升机忙碌中,禁止派发", wrkMast.getWrkNo(), basLift.getLiftNo());
                return false;
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
 
            //小车处于空闲状态
            if (!shuttleProtocol.isIdleNoCharge(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车忙碌中,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //小车令牌是否被任务独占
            if (!shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车令牌被独占,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //判断提升机楼层
            if (liftProtocol.getLev().intValue() != shuttleProtocol.getPoint().getZ()) {
                //提升机不在小车楼层
                //调度提升机
 
                if (liftProtocol.getToken() != 0) {
                    News.info("{}任务,{}号提升机,提升机令牌被独占,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                    return false;//提升机令牌被占用
                }
 
                //获取提升机命令,调度提升机到源站位置
                NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_CAR.id, sourceLiftSta.getStaNo(), sourceLiftSta.getStaNo(), wrkMast.getWrkNo());
 
                int deviceWrk = commonService.getWorkNo(8);//生成提升机设备工作号
                BasLiftOpt basLiftOpt = basLiftOptService.selectByDeviceWrk(String.valueOf(deviceWrk), liftThread.getSlave().getId());
                if (basLiftOpt != null) {
                    News.info("{}任务,{}号提升机,设备工作号出现重复情况,请联系技术人员支持。", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                    return false;
                }
                liftCommand.setTaskNo((short) deviceWrk);//更换随机任务号
 
                ArrayList<NyLiftCommand> commands = new ArrayList<>();
                commands.add(liftCommand);
 
                //提交到线程去工作
                LiftAssignCommand assignCommand = new LiftAssignCommand();
                assignCommand.setCommands(commands);
                assignCommand.setLiftNo(liftProtocol.getLiftNo());
                assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_CAR.id.shortValue());
                assignCommand.setAuto(false);
 
                wrkMast.setLiftNo(liftThread.getSlave().getId());//锁定提升机防止被占用
                wrkMast.setModiTime(now);
                liftProtocol.setToken(wrkMast.getShuttleNo());//提升机令牌绑定当前小车
                if (wrkMastMapper.updateById(wrkMast) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
                }
                return false;//等待提升机到小车楼层
            }
 
            if (liftProtocol.getToken() == 0) {//提升机令牌未被占用
                //独占提升机
                liftProtocol.setToken(wrkMast.getShuttleNo());
                return false;//等待下一次执行
            }
 
            //判断提升机令牌是否为当前小车
            if (!liftProtocol.getToken().equals(wrkMast.getShuttleNo())) {
                News.info("{}任务,{}号提升机,提升机令牌被独占,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//提升机已被独占,禁止再派发任务
            }
 
            //判断小车是否为当前任务独占
            if (!shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车令牌被独占,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //站点节点
            NavigateNode staNode = NavigatePositionConvert.locNoToNode(sourceLiftSta.getLocNo());
 
            //提升机节点
            NavigateNode liftNode = new NavigateNode(basLift.getPoint$().getX(), basLift.getPoint$().getY());
            liftNode.setZ(staNode.getZ());
 
            //获取小车进提升机行走命令
            NyShuttleHttpCommand moveCommand = NyHttpUtils.getInOutLiftCommand(wrkMast.getShuttleNo(), wrkMast.getWrkNo(), staNode, liftNode, true);
            List<NyShuttleHttpCommand> commands = new ArrayList<>();
            commands.add(moveCommand);
            List<NavigateNode> nodes = new ArrayList<>();//行走节点路径
            nodes.add(staNode);
            nodes.add(liftNode);
 
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo()); // 四向穿梭车编号
            assignCommand.setTaskMode(ShuttleTaskModeType.SHUTTLE_MOVE_LOC_NO.id.shortValue());//小车移库任务
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setAuto(true);//自动模式
            assignCommand.setCommands(commands);
            assignCommand.setNodes(nodes);
 
            wrkMast.setWrkSts(104L);//小车移动到提升机中  103.小车移动至站点完成 ==> 104.小车迁入提升机中
            wrkMast.setModiTime(now);
            wrkMast.setLiftNo(liftThread.getSlave().getId());//锁定提升机防止被占用
            liftProtocol.setToken(wrkMast.getShuttleNo());//提升机令牌绑定当前小车
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                return false;
            }
            return false;
        }
 
        return true;
    }
 
    /**
     * 小车迁移-提升机搬运中
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean shuttleMoveExecuteStepLiftMove(WrkMast wrkMast) {
        //--------------------------------------提升机搬运中-----------------------------------------//
        Date now = new Date();
 
        //提升机搬运中  105.小车迁入提升机完成 ==> 106.提升机搬运中
        if (wrkMast.getWrkSts() == 105) {
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, wrkMast.getLiftNo());
            if (liftThread == null) {
                return false;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return false;
            }
            if (!liftProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                News.info("{}任务,{}号提升机,提升机忙碌中,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;
            }
            //判断提升机是否有其他任务
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftThread.getSlave().getId());
            if (liftWrkMast != null) {
                if (!liftWrkMast.getWrkNo().equals(wrkMast.getWrkNo())) {//提升机任务和当前任务不相同
                    News.info("{}任务,{}号提升机,提升机存在未完成任务,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                    return false;//当前提升机存在未完成任务,等待下一次轮询
                }
            }
 
            //判断提升机令牌是否为当前小车
            if (!liftProtocol.getToken().equals(wrkMast.getShuttleNo())) {
                News.info("{}任务,{}号提升机,提升机令牌和当前小车不一致,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//提升机令牌和当前小车不一致,禁止派发
            }
 
            //判断提升机内是否有小车
            if (!liftProtocol.getHasCar()) {
                News.info("{}任务,{}号提升机,提升机内无小车,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//提升机内无小车
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
 
            //小车处于空闲状态
            if (!shuttleProtocol.isIdleNoCharge()) {
                News.info("{}任务,{}号提升机,提升机忙碌中,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;
            }
 
            //判断小车是否为当前任务独占
            if (!shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车当前任务未被独占,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //获取源站
            LiftStaProtocol sourceLiftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getSourceStaNo());
            //获取目标站
            LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
            if (sourceLiftSta == null || liftSta == null) {
                News.info("{}任务,缺少站点信息,禁止派发", wrkMast.getWrkNo());
                return false;//缺少站点信息
            }
 
            //获取提升机命令
            NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_CAR.id, sourceLiftSta.getStaNo(), liftSta.getStaNo(), wrkMast.getWrkNo());
 
            ArrayList<NyLiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
 
            //提交到线程去工作
            LiftAssignCommand assignCommand = new LiftAssignCommand();
            assignCommand.setCommands(commands);
            assignCommand.setLiftNo(liftProtocol.getLiftNo());
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
            assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_CAR.id.shortValue());
 
            wrkMast.setWrkSts(106L);//提升机搬运中  105.小车迁入提升机完成 ==> 106.提升机搬运中
            wrkMast.setLiftNo(liftThread.getSlave().getId());//锁定提升机防止被占用
            wrkMast.setModiTime(now);
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
            }
 
        }
        return true;
    }
 
    /**
     * 小车迁移-小车迁出提升机
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean shuttleMoveExecuteStepOutLift(WrkMast wrkMast) {
        //--------------------------------------小车迁出提升机-----------------------------------------//
        Date now = new Date();
        //小车移动到提升机中  107.提升机搬运完成 ==> 108.小车迁出提升机中
        if (wrkMast.getWrkSts() == 107) {
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, wrkMast.getLiftNo());
            if (liftThread == null) {
                return false;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                return false;
            }
            if (!liftProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                News.info("{}任务,{}号提升机,提升机忙碌中,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;
            }
 
            //判断提升机是否有其他任务
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftThread.getSlave().getId());
            if (liftWrkMast != null) {
                if (!liftWrkMast.getWrkNo().equals(wrkMast.getWrkNo())) {//提升机任务和当前任务不相同
                    News.info("{}任务,{}号提升机,提升机存在未完成任务,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                    return false;//当前提升机存在未完成任务,等待下一次轮询
                }
            }
 
            //判断提升机令牌是否为当前小车
            if (!liftProtocol.getToken().equals(wrkMast.getShuttleNo())) {
                News.info("{}任务,{}号提升机,提升机令牌和当前小车不一致,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//提升机令牌和当前小车不一致,禁止派发
            }
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
 
            //小车处于空闲状态
            if (!shuttleProtocol.isIdleNoCharge()) {
                News.info("{}任务,{}小车,小车忙碌中,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //判断小车是否为当前任务独占
            if (!shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车令牌被独占,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //获取目标站
            LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
            if (liftSta == null) {
                return false;//找不到站点
            }
 
            //获取提升机数据
            BasLift basLift = basLiftService.selectById(liftProtocol.getLiftNo().intValue());
            if (basLift == null) {
                return false;//没有提升机数据
            }
            if (basLift.getPoint() == null) {
                News.info("{}任务,{}号提升机,缺少提升机点位坐标,禁止派发", wrkMast.getWrkNo(), liftProtocol.getLiftNo());
                return false;//没有设置提升机点位坐标
            }
 
            NavigateNode liftNode = new NavigateNode(basLift.getPoint$().getX(), basLift.getPoint$().getY());
            liftNode.setZ(liftSta.getLev());
 
            List<NyShuttleHttpCommand> commands = new ArrayList<>();
            //获取小车更新楼层命令
            NyShuttleHttpCommand updateZCommand = NyHttpUtils.getUpdateZCommand(shuttleThread.getSlave().getId(), liftProtocol.getLev().intValue(), wrkMast.getWrkNo());
            commands.add(updateZCommand);
 
            //获取小车出提升机行走命令
            NyShuttleHttpCommand moveCommand = NyHttpUtils.getInOutLiftCommand(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), liftNode, NavigatePositionConvert.locNoToNode(liftSta.getLocNo()), false);
            commands.add(moveCommand);//添加小车迁出提升机命令
 
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo()); // 四向穿梭车编号
            assignCommand.setTaskMode(ShuttleTaskModeType.SHUTTLE_MOVE_LOC_NO.id.shortValue());//小车移库任务
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setAuto(true);//自动模式
            assignCommand.setCommands(commands);
            assignCommand.setNodes(null);
 
            wrkMast.setWrkSts(108L);//小车迁出提升机中  107.提升机搬运完成 ==> 108.小车迁出提升机中
            wrkMast.setModiTime(now);
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                return false;
            }
            return false;
        }
        return true;
    }
 
    /**
     * 小车迁移-小车移动到目标库位中
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean shuttleMoveExecuteStepMoveLoc(WrkMast wrkMast) {
        //--------------------------------------小车移动到目标库位中-----------------------------------------//
        Date now = new Date();
 
        //小车移动到目标库位中  109.小车迁出提升机完成 ==> 110.小车移动中
        if (wrkMast.getWrkSts() == 109) {
 
            //获取四向穿梭车线程
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                return false;
            }
 
            //小车处于空闲状态
            if (!shuttleProtocol.isIdleNoCharge(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车忙碌中,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
            //判断小车令牌是否为当前任务
            if (shuttleProtocol.getToken() != 0 && !shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                News.info("{}任务,{}小车,小车令牌被独占,禁止派发", wrkMast.getWrkNo(), shuttleProtocol.getShuttleNo());
                return false;
            }
 
//            //跨楼层移动任务
//            if (Utils.getLev(wrkMast.getSourceLocNo()) != Utils.getLev(wrkMast.getLocNo())) {
//                //获取目标站
//                LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
//                if (liftSta == null) {
//                    return false;//找不到站点
//                }
//
//                //*************尝试解锁目标站路径***************
//                List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(liftSta.getStaNo());
//                if (targetNodes == null) {
//                    return false;//未获取到节点
//                }
//                //尝试解锁目标站路径
//                boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(wrkMast.getLocNo()), targetNodes, false);//所使用的路径进行解锁
//                if (!result) {
//                    return false;//路径解锁失败
//                }
//                //*************尝试解锁目标站路径***************
//            }
 
            //小车已经在目标库位,直接认定小车移动任务完成
            if (shuttleProtocol.getCurrentLocNo().equals(wrkMast.getLocNo())) {
                if (shuttleProtocol.getToken().equals(wrkMast.getWrkNo())) {
                    //释放小车令牌
                    shuttleProtocol.setToken(0);
                }
                wrkMast.setWrkSts(111L);//111.小车移动完成
                wrkMast.setLiftNo(null);//释放提升机
                wrkMast.setModiTime(now);
                wrkMastMapper.updateById(wrkMast);
 
                shuttleProtocol.setShuttleNo((short) 0);//释放小车
                shuttleProtocol.setToken(0);//释放小车
 
                if (wrkMast.getWrkSts() == 111) {
                    // 保存工作主档历史档
                    if (wrkMastLogMapper.save(wrkMast.getWrkNo()) <= 0) {
                        log.info("保存工作历史档[workNo={0}]失败", wrkMast.getWrkNo());
                    }
                    // 删除工作主档
                    if (!wrkMastService.deleteById(wrkMast)) {
                        log.info("删除工作主档[workNo={0}]失败", wrkMast.getWrkNo());
                    }
                }
                return false;
            }
 
            NyShuttleOperaResult result = null;
            //跨楼层移动任务
            if (Utils.getLev(wrkMast.getSourceLocNo()) != Utils.getLev(wrkMast.getLocNo())) {
                //需要将前两个节点作为白名单节点传入
                //获取目标站
                LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
                if (liftSta == null) {
                    return false;//找不到站点
                }
 
                List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(liftSta.getStaNo());
                if (targetNodes == null) {
                    return false;//未获取到节点
                }
 
                //设置计算节点的白名单
                ArrayList<int[]> whiteList = new ArrayList<>();//设置计算节点的白名单
                for (NavigateNode node : targetNodes) {
                    whiteList.add(new int[]{node.getX(), node.getY()});
                }
                result = NyShuttleOperaUtils.getStartToTargetCommandsByWhites(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo(), NavigationMapType.NORMAL.id, whiteList);
            }else {
                //获取小车到目标库位命令
                result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo(), NavigationMapType.NORMAL.id);
            }
 
            if (result == null) {
                return false;//路径计算失败
            }
            List<NyShuttleHttpCommand> commands = result.getCommands();
 
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo()); // 四向穿梭车编号
            assignCommand.setTaskMode(ShuttleTaskModeType.SHUTTLE_MOVE_LOC_NO.id.shortValue());//小车移库任务
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setAuto(true);//自动模式
            assignCommand.setCommands(commands);
            assignCommand.setNodes(result.getNodes());
            assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位
            assignCommand.setLocNo(wrkMast.getLocNo());//目标库位
 
            if (wrkMast.getLiftNo() != null) {
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, wrkMast.getLiftNo());
                if (liftThread == null) {
                    return false;
                }
                LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                if (liftProtocol == null) {
                    return false;
                }
                if (liftProtocol.getToken().equals(shuttleProtocol.getShuttleNo().intValue())) {
                    liftProtocol.setToken(0);//释放提升机令牌
                }
            }
 
            wrkMast.setWrkSts(110L);//小车移动到目标库位中  109.小车迁出提升机完成 ==> 110.小车移动中
            wrkMast.setLiftNo(null);//释放提升机
            wrkMast.setModiTime(now);
 
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
        }
        return true;
    }
 
    //扫描设备PakMk标记是否超时
    public synchronized void scanDevicePakMk() {
        try {
            //扫描小车
            for (ShuttleSlave slave : slaveProperties.getShuttle()) {
                NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId());
                NyShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
 
                if ((System.currentTimeMillis() - shuttleProtocol.getSendTime() > (1000 * 60 * 5)) && shuttleProtocol.getPakMk()) {
                    //设备超过5分钟还没复位标记
                    shuttleProtocol.setPakMk(false);//复位标记
                }
            }
 
            //扫描提升机
            for (LiftSlave slave : slaveProperties.getLift()) {
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, slave.getId());
                LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                if (liftProtocol == null) {
                    continue;
                }
 
                if ((System.currentTimeMillis() - liftProtocol.getSendTime() > (1000 * 60 * 5)) && liftProtocol.getPakMk()) {
                    //设备超过5分钟还没复位标记
                    liftProtocol.setPakMk(false);//复位标记
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}