lss
2025-05-30 38eff4fc0100131b180ffa872009b502629743f5
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
using Newtonsoft.Json;
using NongFuWebApi.Services;
using NongFuWebApi.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static NongFuWebApi.Controllers.AMSModel;
using System.Configuration;
 
namespace NongFuWebApi.Models
{
    public class ApiHelper
    {
        private static HttpHelper httpHelper = new HttpHelper();
        static string url = ConfigurationManager.AppSettings["WMSUrl"].ToString();
 
        #region 靖宇接口方法
        /// <summary>
        /// 工单信息获取接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel WorkInfo(TASK_WorkInfoModel model)
        {
            SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
            try
            {
                var db = new SqlHelper<WorkOrder1>().GetInstance();
                var workInfo = db.Queryable<WorkOrder1>().Where(a => a.S_WorkNo == model.WorkNo).First();
                //bool req = false;
                //查询执行中或者处于暂停的  当前产线工单
                //如果不存在,即插入工单表数据信息,默认执行中
                //反之,判断当前是否为  结束工单,如果是,更新结束时间,反之,返回工单已存在
                //WorkState  1-新建  2-完成
 
                if (workInfo == null && model.WorkState == "1")
                {
                    //如果物料名称用物料表物料名称,查询物料表物料名称
 
                    var iteminfo = db.Queryable<ItemInfo>().Where(a => a.S_ITEM_CODE == model.MaterialCode).First();
                    if (iteminfo != null)
                    {
                        model.ItemCode = iteminfo.S_ITEM_NAME;
                    }
 
 
                    WorkOrder1 wmsTask = new WorkOrder1()
                    {
                        S_WorkNo = model.WorkNo,
                        S_PLineNo = model.DeviceName,
                        // SQL_UsingNo = "N",
                        S_ORDER_TYPE = "产线下线",
                        S_ItemCode = model.MaterialCode,
                        SQL_ItemName = model.ItemCode,
                        S_WorkState = "新建",
                        withCode = "是",
                        S_Modify = model.WorkTime,
                        // MaterialCode = model.MaterialCode
                    };
                    TaskHelper.JingYuInsertWork(wmsTask);
                    result.success = true;
                    result.errMsg = $"工单信息接收成功,工单已创建!";
                    string deviceName = model.DeviceName;
                    //SendPlcWorkState(deviceName);
                }
                else if (workInfo != null && model.WorkState == "2")
                {
                    if (!workInfo.S_WorkState.Trim().Contains("完成"))
                    {
                        TaskHelper.UpdateStatus1(workInfo, "已完成", model.WorkTime);
                        result.success = true;
                        result.errMsg = $"工单信息接收成功,工单已完成!";
                        string deviceName = model.DeviceName;
                        //SendPlcWorkState(deviceName, false);
                    }
                    else result.errMsg = $"工单信息接收成功,工单已完成!";
                }
                else if (workInfo != null && model.WorkState == "1") result.errMsg = $"当前工单已存在。工单号:{workInfo.S_WorkNo},产线号:{model.DeviceName}";
            }
            catch (Exception ex)
            {
                result.errMsg = $"工单信息接收异常:{ex.Message}";
            }
            return result;
        }
 
