杨前锦
2025-06-11 e0d89637030791ce1e7dd46ca5fdec9979977960
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
using HH.WCS.Mobox3.YNJT_PT.core;
using HH.WCS.Mobox3.YNJT_PT.device;
using HH.WCS.Mobox3.YNJT_PT.models;
using HH.WCS.Mobox3.YNJT_PT.process;
using HH.WCS.Mobox3.YNJT_PT.util;
using HH.WCS.Mobox3.YNJT_PT.wms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using static HH.WCS.Mobox3.YNJT_PT.api.ApiModel;
using static HH.WCS.Mobox3.YNJT_PT.api.OtherModel;
using static HH.WCS.Mobox3.YNJT_PT.api.WmsController;
using static HH.WCS.Mobox3.YNJT_PT.util.Settings;
using static HH.WCS.Mobox3.YNJT_PT.wms.WMSHelper;
using static System.Net.Mime.MediaTypeNames;
 
namespace HH.WCS.Mobox3.YNJT_PT.api {
    /// <summary>
    /// api接口辅助类
    /// </summary>
    public class ApiHelper {
        static ApiHelper() {
 
        }
 
        /// <summary>
        /// 成型机下线记录
        /// </summary>
        /// <param name="model"></param>
        public static ResponseResult cxjOffLineRecord(OffLineModel model) 
        {
            ResponseResult response = new ResponseResult();
            var container = ContainerHelper.GetCntr(model.rfid);
            if (container == null)
            {
                ContainerHelper.AddCntr(model.rfid);
            }
 
            try
            {
                OffLineRecord record = new OffLineRecord()
                {
                    S_RFID = model.rfid,
                    S_DEVICE_NO = model.deviceNo,
                    N_IS_URGENT = model.isUrgent,
                    T_OFF_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    N_IS_FULL = model.isFull,
 
                };
                WMSHelper.addOffLineRecord(record);
            }
            catch (Exception ex)
            {
                LogHelper.Info($"添加成型机下线记录错误,错误原因:{ex.Message}", "WMS");
                response.code = 500;
                response.msg = "WMS系统内部错误,请联系开发人员排查问题";
            }
            return response;
        }
 
