自动化立体仓库 - WCS系统
#
luxiaotao1123
2022-09-15 e0276af68793e2e7d999f8cf31727ae0657fc84e
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
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.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.BasCrnErrorMapper;
import com.zy.asrs.mapper.WrkMastMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.MatDto;
import com.zy.common.model.SearchLocParam;
import com.zy.common.model.StartupDto;
import com.zy.common.utils.CollectionUtils;
import com.zy.common.utils.HttpHandler;
import com.zy.core.CrnThread;
import com.zy.core.DevpThread;
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.CrnCommand;
import com.zy.core.model.command.LedCommand;
import com.zy.core.model.command.SteCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.model.protocol.SteProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.BarcodeThread;
import com.zy.core.thread.LedThread;
import com.zy.core.thread.SteThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * 立体仓库WCS系统主流程业务
 * Created by vincent on 2020/8/6
 */
@Slf4j
@Service("mainService")
@Transactional
public class MainServiceImpl {
 
    @Value("${wms.url}")
    private String wmsUrl;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private WrkMastMapper wrkMastMapper;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private BasErrLogService basErrLogService;
    @Autowired
    private BasCrnErrorMapper basCrnErrorMapper;
    @Autowired
    private BasSteService basSteService;
 
    /**
     * 组托
     * 入库站,根据条码扫描生成入库工作档,工作状态 2
     */
    @Async
    public void generateStoreWrkFile() {
        // 根据输送线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()) {
 
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if(!Cools.isEmpty(barcode)) {
                        log.info("{}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                        if("NG".endsWith(barcode) || "NoRead".equals(barcode)) {
                            continue;
                        }
                    } else {
                        continue;
                    }
 
                    // 判断重复工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakInStep1(inSta.getStaNo(), barcode);
                    if (wrkMast != null) {
                        log.error("工作档中已存在该站状态为( 2.设备上走 )的数据,工作号={}", wrkMast.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/v1")
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        if (jsonObject.getInteger("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站点信息失败");
                            }
                        } else {
                            log.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl+"/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                }
            }
        }
    }
 
