杨前锦
2025-06-19 d42ad8b01195def2a9309930e14f90ff13ad47b1
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
 
using HH.WCS.Mobox3.HD.core;
using HH.WCS.Mobox3.HD.device;
using HH.WCS.Mobox3.HD.dispatch;
using HH.WCS.Mobox3.HD.models;
using HH.WCS.Mobox3.HD.util;
using HH.WCS.Mobox3.HD.wms;
using Newtonsoft.Json;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.UI.WebControls;
using static HH.WCS.Mobox3.HD.core.Monitor;
using static HH.WCS.Mobox3.HD.dispatch.ShopFloorControl;
using static HH.WCS.Mobox3.HD.wms.WCSHelper;
using static System.Runtime.CompilerServices.RuntimeHelpers;
 
 
namespace HH.WCS.Mobox3.HD.api
{
    /// <summary>
    /// 第三方调用的接口
    /// </summary>
    [RoutePrefix("api")]
    public class WmsController : System.Web.Http.ApiController
    {
        /// <summary>
        /// 0.心跳监测
        /// </summary>
        /// <returns></returns>
        public ResponseResult heatbeat()
        {
            ResponseResult result = new ResponseResult()
            {
                success = true,
                result = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };
           return result;
        }
 
        /// <summary>
        /// 1.创建入库搬运任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseResult createInOrder(InStockInfo model)
        {
            LogHelper.Info("创建入库搬运任务-createInOrder 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult result = new ResponseResult();
            try
            {
                result = ApiHelper.createInOrder(model);
            }
            catch (BusinessException be)
            {
                result.success = false;
                result.message = be.Message;
            }
            catch (Exception ex) 
            {
                result.success = false;
                result.message = "内部异常,请联系开发人员处理";
                LogHelper.Info($"内部异常,错误信息:{ex.Message}", "车间控制器");
                LogHelper.Info(ex.StackTrace, "车间控制器");
            }
            LogHelper.Info("创建入库搬运任务-createInOrder 出参:" + JsonConvert.SerializeObject(result), "车间控制器");
            return result;
        }
 
        public class BusinessException : Exception
        {
            public BusinessException(string message) : base(message) { }
        }
 
        /// <summary>
        /// 2、创建出库搬运任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseResult createOutOrder(OutStockInfo model)
        {
            LogHelper.Info("创建出库搬运任务-createOutOrder 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult result = new ResponseResult();
            try
            {
                result = ApiHelper.createOutOrder(model);
            }
            catch (BusinessException be)
            {
                result.success = false;
                result.message = be.Message;
            }
            catch (Exception ex)
            {
                result.success = false;
                result.message = "内部异常,请联系开发人员处理";
                LogHelper.Info($"内部异常,错误信息:{ex.Message}", "车间控制器");
                LogHelper.Info(ex.StackTrace, "车间控制器");
            }
            
            LogHelper.Info("创建出库搬运任务-createOutOrder 出参:" + JsonConvert.SerializeObject(result), "车间控制器");
            return result;
        }
 
        /// <summary>
        /// 3、校验模具信息接口
        /// </summary>
        /// <returns></returns>
        public ResponseResult verifyMouldInfo(MouldInfo model)
        {
            LogHelper.Info("校验模具信息接口-verifyMouldInfo 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult result = new ResponseResult();
            MouldCntr mouldCntr = MouldHelper.GetMouldCntr(new MouldCntr() { S_MOULD_NO = model.mouldNo, S_CNTR_CODE = model.trayCode });
            if (mouldCntr == null)
            {
                result.success = false;
                result.message = "模具与托盘不匹配";
            }
            else {
                result.success = true;
                result.message = "模具与托盘匹配成功";
            }
            LogHelper.Info("校验模具信息接口-verifyMouldInfo 出参:" + JsonConvert.SerializeObject(result), "车间控制器");
            return result;
        }
 
        /// <summary>
        /// 5、灵动AGV任务状态上报
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseResult lingDongAgvTaskStatus(LDAgvTaskInfo model)
        {
            LogHelper.Info("灵动AGV任务状态上报-lingDongAgvTaskStatus 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult result = new ResponseResult() { 
                success = true,
            };
            // 1、根据车间控制器任务ID查询WMS作业任务
            WMSTask mst = WMSHelper.GetWmsTaskBySrc(model.originNo);
            if (mst != null)
            {
                // 2.查询当前进行中的任务,并判断该任务是否是灵动任务
                WCSTask cst = WCSHelper.GetProceedTaskBySrcNo(mst.S_CODE);
                if (cst != null && cst.S_SCHEDULE_TYPE.Contains("LD")) {
                    var endLoc = LocationHelper.GetLoc(cst.S_END_LOC);
                    // 3、判断任务状态 离开 、完成
                    if (model.status.Equals("LEAVE"))
                    {
                        // 开始货位解绑物料信息、并解锁
                        LocationHelper.UnBindingLoc(cst.S_START_LOC, new List<string> { mst.S_CNTR_CODE });
                        LocationHelper.UnLockLoc(cst.S_START_LOC);
 
                        var line = WCSHelper.GetLinePlcInfo(cst.S_START_LOC);
                        if (line != null)
                        {
                            S7Helper.WriteInt(line.deviceNo, 101, line.writeAddr + 14, 1); // AGV 取货完成 1
                            LogHelper.Info("灵动AGV取货完成,并向输送线下发AGV任务反馈", "车间控制器");
                        }
                    }
                    if (model.status.Equals("FINISH") || ( model.status.Equals("LEAVE") && endLoc==null ) ) 
                    {
                        // 终点货位绑定物料信息、并解锁
                        if (mst.S_TYPE.Contains("入库")) {
                            LocationHelper.BindingLoc(cst.S_END_LOC, new List<string> { mst.S_CNTR_CODE });
                        }
                        LocationHelper.UnLockLoc(cst.S_END_LOC);
                        WCSHelper.End(cst);
 
                        var line = WCSHelper.GetLinePlcInfo(cst.S_END_LOC);
                        if (line != null)
                        {
                            S7Helper.WriteInt(line.deviceNo, 101, line.writeAddr + 14, 2); // AGV 放货完成 2
                            LogHelper.Info("灵动AGV放货完成,并向输送线下发AGV任务反馈", "车间控制器");
                        }
 
                        // 4、判断当前任务是否为最后任务,是则结束任务,并记录物料信息
                        if (mst.S_END_LOC.Equals(cst.S_END_LOC))
                        {
                            mst.N_B_STATE = 2;
                            mst.T_END_TIME = DateTime.Now;
                            WMSHelper.UpdateTaskState(mst);
 
                            // 4.1更新出入库记录
                            var cntrItemRels = ItemHelper.GetCntrItemByCntrCode(mst.S_CNTR_CODE);
                            if (cntrItemRels != null && cntrItemRels.Count > 0)
                            {
                                foreach (var cntrItemRel in cntrItemRels)
                                {
                                    WMSHelper.AddStockRecord(mst, cntrItemRel);
                                    // 4.2 终点货位在系统中没有维护,则删除物料信息
 
                                    if (endLoc == null || cntrItemRel.S_ITEM_TYPE.Equals("半成品"))
                                    {
                                        ContainerHelper.deleteCntrItem(mst.S_CNTR_CODE);
                                    }
                                }
                            }
                            else 
                            {
                                WMSHelper.AddStockRecord(mst, null);
                            }
                        }
                        else
                        {
                            List<string> areaCdoes = Settings.getStoreAreaCodes(2, 1);
                            if (!areaCdoes.Contains(mst.S_END_AREA) && mst.S_TYPE == "半成品/成品入库" && mst.S_END_LOC =="虚拟货位") 
                            {
                                // 横栋半成品称重,并回报车间控制器
                                float weight = WCSCore.getGjWeight(cst.S_END_LOC, mst.S_CNTR_CODE, mst.S_OP_DEF_CODE);
                                ProductWeightInfo productWeightInfo = new ProductWeightInfo() {
                                    wmsId = mst.S_CODE,
                                    trayCode = mst.S_CNTR_CODE,
                                    weight = weight.ToString(),
                                    weightTime = DateTime.Now.ToString(), 
                                };
                                ShopFloorControl.reportSemilFinishedProductWeight(productWeightInfo);
 
                                // 4.3如当前为一段任务,则需要创建二段任务
                                string itemCode = null;
                                var cntrItems = ContainerHelper.GetCntrItemRel(mst.S_CNTR_CODE);
                                if (cntrItems != null && cntrItems.Count > 0)
                                {
                                    itemCode = cntrItems[0].S_ITEM_CODE;
                                }
                                Location twoEndLoc = WMSHelper.GetEndLocation(mst.S_END_AREA, itemCode, 1, 1);
                                mst.S_END_LOC = twoEndLoc.S_CODE;
                                WMSHelper.UpdateTask(mst);
 
                                // 创建入库任务
                                WCSTask twoWcsTask = new WCSTask
                                {
                                    S_OP_NAME = mst.S_OP_DEF_NAME,
                                    S_OP_CODE = mst.S_CODE,
                                    S_CODE = WCSHelper.GenerateTaskNo(),
                                    S_CNTR_CODE = mst.S_CNTR_CODE,
                                    S_TYPE = mst.S_TYPE + "-2",
                                    S_START_LOC = cst.S_END_LOC,
                                    S_START_AREA = cst.S_END_AREA,
                                    S_END_LOC = mst.S_END_LOC,
                                    S_END_AREA = mst.S_END_AREA,
                                    S_SCHEDULE_TYPE = "HL"
                                };
                                if (WCSHelper.CreateTask(twoWcsTask))
                                {
                                    // 接驳位加出库锁,终点货位加入库锁
                                    LocationHelper.LockLoc(twoWcsTask.S_START_LOC, 2);
                                    LocationHelper.LockLoc(twoWcsTask.S_END_LOC, 1);
                                }
                            }
                        }
                    }
                }
            }
            else {
                    result.success = false;
                    result.message = "任务已完成";
            }
            LogHelper.Info("灵动AGV任务状态上报-lingDongAgvTaskStatus 出参:" + JsonConvert.SerializeObject(result), "车间控制器");
            return result;
        }
 
        /// <summary>
        /// 6.AGV安全交互接口(输送线、拆盘机)
        /// </summary>
        /// <returns></returns>
        public ResponseResult agvSafeRequest(SafeRequest model ) {
            LogHelper.Info("AGV安全交互接口-agvSafeRequest 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult()
            {
                success = false,
            };
            Location loc = LocationHelper.GetLoc(model.bit);
           /* var accessAreas = Settings.storeAreaInfos.Where(a => a.areaType == 2 && a.cargoType == 1).Select(a => a.accessArea).ToList();
*/
            PipelineSignalInfo lineSignalInfo = null;
            LinePlcInfo linePlcInfo = WCSHelper.GetLinePlcInfo(model.bit);
            if (linePlcInfo != null)
            {
                lineSignalInfo = WCSHelper.readPipelineInfo(linePlcInfo);
                LogHelper.Info("线体信号: " + JsonConvert.SerializeObject(lineSignalInfo), "车间控制器");
            }
 
            bool allow = false;
            if (loc != null )
            {
                switch (model.reqState) {
                    case "1":
                        // 请求取货
                        LogHelper.Info("请求取货.... " , "车间控制器");
                        if (lineSignalInfo != null && lineSignalInfo.agvInfo == 2 && (lineSignalInfo.faultMessage == 0 || lineSignalInfo.faultMessage == 1))
                        {
                            LogHelper.Info("向输送线下发AGV安全信号【9】 ", "车间控制器");
                            S7Helper.WriteInt(linePlcInfo.deviceNo, 101, linePlcInfo.writeAddr + 16, 9);
                            allow = true;
                        }
                        break;
                    case "2":
                        // 请求放货
                        LogHelper.Info("请求放货.... ", "车间控制器");
                        if (lineSignalInfo != null && lineSignalInfo.agvInfo == 1 && (lineSignalInfo.faultMessage == 0 || lineSignalInfo.faultMessage == 1))
                        {
                            LogHelper.Info("向输送线下发AGV安全信号【9】", "车间控制器");
                            S7Helper.WriteInt(linePlcInfo.deviceNo, 101, linePlcInfo.writeAddr + 16, 9);
                            allow = true;
                        }
                        break;
                    case "3":
                        // 安全退出
                        LogHelper.Info("安全退出.... ", "车间控制器");
                        if (lineSignalInfo != null)
                        {
                            if (linePlcInfo.actType == "入库")
                            {
                                S7Helper.WriteInt(linePlcInfo.deviceNo, 101, linePlcInfo.writeAddr + 14, 2); // AGV 放货完成 2
                                LogHelper.Info("灵动AGV放货完成,并向输送线下发AGV任务反馈", "车间控制器");
                            }
                            else if(linePlcInfo.actType == "出库")
                            {
                                S7Helper.WriteInt(linePlcInfo.deviceNo, 101, linePlcInfo.writeAddr + 14, 1); // AGV 取货完成 1
                                LogHelper.Info("灵动AGV取货完成,并向输送线下发AGV任务反馈", "车间控制器");
                            }
 
                            LogHelper.Info("安全退出【0】 ", "车间控制器");
                            S7Helper.WriteInt(linePlcInfo.deviceNo, 101, linePlcInfo.writeAddr + 16, 0);
                            allow = true;
                        }
                        break;
                }
                responseResult.success = allow;
            }
            
            LogHelper.Info("AGV安全交互接口-agvSafeRequest 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 7.查询分页接口(仅限半成品/成品)
        /// </summary>
        /// <returns></returns>
        public ResponseResult queryItemInfo(QueryItemModel model)
        {
            LogHelper.Info("查询分页接口-queryItemInfo 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            PageInfo page = ContainerHelper.GetCntrItemPage(model);
            ResponseResult result = new ResponseResult()
            {
                success = true,
                result = page
            };
            LogHelper.Info("查询分页接口-queryItemInfo 出参:" + JsonConvert.SerializeObject(result), "车间控制器");
            return result;
        }
 
        /// <summary>
        /// 9.模具数量更新
        /// </summary>
        /// <returns></returns>
        public ResponseResult mouldNumUpdate(MouldUpdateInfo model)
        {
            LogHelper.Info("模具数量更新-mouldNumUpdate 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult();
 
            bool usable = true;
            // 更新模具类型 和 状态
            MouldCntr mouldCntr = MouldHelper.GetMouldCntr(model.mouldNo);
            if (mouldCntr != null)
            {
                mouldCntr.S_MOULD_TYPE = model.mouldType;
 
                // 更新子模具数据
                var mouldDetails = MouldHelper.GetMouldDetails(model.mouldNo);
                foreach (var item in mouldDetails)
                {
                    foreach (var md in model.itemInfoList)
                    {
                        if (item.S_NAME.Equals(md.itemName))
                        {
                            int currentNum = item.N_QTY;
                            if (model.actionType == "1")
                            {
                                currentNum = currentNum + md.num;
                            }
                            else if (model.actionType == "2")
                            {
                                currentNum = currentNum - md.num;
                            }
 
                            if (currentNum >= 0)
                            {
                                if (item.N_INIT_QTY > currentNum)
                                {
                                    usable = false;
                                }
                                else if (item.N_INIT_QTY < currentNum)
                                {
                                    responseResult.success = false;
                                    responseResult.message = md.itemName + "当前数量大于初始数量";
                                    return responseResult;
                                }
                            }
                            else
                            {
                                responseResult.success = false;
                                responseResult.message = md.itemName + "当前数量小于0";
                                return responseResult;
                            }
                            item.N_QTY = currentNum;
                        }
                    }
                }
                MouldHelper.updateMouldNum(mouldDetails);
               /* mouldCntr.S_ABLE_FLAG = usable ? "是" : "否";*/
                MouldHelper.updateMouldStatus(mouldCntr);
            }
            else
            {
                responseResult.success = false;
                responseResult.message = "未找到该模具,请检查模具号是否正确";
                return responseResult;
            }
            responseResult.success = true;
            LogHelper.Info("模具数量更新-mouldNumUpdate 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 11.模具编号数据查询
        /// </summary>
        /// <returns></returns>
        public ResponseResult queryByMouldNo(MouldBase model)
        {
            LogHelper.Info("模具编号数据查询-queryByMouldNo 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult() { 
                success = true,
            };
            MouldCntr mouldCntr = MouldHelper.GetMouldCntr(new MouldCntr() { S_MOULD_NO = model.mouldNo });
            List<TrayMouldInfo> trayItemInfos = new List<TrayMouldInfo>();
            if (mouldCntr != null)
            {
                foreach (var item in mouldCntr.mouldDetails)
                {
                    TrayMouldInfo trayItemInfo = new TrayMouldInfo()
                    {
                        trayCode = mouldCntr.S_CNTR_CODE,
                        itemCode = mouldCntr.S_ITEM_CODE,
                        itemName = item.S_NAME,
                        mouldNo = mouldCntr.S_MOULD_NO,
                        quantity = item.N_INIT_QTY,
                    };
                    trayItemInfos.Add(trayItemInfo);
                }
                responseResult.result = trayItemInfos;
            }
            else {
                responseResult.success = false;
                responseResult.message = "没有此模具信息";
            }
            LogHelper.Info("模具编号数据查询-queryByMouldNo 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
 
        /// <summary>
        /// 12.查询钢卷原始重量
        /// </summary>
        /// <returns></returns>
        public ResponseResult queryCoilInitWeight(CoilItemBase model)
        {
            LogHelper.Info("查询钢卷原始重量-queryCoilInitWeight 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult() { success = true};
            CoilInitWeight coilWeightInfo = new CoilInitWeight();
            var coilInfo = ItemHelper.GetCoilItemInfo(model.coilNo);
            if (coilInfo != null)
            {
                coilWeightInfo.initWeight = coilInfo.F_INIT_WEIGHT.ToString();
                responseResult.result = coilWeightInfo;
            }
            else {
                responseResult.success = false;
                responseResult.message = "没有此钢卷信息";
            }
            LogHelper.Info("查询钢卷原始重量-queryCoilInitWeight 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 13.查询窄钢卷最初入库重量
        /// </summary>
        /// <returns></returns>
        public ResponseResult queryNarrowCoil(CoilItemBase model)
        {
            LogHelper.Info("查询窄钢卷最初入库重量-queryNarrowCoil 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult(){success = true};
            List<CoilInfo> coilInfos = new List<CoilInfo>();
            var coilList = ItemHelper.GetNarrowCoilInfo(model.coilNo);
            if (coilList != null) {
                foreach (var item in coilList)
                {
                    CoilInfo coilInfo = new CoilInfo()
                    {
                        coilNo = item.S_COIL_NO,
                        initWeight = item.F_INIT_WEIGHT.ToString(),
                    };
                    coilInfos.Add(coilInfo);
                }
                responseResult.result = coilInfos;
            }
            LogHelper.Info("查询窄钢卷最初入库重量-queryNarrowCoil 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 14.查询空托货位
        /// </summary>
        /// <returns></returns>
        public ResponseResult queryEmptyTrayLoc(StationBase station)
        {
            LogHelper.Info("查询空托货位-queryEmptyTrayLoc 入参:", "车间控制器");
            ResponseResult responseResult = new ResponseResult() { success = true };
 
            int readDB = 100;
            var devicePlcInfos = Settings.devicePlcInfos.Where(a => a.deviceType == 3).ToList();
            if (station != null && station.bit != null) 
            {
                LinePlcInfo linePlcInfo = WCSHelper.GetLinePlcInfo(station.bit);
                devicePlcInfos = devicePlcInfos.Where(a => a.deviceNo == linePlcInfo.deviceNo).ToList();
            }
 
            try
            {
                foreach (var item in devicePlcInfos)
                {
                    string deviceNo = item.deviceNo;
                    short[] segment1 = S7Helper.ReadInt(deviceNo, readDB, 120, 2);
                    short segment2 = S7Helper.ReadInt(deviceNo, readDB, 130);
                    LogHelper.Info(item.deviceName+",编号:"+item.deviceNo+ ",查询拆盘机信号:" + JsonConvert.SerializeObject(segment1), "车间控制器");
                    if (segment1 != null && segment1[1] == 1 && segment2 == 0)
                    {
                        var sortAreaInfo = Settings.sortAreaInfos.Where(a => a.areaCode == item.area).FirstOrDefault();
                        if (sortAreaInfo != null)
                        {
                            string loc = sortAreaInfo.outLocCode;
                            var location = LocationHelper.GetLoc(loc);
                            if (location.N_LOCK_STATE == 0)
                            {
                                EmptyTrayLoc emptyTrayLoc = new EmptyTrayLoc()
                                {
                                    locCode = location.S_CODE,
                                    areaCode = location.S_AREA_CODE,
                                };
                                responseResult.result = emptyTrayLoc;
                            }
                        }
                    }
                }
            }
            catch (Exception ex) 
            {
                LogHelper.Info("查询空托货位错误,错误原因:" + ex.Message, "车间控制器");
            }
            LogHelper.Info("查询空托货位-queryEmptyTrayLoc 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 15.取消任务
        /// </summary>
        /// <returns></returns>
        public ResponseResult cancelTask(TaskBase model)
        {
            LogHelper.Info("取消任务-cancelTask 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult() { success = true };
            if (model.transferId == null) {
                responseResult.success = false;
                responseResult.message = "物流ID不能为空";
                return responseResult;
            }
            WMSTask wmsTask = WMSHelper.GetWmsTaskBySrc(model.transferId);
            
            if (wmsTask != null && wmsTask.N_B_STATE < 2) {
                List<WCSTask> wcsTasks = WCSHelper.GetTaskBySrcNo(wmsTask.S_CODE);
                if (wcsTasks.Count > 0) 
                {
                    foreach (var wcsTask in wcsTasks)
                    {
                        if (wcsTask.S_SCHEDULE_TYPE.Contains("RB"))
                        {
                            if (wcsTask.N_B_STATE == 2 && wcsTask.S_TYPE.Contains("出库")) 
                            {
                                LocationHelper.UnBindingLoc(wcsTask.S_END_LOC, new List<string>() { wcsTask.S_CNTR_CODE });
                            }
 
                            if (wcsTask.N_B_STATE == 0 && wcsTask.S_TYPE.Contains("入库"))
                            {
                                LocationHelper.UnBindingLoc(wcsTask.S_START_LOC, new List<string>() { wcsTask.S_CNTR_CODE });
                            }
 
                            if (wcsTask.N_B_STATE > 0) 
                            {
                                continue;
                            }
                        }
 
                        if (wcsTask.S_SCHEDULE_TYPE.Contains("LD"))
                        {
                            if (wcsTask.S_TYPE.Contains("出库"))
                            {
                                var locCntrs = LocationHelper.GetLocCntrRel(wcsTask.S_START_LOC);
                                if (locCntrs.Count > 0) 
                                {
                                    var cntrList = locCntrs.Select(a => a.S_CNTR_CODE).ToList();
                                    if (cntrList.Contains(wcsTask.S_CNTR_CODE)) 
                                    {
                                        var linePlcInfo = WCSHelper.GetLinePlcInfo(wcsTask.S_START_LOC);
                                        triggerAlarmCommand(linePlcInfo , 1);
                                    }
                                }
                            }
 
                            LocationHelper.UnBindingLoc(wcsTask.S_START_LOC, new List<string>() { wcsTask.S_CNTR_CODE });
                            LocationHelper.UnBindingLoc(wcsTask.S_END_LOC, new List<string>() { wcsTask.S_CNTR_CODE });
 
                            var locCntrRels = LocationHelper.GetLocCntr(wcsTask.S_CNTR_CODE);
                            if (locCntrRels.Count > 0)
                            {
                                LocationHelper.UnBindingLoc(locCntrRels[0].S_LOC_CODE, new List<string>() { locCntrRels[0].S_CNTR_CODE });
                            }
 
                            var cntrItemRels = ItemHelper.GetCntrItemByCntrCode(wmsTask.S_CNTR_CODE);
                            if (cntrItemRels != null && cntrItemRels.Count > 0)
                            {
                                foreach (var cntrItemRel in cntrItemRels)
                                {
                                    ContainerHelper.deleteCntrItem(wmsTask.S_CNTR_CODE);
                                }
                            }
 
                            
                        }
 
 
                        WCSHelper.Fail(wcsTask);
                        LocationHelper.UnLockLoc(wcsTask.S_START_LOC);
                        LocationHelper.UnLockLoc(wcsTask.S_END_LOC);
 
                        wmsTask.N_B_STATE = 3;
                        wmsTask.T_END_TIME = DateTime.Now;
                        WMSHelper.UpdateTaskState(wmsTask);
                    }
                }
                else
                {
                    wmsTask.N_B_STATE = 3;
                    wmsTask.T_END_TIME = DateTime.Now;
                    WMSHelper.UpdateTaskState(wmsTask);
 
                    LocationHelper.UnLockLoc(wmsTask.S_START_LOC);
                    LocationHelper.UnLockLoc(wmsTask.S_END_LOC);
                }
            }
            LogHelper.Info("取消任务-cancelTask 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 16.入库口补录任务
        /// </summary>
        /// <returns></returns>
        public ResponseResult supplTask(InStockInfo model)
        {
            ResponseResult responseResult = new ResponseResult();
            LogHelper.Info("补录任务-supplTask 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
 
            if (model.taskType.Equals("半成品/成品入库") && (model.trayItemInfoList == null || model.trayItemInfoList.Count == 0))
            {
                responseResult.success = false;
                responseResult.message = "物料信息不能为空!";
                return responseResult;
            }
            Location endLoc = LocationHelper.GetLoc(model.endBit);
            if (endLoc != null)
            {
                // 绑定容器物料信息
                model.trayItemInfoList.ForEach(it =>
                {
                    CntrItemRel cntrItemRel = new CntrItemRel()
                    {
                        S_ITEM_CODE = it.itemCode,
                        S_ITEM_NAME = it.itemName,
                        S_WORK_NO = it.workNo,
                        S_WORK_NAME = it.workName,
                        S_DEPART_NO = it.dePartNo,
                        S_PART_TASK_ID = it.partTaskId,
                        S_PARTDRAW_NO = it.partDrawNo,
                        S_PART_NAME = it.partName,
                        S_OPER_NO = it.operNo,
                        S_OPER_NAME = it.operName,
                        F_QTY = float.Parse(it.quantity == null ? "0" : it.quantity),
                        F_WEIGHT = float.Parse(it.weight == null ? "0" : it.weight),
                        S_ITEM_SPEC = it.spec,
                        S_ITEM_TYPE = it.isFinish
                    };
                    ContainerHelper.BindCntrItemSingle(model.trayCode, cntrItemRel);
                });
                string areaCode = Settings.getStoreAreaByAccessArea(model.endArea);
                var wmsTask = new WMSTask()
                {
                    S_CNTR_CODE = model.trayCode,
                    S_CODE = WMSHelper.GenerateTaskNo(),
                    S_START_LOC = endLoc.S_CODE,
                    S_START_AREA = endLoc.S_AREA_CODE,
                    S_END_LOC = "虚拟货位",
                    S_END_AREA = areaCode,
                    S_TYPE = model.taskType,
                    S_OP_DEF_CODE = model.taskNo,
                    S_OP_DEF_NAME = "入库搬运任务(补)",
                    N_PRIORITY = model.taskPri == null ? 1 : int.Parse(model.taskPri),
                    T_START_TIME = DateTime.Now,
                };
                if (WMSHelper.CreateWmsTask(wmsTask))
                {
                    LocCntrRel locCntrRel = ContainerHelper.getLocCntrByLoc(endLoc.S_CODE);
                    if (locCntrRel != null)
                    {
                        LocationHelper.UnBindingLoc(endLoc.S_CODE, new List<string> { locCntrRel.S_CNTR_CODE });
                    }
                    LocationHelper.BindingLoc(endLoc.S_CODE, new List<string> { model.trayCode });
                }
            }
            else {
                responseResult.success = false;
                responseResult.message = "终点货位不存在";
            }
 
            LogHelper.Info("补录任务-supplTask 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        /// <summary>
        /// 17.查询托盘是否存在WMS任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseResult findWmsTaskExist(TrayInfo model) 
        {
            LogHelper.Info("查询WMS任务-findWmsTask 入参:" + JsonConvert.SerializeObject(model), "车间控制器");
            ResponseResult responseResult = new ResponseResult();
            WmsTaskExistResponse response = new WmsTaskExistResponse() {
                isExist = false
            };
            var wmsTask = WMSHelper.GetWmsTaskByCntr(model.trayCode);
            if (wmsTask != null)
            {
                response.isExist = true;
                response.transferId = wmsTask.S_OP_DEF_CODE;
            }
            responseResult.result = response;
            LogHelper.Info("查询WMS任务-findWmsTask 出参:" + JsonConvert.SerializeObject(responseResult), "车间控制器");
            return responseResult;
        }
 
        public class WmsTaskExistResponse
        {
            public bool isExist { get; set; }
            public string transferId { get; set; }
        }
 
        public class TaskBase {
            public string transferId { get; set; }
        }
 
        public class EmptyTrayLoc {
            public string locCode { get; set; }
            public string areaCode { get; set; }
        }
 
        public class SteelCoilInStockInfo {
            public string coilNo { get; set; }
            public string locCode { get; set; }
        }
 
        public class CoilItemBase {
            public string coilNo { get; set; }
        }
        public class CoilInitWeight {
            public string initWeight { get; set; }
        }
        public class CoilInfo {
            public string coilNo { get; set; }
            public string initWeight { get; set; }
        }
 
        public class SafeRequest
        {
            public string bit { get; set; } // 货位编码
            public string reqState { get; set; } // 请求状态 1-请求取货  2-请求放货 3-安全退出
        }
        public class LDAgvTaskInfo
        {
            public string robotCode { get; set; } // 机器人编号,只有任务下发到车上该字段才会传过去,否则不传
            public string transportNo { get; set; } // 搬运单号(wms)
            public string stationName { get; set; } // 到达点位,离开点位的名称
            public string originNo { get; set; } // 上级任务号 (车间控制器)
            public string status { get; set; } // 任务状态
        }
 
        public class StationBase
        {
            public string bit { get; set; }  // 货位编码
        }
       
        public class MouldUpdateInfo
        {
            public string mouldNo { get; set; } // 模具序列号
            public string actionType { get; set; } // 操作类型 1-入库 2-出库 传数字编号即可
            public string mouldType { get; set; } // 模具类型 主片模具 、项片模具 、大项片模具
           
            public List<itemInfo> itemInfoList { get; set; }
 
            public class itemInfo
            {
                public string itemName { get; set; }
                public int num { get; set; }
            }
        }
 
        public class PageInfo
        {
            public int pageNum { get; set; }
            public int pageSize { get; set; }
            public int totalCount { get; set; }
            public object data { get; set; }
        }
        public class QueryItemModel
        {
            public string trayCode { get; set; }
            public string itemCode { get; set; }  // 物料编码
            public string workNo { get; set; }  // 工作号
            public string dePartNo { get; set; }  // 部套号
            public string partDrawNo { get; set; }  // 零件图号
            public string areaCode { get; set; }  // 库区
            public string startTime { get; set; }  // 开始时间
            public string endTime { get; set; }  // 结束时间
            public int pageNum { get; set; }  // 当前页
            public int pageSize { get; set; }  // 每页数量
        }
        public class MouldBase {
            public string mouldNo{ get; set; }
        }
 
        public class TrayInfo
        {
            public string trayCode { get; set; }
        }
 
        /// <summary>
        /// 入库请求信息
        /// </summary>
        public class InStockInfo
        {
            public string taskNo { get; set; } // 车间控制器的任务号
            public string taskType { get; set; } // 任务类型(1-钢卷入库、2-模具入库、3-半成品/成品入库 、4-空盘垛入库 传输中文)
            public string taskPri { get; set; } // 优先级
            public string startBit { get; set; } // 起点货位编码
            public string startArea { get; set; } // 起点库区编码
            public string endBit { get; set; } // 终点货位编码
            public string endArea { get; set; } // 终点库区编码
            public string reqTime { get; set; } // 请求时间
            public string trayCode { get; set; } // 托盘编码 钢卷入库:传输钢卷号
            public List<TrayItemInfoReq> trayItemInfoList { get; set; } // 托盘物料信息
        }
        
        public class TrayItemInfoReq
        {
            public string workNo { get; set; } //工作号
            public string workName { get; set; } //工作号
            public string dePartNo { get; set; } //部套号
            public string partTaskId { get; set; } //零件任务号
            public string partDrawNo { get; set; } //零件图号
            public string partName { get; set; } //零件名称
            public string operNo { get; set; } //工序编号
            public string operName { get; set; } //工序名称
            public string itemCode { get; set; } //物料编码
            public string itemName { get; set; } //物料名称
            public string quantity { get; set; } //数量-钢卷无数量
            public string weight { get; set; } //重量-仅钢卷有重量
            public string spec { get; set; } // 货品规格
            public string isFinish { get; set; } //是否为成品物料 1-成品 2-半成品
        }
 
        public class TrayItemInfoRes
        {
            public string trayCode { get; set; } //托盘码
            public string workNo { get; set; } //工作号
            public string workName { get; set; } //工作号
            public string dePartNo { get; set; } //部套号
            public string partTaskId { get; set; } //零件任务号
            public string partDrawNo { get; set; } //零件图号
            public string partName { get; set; } //零件名称
            public string operNo { get; set; } //工序编号
            public string operName { get; set; } //工序名称
            public string itemCode { get; set; } //物料编码
            public string itemName { get; set; } //物料名称
            public string quantity { get; set; } //数量-钢卷无数量
            public string weight { get; set; } //重量-仅钢卷有重量
            public string spec { get; set; } // 货品规格
            public string width { get; set; } // 钢卷宽度
            public string isFinish { get; set; } //是否为成品物料 1-成品 2-半成品
            public string fullTime { get; set; } //入库时间
            public string areaCode { get; set; } //所在库区
        }
 
        public class InStockRetureData
        {
            public string startStationCode { get; set; } //起点货位编码
            public string startAreaCode { get; set; } //起点库区编码
            public string endStationCode { get; set; } //起点库区编码
            public string endAreaCode { get; set; } //起点库区编码
            public string wmsId { get; set; } //WMS任务号
        }
 
        public class OutStockInfo
        {
            public string taskNo { get; set; } // 车间控制器的任务号
            public string taskType { get; set; } // 任务类型(1-钢卷出库、2-模具出库、3-半成品/成品出库 4-模具托盘出库 5-叫托盘出库 传输中文)
            public string taskPri { get; set; } // 优先级
            public string startBit { get; set; } // 起点货位编码
            public string startArea { get; set; } // 起点库区编码
            public string endBit { get; set; } // 终点货位编码
            public string endArea { get; set; } // 终点库区编码
            public string reqTime { get; set; } // 请求时间
            public string workNo { get; set; } // 工作号
            public string width { get; set; } // 钢卷宽度
            public string weight { get; set; } // 钢卷重量
            public string coilNo { get; set; } // 钢卷号
 
            // 物料编码 按照车间控制器传输的物料编码出库  模具出库-模具号或者模具编号 钢卷-(不是钢卷号) 成品/半成品-入库传的物料编码
            public string itemCode { get; set; }
            public string partTaskId { get; set; } // 零件任务号(半成品/成品)
            public string trayCode { get; set; } // 托盘号
            
        }
 
        public class OutStockReturnData {
            public string startStationCode { get; set; } //起点货位编码
            public string startAreaCode { get; set; } //起点库区编码
            public string endStationCode { get; set; } //起点库区编码
            public string endAreaCode { get; set; } //起点库区编码
            public string wmsId { get; set; } //WMS任务号
            public List<TrayItemInfoRes> trayItemInfo { get; set; } //WMS任务号
        }
        public class ResponseResult
        {
            public bool success { get; set; } = true; // 是否成功
            public string message { get; set; } // 返回信息
            public object result { get; set; } // 附加数据
            public string e { get; set; } // 错误信息
        }
 
        public class MouldInfo
        {
            public string trayCode { get; set; } // 托盘码
            public string mouldNo { get; set; } // 模具序列号
        }
 
        public class TrayMouldInfo
        {
            public string trayCode { get; set; } // 托盘码
            public string itemCode { get; set; } // 物料编码(模具编码)
            public string itemName { get; set; } // 物料名称
            public string mouldNo { get; set; } // 模具序列号(
            public int quantity { get; set; } // 数量
        }
 
        public class TaskRequest { 
            public int type { get; set; } // 1、入库 2、出库
            public string area_code { get; set; }
            public string item_code { get; set; }
            public string cntr_code { get; set; } // 托盘编码
        }
 
        public class CallbackInfo { 
            public string requestId { get; set; }  // 任务请求ID
 
            public int status { get; set; } // 状态 2、完成 3、取消
        }
 
        public class TaskInfo {
            public int type { get; set; } // 1、入库 2、出库
            public string loc { get; set; } // 货位
            public string item { get; set; } // 物料编码
            public string targetArea { get; set; } // 目标库区
        }
 
    }
}