        /// <summary>
        /// 下线请求
        /// </summary>
        /// <returns></returns>
        public static ResponseResult offLineRequest(NotifyDeviceSignalModel model) 
        {
            ResponseResult response = new ResponseResult();
            var startLoc = LocationHelper.GetLoc(model.loc);
            if (startLoc != null)
            {
                List<BarcodeModel> extData = model.extData;
                int dataCount = extData.Count;
                if (dataCount > 0)
                {
                    var rfidExistNull = extData.Where(a => string.IsNullOrEmpty(a.rfid)).Count()>0;
                    if (rfidExistNull)
                    {
                        response.code = 1;
                        response.msg = "rfid不能为空";
                        return response;
                    }
                    else 
                    {
                        if (extData.Count > 0)
                        {
                            List<BarcodeModel> barcodeList = new List<BarcodeModel>();
                            foreach (var ext in extData)
                            {
                                string rfid = ext.rfid;
                                if (rfid == "88888888")
                                {
                                    // 记录托盘异常
 
                                }
                                else 
                                {
                                    // 查询成型机中间表,rfid 是否存在
                                    var offLineRecord = WMSHelper.getOffLineRecord(rfid);
                                    if (offLineRecord != null)
                                    {
                                        if (offLineRecord.N_IS_FULL == 1) 
                                        {
                                            if (ext.barcode.Contains("99999999"))
                                            {
                                                // 记录条码扫描异常
 
                                            }
                                            else
                                            {
                                                // 2.查询物料条码信息表,条码信息是否存在
                                                var itemBarcodeInfo = WMSHelper.GetGreenTireInformation(ext.barcode);
                                                if (itemBarcodeInfo != null)
                                                {
                                                    barcodeList.Add(ext);
                                                }
                                                else
                                                {
                                                   // 记录查询条码信息异常
 
                                                }
                                            }
                                        }
                                    }
                                }
                            }
 
                            EndLocGroup endLocGroup = null;
                            if (barcodeList.Count > 0) 
                            {
                               endLocGroup = WMSHelper.getInStockEndLoc(barcodeList.Count);
                            }
 
                            foreach (var ext in extData)
                            {
                                Location endLoc = null;
                                string groupNo = null;
                                if (barcodeList.Contains(ext))
                                {
                                    // 满料入库
                                    if (endLocGroup != null)
                                    {
                                        groupNo = endLocGroup.groupNo;
                                        endLoc = endLocGroup.endLocList[0];
                                        endLocGroup.endLocList.Remove(endLoc);
                                    }
 
                                    if (endLoc != null) 
                                    {
                                        var wmsTask = new WMSTask()
                                        {
                                            S_CNTR_CODE = ext.rfid,
                                            S_CODE = WMSHelper.GenerateTaskNo(),
                                            S_START_LOC = startLoc.S_CODE,
                                            S_START_AREA = startLoc.S_AREA_CODE,
                                            S_END_LOC = endLoc.S_CODE,
                                            S_END_AREA = endLoc.S_AREA_CODE,
                                            S_TYPE = "满料下线入库任务",
                                            S_OP_DEF_CODE = model.reqId,
                                            S_OP_DEF_NAME = "成型机满料下线入库",
                                            N_PRIORITY = 1,
                                            T_START_TIME = DateTime.Now,
                                            S_GROUP_NO = groupNo,
                                        };
                                        if (WMSHelper.CreateWmsTask(wmsTask))
                                        {
                                            // 创建一段入库任务
                                            WCSTask wcsTask = new WCSTask()
                                            {
                                                S_OP_NAME = wmsTask.S_OP_DEF_NAME,
                                                S_OP_CODE = wmsTask.S_CODE,
                                                S_CODE = WCSHelper.GenerateTaskNo(),
                                                S_CNTR_CODE = wmsTask.S_CNTR_CODE,
                                                S_TYPE = wmsTask.S_TYPE,
                                                S_START_LOC = startLoc.S_CODE,
                                                S_START_AREA = startLoc.S_AREA_CODE,
                                                S_END_LOC = endLoc.S_CODE,
                                                S_END_AREA = endLoc.S_AREA_CODE,
                                                S_SCHEDULE_TYPE = "WCS",
                                                N_PRIORITY = 1,
                                                T_START_TIME = DateTime.Now,
                                            };
 
                                            if (WCSHelper.CreateTask(wcsTask))
                                            {
                                                // 起点、接驳点、终点加锁
                                                LocationHelper.LockLoc(wcsTask.S_START_LOC, 2);
                                                LocationHelper.LockLoc(wcsTask.S_END_LOC, 1);
 
                                                // 更新作业任务状态
                                                wmsTask.N_B_STATE = 1;
                                                WMSHelper.UpdateTaskState(wmsTask);
 
                                                // 绑定容器物料信息
                                                var itemBarcodeInfo = WMSHelper.GetGreenTireInformation(ext.barcode);
                                                WMSHelper.bindBarcodeItemInfo(wmsTask.S_CNTR_CODE, itemBarcodeInfo);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    // 异常托盘排除
                                    var middleLoc = WMSHelper.getMinTaskMiddleLoc(1);  // 1.异常排出位
                                    endLoc = WMSHelper.getAbnormalAreaEmptyLoc(Settings.abnormalArea);
                                    if (endLoc != null && middleLoc != null)
                                    {
                                        var wmsTask = new WMSTask()
                                        {
                                            S_CNTR_CODE = ext.rfid,
                                            S_CODE = WMSHelper.GenerateTaskNo(),
                                            S_START_LOC = startLoc.S_CODE,
                                            S_START_AREA = startLoc.S_AREA_CODE,
                                            S_END_LOC = endLoc.S_CODE,
                                            S_END_AREA = endLoc.S_AREA_CODE,
                                            S_TYPE = "异常托盘出库任务",
                                            S_OP_DEF_CODE = model.reqId,
                                            S_OP_DEF_NAME = "异常托盘出库检修",
                                            N_PRIORITY = 1,
                                            T_START_TIME = DateTime.Now,
                                        };
                                        if (WMSHelper.CreateWmsTask(wmsTask))
                                        {
                                            // 创建一段入库任务
                                            WCSTask wcsTask = new WCSTask()
                                            {
                                                S_OP_NAME = wmsTask.S_OP_DEF_NAME,
                                                S_OP_CODE = wmsTask.S_CODE,
                                                S_CODE = WCSHelper.GenerateTaskNo(),
                                                S_CNTR_CODE = wmsTask.S_CNTR_CODE,
                                                S_TYPE = wmsTask.S_TYPE,
                                                S_START_LOC = startLoc.S_CODE,
                                                S_START_AREA = startLoc.S_AREA_CODE,
                                                S_END_LOC = middleLoc.S_CODE,
                                                S_END_AREA = middleLoc.S_AREA_CODE,
                                                S_SCHEDULE_TYPE = "WCS",
                                                N_PRIORITY = 1,
                                                T_START_TIME = DateTime.Now,
                                            };
 
                                            if (WCSHelper.CreateTask(wcsTask))
                                            {
                                                // 起点、接驳点、终点加锁
                                                LocationHelper.LockLoc(wcsTask.S_START_LOC, 2);
                                                LocationHelper.LockLoc(wcsTask.S_END_LOC, 1);
 
                                                // 更新作业任务状态
                                                wmsTask.N_B_STATE = 1;
                                                WMSHelper.UpdateTaskState(wmsTask);
                                            }
                                        }
                                    }
                                    else 
                                    {
                                        LogHelper.Info("异常托盘排出,未获取异常排除接驳位", "WMS");
                                    }
                                }
                            }
                        }
                    }
                }
                else 
                {
                    response.code = 1;
                    response.msg = "入参缺少托盘和条码信息";
                }
            }
            else 
            {
                response.code = 1;
                response.msg = "起点货位不存在";
            }
            return response;
        }
 
        /// <summary>
        /// 读码请求
        /// </summary>
        public static ResponseResult readCodeRequest(NotifyDeviceSignalModel model) 
        {
            ResponseResult response = new ResponseResult();
            readCodeFeedbackResponse readCodeFeedbackResponse = new readCodeFeedbackResponse() 
            {
                verifyResult = true
            };
            var wmsTask = WMSHelper.GetWmsTask(model.taskNo);
            if (wmsTask != null)
            {
                if (wmsTask.S_TYPE != "异常托盘出库任务") 
                {
                    Location endLoc = null;
                    Location middleLoc = null;
                    // 判断rfid是否异常
                    if (model.cntrNo != wmsTask.S_CNTR_CODE)
                    {
                        endLoc = LocationHelper.GetLoc(wmsTask.S_END_LOC);
                        var connectLoc = Settings.connectLocList.Where(a => a.roadway == endLoc.N_ROADWAY && a.type == 1).FirstOrDefault();
                        middleLoc = LocationHelper.GetLoc(connectLoc.locCode);
                        endLoc = WMSHelper.getAbnormalAreaEmptyLoc(Settings.abnormalArea);
                        wmsTask.S_END_LOC = endLoc.S_CODE;
                        wmsTask.S_END_AREA = endLoc.S_AREA_CODE;
                        WMSHelper.UpdateTaskEnd(wmsTask);
 
                        var wcsTask = WCSHelper.GetTaskBySrcNo(wmsTask.S_CODE);
                        if (wcsTask != null)
                        {
                            wcsTask.S_END_LOC = middleLoc.S_CODE;
                            wcsTask.S_END_AREA = middleLoc.S_AREA_CODE;
                            WCSHelper.updateTaskEnd(wcsTask);
                        }
 
                        readCodeFeedbackResponse.verifyResult = false;
                        readCodeFeedbackResponse.endLoc = middleLoc.S_CODE;
                        response.data = readCodeFeedbackResponse;
                    }
                    else 
                    {
                        var cntrItemRels = ContainerHelper.GetCntrItemRel(wmsTask.S_CNTR_CODE);
                        if (cntrItemRels.Count > 0) 
                        {
                            if (cntrItemRels[0].S_ITEM_STATE != "OK") 
                            {
                                middleLoc = WMSHelper.getMinTaskMiddleLoc(1);  // 1.异常排出位
                                endLoc = WMSHelper.getAbnormalAreaEmptyLoc(Settings.abnormalArea);
                                wmsTask.S_END_LOC = endLoc.S_CODE;
                                wmsTask.S_END_AREA = endLoc.S_AREA_CODE;
                                WMSHelper.UpdateTaskEnd(wmsTask);
 
                                var wcsTask = WCSHelper.GetTaskBySrcNo(wmsTask.S_CODE);
                                if (wcsTask != null)
                                {
                                    wcsTask.S_END_LOC = middleLoc.S_CODE;
                                    wcsTask.S_END_AREA = middleLoc.S_AREA_CODE;
                                    WCSHelper.updateTaskEnd(wcsTask);
                                }
 
                                readCodeFeedbackResponse.verifyResult = false;
                                readCodeFeedbackResponse.endLoc = middleLoc.S_CODE;
                                response.data = readCodeFeedbackResponse;
                            }
                        }
                    }
                }
            }
            else 
            {
                response.code = 1;
                response.msg = $"任务号:{model.taskNo},未查询到执行中的任务";
            }
            response.data = readCodeFeedbackResponse;
            return response;
        }
 
        /// <summary>
        /// 硫化机呼叫胚胎出库
        /// </summary>
        /// <param name="model"></param>
        public static ResponseResult callItemOutStock(CallItemModel model)
        {
            ResponseResult response = new ResponseResult();
 
            //参数校验
            if (model.locCodes.Count == 0) 
            {
                string msg = $"硫化机:{model.mcn}呼叫胚胎出库,参数校验失败,缺少货位信息";
                LogHelper.Info(msg, "WMS");
                response.code = 1;
                response.msg = msg;
                return response;
            }
 
            // 1.一个硫化机工位只能同时存在一个正在执行中的任务
            WCSTask existTask = WCSHelper.GetTaskByEqNo(model.mcn);
            if (existTask == null)
            {
                // 2.根据当前时间,判断班次日期和班次
                var currentTime = DateTime.Now;
                var shift = getShift(currentTime.TimeOfDay);
                var dateShift = DateTime.Now.ToString("dd/MM/yyyy");
                TimeSpan shiftIII_Start = new TimeSpan(7, 00, 0); // 7:00:00
                if (shift == "III" && currentTime.TimeOfDay < shiftIII_Start)
                {
                    dateShift = DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy");
                }
 
                // 3.根据班次日期+班次+硫化机工位号查询呼叫的物料编码、预计生产数量
                var productionShedule = WMSHelper.getProductionShedule(dateShift, model.mcn, shift);
                if (productionShedule != null && productionShedule.QTY != 0)
                {
                    // 4.查询【胚胎已完成的条码中间表】并计算当前班次的已完成数量
                    int finishNum = WMSHelper.getEmbryoFinishNum(dateShift, model.mcn, shift);
 
                    if (productionShedule.QTY > finishNum)
                    {
                        // 5.计算(1.巷道不报警、2.物料状态OK、3.小于失效时间 大于等于生效时间 4.加急料先出、5.先入先出(生产时间))出库物料,生成任务
                        int locNum = model.locCodes.Count;
                        var startLocData = WMSHelper.getOutStockStartLoc(productionShedule.ITEMCODE,locNum);
                        if (startLocData.startLocList != null && startLocData.startLocList.Count == locNum)
                        {
                            var startLocList = startLocData.startLocList;
                            for (int i = 0; i < locNum; i++)
                            {
                                Location startLoc = startLocList[i];
                                Location endLoc =  LocationHelper.GetLoc(model.locCodes[i]);
                                if (endLoc != null) 
                                {
                                    var locCntrRels = LocationHelper.GetLocCntr(startLoc.S_CODE);
                                    if (locCntrRels != null && locCntrRels.Count > 0) 
                                    {
                                        var wmsTask = new WMSTask()
                                        {
                                            S_CNTR_CODE = locCntrRels[0].S_CNTR_CODE,
                                            S_CODE = WMSHelper.GenerateTaskNo(),
                                            S_START_LOC = startLoc.S_CODE,
                                            S_START_AREA = startLoc.S_AREA_CODE,
                                            S_END_LOC = endLoc.S_CODE,
                                            S_END_AREA = endLoc.S_AREA_CODE,
                                            S_TYPE = "满料下线入库任务",
                                            S_OP_DEF_CODE = model.reqId,
                                            S_OP_DEF_NAME = "成型机满料下线入库",
                                            N_PRIORITY = 1,
                                            T_START_TIME = DateTime.Now,
                                            S_GROUP_NO = startLocData.groupNo,
                                        };
                                        if (WMSHelper.CreateWmsTask(wmsTask))
                                        {
                                            // 创建一段入库任务
                                            WCSTask wcsTask = new WCSTask()
                                            {
                                                S_OP_NAME = wmsTask.S_OP_DEF_NAME,
                                                S_OP_CODE = wmsTask.S_CODE,
                                                S_CODE = WCSHelper.GenerateTaskNo(),
                                                S_CNTR_CODE = wmsTask.S_CNTR_CODE,
                                                S_TYPE = wmsTask.S_TYPE,
                                                S_START_LOC = startLoc.S_CODE,
                                                S_START_AREA = startLoc.S_AREA_CODE,
                                                S_END_LOC = endLoc.S_CODE,
                                                S_END_AREA = endLoc.S_AREA_CODE,
                                                S_SCHEDULE_TYPE = "WCS",
                                                N_PRIORITY = 1,
                                                T_START_TIME = DateTime.Now,
                                            };
 
                                            if (WCSHelper.CreateTask(wcsTask))
                                            {
                                                // 起点、接驳点、终点加锁
                                                LocationHelper.LockLoc(wcsTask.S_START_LOC, 2);
                                                LocationHelper.LockLoc(wcsTask.S_END_LOC, 1);
 
                                                // 更新作业任务状态
                                                wmsTask.N_B_STATE = 1;
                                                WMSHelper.UpdateTaskState(wmsTask);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else 
                        {
                            string msg = $"库内没有满足条件的物料";
                            LogHelper.Info(msg, "WMS");
                            response.code = 1;
                            response.msg = msg;
                        }
                    }
                    else
                    {
                        string msg = $"硫化机:{model.mcn}的当前班次任务已完成,停止叫料";
                        LogHelper.Info(msg, "WMS");
                        response.code = 1;
                        response.msg = msg;
                    }
                }
            }
            else 
            {
                string msg = $"硫化机:{model.mcn}的存在正在执行中的任务,请勿重复叫料";
                LogHelper.Info(msg, "WMS");
                response.code = 1;
                response.msg = msg;
            }
            return response;
        }
 
        /// <summary>
        /// 空托入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ResponseResult emptyTrayInStock(EmptyTrayInStockModel model) 
        {
            ResponseResult response = new ResponseResult();
            Location startLoc = LocationHelper.GetLoc(model.startLoc);
            var endLocGroup = WMSHelper.getInStockEndLoc(1);
            if (endLocGroup.endLocList.Count == 1)
            {
                Location endLoc = endLocGroup.endLocList[0];
                var wmsTask = new WMSTask()
                {
                    S_CNTR_CODE = model.trayCode,
                    S_CODE = WMSHelper.GenerateTaskNo(),
                    S_START_LOC = startLoc.S_CODE,
                    S_START_AREA = startLoc.S_AREA_CODE,
                    S_END_LOC = endLoc.S_CODE,
                    S_END_AREA = endLoc.S_AREA_CODE,
                    S_TYPE = "空托入库任务",
                    S_OP_DEF_CODE = model.reqId,
                    S_OP_DEF_NAME = "空托盘回库任务",
                    N_PRIORITY = 1,
                    T_START_TIME = DateTime.Now,
                };
                if (WMSHelper.CreateWmsTask(wmsTask))
                {
                    // 创建一段入库任务
                    WCSTask wcsTask = new WCSTask()
                    {
                        S_OP_NAME = wmsTask.S_OP_DEF_NAME,
                        S_OP_CODE = wmsTask.S_CODE,
                        S_CODE = WCSHelper.GenerateTaskNo(),
                        S_CNTR_CODE = wmsTask.S_CNTR_CODE,
                        S_TYPE = wmsTask.S_TYPE,
                        S_START_LOC = startLoc.S_CODE,
                        S_START_AREA = startLoc.S_AREA_CODE,
                        S_END_LOC = endLoc.S_CODE,
                        S_END_AREA = endLoc.S_AREA_CODE,
                        S_SCHEDULE_TYPE = "WCS",
                        N_PRIORITY = 1,
                        T_START_TIME = DateTime.Now,
                    };
 
                    if (WCSHelper.CreateTask(wcsTask))
                    {
                        // 起点、接驳点、终点加锁
                        LocationHelper.LockLoc(wcsTask.S_START_LOC, 2);
                        LocationHelper.LockLoc(wcsTask.S_END_LOC, 1);
 
                        // 更新作业任务状态
                        wmsTask.N_B_STATE = 1;
                        WMSHelper.UpdateTaskState(wmsTask);
                    }
                }
            }
            else
            {
                response.code = 1;
                response.msg = "库区没有可入的空货位";
            }
            return response;
        }
 
        /// <summary>
        /// 呼叫空托出库
        /// </summary>
        /// <returns></returns>
        public static ResponseResult callEmptyTrayOutStock(CallEmptyTrayOutStockModel model) 
        {
            ResponseResult responseResult = new ResponseResult();
            Location endLoc = LocationHelper.GetLoc(model.endLoc);
            var startLocData = WMSHelper.getOutStockStartLoc(null, 1);
            if (startLocData.startLocList != null && startLocData.startLocList.Count == 1)
            {
                Location startLoc = startLocData.startLocList[0];
                if (endLoc != null)
                {
                    var locCntrRels = LocationHelper.GetLocCntr(startLoc.S_CODE);
                    if (locCntrRels != null && locCntrRels.Count > 0)
                    {
                        var wmsTask = new WMSTask()
                        {
                            S_CNTR_CODE = locCntrRels[0].S_CNTR_CODE,
                            S_CODE = WMSHelper.GenerateTaskNo(),
                            S_START_LOC = startLoc.S_CODE,
                            S_START_AREA = startLoc.S_AREA_CODE,
                            S_END_LOC = endLoc.S_CODE,
                            S_END_AREA = endLoc.S_AREA_CODE,
                            S_TYPE = "呼叫空托出库",
                            S_OP_DEF_CODE = model.reqId,
                            S_OP_DEF_NAME = "呼叫空托出库",
                            N_PRIORITY = 1,
                            T_START_TIME = DateTime.Now,
                            S_GROUP_NO = startLocData.groupNo,
                        };
                        if (WMSHelper.CreateWmsTask(wmsTask))
                        {
                            // 创建一段入库任务
                            WCSTask wcsTask = new WCSTask()
                            {
                                S_OP_NAME = wmsTask.S_OP_DEF_NAME,
                                S_OP_CODE = wmsTask.S_CODE,
                                S_CODE = WCSHelper.GenerateTaskNo(),
                                S_CNTR_CODE = wmsTask.S_CNTR_CODE,
                                S_TYPE = wmsTask.S_TYPE,
                                S_START_LOC = startLoc.S_CODE,
                                S_START_AREA = startLoc.S_AREA_CODE,
                                S_END_LOC = endLoc.S_CODE,
                                S_END_AREA = endLoc.S_AREA_CODE,
                                S_SCHEDULE_TYPE = "WCS",
                                N_PRIORITY = 1,
                                T_START_TIME = DateTime.Now,
                            };
 
                            if (WCSHelper.CreateTask(wcsTask))
                            {
                                // 起点、接驳点、终点加锁
                                LocationHelper.LockLoc(wcsTask.S_START_LOC, 2);
                                LocationHelper.LockLoc(wcsTask.S_END_LOC, 1);
 
                                // 更新作业任务状态
                                wmsTask.N_B_STATE = 1;
                                WMSHelper.UpdateTaskState(wmsTask);
                            }
                        }
                    }
                }
            }
            else
            {
                string msg = $"库内没有满足条件的物料";
                LogHelper.Info(msg, "WMS");
                responseResult.code = 1;
                responseResult.msg = msg;
            }
            return responseResult;
        }
 
        /// <summary>
        /// 胚胎抽检出库
        /// </summary>
        /// <returns></returns>
        public static ResponseResult embryoCheckOutStock(EmbryoCheckOutStockModel model) 
        {
            ResponseResult responseResult = new ResponseResult();
            var locCntrs = LocationHelper.GetLocCntrRelByCntr(model.trayCode);
            if (locCntrs.Count == 1)
            {
                Location startLoc = LocationHelper.GetLoc(locCntrs[0].S_LOC_CODE);
                var connectLoc = Settings.connectLocList.Where(a => a.roadway == startLoc.N_ROADWAY && a.type == 1).First();
                Location middleLoc = LocationHelper.GetLoc(connectLoc.locCode);
                Location endLoc = LocationHelper.GetLoc("");
                if (endLoc != null)
                {
                    var wmsTask = new WMSTask()
                    {
                        S_CNTR_CODE = model.trayCode,
                        S_CODE = WMSHelper.GenerateTaskNo(),
                        S_START_LOC = startLoc.S_CODE,
                        S_START_AREA = startLoc.S_AREA_CODE,
                        S_END_LOC = endLoc.S_CODE,
                        S_END_AREA = endLoc.S_AREA_CODE,
                        S_TYPE = "胚胎抽检出库任务",
                        S_OP_DEF_CODE = model.reqId,
                        S_OP_DEF_NAME = "胚胎抽检出库出库任务",
                        N_PRIORITY = 1,
                        T_START_TIME = DateTime.Now,
                    };
                    if (WMSHelper.CreateWmsTask(wmsTask))
                    {
                        // 创建一段入库任务
                        WCSTask wcsTask = new WCSTask()
                        {
                            S_OP_NAME = wmsTask.S_OP_DEF_NAME,
                            S_OP_CODE = wmsTask.S_CODE,
                            S_CODE = WCSHelper.GenerateTaskNo(),
                            S_CNTR_CODE = wmsTask.S_CNTR_CODE,
                            S_TYPE = wmsTask.S_TYPE,
                            S_START_LOC = startLoc.S_CODE,
                            S_START_AREA = startLoc.S_AREA_CODE,
                            S_END_LOC = middleLoc.S_CODE,
                            S_END_AREA = middleLoc.S_AREA_CODE,
                            S_SCHEDULE_TYPE = "WCS",
                            N_PRIORITY = 1,
                            T_START_TIME = DateTime.Now,
                        };
 
                        if (WCSHelper.CreateTask(wcsTask))
                        {
                            // 起点、接驳点、终点加锁
                            LocationHelper.LockLoc(wcsTask.S_START_LOC, 2);
                            LocationHelper.LockLoc(wcsTask.S_END_LOC, 1);
 
                            // 更新作业任务状态
                            wmsTask.N_B_STATE = 1;
                            WMSHelper.UpdateTaskState(wmsTask);
                        }
                    }
                }
            }
            return responseResult;
        }
 
        /// <summary>
        /// 异常托盘回库流程
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ResponseResult anomalyTrayInStock(AnomalyTrayInStockModel model) 
        {
            ResponseResult responseResult = new ResponseResult();
            LocationHelper.UnBindingLoc(model.startLoc, new List<string>() { model.trayCode });
            ContainerHelper.deleteCntrItemRelByCntr(model.trayCode);
            return responseResult;
        }
 
        /// <summary>
        /// 任务状态回报
        /// 任务状态 1:开始/执行中;完成;3:准备取货;4:取货完成;5:准备卸货;6:卸货完成;7:异常取消;8:强制完成
        /// 1.根据任务状态回报更新货位状态、任务状态
        /// 2.更新任务中间表状态
        /// 4.更新出库任务中间表状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ReturnResult taskStatusFeedback(TaskStatusFeedbackModel model)
        {
            ReturnResult responseResult = new ReturnResult();
            WCSTask cst = WCSHelper.GetTask(model.taskNo);
            if (cst != null)
            {
                bool isExist = WCSHelper.CheckActionRecordExist(cst.S_CODE, model.status);
                if (!isExist)
                {
                    WMSTask mst = WMSHelper.GetWmsTask(cst.S_OP_CODE);
                    if (mst != null)
                    {
                        switch (model.status)
                        {
                            case 1:
                                WCSHelper.Begin(cst);
                                break;
                            case 2:
                                WCSHelper.End(cst);
                                break;
                            case 3:
                                WCSHelper.UpdateStatus(cst, "准备取货");
                                break;
                            case 4:
                                WCSHelper.UpdateStatus(cst, "取货完成");
                                TaskProcess.OperateStatus(cst, 4);
                                break;
                            case 5:
                                WCSHelper.UpdateStatus(cst, "准备卸货");
                                break;
                            case 6:
                                WCSHelper.UpdateStatus(cst, "卸货完成");
                                TaskProcess.OperateStatus(cst, 6);
                                break;
                            case 7:
                                WCSHelper.Cancel(cst);
                                WCSHelper.UpdateStatus(cst, "取消");
                                TaskProcess.OperateStatus(cst, 7);
                                break;
                            case 8:
                                WCSHelper.UpdateStatus(cst, "强制完成");
                                WCSHelper.End(cst);
                                break;
                        }
                        // 添加WCS动作记录
                        WCSHelper.AddActionRecord(model.taskNo, model.status, model.deviceNo, model.loc);
 
                        if (model.status == 2 || model.status == 8)
                        {
                            if (mst != null)
                            {
                                if (mst.N_B_STATE != 2)
                                {
                                    WCSCore.createLastTask(cst.S_END_LOC, mst);
                                }
                            }
                        }
                    }
                }
            }
            return responseResult;
        }
 
        /// <summary>
        /// 查询班次
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string getShift(TimeSpan time)
        {
            TimeSpan shiftI_Start = new TimeSpan(07, 00, 0);  // 07:00:00
            TimeSpan shiftI_End = new TimeSpan(14, 59, 59);    // 14:59:59
 
            TimeSpan shiftII_Start = new TimeSpan(15, 00, 0); // 15:00:00
            TimeSpan shiftII_End = new TimeSpan(22, 59, 59);    // 22:59:59
 
            TimeSpan shiftIII_Start = new TimeSpan(23, 00, 0); // 23:00:00
            TimeSpan shiftIII_End = new TimeSpan(06, 59, 59);   // 06:59:59
 
            // 判断是否在 Shift III(跨天需特殊处理)
            bool isShiftIII = time >= shiftIII_Start || time <= shiftIII_End;
 
            // 返回结果
            if (time >= shiftI_Start && time <= shiftI_End)
                return "I";
            else if (time >= shiftII_Start && time <= shiftII_End)
                return "II";
            else if (isShiftIII)
                return "III";
            else
                return "No shift"; // 理论上不会触发
        }
 
        /// <summary>
        /// 空托解绑
        /// </summary>
        public static ReturnResult emptyTrayUnBind(EmptyTrayUnBindModel model) 
        {
            ReturnResult responseResult = new ReturnResult();
            ContainerHelper.deleteCntrItemRelByCntr(model.rfid);
            return responseResult;
        }
 
        public class readCodeFeedbackResponse
        {
            public bool verifyResult { get; set; }
            public string endLoc { get; set; }
        }
 
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="model"></param>
        internal static void AddTask(AddTaskModel model) {
            if (!WCSHelper.CheckExist(model.No)) {
                if (LocationHelper.CheckExist(model.From) && LocationHelper.CheckExist(model.To)) {
                    WCSHelper.CreateTask(model.No, model.From, model.To, "搬运", 99, "");
                }
 
            }
        }
        /// <summary>
        /// 创建入库单主子表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult Putaway_Order_In(Putaway_Order_In model) {
            var result = new SimpleResult();
            //创建入库单主子表
            var po = WMSHelper.GetPutawayOrder(model.Data.arrival_no);
            if (po == null) {
                po = new PutawayOrder { S_NO = model.Data.arrival_no, S_BS_TYPE = model.Data.op_type };
                po.Details = new List<PutawayDetail>();
                if (model.Data.items.Count > 0) {
                    model.Data.items.ForEach(a => {
                        po.Details.Add(new PutawayDetail
                        {
                            S_PUTAWAY_NO = model.Data.arrival_no,
                            N_ROW_NO = po.Details.Count + 1,
                            S_ITEM_CODE = a.item_code,
                            F_QTY = a.qty,
                            S_BATCH_NO = a.batch_no,
                        });
                    });
                    WMSHelper.CreatePutawayOrder(po);
                }
 
            }
            return result;
        }
 
        /// <summary>
        /// 创建发货单主子表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult OutboundOrder(OutboundOrder model) {
            var result = new SimpleResult();
            //创建发货单,人工点确认后生成分拣单,没有缺货的自动更新为失败,有货的更新为执行中
            //创建出库单主子表
            var po = WMSHelper.GetShippingOrder(model.Data.out_no);
            if (po == null) {
                po = new ShippingOrder { S_NO = model.Data.out_no, S_BS_TYPE = model.Data.op_type };
                po.Details = new List<ShippingDetail>();
                if (model.Data.items.Count > 0) {
                    model.Data.items.ForEach(a => {
                        po.Details.Add(new ShippingDetail
                        {
                            S_SHIPPING_NO = model.Data.out_no,
                            N_ROW_NO = po.Details.Count + 1,
                            S_ITEM_CODE = a.item_code,
                            F_QTY = a.qty,
                            S_BATCH_NO = a.batch_no,
                        });
                    });
                    WMSHelper.CreateShippingOrder(po);
                }
 
            }
            return result;
        }
        
        public static List<KeyValuePair<string, InstockInfo>> cacheInstockInfos = new List<KeyValuePair<string, InstockInfo>>();
 
        public static void addKeyValuePair(string ip , InstockInfo instockInfo) {
            KeyValuePair<string, InstockInfo> pair = new KeyValuePair<string, InstockInfo>(ip,instockInfo);
            LogHelper.Info("添加缓存信号,缓存信息:" + JsonConvert.SerializeObject(pair), "TSSG");
 
            // 查找并替换键值对
            bool replaced = false;
            for (int i = 0; i < cacheInstockInfos.Count; i++)
            {
                if (cacheInstockInfos[i].Key == pair.Key)
                {
                    cacheInstockInfos[i] = pair;
                    replaced = true;
                    LogHelper.Info("存在相同IP的缓存信号,替换缓存信息:" + JsonConvert.SerializeObject(pair), "TSSG");
                    break; // 如果只需要替换第一个匹配的项,就跳出循环
                }
            }
            // 如果没有找到要替换的项,可以选择添加新的键值对或执行其他操作
            if (!replaced)
            {
                cacheInstockInfos.Add(pair);
            }
        }
 
            internal static CodeInfo GetCodeInfo(string code, string org) {
            //return new CodeInfo {  Fitemid_XK=code, FSourceNo="123456"};
            CodeInfo result = null;
            try {
                var db = new SqlHelper<object>().GetInstance(Settings.SqlServer1);
                var nameP = new SugarParameter("@FBarCode", code);
                var orgP = new SugarParameter("@Forg", org);
                //var ageP = new SugarParameter("@age", null, true);//设置为output
                //var dt = db.Ado.UseStoredProcedure().GetDataTable("sp_school", nameP, ageP);//返回dt
                result = db.Ado.UseStoredProcedure().SqlQuery<CodeInfo>("WMS_FBarCode", nameP, orgP).First();//返回List
                Console.WriteLine($"读存储过程成功,result={result}");
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
 
        /// <summary>
        /// agv 车辆报警
        /// </summary>
        /// <param name="forkliftNo"></param>
        /// <param name="errCode"></param>
        /// <param name="errCode2"></param>
        /// <param name="failCode"></param>
        public static void agvCarAlarm(string  forkliftNo, string errCode , string errCode2 , string failCode) {
            bool turnLight = false;
 
         
        }
 
        public class OffLineModel
        {
            public string rfid { get; set; } // 托盘号
            public string deviceNo { get; set; } // 机台号
            public int isUrgent { get; set; } // N_IS_URGENT 是否加急(0.否 1.是)
            public int isFull { get; set; } // 是否满拖 0.否 1.是
        }
        public class AddTaskModel {
            public string From { get; set; }
            public string To { get; set; }
            public string No { get; set; }
        }
        public class TN_LocationModel {
            public string TN_Location { get; set; }
        }
        public class CodeInfo {
            /// <summary>
            /// 生产订单内码
            /// </summary>
            public string FInterID { get; set; }
            /// <summary>
            /// 生产订单编号
            /// </summary>
            public string FSourceNo { get; set; }
            /// <summary>
            /// 批号
            /// </summary>
            public string FGMPBatchNo { get; set; }
            public string FState { get; set; }
            /// <summary>
            /// 物料编码(内码就是编码)
            /// </summary>
            public string Fitemid_XK { get; set; }
            /// <summary>
            /// 分录id
            /// </summary>
            public string Fentryid { get; set; }
        }
        public class NoteInfo : CodeInfo {
            public string WmsBillNo { get; set; }
        }
    }
}