#
Junjie
2025-11-17 494ddc6f912ee454ce913cb9f1c99ebf6f85672a
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
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.DateUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.BasCrnErrorMapper;
import com.zy.asrs.mapper.WaitPakinMapper;
import com.zy.asrs.mapper.WrkMastMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.RedisConstantType;
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.service.CommonService;
import com.zy.common.utils.CollectionUtils;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.News;
import com.zy.common.utils.RedisUtil;
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.RgvCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.RgvProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.BarcodeThread;
import com.zy.core.thread.LedThread;
import com.zy.core.thread.RgvThread;
import com.zy.core.thread.SiemensDevpThread;
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 {
 
    public static final long COMMAND_TIMEOUT = 5 * 1000;
 
    @Autowired
    private CommonService commonService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private WrkMastMapper wrkMastMapper;
    @Autowired
    private WrkMastLogService wrkMastLogService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private WaitPakinMapper waitPakinMapper;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private BasErrLogService basErrLogService;
    @Autowired
    private BasCrnErrorMapper basCrnErrorMapper;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private RedisUtil redisUtil;
 
    @Value("${wms.url}")
    private String wmsUrl;
 
    /**
     * 组托
     * 入库站,根据条码扫描生成入库工作档,工作状态 2
     */
    public synchronized void generateStoreWrkFile(Integer mark) {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取条码扫描仪信息
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                if (barcodeThread == null) {
                    continue;
                }
 
                // 获取入库站信息
                SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
 
                //LED
                LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
 
                // 判断是否满足入库条件
                if (!staProtocol.isLoading()) {
                    continue;
                }
 
                if (staProtocol.isAutoing()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk()
                        && staProtocol.getWorkNo() == 9999
                        && staProtocol.isPakMk() && (staProtocol.getEmptyInType() != 1 && staProtocol.getEmptyInType() != 2)) {
                    News.warnNoLog("" + mark + " - 0" + " - 开始执行");
 
                    String barcode = barcodeThread.getBarcode();
                    if (!Cools.isEmpty(barcode)) {
                        News.info("" + mark + " - 1" + " - {}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                        if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
//                            staProtocol.setWorkNo((short) 9992);
//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            News.info("" + mark + " - 2" + " - 扫码失败1 ===>> {}号条码扫描器检测条码信息:{},站点:{}", inSta.getBarcode(), barcode, inSta.getStaNo());
                            // led 异常显示
                            if (ledThread != null) {
                                String errorMsg = "扫码失败,请重试";
                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(5, errorMsg));
                            }
                            continue;
                        }
                    } else {
//                        staProtocol.setWorkNo((short) 9992);
//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                        News.info("" + mark + " - 3" + " - 扫码失败2 ===>> {}号条码扫描器检测条码信息:{},站点:{}", inSta.getBarcode(), barcode, inSta.getStaNo());
                        // led 异常显示
                        if (ledThread != null) {
                            String errorMsg = "扫码失败,请重试";
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(5, errorMsg));
                        }
                        continue;
                    }
 
                    // 判断重复工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakInStep1(inSta.getStaNo(), barcode);
                    //过滤判断,防止拣料再入库货物,经过入库站再入库时,被退回到退库站
                    WrkMast wrkMast1 = wrkMastMapper.selectPakInStepBarcode(barcode);
                    if (wrkMast1 != null) {
                        if (wrkMast1.getIoType() == 103 || wrkMast1.getIoType() == 107 || wrkMast1.getIoType() == 104) {
                            continue;
                        }
                    }
                    if (wrkMast != null) {
                        News.error("" + mark + " - 4" + " - 工作档中已存在该站状态为( 2.设备上走 )的数据,工作号={}", wrkMast.getWrkNo());
//                        staProtocol.setWorkNo((short)9992);
//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                        if (!result) {
//                            throw new CoolException("更新plc站点信息失败");
//                        }
 
                        // led 异常显示
                        if (ledThread != null) {
                            String errorMsg = "工作档已存在该条码号===>>" + barcode;
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(5, errorMsg));
                        }
                        continue;
                    }
 
                    if (inSta.getStaNo() == 1002 || inSta.getStaNo() == 1007) {
                        //检测是否有出库任务
                        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                                .in("io_type", 101, 103, 107)
                                .in("sta_no", 1003, 1004)
                        );
                        if (!wrkMasts.isEmpty()) {
                            News.error("" + mark + " - 4" + " - 入库,检测存在出库任务,等待出库任务执行完成,托盘码={}", barcode);
                            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);
 
                            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                                    .eq("loc_no", dto.getLocNo()));
 
                            short staNo = dto.getStaNo().shortValue();
 
                            int sourceStaNo = dto.getSourceStaNo();
                            if (sourceStaNo == 1007) {
                                staNo = 2002;
                            }
 
                            if (inSta.getStaNo() == 1002) {
                                staNo = 2002;
                            }
 
                            barcodeThread.setBarcode("");
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(staNo);
                            staProtocol.setPalletSize(locMast.getLocType2());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
 
