#
Junjie
5 天以前 4b97844365fddeeb66a00f4ac42389cadb545bcf
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
package com.zy.core.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.exception.CoolException;
import com.zy.asrs.domain.param.CreateLocMoveTaskParam;
import com.zy.asrs.entity.BasDualCrnp;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasDualCrnpService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.Task;
import com.zy.core.model.command.DualCrnCommand;
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.DualCrnProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.thread.DualCrnThread;
import com.zy.core.thread.StationThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class DualCrnOperateProcessUtils {
 
    @Value("${mainProcessPlugin}")
    private String mainProcessPlugin;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private BasDualCrnpService basDualCrnpService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WmsOperateUtils wmsOperateUtils;
    @Autowired
    private CommonService commonService;
 
    //入出库  ===>>  双工位堆垛机入出库作业下发
    public synchronized void dualCrnIoExecute() {
        List<BasDualCrnp> basDualCrnps = basDualCrnpService.selectList(new EntityWrapper<>());
        for (BasDualCrnp basDualCrnp : basDualCrnps) {
            DualCrnThread dualCrnThread = (DualCrnThread) SlaveConnection.get(SlaveType.DualCrn, basDualCrnp.getCrnNo());
            if(dualCrnThread == null){
                continue;
            }
 
            DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
            if(dualCrnProtocol == null){
                continue;
            }
 
            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                    .eq("dual_crn_no", basDualCrnp.getCrnNo())
                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts)
            );
            if(wrkMasts.size() >= 2){
                continue;
            }
 
            if(dualCrnProtocol.getMode() != DualCrnModeType.AUTO.id) {
                continue;
            }
 
            if(dualCrnProtocol.getAlarm() != 0) {
                continue;
            }
 
            this.crnExecute(basDualCrnp, dualCrnThread);
        }
    }
 
    private synchronized void crnExecute(BasDualCrnp basDualCrnp, DualCrnThread dualCrnThread) {
        DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
        if(dualCrnProtocol == null){
            return;
        }
 
        List<WrkMast> allTaskList = new ArrayList<>();
        List<WrkMast> inTaskList = getInTaskList(basDualCrnp);
        List<WrkMast> outTaskList = getOutTaskList(basDualCrnp);
        List<WrkMast> locMoveTaskList = getLocMoveTaskList(basDualCrnp);
 
        if (!locMoveTaskList.isEmpty()) {
            allTaskList.addAll(locMoveTaskList);
        }
 
        // 如果最近一次是出库模式
        if (dualCrnProtocol.getLastIo().equals("O")) {
            allTaskList.addAll(inTaskList);
            allTaskList.addAll(outTaskList);
        }
        // 如果最近一次是入库模式
        else if (dualCrnProtocol.getLastIo().equals("I")) {
            allTaskList.addAll(outTaskList);
            allTaskList.addAll(inTaskList);
        }
 
        for (WrkMast wrkMast : allTaskList) {
            if (wrkMast.getIoType() == WrkIoType.IN.id) {
                boolean result = this.crnExecuteIn(basDualCrnp, dualCrnThread, wrkMast);
                if (result) {
                    break;
                }
            } else if (wrkMast.getIoType() == WrkIoType.OUT.id) {
                boolean result = this.crnExecuteOut(basDualCrnp, dualCrnThread, wrkMast);
                if (result) {
                    break;
                }
            } else if (wrkMast.getIoType() == WrkIoType.LOC_MOVE.id) {
                boolean result = this.crnExecuteLocMove(basDualCrnp, dualCrnThread, wrkMast);
                if (result) {
                    break;
                }
            }
        }
    }
 
    private List<WrkMast> getInTaskList(BasDualCrnp basDualCrnp) {
        List<WrkMast> list = new ArrayList<>();
        if(!basDualCrnp.getInEnable().equals("Y")){
            News.info("双工位堆垛机:{} 可入信号不满足", basDualCrnp.getCrnNo());
            return list;
        }
 
        List<StationObjModel> inStationList = basDualCrnp.getInStationList$();
        if(inStationList.isEmpty()){
            News.info("双工位堆垛机:{} 入库站点未设置", basDualCrnp.getCrnNo());
            return list;
        }
 
        for (StationObjModel stationObjModel : inStationList) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
 
            Map<Integer, StationProtocol> stationProtocolMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = stationProtocolMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                continue;
            }
 
            if (!stationProtocol.isAutoing()) {
                continue;
            }
 
            if (!stationProtocol.isLoading()) {
                continue;
            }
 
            if (stationProtocol.getTaskNo() <= 0) {
                continue;
            }
 
            if (!stationProtocol.isInEnable()) {
                News.taskInfo(stationProtocol.getTaskNo(), "取货站点:{} 没有可入信号", stationObjModel.getStationId());
                continue;
            }
 
            // 获取任务
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (null == wrkMast) {
                News.taskInfo(stationProtocol.getTaskNo(), "工作号:{} 任务信息不存在", stationProtocol.getTaskNo());
                continue;
            }
 
            if(wrkMast.getWrkSts() != WrkStsType.INBOUND_DEVICE_RUN.sts){
                continue;
            }
 
            list.add(wrkMast);
        }
 
        return list;
    }
 
    private List<WrkMast> getOutTaskList(BasDualCrnp basDualCrnp) {
        List<WrkMast> list = new ArrayList<>();
        if(!basDualCrnp.getOutEnable().equals("Y")){
            News.info("双工位堆垛机:{} 可出信号不满足", basDualCrnp.getCrnNo());
            return list;
        }
 
        List<StationObjModel> outStationList = basDualCrnp.getOutStationList$();
        if(outStationList.isEmpty()){
            News.info("双工位堆垛机:{} 出库站点未设置", basDualCrnp.getCrnNo());
            return list;
        }
 
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("dual_crn_no", basDualCrnp.getCrnNo())
                .eq("wrk_sts", WrkStsType.NEW_OUTBOUND.sts)
        );
        list.addAll(wrkMasts);
        return list;
    }
 
    private List<WrkMast> getLocMoveTaskList(BasDualCrnp basDualCrnp) {
        List<WrkMast> list = new ArrayList<>();
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("dual_crn_no", basDualCrnp.getCrnNo())
                .eq("wrk_sts", WrkStsType.NEW_LOC_MOVE.sts)
        );
        list.addAll(wrkMasts);
        return list;
    }
 
    private synchronized boolean crnExecuteIn(BasDualCrnp basDualCrnp, DualCrnThread dualCrnThread, WrkMast wrkMast) {
        DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
        if (dualCrnProtocol == null) {
            return false;
        }
 
        if (!basDualCrnp.getInEnable().equals("Y")) {
            News.info("双工位堆垛机:{} 可入信号不满足", basDualCrnp.getCrnNo());
            return false;
        }
 
        List<StationObjModel> inStationList = basDualCrnp.getInStationList$();
        if(inStationList.isEmpty()){
            News.info("双工位堆垛机:{} 入库站点未设置", basDualCrnp.getCrnNo());
            return false;
        }
 
        Integer crnNo = basDualCrnp.getCrnNo();
        int station = calcStation(dualCrnProtocol, "in");
        if (station == 0) {
            News.info("双工位堆垛机:{} 无可用工位", basDualCrnp.getCrnNo());
            return false;
        }
 
        if (wrkMast.getWrkSts() != WrkStsType.INBOUND_DEVICE_RUN.sts) {
            return false;
        }
 
        // 获取库位信息
        LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
        if (locMast == null) {
            News.taskInfo(wrkMast.getWrkNo(), "目标库位:{} 信息不存在", wrkMast.getLocNo());
            return false;
        }
 
        if (!locMast.getLocSts().equals("S")) {
            News.taskInfo(wrkMast.getWrkNo(), "目标库位:{} 状态异常", wrkMast.getLocNo());
            return false;
        }
 
        //检测浅库位状态
        boolean checkStatus = checkShallowLocStatus(locMast.getLocNo(), wrkMast.getWrkNo());
        if (!checkStatus) {
            News.taskInfo(wrkMast.getWrkNo(), "因浅库位堵塞无法执行");
            return false;
        }
 
        StationObjModel inStationObjModel = null;
        for (StationObjModel stationObjModel : inStationList) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
 
            Map<Integer, StationProtocol> stationProtocolMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = stationProtocolMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                continue;
            }
 
            if (!stationProtocol.isAutoing()) {
                continue;
            }
 
            if (!stationProtocol.isLoading()) {
                continue;
            }
 
            if (stationProtocol.getTaskNo() <= 0) {
                continue;
            }
 
            if (!stationProtocol.isInEnable()) {
                News.taskInfo(stationProtocol.getTaskNo(), "取货站点:{} 没有可入信号", stationObjModel.getStationId());
                continue;
            }
 
            if (stationProtocol.getTaskNo().equals(wrkMast.getWrkNo())) {
                inStationObjModel = stationObjModel;
                break;
            }
        }
 
        if (inStationObjModel == null) {
            News.taskInfo(wrkMast.getWrkNo(), "未搜索到取货站点");
            return false;
        }
 
        String sourceLocNo = Utils.getLocNo(inStationObjModel.getDeviceRow(), inStationObjModel.getDeviceBay(), inStationObjModel.getDeviceLev());
 
        List<DualCrnCommand> commandList = new ArrayList<>();
        DualCrnCommand pickCommand = dualCrnThread.getPickCommand(sourceLocNo, wrkMast.getWrkNo(), crnNo, station);
        DualCrnCommand putCommand = dualCrnThread.getPutCommand(wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo, station);
        commandList.add(pickCommand);
        commandList.add(putCommand);
 
        wrkMast.setWrkSts(WrkStsType.INBOUND_RUN.sts);
        wrkMast.setDualCrnNo(crnNo);
        wrkMast.setSystemMsg("");
        wrkMast.setIoTime(new Date());
        if (wrkMastService.updateById(wrkMast)) {
            MessageQueue.offer(SlaveType.DualCrn, crnNo, new Task(2, commandList));
            News.info("双工位堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(commandList));
            dualCrnProtocol.setLastIo("I");
            return true;
        }
        return false;
    }
 
    private synchronized boolean crnExecuteOut(BasDualCrnp basDualCrnp, DualCrnThread dualCrnThread, WrkMast wrkMast) {
        DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
        if (dualCrnProtocol == null) {
            return false;
        }
 
        if (!basDualCrnp.getOutEnable().equals("Y")) {
            News.info("双工位堆垛机:{} 可出信号不满足", basDualCrnp.getCrnNo());
            return false;
        }
 
        List<StationObjModel> outStationList = basDualCrnp.getOutStationList$();
        if (outStationList.isEmpty()) {
            News.info("双工位堆垛机:{} 出库站点未设置", basDualCrnp.getCrnNo());
            return false;
        }
 
        Integer crnNo = basDualCrnp.getCrnNo();
        int station = calcStation(dualCrnProtocol, "out");
        if (station == 0) {
            News.info("双工位堆垛机:{} 无可用工位", basDualCrnp.getCrnNo());
            return false;
        }
 
        for (StationObjModel stationObjModel : outStationList) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
            if (stationThread == null) {
                continue;
            }
 
            Map<Integer, StationProtocol> stationProtocolMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = stationProtocolMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                continue;
            }
 
            if (!stationProtocol.isAutoing()) {
                continue;
            }
 
            if (stationProtocol.isLoading()) {
                continue;
            }
 
            if (stationProtocol.getTaskNo() != 0) {
                continue;
            }
 
            if (!stationProtocol.isOutEnable()) {
                News.info("放货站点:{} 没有可出信号", stationObjModel.getStationId());
                continue;
            }
 
            // 获取库位信息
            LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
            if (locMast == null) {
                News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 信息不存在", wrkMast.getSourceLocNo());
                continue;
            }
 
            if (!locMast.getLocSts().equals("R")) {
                News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 状态异常", wrkMast.getSourceLocNo());
                continue;
            }
 
            //检测浅库位状态
            boolean checkStatus = checkShallowLocStatus(locMast.getLocNo(), wrkMast.getWrkNo());
            if (!checkStatus) {
                News.taskInfo(wrkMast.getWrkNo(), "因浅库位堵塞无法执行");
                continue;
            }
 
            String targetLocNo = Utils.getLocNo(stationObjModel.getDeviceRow(), stationObjModel.getDeviceBay(), stationObjModel.getDeviceLev());
 
            List<DualCrnCommand> commandList = new ArrayList<>();
            DualCrnCommand pickCommand = dualCrnThread.getPickCommand(wrkMast.getSourceLocNo(), wrkMast.getWrkNo(), crnNo, station);
            DualCrnCommand putCommand = dualCrnThread.getPutCommand(targetLocNo, wrkMast.getWrkNo(), crnNo, station);
            commandList.add(pickCommand);
            commandList.add(putCommand);
 
            wrkMast.setWrkSts(WrkStsType.OUTBOUND_RUN.sts);
            wrkMast.setDualCrnNo(crnNo);
            wrkMast.setSystemMsg("");
            wrkMast.setIoTime(new Date());
            if (wrkMastService.updateById(wrkMast)) {
                MessageQueue.offer(SlaveType.DualCrn, crnNo, new Task(2, commandList));
                News.info("双工位堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(commandList));
                dualCrnProtocol.setLastIo("O");
 
                redisUtil.set(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo(), JSON.toJSONString(stationObjModel, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
                return true;
            }
        }
        return false;
    }
 
    private synchronized boolean crnExecuteLocMove(BasDualCrnp basDualCrnp, DualCrnThread dualCrnThread, WrkMast wrkMast) {
        DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
        if (dualCrnProtocol == null) {
            return false;
        }
 
        Integer crnNo = basDualCrnp.getCrnNo();
        int station = calcStation(dualCrnProtocol, "locMove");
        if (station == 0) {
            News.info("双工位堆垛机:{} 无可用工位", basDualCrnp.getCrnNo());
            return false;
        }
 
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("crn_no", crnNo)
                .eq("wrk_sts", WrkStsType.NEW_LOC_MOVE.sts)
        );
 
        // 获取源库位信息
        LocMast sourceLocMast = locMastService.selectById(wrkMast.getSourceLocNo());
        if (sourceLocMast == null) {
            News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 信息不存在", wrkMast.getSourceLocNo());
            return false;
        }
 
        if(!sourceLocMast.getLocSts().equals("R")){
            News.taskInfo(wrkMast.getWrkNo(), "源库位:{} 状态异常,不属于出库预约状态", wrkMast.getSourceLocNo());
            return false;
        }
 
        // 获取库位信息
        LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
        if (locMast == null) {
            News.taskInfo(wrkMast.getWrkNo(), "库位:{} 信息不存在", wrkMast.getLocNo());
            return false;
        }
 
        if (!locMast.getLocSts().equals("S")) {
            News.taskInfo(wrkMast.getWrkNo(), "库位:{} 状态异常,不属于入库预约状态", wrkMast.getLocNo());
            return false;
        }
 
        //检测浅库位状态
        boolean checkStatus = checkShallowLocStatus(locMast.getLocNo(), wrkMast.getWrkNo());
        if (!checkStatus) {
            News.taskInfo(wrkMast.getWrkNo(), "因浅库位堵塞无法执行");
            return false;
        }
 
        List<DualCrnCommand> commandList = new ArrayList<>();
        DualCrnCommand pickCommand = dualCrnThread.getPickCommand(wrkMast.getSourceLocNo(), wrkMast.getWrkNo(), crnNo, station);
        DualCrnCommand putCommand = dualCrnThread.getPutCommand(wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo, station);
        commandList.add(pickCommand);
        commandList.add(putCommand);
 
        wrkMast.setWrkSts(WrkStsType.LOC_MOVE_RUN.sts);
        wrkMast.setDualCrnNo(crnNo);
        wrkMast.setSystemMsg("");
        wrkMast.setIoTime(new Date());
        if (wrkMastService.updateById(wrkMast)) {
            MessageQueue.offer(SlaveType.DualCrn, crnNo, new Task(2, commandList));
            News.info("双工位堆垛机命令下发成功,堆垛机号={},任务数据={}", crnNo, JSON.toJSON(commandList));
            dualCrnProtocol.setLastIo("I");
            return true;
        }
        return false;
    }
 
    //双工位堆垛机任务执行完成
    public synchronized void dualCrnIoExecuteFinish() {
        List<BasDualCrnp> basDualCrnps = basDualCrnpService.selectList(new EntityWrapper<>());
        for (BasDualCrnp basDualCrnp : basDualCrnps) {
            DualCrnThread dualCrnThread = (DualCrnThread) SlaveConnection.get(SlaveType.DualCrn, basDualCrnp.getCrnNo());
            if(dualCrnThread == null){
                continue;
            }
 
            DualCrnProtocol dualCrnProtocol = dualCrnThread.getStatus();
            if(dualCrnProtocol == null){
                continue;
            }
 
            if(dualCrnProtocol.getMode() != DualCrnModeType.AUTO.id) {
                continue;
            }
 
            if(dualCrnProtocol.getAlarm() != 0) {
                continue;
            }
 
            if(dualCrnProtocol.getTaskNo() > 0 && dualCrnProtocol.getStatus() == DualCrnStatusType.WAITING.id) {
                executeFinish(basDualCrnp, dualCrnThread, dualCrnProtocol, dualCrnProtocol.getTaskNo(), 1);
            }
 
            if(dualCrnProtocol.getTaskNoTwo() > 0 && dualCrnProtocol.getStatusTwo() == DualCrnStatusType.WAITING.id) {
                executeFinish(basDualCrnp, dualCrnThread, dualCrnProtocol, dualCrnProtocol.getTaskNoTwo(), 2);
            }
        }
    }
 
    private void executeFinish(BasDualCrnp basDualCrnp, DualCrnThread dualCrnThread, DualCrnProtocol dualCrnProtocol, int taskNo, int station) {
        Object lock = redisUtil.get(RedisKeyType.DUAL_CRN_IO_EXECUTE_FINISH_LIMIT.key + basDualCrnp.getCrnNo() + "_" + taskNo);
        if (lock != null) {
            return;
        }
 
        // 获取待确认工作档
        WrkMast wrkMast = wrkMastService.selectByWorkNo(taskNo);
        if (wrkMast == null) {
            News.error("双工位堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", basDualCrnp.getCrnNo(), taskNo);
            return;
        }
 
        Object commandObj = redisUtil.get(RedisKeyType.DUAL_CRN_COMMAND_.key + wrkMast.getWrkNo());
        if (commandObj == null) {
            News.error("双工位堆垛机处于等待确认且任务完成状态,但未找到命令。堆垛机号={},工作号={}", basDualCrnp.getCrnNo(), taskNo);
            return;
        }
 
        JSONObject commandMap = JSON.parseObject(commandObj.toString());
        Integer idx = commandMap.getInteger("idx");
        List<DualCrnCommand> commandList = commandMap.getJSONArray("commands").toJavaList(DualCrnCommand.class);
        if (idx >= commandList.size()) {
            Long updateWrkSts = null;
            if (wrkMast.getWrkSts() == WrkStsType.INBOUND_RUN.sts) {
                updateWrkSts = WrkStsType.COMPLETE_INBOUND.sts;
            } else if (wrkMast.getWrkSts() == WrkStsType.OUTBOUND_RUN.sts) {
                updateWrkSts = WrkStsType.OUTBOUND_RUN_COMPLETE.sts;
 
                if(mainProcessPlugin.contains("Fake")) {
                    //生成仿真站点数据
                    List<StationObjModel> outStationList = basDualCrnp.getOutStationList$();
                    if(outStationList.isEmpty()){
                        News.info("双工位堆垛机:{} 出库站点未设置", basDualCrnp.getCrnNo());
                        return;
                    }
 
                    for (StationObjModel stationObjModel : outStationList) {
                        if (!stationObjModel.getStationId().equals(wrkMast.getSourceStaNo())) {
                            continue;
                        }
 
                        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
                        if (stationThread == null) {
                            continue;
                        }
                        //生成仿真站点数据
                        StationCommand command = stationThread.getMoveCommand(9998, wrkMast.getSourceStaNo(), 0, 0);
                        MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                    }
                }
 
            } else if (wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts) {
                updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
            } else {
                News.error("双工位堆垛机处于等待确认且任务完成状态,但工作状态异常。堆垛机号={},工作号={}", basDualCrnp.getCrnNo(), taskNo);
                return;
            }
 
            DualCrnCommand resetCommand = dualCrnThread.getResetCommand(dualCrnProtocol.getCrnNo(), station);
            boolean offer = MessageQueue.offer(SlaveType.DualCrn, dualCrnProtocol.getCrnNo(), new Task(3, resetCommand));
            if (offer) {
                wrkMast.setWrkSts(updateWrkSts);
                wrkMast.setSystemMsg("");
                wrkMast.setIoTime(new Date());
                if (wrkMastService.updateById(wrkMast)) {
                    News.info("双工位堆垛机任务状态更新成功,堆垛机号={},工作号={}", basDualCrnp.getCrnNo(), taskNo);
                }
                redisUtil.set(RedisKeyType.DUAL_CRN_IO_EXECUTE_FINISH_LIMIT.key + basDualCrnp.getCrnNo() + "_" + taskNo, "lock", 10);
            }
        }else {
            DualCrnCommand command = commandList.get(idx - 1);
            if (command.getTaskMode() == DualCrnTaskModeType.PICK.id.shortValue()) {
                //取货后等待下一个任务时长
                redisUtil.set(RedisKeyType.DUAL_CRN_PICK_WAIT_NEXT_TASK.key + basDualCrnp.getCrnNo(), "wait", 10);
            }
 
            DualCrnCommand resetCommand = dualCrnThread.getResetCommand(dualCrnProtocol.getCrnNo(), station);
            MessageQueue.offer(SlaveType.DualCrn, dualCrnProtocol.getCrnNo(), new Task(3, resetCommand));
            News.info("双工位堆垛机命令完成确认成功,堆垛机号={},工作号={}", basDualCrnp.getCrnNo(), taskNo);
        }
    }
 
    //检测浅库位状态
    public synchronized boolean checkShallowLocStatus(String locNo, Integer taskNo) {
        String checkDeepLocOutTaskBlockReport = "Y";
        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
        if (systemConfigMapObj != null) {
            HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
            checkDeepLocOutTaskBlockReport = systemConfigMap.get("checkDeepLocOutTaskBlockReport");
        }
 
        if (!checkDeepLocOutTaskBlockReport.equals("Y")) {
            return true;
        }
 
        Object lock = redisUtil.get(RedisKeyType.CHECK_SHALLOW_LOC_STATUS_LIMIT.key + taskNo);
        if (lock != null) {
            return false;
        }
        redisUtil.set(RedisKeyType.CHECK_SHALLOW_LOC_STATUS_LIMIT.key + taskNo, "lock", 5);
 
        Integer shallowRow = Utils.getShallowRowByDeepRow(Utils.getRow(locNo));
        if (shallowRow == null) {
            return true;
        }
 
        String shallowLocNo = Utils.getLocNo(shallowRow, Utils.getBay(locNo), Utils.getLev(locNo));
        LocMast shallowLocMast = locMastService.queryByLoc(shallowLocNo);
        if (shallowLocMast == null) {
            News.taskInfo(taskNo, "浅库位:{} 数据不存在", shallowLocNo);
            return false;
        }
 
        if (shallowLocMast.getLocSts().equals("O")) {
            return true;
        }
 
        if (shallowLocMast.getLocSts().equals("F")) {
            //浅库位状态有货,申请更换库位
            String response = wmsOperateUtils.applyChangeLocNo(shallowLocNo);
            if (response == null) {
                News.taskError(taskNo, "WCS申请在库库位更换库位失败,WMS接口未响应!!!response:{}", response);
                return false;
            }
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code").equals(200)) {
                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                String moveLocNo = dto.getLocNo();
 
                CreateLocMoveTaskParam moveTaskParam = new CreateLocMoveTaskParam();
                moveTaskParam.setTaskNo(dto.getTaskNo());
                moveTaskParam.setSourceLocNo(shallowLocNo);
                moveTaskParam.setLocNo(moveLocNo);
                try {
                    boolean result = commonService.createLocMoveTask(moveTaskParam);
                } catch (CoolException e) {
                    News.taskInfo(taskNo, e.getMessage());
                }
            } else {
                News.error("请求WMS申请更换库位接口失败!!!response:{}", response);
            }
        }
        return false;
    }
 
    private int calcStation(DualCrnProtocol dualCrnProtocol, String type) {
        List<Integer> idleStationList = new ArrayList<>();
 
        if (dualCrnProtocol.getTaskNo() == 0
                && dualCrnProtocol.getLoaded() == 0
                && dualCrnProtocol.getStatusType().equals(DualCrnStatusType.IDLE)
                && dualCrnProtocol.getTaskReceive() == 0
        ) {
            idleStationList.add(1);
        }
 
        if (dualCrnProtocol.getTaskNoTwo() == 0
                && dualCrnProtocol.getLoadedTwo() == 0
                && dualCrnProtocol.getStatusTypeTwo().equals(DualCrnStatusType.IDLE)
                && dualCrnProtocol.getTaskReceiveTwo() == 0
        ) {
            idleStationList.add(2);
        }
 
        if (type.equals("locMove")) {
            if (idleStationList.size() != 2) {
                return 0;
            }
            return idleStationList.get(0);
        }
 
        if (idleStationList.size() == 2) {
            return idleStationList.get(0);
        }
 
        Integer idleStation = idleStationList.get(0);
        int workingStation = idleStation == 1 ? 2 : 1;
 
        int executeTaskNo = 0;
        if (workingStation == 1) {
            executeTaskNo = dualCrnProtocol.getTaskNo();
            //检测工位已进入放货阶段,放货阶段不接新任务
            if (dualCrnProtocol.getStatusType().equals(DualCrnStatusType.PUT_MOVING)
                    || dualCrnProtocol.getStatusType().equals(DualCrnStatusType.PUTTING)
                    || dualCrnProtocol.getStatusType().equals(DualCrnStatusType.WAITING)
            ) {
                return 0;
            }
        } else {
            executeTaskNo = dualCrnProtocol.getTaskNoTwo();
            //检测工位已进入放货阶段,放货阶段不接新任务
            if (dualCrnProtocol.getStatusTypeTwo().equals(DualCrnStatusType.PUT_MOVING)
                    || dualCrnProtocol.getStatusTypeTwo().equals(DualCrnStatusType.PUTTING)
                    || dualCrnProtocol.getStatusTypeTwo().equals(DualCrnStatusType.WAITING)
            ) {
                return 0;
            }
        }
 
        if (executeTaskNo == 0) {
            return 0;
        }
 
        WrkMast wrkMast = wrkMastService.selectByWorkNo(executeTaskNo);
        if (wrkMast.getIoType().equals(WrkIoType.IN.id) && type.equals("in")) {
            return idleStation;
        }
 
        if (wrkMast.getIoType().equals(WrkIoType.OUT.id) && type.equals("out")) {
            return idleStation;
        }
 
        return 0;
    }
 
}