    /**
     * wms入库
     * 入库站,根据条码扫描生成入库工作档,工作状态 1 ==>> 2
     */
    @Async
    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()) {
                    // 判断重复工作档
                    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) {
                            log.error("更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
                    } else {
                        log.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                    }
                }
            }
        }
    }
 
    /**
     * 拣料、并板、盘点再入库
     */
    public synchronized void stnToCrnStnPick(){
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历拣料入库口
            for (DevpSlave.Sta pickSta : devp.getPickSta()) {
 
                // 获取拣料入库站信息
                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.isPakMk()){
 
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, pickSta.getBarcode());
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if(!Cools.isEmpty(barcode)) {
                        log.info("{}号条码扫描器检测条码信息:{}", pickSta.getBarcode(), barcode);
                        if("NG".endsWith(barcode) || "NoRead".equals(barcode)) {
                            continue;
                        }
                    } else {
                        continue;
                    }
 
                    WrkMast wrkMast = wrkMastMapper.selectPickStep(barcode);
                    if (wrkMast == null) {
                        // 无拣料数据
                        continue;
                    }
                    if ((wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 107)
                        || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo()) ) {
                        continue;
                    }
 
                    // 获取目标站
                    Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                            .eq("type_no", wrkMast.getIoType() - 50)
                            .eq("stn_no", pickSta.getStaNo()) // 作业站点 = 拣料出库的目标站
                            .eq("crn_no", wrkMast.getCrnNo()); // 堆垛机号
                    StaDesc staDesc = staDescService.selectOne(wrapper);
                    if (Cools.isEmpty(staDesc)) {
                        log.error("入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
                        continue;
                    }
 
                    try {
                        // 保存工作明细档历史档
                        if (wrkMastMapper.saveWrkDetlLog(wrkMast.getWrkNo()) == 0) {
                            throw new CoolException("保存工作明细档历史档失败");
                        }
                        // 保存工作主档历史档
                        if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
                            throw new CoolException("保存工作主档历史档失败");
                        }
 
                        // 堆垛机站点(目标站)
                        Integer staNo = staDesc.getCrnStn();
                        // 更新工作档数据状态
                        wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 103->53,104->54,107->57
                        wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
                        wrkMast.setSourceStaNo(wrkMast.getStaNo()); // 源站
                        wrkMast.setStaNo(staNo); // 目标站
                        wrkMast.setLocNo(wrkMast.getSourceLocNo()); // 目标库位 = 出库时的源库位
                        wrkMast.setSourceLocNo(""); // 源库位清空
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            throw new CoolException("更新工作档数据状态失败");
                        }
                        // 修改库位状态 Q.拣料/盘点/并板再入库
                        LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
                        locMast.setLocSts("Q");
                        locMast.setModiTime(new Date());
                        if (!locMastService.updateById(locMast)) {
                            throw new CoolException("修改库位状态失败");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        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) {
                        log.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                    }
 
                }
 
            }
 
        }
    }
 
    /**
     * 堆垛机站出库到出库站
     */
    @Async
    public void crnStnToOutStn() {
        for (CrnSlave crnSlave : slaveProperties.getCrn()) {
            // 遍历堆垛机出库站
            for (CrnSlave.CrnStn crnStn : crnSlave.getCrnOutStn()) {
                // 获取堆垛机出库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
                StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                if (staProtocol.isAutoing() && staProtocol.isLoading() && (staProtocol.getWorkNo() == 0 || staProtocol.getStaNo() == null)) {
                    // 查询工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakOutStep16(staProtocol.getSiteId());
                    if (wrkMast == null) {
                        continue;
                    }
                    // 判断工作档条件
                    if (wrkMast.getIoType() < 100 || wrkMast.getStaNo() == null || wrkMast.getSourceStaNo() == null) {
                        continue;
                    }
                    // 判断吊车是否实际已完成,且电脑状态在move中,以备电脑进行更新工作档
                    CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, wrkMast.getCrnNo());
                    CrnProtocol crnProtocol = crnThread.getCrnProtocol();
                    if (crnProtocol.statusType == CrnStatusType.FETCHING || crnProtocol.statusType == CrnStatusType.PUTTING) {
                        // 移动中
                        continue;
                    }
                    //  判断堆垛机状态等待确认
                    if (crnProtocol.modeType == CrnModeType.AUTO && crnProtocol.getTaskNo().equals(wrkMast.getWrkNo().shortValue())
                            && crnProtocol.statusType == CrnStatusType.WAITING
                            && crnProtocol.forkPosType == CrnForkPosType.HOME) {
 
                        // 命令下发区 --------------------------------------------------------------------------
 
                        // 下发站点信息
                        staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                        staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                        if (!MessageQueue.offer(SlaveType.Devp, crnStn.getDevpPlcId(), new Task(2, staProtocol))) {
                            continue;
                        }
 
                        // 更新工作档状态为 17.出库完成
                        wrkMast.setWrkSts(17L);
                        wrkMast.setCrnEndTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) != 0) {
                            // 复位堆垛机
                            crnThread.setResetFlag(true);
                        } else {
                            log.error("更新工作档的工作状态为 17.出库完成 失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
 
                    }
 
                }
            }
        }
    }
 
    /**
     * 入出库  ===>>  堆垛机入出库作业下发
     */
    public synchronized void crnIoExecute(){
        for (CrnSlave crn : slaveProperties.getCrn()) {
            // 获取堆垛机信息
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
            if (crnProtocol == null) { continue; }
            BasCrnp basCrnp = basCrnpService.selectById(crn.getId());
            if (basCrnp == null) {
                log.error("{}号堆垛机尚未在数据库进行维护!", crn.getId());
                continue;
            }
            // 只有当堆垛机空闲 并且 无任务时才继续执行
            if (crnProtocol.getStatusType() == CrnStatusType.IDLE && crnProtocol.getTaskNo() == 0 && crnProtocol.getModeType() == CrnModeType.AUTO) {
                // 如果最近一次是入库模式
                if (crnProtocol.getLastIo().equals("I")) {
                    if (basCrnp.getInEnable().equals("Y")) {
                        //  入库
                        if (this.crnStnToLoc(crn, crnProtocol)) {
                            crnProtocol.setLastIo("O");
                        }
                    } else if (basCrnp.getOutEnable().equals("Y")) {
                        //  出库
                        if (this.locToCrnStn(crn, crnProtocol)) {
                            crnProtocol.setLastIo("I");
                        }
                    }
                }
                // 如果最近一次是出库模式
                else if (crnProtocol.getLastIo().equals("O")) {
                    if (basCrnp.getOutEnable().equals("Y")) {
                        //  出库
                        if (this.locToCrnStn(crn, crnProtocol)) {
                            crnProtocol.setLastIo("I");
                        }
                    } else if (basCrnp.getInEnable().equals("Y")) {
                        //  入库
                        if (this.crnStnToLoc(crn, crnProtocol)) {
                            crnProtocol.setLastIo("O");
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 入库  ===>>  堆垛机站到库位
     */
    public boolean crnStnToLoc(CrnSlave slave, CrnProtocol crnProtocol){
        for (CrnSlave.CrnStn crnStn : slave.getCrnInStn()) {
            boolean flag = false;
            // 获取堆垛机入库站信息
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
            StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
            if (staProtocol == null) {
                continue;
            } else {
                staProtocol = staProtocol.clone();
            }
            // 查询站点详细信息
            BasDevp staDetl = basDevpService.selectById(crnStn.getStaNo());
            if (staDetl == null) {
                log.error("入库 ===>> 堆垛机站点在数据库不存在, 站点编号={}", crnStn.getStaNo());
                continue;
            }
            // 获取工作状态为 2,3,4,5,6 的入库工作档
            WrkMast wrkMast = wrkMastMapper.selectPakInStep23456(slave.getId(), staProtocol.getWorkNo().intValue(), crnStn.getStaNo());
            if(null == wrkMast) {
                log.error("{}站点查询无待入库数据 工作号={}", crnStn.getStaNo(), staProtocol.getWorkNo());
                continue;
            }
            if (wrkMast.getWrkSts() < 3) {
                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() > 0 && staProtocol.isInEnable()
                        && staDetl.getCanining()!=null && staDetl.getCanining().equals("Y")) {
                    flag = true;
                }
                if (!flag) {
                    continue;
                }
            }
            // 获取库位信息
            LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
            if (locMast == null) {
                log.error("查询库存无数据--库位号{}", wrkMast.getLocNo());
                continue;
            }
            if (!locMast.getLocSts().equals("S") && !locMast.getLocSts().equals("Q")) {
                log.error("入库操作库位状态不符合--状态, 库位号={},库位状态={}", wrkMast.getLocNo(), locMast.getLocSts());
                continue;
            }
 
            // 目标库位 ===>> 最外层库位
            if (locMastService.isOutMost(wrkMast.getLocNo())) {
                // 判断小车是否在最外层库位,如果是则搬走,如果不是,则直接堆垛机入库
                Integer steNo = this.hasCarOfIdle(wrkMast.getLocNo());
                // 有小车
                if (steNo != null) {
                    // 小车行走到堆垛机待搬移点
                    if (wrkMast.getWrkSts() == 2L && wrkMast.getSteNo() == null) {
                        this.letCarBeReady(wrkMast, steNo, wrkMast.getLocNo());
                    }
                    // 小车搬走
                    if (wrkMast.getWrkSts() == 3L) {
                        this.carMoveOut(wrkMast, steNo, crnProtocol);
                    }
                // 没有小车
                } else {
 
                    // 当前入库库位组没有小车 堆垛机则去站点取货入库
                    // case 1:从始至终库位组都没有小车,没有迁出小车动作,所以工作状态 = 2
                    // case 2:堆垛机已经搬出小车,有迁出小车动作,所以工作状态 = 6
                    if (wrkMast.getWrkSts() == 2L || wrkMast.getWrkSts() == 6L) {
 
                        // 已经存在吊车执行任务时,则过滤
                        if (wrkMastMapper.selectWorking(slave.getId()) != null) {
                            continue;
                        }
 
                        // 堆垛机入库 命令下发区 --------------------------------------------------------------------------
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setCrnNo(slave.getId()); // 堆垛机编号
                        crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                        crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                        crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
                        crnCommand.setSourcePosX(crnStn.getRow().shortValue());     // 源库位排
                        crnCommand.setSourcePosY(crnStn.getBay().shortValue());     // 源库位列
                        crnCommand.setSourcePosZ(crnStn.getLev().shortValue());     // 源库位层
                        crnCommand.setDestinationPosX(locMast.getRow1().shortValue());     // 目标库位排
                        crnCommand.setDestinationPosY(locMast.getBay1().shortValue());     // 目标库位列
                        crnCommand.setDestinationPosZ(locMast.getLev1().shortValue());     // 目标库位层
                        if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                            log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                        } else {
                            // 修改工作档状态  7.吊车入库中
                            Date now = new Date();
                            wrkMast.setWrkSts(7L);
                            wrkMast.setCrnStrTime(now);
                            wrkMast.setModiTime(now);
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                log.error("修改工作档状态 {} => 7.吊车入库中 失败!!,工作号={}", wrkMast.getWrkSts$(), wrkMast.getWrkNo());
                            }
                            return true;
                        }
                    }
 
                }
 
            // 目标库位 ===>> 非最外层库位
            } else {
                // 判断小车是否在当前组库位,如果是则将穿梭车移至最外层,等待堆垛机放货;如果不是,则堆垛机寻找穿梭车,并放置当前组最外层
                Integer steNo = this.hasCarOfIdle(wrkMast.getLocNo());
                // 有小车
                if (steNo != null) {
                    // 小车行走到堆垛机待搬移点
                    if (wrkMast.getWrkSts() == 2L && wrkMast.getSteNo() == null) {
                        this.letCarBeReady(wrkMast, steNo, wrkMast.getLocNo());
                    }
                    // 堆垛机将货放至小车上 3.小车待搬(小车不用搬运,已经在当前组库位) / 6.小车待入  ===>> 7.吊车入库中
                    if (wrkMast.getWrkSts() == 3L || wrkMast.getWrkSts() == 6L) {
                        // 小车处于空闲
                        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
                        SteProtocol steProtocol = steThread.getSteProtocol();
                        if (steProtocol == null) { continue; }
                        if (steProtocol.isIdle()) {
 
                            // 已经存在吊车执行任务时,则过滤
                            if (wrkMastMapper.selectWorking(slave.getId()) != null) {
                                continue;
                            }
                            // 堆垛机入库 命令下发区 --------------------------------------------------------------------------
                            CrnCommand crnCommand = new CrnCommand();
                            crnCommand.setCrnNo(slave.getId()); // 堆垛机编号
                            crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                            crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                            crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
                            crnCommand.setSourcePosX(crnStn.getRow().shortValue());     // 源库位排
                            crnCommand.setSourcePosY(crnStn.getBay().shortValue());     // 源库位列
                            crnCommand.setSourcePosZ(crnStn.getLev().shortValue());     // 源库位层
                            crnCommand.setDestinationPosX(Utils.getGroupRow(locMast.getLocNo()).shortValue());     // 目标库位排
                            crnCommand.setDestinationPosY(locMast.getBay1().shortValue());     // 目标库位列
                            crnCommand.setDestinationPosZ(locMast.getLev1().shortValue());     // 目标库位层
                            if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                                log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                            } else {
                                // 修改工作档状态  3.小车待搬 ===>> 7.吊车入库中
                                Date now = new Date();
                                wrkMast.setWrkSts(7L);
                                wrkMast.setCrnStrTime(now);
                                wrkMast.setModiTime(now);
                                if (wrkMastMapper.updateById(wrkMast) == 0) {
                                    log.error("修改工作档状态 3.小车待搬/6.小车待入 => 7.吊车入库中 失败!!,工作号={}", wrkMast.getWrkNo());
                                }
                                return true;
                            }
                        }
                    }
                // 没有小车
                } else {
                    if (wrkMast.getWrkSts() == 2L && wrkMast.getSteNo() == null) {
                        // 寻找当前堆垛机对应的小车
                        SteThread steThread = queryIdleCar(wrkMast);
                        if (steThread != null) {
                            // 让小车等待搬运待续
                            this.letCarBeReady(wrkMast, steThread.getSlave().getId(), wrkMast.getLocNo());
                        }
                    }
                    // 堆垛机搬运小车
                    if (wrkMast.getWrkSts() == 3L) {
                        this.carMoveIn(wrkMast, wrkMast.getSteNo(), crnProtocol);
                    }
                }
            }
 
        }
        return false;
    }
 
    /**
     * 出库  ===>>  库位到堆垛机站
     */
    public boolean locToCrnStn(CrnSlave slave, CrnProtocol crnProtocol){
        for (CrnSlave.CrnStn crnStn : slave.getCrnOutStn()) {
            // 获取工作状态为11(生成出库ID)的出库工作档
            WrkMast wrkMast = wrkMastMapper.selectPakOutStep11(slave.getId(), crnStn.getStaNo());
            if (wrkMast == null) {
                continue;
            }
            // 工作档状态判断
            if (wrkMast.getIoType() < 100 || wrkMast.getSourceStaNo() == null){
                log.error("查询工作档数据不符合条件--入出类型/站点, 工作号={},源库位={},入出类型={}", wrkMast.getWrkNo(), wrkMast.getSourceLocNo(), wrkMast.getIoType());
                continue;
            }
            // 获取源库位信息
            LocMast sourceSta = locMastService.selectById(wrkMast.getSourceLocNo());
            if (!sourceSta.getLocSts().equals("R") &&!sourceSta.getLocSts().equals("P")) {
                log.error("出库操作库位状态不符合--状态, 库位号={},库位状态={}", wrkMast.getLocNo(), sourceSta.getLocSts());
                continue;
            }
            // 获取堆垛机出库站信息
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
            StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
            if (staProtocol == null) {
                continue;
            } else {
                staProtocol = staProtocol.clone();
            }
            // 查询站点详细信息
            BasDevp staDetl = basDevpService.selectById(crnStn.getStaNo());
            if (staDetl == null) {
                log.error("出库 ===>> 堆垛机站点在数据库不存在, 站点编号={}", crnStn.getStaNo());
                continue;
            }
            // 判断堆垛机出库站状态
            if (staProtocol.isAutoing() && !staProtocol.isLoading() && staDetl.getCanouting() !=null && staDetl.getCanouting().equals("Y")
                    && staProtocol.getWorkNo() == 0 && staProtocol.isOutEnable()) {
                // 命令下发区 --------------------------------------------------------------------------
 
                // 堆垛机控制过滤
                if (!crnProtocol.getStatusType().equals(CrnStatusType.IDLE) || crnProtocol.getTaskNo() != 0) {
                    continue;
                }
 
                // 最外层库位,直接堆垛机出库
                if (locMastService.isOutMost(wrkMast.getSourceLocNo())) {
                    // 已经存在吊车执行任务时,则过滤
                    if (wrkMastMapper.selectWorking(slave.getId()) != null) {
                        continue;
                    }
 
                    // 堆垛机出库 命令下发区 --------------------------------------------------------------------------
                    CrnCommand crnCommand = new CrnCommand();
                    crnCommand.setCrnNo(slave.getId()); // 堆垛机编号
                    crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                    crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                    crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
                    crnCommand.setSourcePosX(sourceSta.getRow1().shortValue());     // 源库位排
                    crnCommand.setSourcePosY(sourceSta.getBay1().shortValue());     // 源库位列
                    crnCommand.setSourcePosZ(sourceSta.getLev1().shortValue());     // 源库位层
                    crnCommand.setDestinationPosX(crnStn.getRow().shortValue());     // 目标库位排
                    crnCommand.setDestinationPosY(crnStn.getBay().shortValue());     // 目标库位列
                    crnCommand.setDestinationPosZ(crnStn.getLev().shortValue());     // 目标库位层
                    if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                        log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                    } else {
                        // 修改工作档状态 11.生成出库ID => 16.吊车出库中
                        Date now = new Date();
                        wrkMast.setWrkSts(16L);
                        wrkMast.setCrnStrTime(now);
                        wrkMast.setModiTime(now);
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            log.error("修改工作档状态 11.生成出库ID => 16.吊车出库中 失败!!,工作号={}", wrkMast.getWrkNo());
                        }
                        return true;
                    }
                // 不是最外层库位,需要使用穿梭车搬运后,再堆垛机出库
                } else {
                    // 当前组库位是否有穿梭车
                    Integer steNo = this.hasCarOfIdle(wrkMast.getSourceLocNo());
                    // 有小车
                    if (steNo != null) {
                        if (wrkMast.getWrkSts() == 11L && wrkMast.getSteNo() == null) {
 
                            // 给穿梭车下发命令 让其将货物挪至堆垛机搬运点
                            SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
                            SteProtocol steProtocol = steThread.getSteProtocol();
                            if (steProtocol == null) { continue; }
                            if (steProtocol.isIdle()) {
 
                                // 命令下发区 --------------------------------------------------------------------------
                                SteCommand steCommand = new SteCommand();
                                steCommand.setSteNo(steNo); // 穿梭车编号
                                steCommand.setTaskNo(wrkMast.getWrkNo()); // 工作号
                                steCommand.setTaskMode(SteTaskModeType.findOutByLoc(wrkMast.getSourceLocNo())); // 任务模式:  去近点 等待堆垛机叉取
 
                                if (!MessageQueue.offer(SlaveType.Ste, steNo, new Task(2, steCommand))) {
                                    log.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", steNo, JSON.toJSON(steCommand));
                                } else {
                                    // 修改工作档状态 11.生成出库ID => 14.小车搬出库
                                    Date now = new Date();
                                    wrkMast.setWrkSts(14L);
                                    wrkMast.setSteNo(steNo);
                                    wrkMast.setCrnStrTime(now);
                                    wrkMast.setModiTime(now);
                                    if (wrkMastMapper.updateById(wrkMast) == 0) {
                                        log.error("修改工作档状态 11.生成出库ID => 14.小车搬出库 失败!!,工作号={}", wrkMast.getWrkNo());
                                    }
                                }
                            }
                        }
                        if (wrkMast.getWrkSts() == 15L) {
                            // 已经存在吊车执行任务时,则过滤
                            if (wrkMastMapper.selectWorking(slave.getId()) != null) {
                                continue;
                            }
 
                            // 堆垛机出库 命令下发区 --------------------------------------------------------------------------
                            CrnCommand crnCommand = new CrnCommand();
                            crnCommand.setCrnNo(slave.getId()); // 堆垛机编号
                            crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                            crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                            crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
                            crnCommand.setSourcePosX(sourceSta.getRow1().shortValue());     // 源库位排
                            crnCommand.setSourcePosY(sourceSta.getBay1().shortValue());     // 源库位列
                            crnCommand.setSourcePosZ(sourceSta.getLev1().shortValue());     // 源库位层
                            crnCommand.setDestinationPosX(crnStn.getRow().shortValue());     // 目标库位排
                            crnCommand.setDestinationPosY(crnStn.getBay().shortValue());     // 目标库位列
                            crnCommand.setDestinationPosZ(crnStn.getLev().shortValue());     // 目标库位层
                            if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                                log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                            } else {
                                // 修改工作档状态 15.等待吊车 => 16.吊车出库中
                                Date now = new Date();
                                wrkMast.setWrkSts(16L);
                                wrkMast.setCrnStrTime(now);
                                wrkMast.setModiTime(now);
                                if (wrkMastMapper.updateById(wrkMast) == 0) {
                                    log.error("修改工作档状态 15.等待吊车 => 16.吊车出库中 失败!!,工作号={}", wrkMast.getWrkNo());
                                }
                                return true;
                            }
                        }
 
                    // 没有小车
                    } else {
                        if (wrkMast.getWrkSts() == 11L && wrkMast.getSteNo() == null) {
                            // 寻找最近的小车
                            SteThread steThread = queryIdleCar(wrkMast);
                            if (steThread != null) {
                                // 让小车等待搬运待续
                                this.letCarBeReady(wrkMast, steThread.getSlave().getId(), wrkMast.getSourceLocNo());
                            }
                        }
                        // 堆垛机搬运小车
                        if (wrkMast.getWrkSts() == 12L) {
                            this.carMoveIn(wrkMast, wrkMast.getSteNo(), crnProtocol);
                        }
                    }
                }
 
            }
        }
        return false;
    }
 
    /**
     * 当前库位组是否存在空闲小车
     */
    public Integer hasCarOfIdle(String locNo) {
        for (SteSlave ste : slaveProperties.getSte()) {
            // 获取堆垛机信息
            SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, ste.getId());
            SteProtocol steProtocol = steThread.getSteProtocol();
            if (steProtocol == null) { continue; }
            if (steProtocol.isIdle()) {
                if (Utils.getGroupRow(locNo).equals(Utils.getGroupRow(steProtocol.getRow().intValue())) && steProtocol.getBay() == Utils.getBay(locNo) && steProtocol.getLev() == Utils.getLev(locNo)) {
                    return steProtocol.getSteNo().intValue();
                }
            }
        }
        return null;
    }
 
    /**
     * 查找当前库位最适合的穿梭车来作业
     */
    public SteThread queryIdleCar(WrkMast wrkMast) {
        Integer crnNo = wrkMast.getCrnNo();
        BasSte basSte = basSteService.findByCrnNo(crnNo);
        // 获取穿梭车信息
        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, basSte.getSteNo());
        SteProtocol steProtocol = steThread.getSteProtocol();
        if (steProtocol != null) {
            if (steProtocol.isIdle()) {
                return steThread;
            }
        }
        return null;
    }
 
    /**
     * 让小车从 远点 ====>> 移动到近点
     *  等待堆垛机搬运
     */
    public void letCarBeReady(WrkMast wrkMast, Integer steNo, String locNo) {
        // 获取穿梭车信息
        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
        SteProtocol steProtocol = steThread.getSteProtocol();
        if (steProtocol == null) { return; }
        if (steProtocol.isIdle()) {
 
            // 命令下发区 --------------------------------------------------------------------------
            SteCommand steCommand = new SteCommand();
            steCommand.setSteNo(steNo); // 穿梭车编号
            steCommand.setTaskNo(wrkMast.getWrkNo()); // 工作号
            steCommand.setTaskMode(SteTaskModeType.findOriginByLoc(steProtocol)); // 任务模式:  去近点 等待堆垛机叉取
 
            steCommand.setRow(Utils.getGroupRow(steProtocol.getRow().intValue()).shortValue());
            steCommand.setBay(steProtocol.getBay());
            steCommand.setLev(steProtocol.getLev());
 
            if (!MessageQueue.offer(SlaveType.Ste, steNo, new Task(2, steCommand))) {
                log.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", steNo, JSON.toJSON(steCommand));
            } else {
                // 修改工作档状态 绑定穿梭车
                wrkMast.setSteNo(steNo);
                wrkMast.setModiTime(new Date());
                if (wrkMastMapper.updateById(wrkMast) == 0) {
                    log.error("修改工作档状态 绑定穿梭车 失败!!,工作号={}", wrkMast.getWrkNo());
                }
            }
        }
    }
 
    /**
     * 入出库  ===>>  堆垛机搬入小车
     */
    public void carMoveIn(WrkMast wrkMast, Integer steNo, CrnProtocol crnProtocol) {
        // 获取堆垛机信息
        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
        SteProtocol steProtocol = steThread.getSteProtocol();
        if (steProtocol == null) { return; }
        if (steProtocol.isIdle()) {
            // 堆垛机空闲
            if (crnProtocol.getStatusType().equals(CrnStatusType.IDLE) || crnProtocol.getTaskNo() == 0) {
                LocMast locMast;
                // 入库搬
                if (wrkMast.getWrkSts() <= 10) {
                    locMast = locMastService.selectById(wrkMast.getLocNo());
                    // 堆垛机命令下发区 --------------------------------------------------------------------------
                    CrnCommand crnCommand = new CrnCommand();
                    crnCommand.setCrnNo(crnProtocol.getCrnNo()); // 堆垛机编号
                    crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                    crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                    crnCommand.setTaskMode(CrnTaskModeType.STE_MOVE); // 任务模式:  库位移转
                    crnCommand.setSourcePosX(steProtocol.getRow());     // 源库位排
                    crnCommand.setSourcePosY(steProtocol.getBay());     // 源库位列
                    crnCommand.setSourcePosZ(steProtocol.getLev());     // 源库位层
                    crnCommand.setDestinationPosX(Utils.getGroupRow(locMast.getLocNo()).shortValue());     // 目标库位排
                    crnCommand.setDestinationPosY(locMast.getBay1().shortValue());     // 目标库位列
                    crnCommand.setDestinationPosZ(locMast.getLev1().shortValue());     // 目标库位层
                    if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                        log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                    } else {
                        // 修改穿梭车运行中排列层
                        steThread.modifyPos(Utils.getGroupRow(locMast.getLocNo()), locMast.getBay1(), locMast.getLev1());
                        // 修改工作档状态 3.小车待搬 => 4.迁入小车
                        Date now = new Date();
                        wrkMast.setWrkSts(4L);
                        wrkMast.setCrnStrTime(now);
                        wrkMast.setModiTime(now);
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            log.error("修改工作档状态 3.小车待搬 => 4.迁入小车 失败!!,工作号={}", wrkMast.getWrkNo());
                        }
                    }
                // 出库搬
                } else {
                    locMast = locMastService.selectById(wrkMast.getSourceLocNo());
                    // 堆垛机命令下发区 --------------------------------------------------------------------------
                    CrnCommand crnCommand = new CrnCommand();
                    crnCommand.setCrnNo(crnProtocol.getCrnNo()); // 堆垛机编号
                    crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                    crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                    crnCommand.setTaskMode(CrnTaskModeType.STE_MOVE); // 任务模式:  库位移转
                    crnCommand.setSourcePosX(steProtocol.getRow());     // 源库位排
                    crnCommand.setSourcePosY(steProtocol.getBay());     // 源库位列
                    crnCommand.setSourcePosZ(steProtocol.getLev());     // 源库位层
                    crnCommand.setDestinationPosX(Utils.getGroupRow(locMast.getLocNo()).shortValue());     // 目标库位排
                    crnCommand.setDestinationPosY(locMast.getBay1().shortValue());     // 目标库位列
                    crnCommand.setDestinationPosZ(locMast.getLev1().shortValue());     // 目标库位层
                    if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                        log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                    } else {
                        // 修改穿梭车运行中排列层
                        steThread.modifyPos(Utils.getGroupRow(locMast.getLocNo()), locMast.getBay1(), locMast.getLev1());
                        // 修改工作档状态 12.小车待搬 => 13.迁入小车
                        Date now = new Date();
                        wrkMast.setWrkSts(13L);
                        wrkMast.setCrnStrTime(now);
                        wrkMast.setModiTime(now);
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            log.error("修改工作档状态 12.小车待搬 => 13.迁入小车 失败!!,工作号={}", wrkMast.getWrkNo());
                        }
                    }
                }
            }
 
        }
    }
 
    /**
     * 入库  ===>>  堆垛机搬出小车
     */
    public void carMoveOut(WrkMast wrkMast, Integer steNo, CrnProtocol crnProtocol) {
        // 获取穿梭车信息
        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
        SteProtocol steProtocol = steThread.getSteProtocol();
        if (steProtocol == null) { return; }
        // 穿梭车空闲
        if (steProtocol.isIdle()) {
            // 堆垛机空闲
            if (crnProtocol.getStatusType().equals(CrnStatusType.IDLE) || crnProtocol.getTaskNo() == 0) {
 
                BasSte basSte = basSteService.selectById(steNo);
                if (basSte == null) {
                    log.error("{}号穿梭车在数据库不存在!!!", steNo);
                    return;
                }
                String idleLocNo = basSte.getIdleLoc();
 
                // 堆垛机命令下发区 --------------------------------------------------------------------------
                CrnCommand crnCommand = new CrnCommand();
                crnCommand.setCrnNo(crnProtocol.getCrnNo()); // 堆垛机编号
                crnCommand.setTaskNo(wrkMast.getWrkNo().shortValue()); // 工作号
                crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
                crnCommand.setSourcePosX(steProtocol.getRow());     // 源库位排
                crnCommand.setSourcePosY(steProtocol.getBay());     // 源库位列
                crnCommand.setSourcePosZ(steProtocol.getLev());     // 源库位层
                crnCommand.setDestinationPosX(Utils.getGroupRow(idleLocNo).shortValue());     // 目标库位排
                crnCommand.setDestinationPosY((short) Utils.getBay(idleLocNo));     // 目标库位列
                crnCommand.setDestinationPosZ((short) Utils.getLev(idleLocNo));     // 目标库位层
                if (!MessageQueue.offer(SlaveType.Crn, wrkMast.getCrnNo(), new Task(2, crnCommand))) {
                    log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", wrkMast.getCrnNo(), JSON.toJSON(crnCommand));
                } else {
                    // 修改穿梭车运行中排列层
                    steThread.modifyPos(Utils.getGroupRow(idleLocNo), Utils.getBay(idleLocNo), Utils.getLev(idleLocNo));
                    // 修改工作档状态 3.小车待搬 => 5.迁出小车
                    Date now = new Date();
                    wrkMast.setWrkSts(5L);
                    wrkMast.setCrnStrTime(now);
                    wrkMast.setModiTime(now);
                    if (wrkMastMapper.updateById(wrkMast) == 0) {
                        log.error("修改工作档状态 3.小车待搬 => 5.迁出小车 失败!!,工作号={}", wrkMast.getWrkNo());
                    }
                }
            }
        }
    }
 
 
    /**
     * 执行对工作档的完成操作
     */
    @Async
    @Transactional
    public void storeFinished() {
        for (CrnSlave crn : slaveProperties.getCrn()) {
            // 获取堆垛机信息
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
            if (crnProtocol == null) { continue; }
            //  状态:等待确认 并且  任务完成位 = 1
            if (crnProtocol.statusType == CrnStatusType.WAITING && crnProtocol.getTaskNo() != 0) {
                // 获取入库待确认工作档
                WrkMast wrkMast = wrkMastMapper.selectCrnWaiting(crnProtocol.getTaskNo().intValue());
                if (wrkMast == null) {
                    log.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                    continue;
                }
                // 入库 ==>> 货物搬入库
                if (wrkMast.getWrkSts() == 7){
                    // 判断是否需要小车入库
                    if (locMastService.isOutMost(wrkMast.getLocNo())) {
                        // ==> 9.入库完成
                        wrkMast.setWrkSts(9L);
                        Date now = new Date();
                        wrkMast.setCrnEndTime(now);
                        wrkMast.setModiTime(now);
                        // 修改成功后复位堆垛机
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            // 堆垛机复位
                            crnThread.setResetFlag(true);
                        } else {
                            log.error("修改工作档状态 7.吊车入库中 => 9.入库完成 失败!!,工作号={}", wrkMast.getWrkNo());
                        }
                    } else {
                        // 给穿梭车下发命令
                        Integer steNo = wrkMast.getSteNo();
                        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
                        SteProtocol steProtocol = steThread.getSteProtocol();
                        if (steProtocol == null) { continue; }
                        if (steProtocol.isIdle()) {
                            // 命令下发区 --------------------------------------------------------------------------
                            SteCommand steCommand = new SteCommand();
                            steCommand.setSteNo(steNo); // 穿梭车编号
                            steCommand.setTaskNo(wrkMast.getWrkNo()); // 工作号
                            steCommand.setTaskMode(SteTaskModeType.findInByLoc(wrkMast.getLocNo())); // 任务模式: 搬入库
                            if (!MessageQueue.offer(SlaveType.Ste, steNo, new Task(2, steCommand))) {
                                log.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", steNo, JSON.toJSON(steCommand));
                            } else {
                                // 修改工作档状态 7.吊车入库中 => 8.小车搬入库
                                wrkMast.setWrkSts(8L);
                                Date now = new Date();
                                wrkMast.setCrnEndTime(now);
                                wrkMast.setModiTime(now);
                                if (wrkMastMapper.updateById(wrkMast) > 0) {
                                    // 堆垛机复位
                                    crnThread.setResetFlag(true);
                                } else {
                                    log.error("修改工作档状态 7.吊车入库中 => 8.小车搬入库 失败!!,工作号={}", wrkMast.getWrkNo());
                                }
                            }
                        }
                    }
                // 入库 ===>> 迁入小车 完成
                } else if (wrkMast.getWrkSts() == 4) {
                    // 4.迁入小车 ==> 6.小车待入
                    wrkMast.setWrkSts(6L);
                    Date now = new Date();
                    wrkMast.setCrnEndTime(now);
                    wrkMast.setModiTime(now);
                    // 修改成功后复位堆垛机
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        // 堆垛机复位
                        crnThread.setResetFlag(true);
                        // 穿梭车重新定位排列层
                        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, wrkMast.getSteNo());
                        if (!steThread.confirmPos()) {
                            log.error("{}号穿梭车重新定位失败!作业工作档任务号:{}", wrkMast.getSteNo(), wrkMast.getWrkNo());
                        }
                        // 穿梭车去待机位
                        BasSte basSte = basSteService.selectById(wrkMast.getSteNo());
                        SteCommand steCommand = new SteCommand();
                        steCommand.setSteNo(wrkMast.getSteNo()); // 穿梭车编号
                        steCommand.setTaskNo(9999); // 工作号
                        steCommand.setTaskMode(SteTaskModeType.findWaiting(basSte.getRow())); // 任务模式:  去近点 等待堆垛机叉取
//                        steCommand.setRow(Utils.getGroupRow(steProtocol.getRow().intValue()).shortValue());
//                        steCommand.setBay(steProtocol.getBay());
//                        steCommand.setLev(steProtocol.getLev());
                        if (!MessageQueue.offer(SlaveType.Ste, wrkMast.getSteNo(), new Task(2, steCommand))) {
                            log.error("穿梭车待机位命令下发失败,穿梭车号={},任务数据={}", wrkMast.getSteNo(), JSON.toJSON(steCommand));
                        }
                    } else {
                        log.error("修改工作档状态 4.迁入小车 => 6.小车待入 失败!!,工作号={}", wrkMast.getWrkNo());
                    }
                // 入库 ===>> 迁出小车 完成
                } else if (wrkMast.getWrkSts() == 5) {
                    // 5.迁出小车 ==> 6.小车待入
                    wrkMast.setWrkSts(6L);
                    Date now = new Date();
                    wrkMast.setCrnEndTime(now);
                    wrkMast.setModiTime(now);
                    // 修改成功后复位堆垛机
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        // 堆垛机复位
                        crnThread.setResetFlag(true);
                        // 穿梭车重新定位排列层
                        SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, wrkMast.getSteNo());
                        if (!steThread.confirmPos()) {
                            log.error("{}号穿梭车重新定位失败!作业工作档任务号:{}", wrkMast.getSteNo(), wrkMast.getWrkNo());
                        }
                    } else {
                        log.error("修改工作档状态 5.迁出小车 => 6.小车待入 失败!!,工作号={}", wrkMast.getWrkNo());
                    }
                // 出库 ===>> 迁入小车 完成
                } else if (wrkMast.getWrkSts() == 13) {
                    // 给穿梭车下发命令
                    Integer steNo = wrkMast.getSteNo();
                    SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, steNo);
                    SteProtocol steProtocol = steThread.getSteProtocol();
                    if (steProtocol == null) { continue; }
                    if (steProtocol.isIdle()) {
                        // 穿梭车重新定位排列层
                        if (!steThread.confirmPos()) {
                            log.error("{}号穿梭车重新定位失败!作业工作档任务号:{}", wrkMast.getSteNo(), wrkMast.getWrkNo());
                        }
                        // 命令下发区 --------------------------------------------------------------------------
                        SteCommand steCommand = new SteCommand();
                        steCommand.setSteNo(steNo); // 穿梭车编号
                        steCommand.setTaskNo(wrkMast.getWrkNo()); // 工作号
                        steCommand.setTaskMode(SteTaskModeType.findOutByLoc(wrkMast.getSourceLocNo())); // 任务模式:  搬出库
                        if (!MessageQueue.offer(SlaveType.Ste, steNo, new Task(2, steCommand))) {
                            log.error("穿梭车命令下发失败,穿梭车号={},任务数据={}", steNo, JSON.toJSON(steCommand));
                        } else {
                            // 13.迁入小车 ==> 14.小车搬出库
                            wrkMast.setWrkSts(14L);
                            Date now = new Date();
                            wrkMast.setCrnEndTime(now);
                            wrkMast.setModiTime(now);
                            // 修改成功后复位堆垛机
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                // 堆垛机复位
                                crnThread.setResetFlag(true);
                            } else {
                                log.error("修改工作档状态 13.迁入小车 ==> 14.小车搬出库 失败!!,工作号={}", wrkMast.getWrkNo());
                            }
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 执行对工作档的完成操作
     */
    public void carGenerateStore() {
        Date now = new Date();
        for (SteSlave ste : slaveProperties.getSte()) {
            // 获取穿梭车信息
            SteThread steThread = (SteThread) SlaveConnection.get(SlaveType.Ste, ste.getId());
            SteProtocol steProtocol = steThread.getSteProtocol();
            if (steProtocol == null) { continue; }
            if (steProtocol.getWaiting() && steProtocol.getTaskNo() != 0) {
                if (steProtocol.getTaskNo() == 9999) {
                    steThread.setResetFlag(true);
                } else {
                    // 查询是否有待入库的任务
                    WrkMast wrkMast = wrkMastMapper.selectCarWaiting(steProtocol.getTaskNo().intValue());
                    if (wrkMast == null) { continue; }
 
                    switch (wrkMast.getWrkSts().intValue()) {
                        case 2:
                            // 修改工作档状态 2.设备上走 => 3.小车待搬
                            wrkMast.setWrkSts(3L);
                            wrkMast.setModiTime(now);
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                log.error("修改工作档状态 2.设备上走 => 3.小车待搬 失败!!,工作号={}", wrkMast.getWrkNo());
                            } else {
                                steThread.setResetFlag(true);
                            }
                            break;
                        case 8:
                            // 修改工作档状态 8.小车搬入库 => 9.入库完成
                            wrkMast.setWrkSts(9L);
                            wrkMast.setModiTime(now);
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                log.error("修改工作档状态 8.小车搬入库 => 9.入库完成 失败!!,工作号={}", wrkMast.getWrkNo());
                            } else {
                                steThread.setResetFlag(true);
                            }
                            break;
                        case 11:
                            // 修改工作档状态 11.生成出库ID => 12.小车待搬
                            wrkMast.setWrkSts(12L);
                            wrkMast.setModiTime(now);
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                log.error("修改工作档状态 11.生成出库ID => 12.小车待搬 失败!!,工作号={}", wrkMast.getWrkNo());
                            } else {
                                steThread.setResetFlag(true);
                            }
                            break;
                        case 14:
                            // 修改工作档状态 14.小车搬出库 => 15.等待吊车
                            wrkMast.setWrkSts(15L);
                            wrkMast.setModiTime(now);
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                log.error("修改工作档状态 14.小车搬出库 => 15.等待吊车 失败!!,工作号={}", wrkMast.getWrkNo());
                            } else {
                                steThread.setResetFlag(true);
                            }
                            break;
                    }
                }
            }
        }
    }
 
    /**
     * 堆垛机异常信息记录
     */
    @Async
    public void recCrnErr(){
        Date now = new Date();
        for (CrnSlave crn : slaveProperties.getCrn()) {
            // 获取堆垛机信息
            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
            if (crnProtocol == null) {
                continue;
            }
            if (true) {
//            if (crnProtocol.getModeType() != CrnModeType.STOP) {
                // 有任务
                if (crnProtocol.getTaskNo() != 0) {
                    BasErrLog latest = basErrLogService.findLatestByTaskNo(crn.getId(), crnProtocol.getTaskNo().intValue());
                    // 有异常
                    if (latest == null) {
                        if (crnProtocol.getAlarm() != null && crnProtocol.getAlarm() > 0) {
                            WrkMast wrkMast = wrkMastMapper.selectById(crnProtocol.getTaskNo());
                            if (wrkMast == null) {
                                continue;
                            }
                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm());
                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm()):crnError.getErrName();
                            BasErrLog basErrLog = new BasErrLog(
                                    null,    // 编号
                                    wrkMast.getWrkNo(),    // 工作号
                                    now,    // 发生时间
                                    null,    // 结束时间
                                    wrkMast.getWrkSts(),    // 工作状态
                                    wrkMast.getIoType(),    // 入出库类型
                                    crn.getId(),    // 堆垛机
                                    null,    // plc
                                    wrkMast.getLocNo(),    // 目标库位
                                    wrkMast.getStaNo(),    // 目标站
                                    wrkMast.getSourceStaNo(),    // 源站
                                    wrkMast.getSourceLocNo(),    // 源库位
                                    wrkMast.getBarcode(),    // 条码
                                    (int) crnProtocol.getAlarm(),    // 异常码
                                    errName,    // 异常
                                    1,    // 异常情况
                                    now,    // 添加时间
                                    null,    // 添加人员
                                    now,    // 修改时间
                                    null,    // 修改人员
                                    "任务中异常"    // 备注
                            );
                            if (!basErrLogService.insert(basErrLog)) {
                                log.error("堆垛机plc异常记录失败 ===>> [id:{}] [error:{}]", crn.getId(), errName);
                            }
                        }
                    } else {
                        // 异常修复
                        if (crnProtocol.getAlarm() == null || crnProtocol.getAlarm() == 0) {
                            latest.setEndTime(now);
                            latest.setUpdateTime(now);
                            latest.setStatus(2);
                            if (!basErrLogService.updateById(latest)) {
                                log.error("堆垛机plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", crn.getId(), latest.getId());
                            }
                        }
                    }
                // 无任务
                } else {
                    BasErrLog latest = basErrLogService.findLatest(crn.getId());
                    // 有异常
                    if (crnProtocol.getAlarm() != null && crnProtocol.getAlarm() > 0) {
                        // 记录新异常
                        if (latest == null || (latest.getErrCode() != crnProtocol.getAlarm().intValue())) {
                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm());
                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm()):crnError.getErrName();
                            BasErrLog basErrLog = new BasErrLog(
                                    null,    // 编号
                                    null,    // 工作号
                                    now,    // 发生时间
                                    null,    // 结束时间
                                    null,    // 工作状态
                                    null,    // 入出库类型
                                    crn.getId(),    // 堆垛机
                                    null,    // plc
                                    null,    // 目标库位
                                    null,    // 目标站
                                    null,    // 源站
                                    null,    // 源库位
                                    null,    // 条码
                                    (int)crnProtocol.getAlarm(),    // 异常码
                                    errName,    // 异常
                                    1,    // 异常情况
                                    now,    // 添加时间
                                    null,    // 添加人员
                                    now,    // 修改时间
                                    null,    // 修改人员
                                    "无任务异常"    // 备注
                            );
                            if (!basErrLogService.insert(basErrLog)) {
                                log.error("堆垛机plc异常记录失败 ===>> [id:{}] [error:{}]", crn.getId(), errName);
                            }
                        }
                    // 无异常
                    } else {
                        // 异常修复
                        if (latest != null && latest.getStatus() == 1) {
                            latest.setEndTime(now);
                            latest.setUpdateTime(now);
                            latest.setStatus(2);
                            if (!basErrLogService.updateById(latest)) {
                                log.error("堆垛机plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", crn.getId(), latest.getId());
                            }
                        }
                    }
                }
            }
 
        }
    }
 
 
    // -------------------------------------------------------------------------------
 
    /**
     * 空栈板初始化入库,叉车入库站放货
     */
    @Async
    public void storeEmptyPlt(){
        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() == 9999 || staProtocol.getWorkNo() == 9990 || staProtocol.getWorkNo() == 9997) && staProtocol.isPakMk()) {
 
                    try {
                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
 
                        SearchLocParam param = new SearchLocParam();
                        param.setIoType(10);
                        param.setSourceStaNo(emptyInSta.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);
                        if (jsonObject.getInteger("code").equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
 
                            // 更新站点信息 且 下发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 {
                            log.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl+"/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
 
                }
 
 
            }
        }
    }
 
    /**
     * 出库  ===>> 工作档信息写入led显示器
     */
    @Async
    public void ledExecute() {
        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<>();
            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());
                if (null == wrkMast || wrkMast.getWrkSts() < 14 || wrkMast.getIoType() < 100) { continue; }
                wrkMasts.add(wrkMast);
                // 组装命令
                LedCommand ledCommand = new LedCommand();
                ledCommand.setWorkNo(wrkMast.getWrkNo());
                // 出库模式
                switch (wrkMast.getIoType()) {
                    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:
                        log.error("任务入出库类型错误!!![工作号:{}] [入出库类型:{}]", wrkMast.getWrkNo(), wrkMast.getIoType());
                        break;
                }
                ledCommand.setSourceLocNo(wrkMast.getSourceLocNo());
                ledCommand.setStaNo(wrkMast.getStaNo());
                if (wrkMast.getIoType() != 110) {
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    wrkDetls.forEach(wrkDetl -> ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme())));
                }
                commands.add(ledCommand);
            }
            Set<Integer> workNos = wrkMasts.stream().map(WrkMast::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();
            }
 
        }
    }
 
    /**
     * 其他  ===>> LED显示器复位,显示默认信息
     */
    @Async
    public void ledReset() {
        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) {
                    reset = false;
                    break;
                }
            }
            // 获取led线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getDevpPlcId());
            // led显示默认内容
            if (reset && !ledThread.isLedMk()) {
                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());
                } else {
 
                }
            }
        }
    }
 
 
 
 
 
}