//                            // led 入库信息显示
//                            if (ledThread != null) {
//                                // 命令集合
//                                List<LedCommand> commands = new ArrayList<>();
//                                // 组装命令
//                                LedCommand ledCommand = new LedCommand();
//                                ledCommand.setWorkNo(dto.getWorkNo());
//                                ledCommand.setIoType(1);
//                                ledCommand.setTitle("全板入库");
//                                ledCommand.setLocNo(dto.getLocNo());
//                                ledCommand.setStaNo(dto.getStaNo());
//                                commands.add(ledCommand);
//                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, commands));
////                                ledThread.errorReset();
//                            }
                        } else {
                            News.error("" + mark + " - 5" + " - 请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
//                            staProtocol.setWorkNo((short)9992);
//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                throw new CoolException("更新plc站点信息失败");
//                            }
 
                            // led 异常显示
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(5, errorMsg));
                            }
                        }
 
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
 
                } else {
                    News.errorNoLog("" + mark + " - 6" + " - 站点信息不符合入库条件!!!" + " 自动信号:" + staProtocol.isAutoing() + "、可入信号:" + staProtocol.isInEnable()
                            + "、空板信号:" + staProtocol.isEmptyMk() + "、工作号:" + staProtocol.getWorkNo()
                            + "、锁定标记" + staProtocol.isPakMk());
                }
 
            }
        }
        News.infoNoLog("" + mark + " - 0" + " - 组托  ===》执行完成");
 
    }
 
    /**
     * 拣料、并板、盘点再入库
     */
    public synchronized void stnToCrnStnPick(Integer mark) {
        try {
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历拣料入库口
                for (DevpSlave.Sta pickSta : devp.getPickSta()) {
                    // 获取拣料入库站信息
                    SiemensDevpThread devpThread = (SiemensDevpThread) 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()) {
                        News.warnNoLog("" + mark + " - 0" + " - 开始执行");
 
                        // 获取条码扫描仪信息
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, pickSta.getBarcode());
                        if (barcodeThread == null) {
                            continue;
                        }
                        String barcode = barcodeThread.getBarcode();
                        if (!Cools.isEmpty(barcode)) {
                            News.info("" + mark + " - 1" + " - {}号条码扫描器检测条码信息:{}", pickSta.getBarcode(), barcode);
                            if ("NG".endsWith(barcode) || "NoRead".equals(barcode)) {
                                continue;
                            }
                        } else {
                            continue;
                        }
 
                    WrkMast wrkMast = wrkMastMapper.selectPickStep(barcode);
//                        WrkMast wrkMast = wrkMastMapper.selectPakInStep3(staProtocol.getWorkNo().intValue());
//                        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)) {
//                        News.error(""+mark+" - 2"+" - 入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
//                        staProtocol.setWorkNo((short) 9989);
//                        staProtocol.setStaNo((short) (pickSta.getStaNo().shortValue()-(short)1));
//                        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());
                            // led 异常显示
                            if (ledThread != null) {
                                String errorMsg = "此为拣料、并板、盘点再入库.请放在" + pickSta.getBackSta().shortValue() + "站点";
                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(5, errorMsg));
                            }
                            continue;
                        }
 
                        try {
                            LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
 
                            SearchLocParam param = new SearchLocParam();
                            param.setBarcode(wrkMast.getBarcode());
                            param.setSourceStaNo(pickSta.getStaNo());
                            param.setLocType1(locTypeDto.getLocType1());
                            String response = new HttpHandler.Builder()
                                    .setUri(wmsUrl)
                                    .setPath("/rpc/pakin/pick/loc/v1")
                                    .setJson(JSON.toJSONString(param))
                                    .build()
                                    .doPost();
                            JSONObject jsonObject = JSON.parseObject(response);
                            if (jsonObject.getInteger("code").equals(200)) {
                                WrkMast newWrkMast = wrkMastMapper.selectPakInStepBarcode(wrkMast.getBarcode());
                                if (newWrkMast == null) {
                                    // 无拣料数据
                                    News.error("" + mark + " - 3" + " - 拣选盘点回库获取工作档失败!!! [plc编号:{}]", devp.getId());
                                    continue;
                                }
 
                                if(newWrkMast.getWrkSts() != 53 && newWrkMast.getWrkSts() != 57) {
                                    News.error("" + mark + " - 3" + " - 拣选盘点回库工作档类型异常!!! [plc编号:{}]", devp.getId());
                                    continue;
                                }
 
                                if (wrkMast.getWrkSts() != 2) {
                                    News.error("" + mark + " - 3" + " - 拣选盘点回库工作档状态异常!!! [plc编号:{}]", devp.getId());
                                    continue;
                                }
 
                                LocMast locMast = locMastService.selectById(newWrkMast.getLocNo());
                                // 更新站点信息 且 下发plc命令
                                staProtocol.setWorkNo(newWrkMast.getWrkNo().shortValue());
                                staProtocol.setStaNo(newWrkMast.getStaNo().shortValue());
                                staProtocol.setPalletSize(locMast.getLocType2());
                                devpThread.setPakMk(staProtocol.getSiteId(), false);
                                boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                                if (!result) {
                                    News.error("" + mark + " - 3" + " - 发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                                }
                            } else {
                                News.error("" + mark + " - 5" + " - 请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/pick/loc/v1", JSON.toJSONString(param), response);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
 
                    } else {
                        News.errorNoLog("" + mark + " - 6" + " - 站点信息不符合入库条件!!!" + " 自动信号:" + staProtocol.isLoading() + "、可入信号:" + staProtocol.isInEnable()
                                + "、空板信号:" + staProtocol.isEmptyMk());
                    }
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        News.infoNoLog("" + mark + " - 0" + " - 拣料、并板、盘点再入库  ===》执行完成");
    }
 
    /**
     * 堆垛机异常信息记录
     */
    public synchronized void recCrnErr(Integer mark) {
        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) {
                        News.warnNoLog("" + mark + " - 1" + " - 开始执行:堆垛机异常信息记录 : 有任务,有异常");
                        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)) {
                                News.error("" + mark + " - 2" + " - 堆垛机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)) {
                                News.error("" + mark + " - 3" + " - 堆垛机plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", crn.getId(), latest.getId());
                            }
                        }
                    }
                    // 无任务
                } else {
                    BasErrLog latest = basErrLogService.findLatest(crn.getId());
                    // 有异常
                    if (crnProtocol.getAlarm() != null && crnProtocol.getAlarm() > 0) {
                        News.warnNoLog("" + mark + " - 4" + " - 开始执行:堆垛机异常信息记录 : 无任务,有异常");
                        // 记录新异常
                        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)) {
                                News.error("" + mark + " - 5" + " - 堆垛机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)) {
                                News.error("" + mark + " - 6" + " - 堆垛机plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", crn.getId(), latest.getId());
                            }
                        }
                    }
                }
            }
 
        }
        News.infoNoLog("" + mark + " - 0" + " - 堆垛机异常信息记录执行完成");
    }
 
    /**
     * 出库  ===>> 工作档信息写入led显示器
     */
    public synchronized void ledExecute(Integer mark) {
 
        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<Integer> workNoList = 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();
                }
                Integer wrkNo = null;
                Integer ioType = null;
                Integer targetStaNo = null;
                String sourceLocNo = null;
                String locNo = null;
                String barcode = null;
 
                // 获取工作档数据
                WrkMast wrkMast = wrkMastMapper.selectById(staProtocol.getWorkNo());