        /// <summary>
        /// 富勒托盘  带更改托盘码的托盘信息下发方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel SendTrayCode(SendTrayCodeModel model)
        {
            SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
            var db = new SqlHelper<object>().GetInstance();
            string msg = "";
            try
            {
                model.TrayInfo.ForEach(a =>
                {
                    /* 富勒更新异常托盘流程
                     * 1.替换码必定有值
                     * 2.如果存在原码:
                     *      a.中间表有数据,直接使用 替换码 替换中间表 原码 托盘数据      --OVER
                     *      b.中间表,托盘物料表 均无 原码 数据,则 使用托盘码插入数据    --OVER
                     *      c.中间表无数据,托盘物料表有 原码 数据,则表示,当前托盘已经生成任务,
                     *        直接替换 任务表,托盘物料表数据  备注:如果 货位托盘表 存在数据,则需要一并替换    --OVER
                     * 3.如果不存在原码:
                     *      a.直接使用 替换码 插入中间表 数据     --OVER
                     * **/
                    var trayInfo = TaskHelper.GetJingYuTrayInfo(a.trayCode, a.trayNum);
                    if (trayInfo == null)
                    {
                        string TrayCode = string.IsNullOrEmpty(a.trayCode) ? a.sign : a.trayCode;
                        if (!string.IsNullOrEmpty(a.sign) && !string.IsNullOrEmpty(a.trayCode))
                        {
                            //中间表无数据,判断托盘物料表
                            var trayItemTable = db.Queryable<CntrItemRel>().Where(it => it.S_CNTR_CODE == a.trayCode).First();
                            if (trayItemTable != null)
                            {
                                //存在数据--更新相关任务数据以及当前托盘物料数据
                                trayItemTable.S_CNTR_CODE = a.sign;
                                var taskInfo = db.Queryable<WMSTask>().Where(it => it.S_CNTRS.Contains(a.trayCode)).OrderByDescending(it => it.T_CREATE).First();
                                if (taskInfo != null)
                                {
                                    string cntrs = taskInfo.S_CNTRS.Replace(a.trayCode, a.sign);
                                    taskInfo.S_CNTRS = cntrs;
                                    db.Updateable(trayItemTable).WhereColumns(it => new { it.S_ID }).UpdateColumns(it => new { it.S_CNTR_CODE }).ExecuteCommand();
                                    db.Updateable(taskInfo).UpdateColumns(b => new { b.S_CNTRS }).ExecuteCommand();
                                    // UpdateScreenTable(a, db);//更新 屏幕显示表 托盘数据
                                    msg = msg + $"更新托盘数据成功:已同步任务以及托盘物料表相关更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                                    var locTrayInfo = db.Queryable<LocCntrRel>().Where(it => it.S_CNTR_CODE == a.trayCode).First();
                                    if (locTrayInfo != null)
                                    {
                                        locTrayInfo.S_CNTR_CODE = a.sign;
                                        db.Updateable(locTrayInfo).UpdateColumns(b => new { b.S_CNTR_CODE }).ExecuteCommand();
                                    }
                                    result.success = true;
                                }
                            }
                            else
                            {
                                TaskHelper.InsertTrayCodeJy(a, a.sign, a.deviceName, db, ref msg);//不存在数据--插入中间表数据
                                msg = msg + $"更新托盘数据异常:当前 托盘物料表不存在此托盘信息,直接插入中间表数据。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            }
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据处理反馈:" + $"{msg}", "ThirdSystemLog");
                        }
                        else
                        {
                            TaskHelper.InsertTrayCodeJy(a, TrayCode, a.deviceName, db, ref msg);
                            result.success = true;
                            result.errCode = 0;
                            if (!string.IsNullOrEmpty(a.trayCode)) msg = msg + $"插入托盘数据成功:托盘号:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            else msg = msg + $"更新托盘数据成功:托盘号:{a.trayCode},更新码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:处理托盘数据成功!" + $"托盘码:{a.trayCode},更新码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(a.sign))
                        {
                            trayInfo.workNo = a.workNo;
                            trayInfo.trayCode = a.sign;
                            trayInfo.trayNum = a.trayNum;
                            trayInfo.batchNo = a.batchNo;
                            trayInfo.trayRule = a.trayRule;
                            trayInfo.itemLayer = a.itemLayer;
                            db.Updateable(trayInfo).UpdateColumns(b => new { b.workNo, b.trayCode, b.trayNum, b.batchNo, b.trayRule, b.itemLayer }).ExecuteCommand();
                            var workInfo = db.Queryable<WorkOrder1>().Where(b => b.S_PLineNo == a.deviceName && b.S_WorkNo == a.workNo).First();
                            if (workInfo != null)
                            {
                                workInfo.S_ItemLayer = a.itemLayer;
                                db.Updateable(workInfo).UpdateColumns(b => new { b.S_ItemLayer }).ExecuteCommand();
                            }
                            //UpdateScreenTable(a, db);//更新 屏幕显示表 托盘数据
                            result.success = true;
                            msg = msg + $"更新托盘数据成功:托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据成功!" + $"托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};", "ThirdSystemLog");
                        }
                        else
                        {
                            result.success = false;
                            msg = msg + $"更新托盘数据失败:未传输替换码。托盘号:{a.trayCode},替换码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据失败!" + $"托盘码:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
                        }
                    }
                });
                result.errMsg = msg;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"SendTrayCode Error:{ex.Message}", ex);
            }
            return result;
        }
 