//                if (null == wrkMast || wrkMast.getWrkSts() < 14 || wrkMast.getIoType() < 100) { continue; }
                if (null == wrkMast) {
                    List<WrkMastLog> wrkMastLogs = wrkMastLogService.selectList(new EntityWrapper<WrkMastLog>()
                            .eq("wrk_no", staProtocol.getWorkNo())
                            .orderBy("modi_time", false)
                    );
                    if (wrkMastLogs.isEmpty()) {
                        continue;
                    }
 
                    WrkMastLog wrkMastLog = wrkMastLogs.get(0);
                    wrkNo = wrkMastLog.getWrkNo();
                    ioType = wrkMastLog.getIoType();
                    sourceLocNo = wrkMastLog.getSourceLocNo();
                    locNo = wrkMastLog.getLocNo();
                    targetStaNo = wrkMastLog.getStaNo();
                    barcode = wrkMastLog.getBarcode();
                }else {
                    wrkNo = wrkMast.getWrkNo();
                    ioType = wrkMast.getIoType();
                    sourceLocNo = wrkMast.getSourceLocNo();
                    locNo = wrkMast.getLocNo();
                    targetStaNo = wrkMast.getStaNo();
                    barcode = wrkMast.getBarcode();
                }
                News.warnNoLog("" + mark + " - 0" + " - 开始执行:出库  ===>> 工作档信息写入led显示器");
                workNoList.add(wrkNo);
                // 组装命令
                LedCommand ledCommand = new LedCommand();
                ledCommand.setWorkNo(wrkNo);
                ledCommand.setIoType(ioType);
                // 出库模式
                switch (ioType) {
                    case 1:
                        ledCommand.setTitle("全板入库");
                        break;
                    case 10:
                        ledCommand.setTitle("空板入库");
                        break;
                    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;
                    case 53:
                        ledCommand.setTitle("拣料入库");
                        break;
                    case 57:
                        ledCommand.setTitle("盘点入库");
                        break;
                    case 54:
                        ledCommand.setTitle("并板入库");
                        break;
                    default:
                        News.error("" + mark + " - 1" + " - 任务入出库类型错误!!![工作号:{}] [入出库类型:{}]", wrkNo, ioType);
                        break;
                }
                ledCommand.setSourceLocNo(sourceLocNo);
                ledCommand.setLocNo(locNo);
                ledCommand.setStaNo(targetStaNo);
                ledCommand.setBarcode(barcode);
//                ledCommand.setSourceStaNo(wrkMast.getSourceStaNo());
                if (ioType != 110 && ioType != 10) {
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkNo);
 
                    if (!wrkDetls.isEmpty()) {
                        WrkDetl wrkDetl = wrkDetls.get(0);
                        double anfme = 0D;
                        double weight = 0D;
                        for (WrkDetl detl : wrkDetls) {
                            anfme += detl.getAnfme();
                            weight += detl.getWeight();
                            wrkDetl.setAnfme(anfme);
                            wrkDetl.setWeight(weight);
                        }
 
                        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
                                .eq("loc_no", wrkMast.getSourceLocNo()));
                        double totalAnfme = 0D;
                        double totalWeight = 0D;
                        for (LocDetl locDetl : locDetls) {
                            totalAnfme += locDetl.getAnfme();
                            totalWeight += locDetl.getWeight();
                        }
                        wrkDetl.setStockNum(totalAnfme);
                        wrkDetl.setStockNum2(totalWeight);
 
                        ledCommand.getMatDtos().add(new MatDto(
                                wrkDetl.getMatnr()
                                , wrkDetl.getMaktx()
                                , wrkDetl.getAnfme()
                                , wrkDetl.getWeight()
                                , wrkDetl.getStockNum()
                                , wrkDetl.getStockNum2()
                                , wrkDetl.getSpecs()
                                , wrkDetl.getSku()
                                , wrkDetl.getZpallet()
                                , wrkDetl.getModel()
                                , wrkDetl.getSupp()
                                , wrkDetl.getKpCstmrName()
                                , wrkDetl.getOrderNo()
                                , wrkDetl.getCstateid$()
                        ));
 
                    }else {
                        List<WrkDetlLog> wrkDetlLogs = wrkDetlLogService.selectTodayByWrkNo(wrkNo);
                        if(!wrkDetlLogs.isEmpty()) {
                            WrkDetlLog wrkDetlLog = wrkDetlLogs.get(0);
 
                            double anfme = 0D;
                            double weight = 0D;
                            for (WrkDetlLog detl : wrkDetlLogs) {
                                anfme += detl.getAnfme();
                                weight += detl.getWeight();
                                wrkDetlLog.setAnfme(anfme);
                                wrkDetlLog.setWeight(weight);
                            }
 
                            List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>()
                                    .eq("loc_no", sourceLocNo));
                            double totalAnfme = 0D;
                            double totalWeight = 0D;
                            for (LocDetl locDetl : locDetls) {
                                totalAnfme += locDetl.getAnfme();
                                totalWeight += locDetl.getWeight();
                            }
                            wrkDetlLog.setStockNum(totalAnfme);
                            wrkDetlLog.setStockNum2(totalWeight);
 
                            ledCommand.getMatDtos().add(new MatDto(
                                    wrkDetlLog.getMatnr()
                                    , wrkDetlLog.getMaktx()
                                    , wrkDetlLog.getAnfme()
                                    , wrkDetlLog.getWeight()
                                    , wrkDetlLog.getStockNum()
                                    , wrkDetlLog.getStockNum2()
                                    , wrkDetlLog.getSpecs()
                                    , wrkDetlLog.getSku()
                                    , wrkDetlLog.getZpallet()
                                    , wrkDetlLog.getModel()
                                    , wrkDetlLog.getSupp()
                                    , wrkDetlLog.getKpCstmrName()
                                    , wrkDetlLog.getOrderNo()
                                    , wrkDetlLog.getCstateid$()
                            ));
                        }
                    }
 
                }
                commands.add(ledCommand);
            }
            Set<Integer> workNos = new HashSet<>(workNoList);
            // 获取LED线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
            // 相同工作号集合则过滤
            if (CollectionUtils.equals(ledThread.getWorkNos(), workNos)) {
                continue;
            }