        /// <summary>
        /// 人工分拣出库接口解绑托盘
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel UnbindingTrayCode(UnbindingTrayCodeModel model)
        {
            var result = new SimpleResultModel { success = false, errCode = -1 };
            string msg = ""; int n = 0; string locCode = "";
            var db = new SqlHelper<object>().GetInstance();
            if (model.TrayInfo != null)
            {
                for (int i = 0; i < model.TrayInfo.Count(); i++)
                {
 
                    string cntrCode = model.TrayInfo[i];
                    LogHelper.Info($"删除托盘{cntrCode}");
                    var trayInfo = db.Queryable<LocCntrRel>().Where(a => a.S_CNTR_CODE == cntrCode).First();
                    if (trayInfo != null)
                    {
                        locCode = trayInfo.S_LOC_CODE.Trim();
                        //排锁表操作
                        //if (!string.IsNullOrEmpty(locCode))
                        //{
                        //    var locInfo = db.Queryable<Location>().Where(a => a.S_LOC_CODE == locCode).First();
                        //    if (locInfo != null)
                        //    {
                        //        TaskHelper.YiKuUnLockRow(db, locInfo.N_ROW.Trim(), locInfo.S_AREA_CODE.Trim(), false, false, true);
                        //        var Srow = db.Queryable<RowLock>().Where(a => a.N_ROW == locInfo.N_ROW.Trim() && a.S_AREA_CODE == locInfo.S_AREA_CODE.Trim()).First();
                        //        if (Srow != null)
                        //        {
                        //            Srow.S_LOCK_STATE = "其他锁";
                        //            db.Updateable(Srow).UpdateColumns(a => new { a.S_LOCK_STATE }).ExecuteCommand();
                        //        }
                        //    }
                        //}
 
 
                        result.success = true;
                        if (db.Deleteable<LocCntrRel>(a => a.S_CNTR_CODE == cntrCode).ExecuteCommand() <= 0)
                        {
                            db.Deleteable<LocCntrRel>(a => a.S_CNTR_CODE == cntrCode).ExecuteCommand();
                        }
                        if (db.Deleteable<CntrItemRel>(a => a.S_CNTR_CODE == cntrCode).ExecuteCommand() <= 0)
                        {
                            db.Deleteable<CntrItemRel>(a => a.S_CNTR_CODE == cntrCode).ExecuteCommand();
                        }
 
                        msg = msg + $"{n++}:当前托盘号:{model.TrayInfo[i]},解绑成功!";
                    }
                    else
                    {
                        msg = msg + $"{n++}:当前托盘号:{model.TrayInfo[i]},不存在对应货位!";
                    }
                    if (!string.IsNullOrEmpty(locCode))
                    {
                        var locNum = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == locCode).ToList();
                        int num = 0;
                        var locInfo = db.Queryable<Location>().Where(a => a.S_LOC_CODE == locCode).First();
 
                        if (locInfo != null)
                        {
                            RowLocLock(locInfo);
                            locInfo.N_CURRENT_NUM = GetLocTrayNum(locNum, num);
                            LogHelper.Info("UnbindingTrayCode Dispose:更新当前货位当前数量!" + $"货位编码:{locCode},数量:{locInfo.N_CURRENT_NUM}", "ThirdSystemLog");
                            db.Updateable(locInfo).UpdateColumns(a => new { a.N_CURRENT_NUM }).ExecuteCommand();
                            var locaHaveTray = db.Queryable<Location>().Where(a => a.N_ROW == locInfo.N_ROW.Trim() && a.N_CURRENT_NUM > 0).ToList();
                            if (locaHaveTray.Count == 0) TaskHelper.YiKuUnLockRow(db, locInfo.N_ROW.Trim(), locInfo.S_AREA_CODE.Trim(), true, false, true);
                            //else TaskController.Monitor.YiKuUnLockRow(db, locInfo.N_ROW.Trim(), locInfo.S_AREA_CODE.Trim(), false);
                        }
                    }
                }
            }
            else
            {
                msg = $"人工分拣出库:富勒传输数据异常!传输为空值!";
            }
            result.errMsg = msg;
            LogHelper.Info("UnbindingTrayCode Dispose:" + result.errMsg, "ThirdSystemLog");
            return result;
        }
        /// <summary>
        /// 分拣出库锁定一排,托盘全部解绑完成解锁单个货位
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="res"></param>
        private static void RowLocLock(Location loc)
        {
            var db = new SqlHelper<object>().GetInstance();
            if (loc.S_LOCK_STATE != "无")
            {
                var locNum = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == loc.S_LOC_CODE).ToList();
                if (locNum.Count <= 0)
                {
                    var loclist = db.Queryable<Location>().Where(a => a.S_AREA_CODE == loc.S_AREA_CODE && a.N_ROW == loc.N_ROW && a.N_CURRENT_NUM > 0).ToList();
                    if (loclist.Count > 1)
                    {
                        TaskHelper.UpdateLocStock(loc, "无");
                    }
 
                }
            }
            else
            {
                var loclist = db.Queryable<Location>().Where(a => a.S_AREA_CODE == loc.S_AREA_CODE && a.N_ROW == loc.N_ROW && a.N_CURRENT_NUM > 0).ToList();
                if (loclist.Count > 0)
                {
                    loclist.ForEach(a =>
                    {
                        TaskHelper.UpdateLocStock(a, "出库锁");
 
                    });
                }
            }
 
 
        }
 
        #endregion
 
 
 
        /// <summary>
        /// 工单信息获取接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel WorkInfoCA(TASK_WorkInfoModel model)
        {
            SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
            try
            {
                var db = new SqlHelper<ChunAnWorkOrder>().GetInstance();
                var workInfo = db.Queryable<ChunAnWorkOrder>().Where(a => a.SQL_WorkNo == model.WorkNo).First();
                //bool req = false;
                //查询执行中或者处于暂停的  当前产线工单
                //如果不存在,即插入工单表数据信息,默认执行中
                //反之,判断当前是否为  结束工单,如果是,更新结束时间,反之,返回工单已存在
                //WorkState  1-新建  2-完成
                if (workInfo == null && model.WorkState == "1")
                {
                    ChunAnWorkOrder wmsTask = new ChunAnWorkOrder()
                    {
                        SQL_WorkNo = model.WorkNo,
                        SQL_PLineNo = model.DeviceName,
                        SQL_UsingNo = "N",
                        SQL_ORDER_TYPE = "产线下线",
                        SQL_ItemCode = model.ItemCode,
                        SQL_State = "新建",
                        SQL_Modify = model.WorkTime,
                        MaterialCode = model.MaterialCode
                    };
                    TaskHelper.ChunAnInsertWork(wmsTask);
                    result.success = true;
                    result.errMsg = $"工单信息接收成功,工单已创建!";
                    string deviceName = model.DeviceName;
                    SendPlcWorkState(deviceName);
                }
                else if (workInfo != null && model.WorkState == "2")
                {
                    if (!workInfo.SQL_State.Trim().Contains("完成"))
                    {
                        TaskHelper.UpdateStatus(workInfo, "已完成", model.WorkTime);
                        result.success = true;
                        result.errMsg = $"工单信息接收成功,工单已完成!";
                        string deviceName = model.DeviceName;
                        SendPlcWorkState(deviceName, false);
                    }
                    else result.errMsg = $"工单信息接收成功,工单已完成!";
                }
                else if (workInfo != null && model.WorkState == "1") result.errMsg = $"当前工单已存在。工单号:{workInfo.SQL_WorkNo},产线号:{model.DeviceName}";
            }
            catch (Exception ex)
            {
                result.errMsg = $"工单信息接收异常:{ex.Message}";
            }
            return result;
        }
 
        /// <summary>
        /// 工单信息获取接口(标准工单模型)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel WorkInfoStand(TASK_WorkInfoModel model)
        {
            SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
            try
            {
                var db = new SqlHelper<object>().GetInstance();
                var workInfo = db.Queryable<ConveryWorkOrder>().Where(a => a.S_WorkNo == model.WorkNo).First();
                //bool req = false;
                //查询执行中或者处于暂停的  当前产线工单
                //如果不存在,即插入工单表数据信息,默认执行中
                //反之,判断当前是否为  结束工单,如果是,更新结束时间,反之,返回工单已存在
                //WorkState  1-新建  2-完成
                if (workInfo == null && model.WorkState == "1")
                {
                    ConveryWorkOrder wmsTask = new ConveryWorkOrder()
                    {
                        S_WorkNo = model.WorkNo,
                        S_PLineNo = model.DeviceName,
                        S_ORDER_TYPE = "产线下线",
                        S_ItemCode = model.MaterialCode,
                        S_WorkState = "新建",
                        T_MODIFY = DateTime.Parse(model.WorkTime)
                    };
                    result.success = db.Insertable(wmsTask).ExecuteCommand() > 0;
                    result.success = true;
                    result.errMsg = $"工单信息接收成功,工单已创建!";
                }
                else if (workInfo != null && model.WorkState == "2")
                {
                    if (!workInfo.S_WorkState.Trim().Contains("完成"))
                    {
                        workInfo.S_WorkState = "已完成";
                        db.Updateable(workInfo).UpdateColumns(it => new { it.S_WorkState }).ExecuteCommand();
                        result.success = true;
                        result.errMsg = $"工单信息接收成功,工单已完成!";
                    }
                    else result.errMsg = $"工单信息接收成功,工单已完成!";
                }
                else if (workInfo != null && model.WorkState == "1") result.errMsg = $"当前工单已存在。工单号:{workInfo.S_WorkNo},产线号:{model.DeviceName}";
            }
            catch (Exception ex)
            {
                result.errMsg = $"工单信息接收异常:{ex.Message}";
            }
            return result;
        }
 
        /// <summary>
        /// 向PLC发送工单开启或关闭状态
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="state">true-新建 false-完成</param>
        private static bool SendPlcWorkState(string deviceName, bool state = true)
        {
            bool result = false;
            string sendTcpMsg = "";
            string deviceIP = ConfigurationManager.AppSettings[deviceName].ToString();
            if (state) sendTcpMsg = "3F00300d0a";
            else sendTcpMsg = "3F00400d0a";
            if (!string.IsNullOrEmpty(sendTcpMsg) && !string.IsNullOrEmpty(deviceIP))
            {
                var db = new SqlHelper<ChunAnCacheData>().GetInstance();
                var cacheData = db.Queryable<ChunAnCacheData>().Where(a => a.DataSign == "TcpData" && a.Ext1 == deviceIP && a.Ext2 == sendTcpMsg).First();
                if (cacheData == null)
                {
                    ChunAnCacheData chunAnCacheData = new ChunAnCacheData
                    {
                        DataSign = "TcpData",
                        Ext1 = deviceIP,
                        Ext2 = sendTcpMsg
                    };
                    result = db.Insertable(chunAnCacheData).ExecuteCommand() > 0;
                }
            }
            return result;
        }
 
        /// <summary>
        /// 生产下线托盘信息下发接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
 
        #region   标准托盘信息下发方法
        //internal static SimpleResultModel SendTrayCode(SendTrayCodeModel model)
        //{
        //    SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
        //    var db = new SqlHelper<object>().GetInstance();
        //    string msg = "";
        //    try
        //    {
        //        model.TrayInfo.ForEach(a =>
        //        {
        //            //托盘码唯一:已存在入库的托盘码不允许再插入中间表
        //            var trayAreaInfo = db.Queryable<LocCntrRel>().Where(b => b.S_CNTR_CODE == a.trayCode).First();
        //            if (trayAreaInfo == null)
        //            {
        //                var trayInfo = TaskHelper.GetChunAnTrayInfo(a.trayCode, a.trayNum);
        //                if (trayInfo == null)
        //                {
        //                    if (!string.IsNullOrEmpty(a.sign))
        //                    {
        //                        msg = msg + $"更新托盘数据失败:托盘已不存在。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
        //                        LogHelper.Info("SendTrayCode Dispose:更新托盘数据失败!" + $"托盘已不存在。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};", "ThirdSystemLog");
        //                    }
        //                    else
        //                    {
        //                        ChunAnTrayInfo ChunAnTrayInfo = new ChunAnTrayInfo()
        //                        {
        //                            deviceName = a.deviceName,
        //                            workNo = a.workNo,
        //                            trayCode = a.trayCode,
        //                            trayNum = a.trayNum,
        //                            dateTime = a.dateTime,
        //                            batchNo = a.batchNo,
        //                            trayRule = a.trayRule,
        //                            itemLayer = a.itemLayer
        //                        };
        //                        TaskHelper.ChunAnInsertTrayInfo(ChunAnTrayInfo);
        //                        result.success = true;
        //                        result.errCode = 0;
        //                        msg = msg + $"插入托盘数据成功:托盘号:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
        //                        LogHelper.Info("SendTrayCode Dispose:插入托盘数据成功!" + $"托盘码:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
        //                    }
        //                }
        //                else
        //                {
        //                    if (!string.IsNullOrEmpty(a.sign))
        //                    {
        //                        trayInfo.workNo = a.workNo;
        //                        trayInfo.trayCode = a.sign;
        //                        trayInfo.trayNum = a.trayNum;
        //                        trayInfo.batchNo = a.batchNo;
        //                        trayInfo.trayRule = a.trayRule;
        //                        trayInfo.itemLayer = a.itemLayer;
        //                        db.Updateable(trayInfo).UpdateColumns(b => new { b.workNo, b.trayCode, b.trayNum, b.batchNo, b.trayRule, b.itemLayer }).ExecuteCommand();
        //                        msg = msg + $"更新托盘数据成功:托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
        //                        LogHelper.Info("SendTrayCode Dispose:更新托盘数据成功!" + $"托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};", "ThirdSystemLog");
        //                    }
        //                    else
        //                    {
        //                        result.success = false;
        //                        msg = msg + $"插入托盘数据失败:托盘号:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
        //                        LogHelper.Info("SendTrayCode Dispose:插入托盘数据失败!" + $"托盘码:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                result.success = false;
        //                msg = msg + $"插入托盘数据失败:托盘已存在。货位号:{trayAreaInfo.S_LOC_CODE.Trim()},托盘号:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
        //                LogHelper.Info("SendTrayCode Dispose:插入托盘数据失败!" + $"托盘已存在。货位号:{trayAreaInfo.S_LOC_CODE.Trim()},托盘码:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
        //            }
        //        });
        //        result.errMsg = msg;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.Error($"SendTrayCode Error:{ex.Message}", ex);
        //        result.errMsg = ex.Message;
        //    }
        //    return result;
        //}
        #endregion
 
        #region   带更改托盘码的托盘信息下发方法
        internal static SimpleResultModel SendTrayCodeCA(SendTrayCodeModel model)
        {
            SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
            var db = new SqlHelper<object>().GetInstance();
            string msg = "";
            try
            {
                model.TrayInfo.ForEach(a =>
                {
                    /* 富勒更新异常托盘流程
                     * 1.替换码必定有值
                     * 2.如果存在原码:
                     *      a.中间表有数据,直接使用 替换码 替换中间表 原码 托盘数据      --OVER
                     *      b.中间表,托盘物料表 均无 原码 数据,则 使用托盘码插入数据    --OVER
                     *      c.中间表无数据,托盘物料表有 原码 数据,则表示,当前托盘已经生成任务,
                     *        直接替换 任务表,托盘物料表数据  备注:如果 货位托盘表 存在数据,则需要一并替换    --OVER
                     * 3.如果不存在原码:
                     *      a.直接使用 替换码 插入中间表 数据     --OVER
                     * **/
                    var trayInfo = TaskHelper.GetChunAnTrayInfo(a.trayCode, a.trayNum);
                    if (trayInfo == null)
                    {
                        string TrayCode = string.IsNullOrEmpty(a.trayCode) ? a.sign : a.trayCode;
                        if (!string.IsNullOrEmpty(a.sign) && !string.IsNullOrEmpty(a.trayCode))
                        {
                            //中间表无数据,判断托盘物料表
                            var trayItemTable = db.Queryable<CntrItemRel>().Where(it => it.S_CNTR_CODE == a.trayCode).First();
                            if (trayItemTable != null)
                            {
                                //存在数据--更新相关任务数据以及当前托盘物料数据
                                trayItemTable.S_CNTR_CODE = a.sign;
                                var taskInfo = db.Queryable<WMSTask>().Where(it => it.S_CNTRS.Contains(a.trayCode)).OrderByDescending(it => it.T_CREATE).First();
                                if (taskInfo != null)
                                {
                                    string cntrs = taskInfo.S_CNTRS.Replace(a.trayCode, a.sign);
                                    taskInfo.S_CNTRS = cntrs;
                                    db.Updateable(trayItemTable).WhereColumns(it => new { it.S_ID }).UpdateColumns(it => new { it.S_CNTR_CODE }).ExecuteCommand();
                                    db.Updateable(taskInfo).UpdateColumns(b => new { b.S_CNTRS }).ExecuteCommand();
                                    UpdateScreenTable(a, db);//更新 屏幕显示表 托盘数据
                                    msg = msg + $"更新托盘数据成功:已同步任务以及托盘物料表相关更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                                    var locTrayInfo = db.Queryable<LocCntrRel>().Where(it => it.S_CNTR_CODE == a.trayCode).First();
                                    if (locTrayInfo != null)
                                    {
                                        locTrayInfo.S_CNTR_CODE = a.sign;
                                        db.Updateable(locTrayInfo).UpdateColumns(b => new { b.S_CNTR_CODE }).ExecuteCommand();
                                    }
                                    result.success = true;
                                }
                            }
                            else
                            {
                                TaskHelper.InsertTrayCode(a, a.sign, a.deviceName, db, ref msg);//不存在数据--插入中间表数据
                                msg = msg + $"更新托盘数据异常:当前 托盘物料表不存在此托盘信息,直接插入中间表数据。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            }
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据处理反馈:" + $"{msg}", "ThirdSystemLog");
                        }
                        else
                        {
                            TaskHelper.InsertTrayCode(a, TrayCode, a.deviceName, db, ref msg);
                            result.success = true;
                            result.errCode = 0;
                            if (!string.IsNullOrEmpty(a.trayCode)) msg = msg + $"插入托盘数据成功:托盘号:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            else msg = msg + $"更新托盘数据成功:托盘号:{a.trayCode},更新码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:处理托盘数据成功!" + $"托盘码:{a.trayCode},更新码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(a.sign))
                        {
                            trayInfo.workNo = a.workNo;
                            trayInfo.trayCode = a.sign;
                            trayInfo.trayNum = a.trayNum;
                            trayInfo.batchNo = a.batchNo;
                            trayInfo.trayRule = a.trayRule;
                            trayInfo.itemLayer = a.itemLayer;
                            db.Updateable(trayInfo).UpdateColumns(b => new { b.workNo, b.trayCode, b.trayNum, b.batchNo, b.trayRule, b.itemLayer }).ExecuteCommand();
                            UpdateScreenTable(a, db);//更新 屏幕显示表 托盘数据
                            result.success = true;
                            msg = msg + $"更新托盘数据成功:托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据成功!" + $"托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};", "ThirdSystemLog");
                        }
                        else
                        {
                            result.success = false;
                            msg = msg + $"更新托盘数据失败:未传输替换码。托盘号:{a.trayCode},替换码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据失败!" + $"托盘码:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
                        }
                    }
                });
                result.errMsg = msg;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"SendTrayCode Error:{ex.Message}", ex);
            }
            return result;
        }
 
        internal static SimpleResultModel SendTrayCodeStand(SendTrayCodeModel model)
        {
            SimpleResultModel result = new SimpleResultModel { success = false, errCode = -1 };
            var db = new SqlHelper<object>().GetInstance();
            string msg = "";
            try
            {
                model.TrayInfo.ForEach(a =>
                {
                    var trayInfo = db.Queryable<ChunAnTrayInfoTwo>().Where(it => it.trayCode == a.trayCode && it.trayNum == a.trayNum).First();
                    if (trayInfo == null)
                    {
                        // 如果富勒未传输托盘码,则将替换码作为托盘码使用
                        string TrayCode = string.IsNullOrEmpty(a.trayCode) ? a.sign : a.trayCode;
                        if (!string.IsNullOrEmpty(a.sign) && !string.IsNullOrEmpty(a.trayCode))
                        {
                            //调用WMS接口查询当前托盘是否已经入库,如果入库则传输托盘码给WMS;反之,则插入托盘中间表作为新托盘使用
 
                            string res = JsonConvert.SerializeObject(new
                            {
                                trayCode = a.trayCode
                            });
                            string req = httpHelper.WebPost(url + "QueryTrayCode", res);
                            msg = msg + $"调用WMS返回结果:入参:{res},回参:{req};";
                            if (!string.IsNullOrEmpty(req))
                            {
                                var reqInfo = JsonConvert.DeserializeObject<SimpleResultModel>(req);
                                if (reqInfo.success)
                                {
                                    //调用WMS接口传输托盘数据
                                    string res1 = JsonConvert.SerializeObject(new
                                    {
                                        deviceName = a.deviceName,
                                        workNo = a.workNo,
                                        trayCode = a.trayCode,
                                        trayNum = a.trayNum,
                                        dateTime = a.dateTime,
                                        batchNo = a.batchNo,
                                        trayRule = a.trayRule,
                                        itemLayer = a.itemLayer,
                                        sign = a.sign
                                    });
                                    string req1 = httpHelper.WebPost(url + "SendTrayCode", res1);
                                    LogHelper.Info($"SendTrayCode WMSReq:{req1}", "ThirdSystemLog");
                                }
                                else
                                {
                                    SendTrayCodeStandInsertTrayData(a, result, db, trayInfo, TrayCode);
                                    msg = msg + $"更新托盘数据异常:当前 托盘物料表不存在此托盘信息,直接插入中间表数据。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                                }
                            }
                            else
                            {
                                SendTrayCodeStandInsertTrayData(a, result, db, trayInfo, TrayCode);
                                msg = msg + $"更新托盘数据异常:调用WMS查询托盘失败,直接插入中间表数据。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            }
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据处理反馈:" + $"{msg}", "ThirdSystemLog");
                        }
                        else
                        {
                            SendTrayCodeStandInsertTrayData(a, result, db, trayInfo, TrayCode);
                            result.errCode = 0;
                            if (!string.IsNullOrEmpty(a.trayCode)) msg = msg + $"插入托盘数据成功:托盘号:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            else msg = msg + $"更新托盘数据成功:托盘号:{a.trayCode},更新码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:处理托盘数据成功!" + $"托盘码:{a.trayCode},更新码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(a.sign))
                        {
                            trayInfo.workNo = a.workNo;
                            trayInfo.trayCode = a.sign;
                            trayInfo.trayNum = a.trayNum;
                            trayInfo.batchNo = a.batchNo;
                            trayInfo.trayRule = a.trayRule;
                            trayInfo.itemLayer = a.itemLayer;
                            db.Updateable(trayInfo).UpdateColumns(b => new { b.workNo, b.trayCode, b.trayNum, b.batchNo, b.trayRule, b.itemLayer }).ExecuteCommand();
                            result.success = true;
                            msg = msg + $"更新托盘数据成功:托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据成功!" + $"托盘已更新。托盘号:{a.trayCode},更新托盘号:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};", "ThirdSystemLog");
                        }
                        else
                        {
                            result.success = false;
                            msg = msg + $"更新托盘数据失败:未传输替换码。托盘号:{a.trayCode},替换码:{a.sign},托盘数量:{a.trayNum},托盘时间:{a.dateTime};";
                            LogHelper.Info("SendTrayCode Dispose:更新托盘数据失败!" + $"托盘码:{a.trayCode},托盘数量:{a.trayNum},托盘时间:{a.dateTime}", "ThirdSystemLog");
                        }
                    }
                });
                result.errMsg = msg;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"SendTrayCode Error:{ex.Message}", ex);
            }
            return result;
        }
 
        /// <summary>
        /// 梅坪-插入托盘数据
        /// </summary>
        /// <param name="a"></param>
        /// <param name="result"></param>
        /// <param name="db"></param>
        /// <param name="trayInfo"></param>
        /// <param name="TrayCode"></param>
        private static void SendTrayCodeStandInsertTrayData(TrayInfo a, SimpleResultModel result, SqlSugar.SqlSugarClient db, ChunAnTrayInfoTwo trayInfo, string TrayCode)
        {
            ChunAnTrayInfoTwo ChunAnTrayInfo = new ChunAnTrayInfoTwo()
            {
                deviceName = a.deviceName,
                workNo = a.workNo,
                trayCode = TrayCode,
                trayNum = a.trayNum,
                dateTime = a.dateTime,
                batchNo = a.batchNo,
                trayRule = a.trayRule,
                itemLayer = a.itemLayer
            };
            result.success = db.Insertable(ChunAnTrayInfo).ExecuteCommand() > 0;
        }
 
        /// <summary>
        /// 更新屏幕表数据
        /// </summary>
        /// <param name="a"></param>
        /// <param name="db"></param>
        private static void UpdateScreenTable(TrayInfo a, SqlSugar.SqlSugarClient db)
        #endregion
        {
            var screen = db.Queryable<ChunAnScreenData>().Where(it => it.DataTypeNo == "托盘数据" && it.TrayCode == a.trayCode).First();
            if (screen != null)
            {
                screen.TrayCode = a.sign;
                db.Updateable(screen).UpdateColumns(b => new { b.TrayCode }).ExecuteCommand();
            }
        }
 
        /// <summary>
        /// 人工分拣出库接口解绑托盘
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel UnbindingTrayCodeCA(UnbindingTrayCodeModel model)
        {
            var result = new SimpleResultModel { success = false, errCode = -1 };
            string msg = ""; int n = 0; string locCode = "";
            var db = new SqlHelper<object>().GetInstance();
            if (model.TrayInfo != null)
            {
                for (int i = 0; i < model.TrayInfo.Count(); i++)
                {
                    string cntrCode = model.TrayInfo[i];
                    var trayInfo = db.Queryable<LocCntrRel>().Where(a => a.S_CNTR_CODE == cntrCode).First();
                    if (trayInfo != null)
                    {
                        locCode = trayInfo.S_LOC_CODE.Trim();
                        if (!string.IsNullOrEmpty(locCode))
                        {
                            var locInfo = db.Queryable<Location>().Where(a => a.S_LOC_CODE == locCode).First();
                            if (locInfo != null)
                            {
                                TaskHelper.YiKuUnLockRow(db, locInfo.N_ROW.Trim(), locInfo.S_AREA_CODE.Trim(), false, false, true);
                                var Srow = db.Queryable<RowLock>().Where(a => a.N_ROW == locInfo.N_ROW.Trim() && a.S_AREA_CODE == locInfo.S_AREA_CODE.Trim()).First();
                                if (Srow != null)
                                {
                                    Srow.S_LOCK_STATE = "其他锁";
                                    db.Updateable(Srow).UpdateColumns(a => new { a.S_LOCK_STATE }).ExecuteCommand();
                                }
                            }
                        }
                        result.success = true;
                        db.Deleteable<LocCntrRel>(a => a.S_CNTR_CODE == cntrCode).ExecuteCommand();
                        db.Deleteable<CntrItemRel>(a => a.S_CNTR_CODE == cntrCode).ExecuteCommand();
                        msg = msg + $"{n++}:当前托盘号:{model.TrayInfo[i]},解绑成功!";
                    }
                    else msg = msg + $"{n++}:当前托盘号:{model.TrayInfo[i]},不存在对应货位!";
                    if (!string.IsNullOrEmpty(locCode))
                    {
                        var locNum = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == locCode).ToList();
                        int num = 0;
                        var locInfo = db.Queryable<Location>().Where(a => a.S_LOC_CODE == locCode).First();
                        if (locInfo != null)
                        {
                            locInfo.N_CURRENT_NUM = GetLocTrayNum(locNum, num);
                            LogHelper.Info("UnbindingTrayCode Dispose:更新当前货位当前数量!" + $"货位编码:{locCode},数量:{locInfo.N_CURRENT_NUM}", "ThirdSystemLog");
                            db.Updateable(locInfo).UpdateColumns(a => new { a.N_CURRENT_NUM }).ExecuteCommand();
                            var locaHaveTray = db.Queryable<Location>().Where(a => a.N_ROW == locInfo.N_ROW.Trim() && a.N_CURRENT_NUM > 0).ToList();
                            if (locaHaveTray.Count == 0) TaskHelper.YiKuUnLockRow(db, locInfo.N_ROW.Trim(), locInfo.S_AREA_CODE.Trim(), true, false, true);
                            //else TaskController.Monitor.YiKuUnLockRow(db, locInfo.N_ROW.Trim(), locInfo.S_AREA_CODE.Trim(), false);
                        }
                    }
                }
            }
            else msg = $"人工分拣出库:富勒传输数据异常!传输为空值!";
            result.errMsg = msg;
            LogHelper.Info("UnbindingTrayCode Dispose:" + result.errMsg, "ThirdSystemLog");
            return result;
        }
        /// <summary>
        /// 人工分拣出库接口解绑托盘-梅坪项目(直接调用WMS接口传输数据)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResultModel UnbindingTrayCodeStand(UnbindingTrayCodeModel model)
        {
            var result = new SimpleResultModel { success = false, errCode = -1 };
            string msg = "";
            if (model.TrayInfo != null)
            {
                string res = JsonConvert.SerializeObject(new
                {
                    TrayInfo = model.TrayInfo
                });
                string req = httpHelper.WebPost(url + "UnbindingTrayCode", res);
                if (req.Contains("true")) result.success = true;
                LogHelper.Info($"UnbindingTrayCode WMSReq:{req}", "ThirdSystemLog");
            }
            else msg = $"人工分拣出库:富勒传输数据异常!传输为空值!";
            result.errMsg = msg;
            LogHelper.Info("UnbindingTrayCode Dispose:" + result.errMsg, "ThirdSystemLog");
            return result;
        }
        private static int GetLocTrayNum(List<LocCntrRel> locNum, int num)
        {
            int trayNum = locNum.Count();
            switch (trayNum)
            {
                case 0:
                    num = 0;
                    break;
                case 1:
                    num = 1;
                    break;
                case 2:
                    num = 1;
                    break;
                case 3:
                    num = 2;
                    break;
                case 4:
                    num = 2;
                    break;
            }
 
            return num;
        }
    }
}