//             命令下发 -------------------------------------------------------------------------------
//            if (!commands.isEmpty()) {
//                if (led.getId() < 7) {
//                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(3, commands))) {
//                        News.error(""+mark+" - 2"+" - {}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
//                        continue;
//                    } else {
//                        ledThread.setLedMk(false);
//                    }
//                } else {
//                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
//                        News.error(""+mark+" - 3"+" - {}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
//                        continue;
//                    } else {
//                        ledThread.setLedMk(false);
//                    }
//                }
//
//            }
            // 命令下发 -------------------------------------------------------------------------------
            if (!commands.isEmpty()) {
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(3, commands))) {
                    News.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                    continue;
                } else {
                    ledThread.setLedMk(false);
                }
            }
 
            try {
                // 修改主档led标记
                for (Integer wrkNo : workNoList) {
                    WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", wrkNo));
                    if (wrkMast != null) {
                        wrkMast.setOveMk("Y");
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            News.errorNoLog("" + mark + " - 4" + " - 更新工作档失败");
                            throw new CoolException("更新工作档失败");
                        }
                    }
                }
 
                // 更新线程当前工作号集合
                ledThread.setWorkNos(workNos);
 
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
 
        }
        News.infoNoLog("" + mark + " - 0" + " - 出库  ===>> 工作档信息写入led显示器执行完成");
    }
 
    /**
     * 其他  ===>> LED显示器复位,显示默认信息
     */
    public synchronized void ledReset(Integer mark) {
 
        News.warnNoLog("" + mark + " - 0" + " - 开始执行:其他  ===>> LED显示器复位,显示默认信息");
        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("" + mark + " - 1" + " - {}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                } else {
 
                }
            }
        }
        News.infoNoLog("" + mark + " - 0" + " - 执行完成:其他  ===>> LED显示器复位,显示默认信息");
    }
}