111
cjs
2025-07-08 62f58ae538c32889a9adae09cf54543f00eb9fea
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
using Hanhe.iWCS.Common;
using Hanhe.iWCS.DeviceDriver;
using Hanhe.iWCS.MData;
using Hanhe.iWCS.Model;
using Hanhe.iWCS.JingmenGEMNorthProtocol;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static Hanhe.iWCS.JingmenGEMNorthProtocol.ApiHelper;
using static Hanhe.iWCS.JingmenGEMNorthProtocol.ERPService;
using static Hanhe.iWCS.JingmenGEMNorthProtocol.MESHelper;
 
namespace Hanhe.iWCS.JingmenGEMNorthProtocol
{
    public class WMSHelper
    {
        private static WebApiHelper helper = new WebApiHelper();
        private static T WmsRequest<T>(string name, string param) where T : BaseResponse {
            HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_WMS);
            string msg = "";
            string feedback = "";
            T response = default(T);
            if (hao != null) {
                try {
                    //http://[IP]:[端口]/api/ApiTask/
                    string webAPIUrl = hao.WebUrl + name;
                    //Console.WriteLine(webAPIUrl);
                    //Console.WriteLine(param);
                    feedback = new WebApiHelper().WebPost(webAPIUrl, param).Replace(@"\", "").Trim();
                    //feedback = "{\"success\":false,\"errCode\":\"5001\",\"errMsg\":\"托盘号不存在121!\"}";
                    //feedback = "{\"success\":true,\"errCode\":\"0\",\"task\":{\"taskNo\":\"RWH2103300000013\",\"warehouse\":\"CK002\",\"warehouseArea\":\"BCPLK01\",\"bit\":\"P01-02-06-08\"}}";
                    if (!string.IsNullOrEmpty(feedback)) {
                        feedback = feedback.Substring(1, feedback.Length - 2);
                        Console.WriteLine(feedback);
                        response = JsonConvert.DeserializeObject<T>(feedback);
                        if (response.success == true) {
                            msg = $"【wms {name}】success!;WebUrl=" + webAPIUrl + ";param=" + param + ";feedback=" + feedback;
                        }
                        else {
                            string errMsg = response.errMsg;
                            msg = $"【wms {name}】fail!err=" + errMsg + ";WebUrl=" + webAPIUrl + ";param=" + param + ";feedback=" + feedback;
                        }
                    }
                    else {
                        string errMsg = "can't find the address";
                        msg = $"【wms {name}】fail!err=" + errMsg + ";WebUrl=" + webAPIUrl + ";param=" + param;
                    }
                    //Console.WriteLine(msg);
                    CMMLog.Info(msg);
                }
                catch (Exception e) {
                    CMMLog.Error($"【wms {name}】fail!err={e.Message}", e);
                }
            }
            else {
                msg = string.Format($"wms {name} fail,ams workcenter not set!");
            }
 
            return response;
        }
 
        internal static void ExecuteState(string taskNo, int state, string bit = "") {
            var param = new { taskNo = taskNo, stateNo = state };
            WmsRequest<BaseResponse>("ExecuteState", JsonConvert.SerializeObject(param));
        }
        
 
        public class BaseResponse
        {
            public bool success { get; set; }
            public string errCode { get; set; }
            public string errMsg { get; set; }
        }
        public class WMSInModel
        {
            public string taskNo { get; set; }
            public string startStock { get; set; }
            public string startArea { get; set; }
            public string startBit { get; set; }
            public string trayDimension { get; set; }
            public string isTransport { get; set; }
            public string needCreateAMSTask { get; set; }
            public int priority { get; set; }
            public int isFull { get; set; }
            public string projectCode { get; set; }
            /// <summary>
            /// 托盘码
            /// </summary>
            public string trayCode { get; set; }
            /// <summary>
            /// 托盘类型
            /// </summary>
            public string trayType { get; set; }
            public string locationGear { get; set; }
            public List<ItemData> data { get; set; }
            //public List<TimeData> TimeData { get; set; }
        }
 
        public class WMSOutModel
        {
            //public string taskNo { get; set; }
            //public string endStock { get; set; }
            //public string endArea { get; set; }
            public string endBit { get; set; }
            public string trayDimension { get; set; }
            public string needCreateAMSTask { get; set; }
            public int priority { get; set; }
            //public int isFull { get; set; }
            public string projectCode { get; set; }
            //public List<ItemData> Data { get; set; }
        }
        public class WMSEmptyOutModel
        {
            //public string taskNo { get; set; }
            //public string endStock { get; set; }
            //public string endArea { get; set; }
            public string endBit { get; set; }
            public string trayDimension { get; set; }
            public string needCreateAMSTask { get; set; }
            public string trayType { get; set; }
            public int priority { get; set; }
            //public int isFull { get; set; }
            public string projectCode { get; set; }
        }
        public class WMSResponseModel
        {
            //"{\"Success\":false,\"ErrCode\":null,\"ErrMsg\":\"\",\"OkList\":[],\"FailList\":[{\"Index\":0,\"ErrCode\":null,\"ErrMsg\":\"未找到aaa位置的所属仓库\"}],\"Tasks\":[]}"
            // "{\"Success\":true,\"ErrCode\":null,\"ErrMsg\":null,\"OkList\":[{\"Index\":0,\"TaskNo\":\"RKT00032\",\"StartStock\":\"XMZ001\",\"StartArea\":\"XMZ001005\",\"StartBit\":\"XMZ001Q71-1\",\"EndStock\":\"XMZ001\",\"EndArea\":\"XMZ001001\",\"EndBit\":\"XMZ001B61-2\",\"Priority\":0}],\"FailList\":[],\"Tasks\":[]}"
            public int Status { get; set; }
            public string Code { get; set; }
            public string Msg { get; set; }
            public List<TaskInfo> Data { get; set; }
            public int AffectedRows { get; set; }
 
            public bool Success { get; set; }
            //public List<TaskInfo> failList { get; set; }
            //public List<TaskInfo> tasks { get; set; }
            //{"taskNo":"任务号","startStock":"起始仓库","startArea":"起始库区","startBit":"起始货位","endStock":"目的仓库","endArea":"目的库区","endBit":"目的货位"}
            public class TaskInfo
            {
                public string taskNo { get; set; }
                public string startStock { get; set; }
                public string startArea { get; set; }
                public string startBit { get; set; }
                public string endStock { get; set; }
                public string endArea { get; set; }
                public string endBit { get; set; }
                #region   设备通道信息
 
                /// <summary>
                /// 设备通道数据3——托盘码          
                /// </summary>
                public string trayCode { get; set; }
 
                #endregion
            }
        }
        public class ItemData
        {
            //托盘尺寸:trayDimension
            //物料编号:itemCode
            //物料数量:qty
            public string itemCode { get; set; }
            //public string itemFeature { get; set; }
            //public string itemState { get; set; }
            //public string ProductionDate { get; set; }
            //public string ColourNumber { get; set; }
            //public string Specification { get; set; }
            //public string BatchNumber { get; set; }
            //public int qty { get; set; }
 
            /// <summary>
            /// 是否叠底托(1-不叠底托    2-叠底托)
            /// </summary>
            public string palletLayers { get; set; }
 
            #region   MES传输的数据
            //传输MES信息以及设备信息给WMS
            /// <summary>
            /// mes返回的包装机号(T1,T2,......,T9)
            /// </summary>
            public string machineNo { get; set; }
            /// <summary>
            /// mes返回的成品批次号
            /// </summary>
            public string lotNo { get; set; }
            /// <summary>
            /// mes返回的袋号
            /// </summary>
            //public string bagNo { get; set; }
            /// <summary>
            /// mes返回的产品型号
            /// </summary>
            //public string productType { get; set; }
            /// <summary>
            /// mes返回的物料编码
            /// </summary>
            //public string materialCode { get; set; }
            /// <summary>
            /// mes返回的叠包层数  (例1,2,3:1代表不叠包,2代表叠2层,3代表叠3层)
            /// </summary>
            public string foldingbag { get; set; }
            #endregion
 
            #region   设备通道信息
 
            /// <summary>
            /// 设备通道数据3——托盘码          
            /// </summary>
            //public string trayCode { get; set; }
            /// <summary>
            /// 设备通道数据1——包装机号        
            /// </summary>
            //public string location { get; set; }
            /// <summary>
            /// 设备通道数据2——产品需求重量:32位整数
            /// </summary>
            public string productWeight { get; set; }
            /// <summary>
            /// 设备通道数据2——托盘重量:32位整数
            /// </summary>
            public string trayCodeWeight { get; set; }
            /// <summary>
            /// 设备通道数据2——单托实际重量:32位整数
            /// </summary>
            public string oneTrayWeight { get; set; }
            /// <summary>
            /// 设备通道数据2——叠包后实际重量:32位整数
            /// </summary>
            //public string addWeight { get; set; }
            /// <summary>
            /// 设备通道数据1——包装机出口称重结果:1:OK,2:NG
            /// </summary>
            //public int packNg { get; set; }
            /// <summary>
            /// 设备通道数据1——复称口称重结果:1:OK,2:NG
            /// </summary>
            public int itemState { get; set; }
            /// <summary>
            /// 设备通道数据1——是否需要叠包:1:叠包,2:不叠包
            /// </summary>
            //public int addState { get; set; }
            /// <summary>
            /// 设备通道数据1——袋号(001,002,003…999)
            /// </summary>
            public string packageCode { get; set; }
 
            #endregion
 
            #region   时间戳信息
            /// <summary>
            /// 是否叠包(1,2,3)
            /// </summary>
            public int isFold { get; set; }
            /// <summary>
            /// 包装机号
            /// </summary>
            public int packingMachineNumber { get; set; }
            /// <summary>
            /// 袋号(001,002,003…999)1
            /// </summary>
            public string bagNumber { get; set; }
            /// <summary>
            /// 产品需求重量1:32位整数
            /// </summary>
            public int needWeight { get; set; }
            /// <summary>
            /// 单托实际重量1:32位整数
            /// </summary>
            public int realWeight { get; set; }
            /// <summary>
            /// 叠托后的实际重量:32位整数
            /// </summary>
            public int totalWeight { get; set; }
            /// <summary>
            /// 产品批次号
            /// </summary>
            public string batchNumber { get; set; }
            /// <summary>
            /// 产品型号
            /// </summary>
            public string productModel { get; set; }
            /// <summary>
            /// 计数(只在数据库)
            /// </summary>
            public int totalCount { get; set; }
            /// <summary>
            /// 时间戳
            /// </summary>
            public int timeStamp { get; set; }
            /// <summary>
            /// 照片url
            /// </summary>
            public string photoURL { get; set; }
            #endregion
 
            #region   WMS显示信息
            /// <summary>
            /// 物料编码
            /// </summary>
            public string Jm_Item_Code { get; set; }
            /// <summary>
            /// 物料名称
            /// </summary>
            public string Jm_Item_Name { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            public string Jm_Item_Model { get; set; }
            /// <summary>
            /// 单位 
            /// </summary>
            public string Jm_Item_Unit { get; set; }
            /// <summary>
            /// 员工
            /// </summary>
            public string Jm_Item_Staff { get; set; }
            #endregion
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="startBit"></param>
        /// <param name="ItemCode"></param>
        /// <param name="taskNo"></param>
        /// <param name="trayCode"></param>
        /// <param name="timeStamp"></param>
        /// <param name="employee_id">员工编码</param>
        /// <returns></returns>
        public static bool WMSIn(string startBit, string ItemCode, ref string taskNo,ref string trayCode,string timeStamp = "",string employee_id = "") {
            //XMZ001A11-1
            //startBit = startBit.Substring(0, 9);
            bool result = false;
            HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_WMS);
            string msg = "";
            if (hao != null) {
                //http://192.168.1.199:9001/api/wmsapi/InWorkArea
                WMSInModel model = new WMSInModel();
                model.startBit = startBit;
                model.isTransport = "N";
                model.needCreateAMSTask = "Y";
                model.projectCode = "glm";
                model.data = new List<ItemData>();
                CMMLog.Info($"WMSIn-{startBit}:ItemCode类型为:{ItemCode}");
                if (!string.IsNullOrEmpty(ItemCode)) {
                    //var time = MongoDBSingleton.Instance.FindOne<TimeCuoInfoCom>(Query.Or(Query.EQ("isNeedTray", 1), Query.EQ("isNeedTray", 2)), "TimeCuoInfoCom");
                    if (ItemCode == "time" || ItemCode == "拆盘变更")
                    {
                        int timeInt = int.Parse(trayCode);
                        var time = MongoDBSingleton.Instance.FindOne<TimeCuoInfoCom>(Query.EQ("timeStamp", timeInt), "TimeCuoInfoCom");
                        CMMLog.Info($"WMSIn-{startBit}:{ItemCode}:查询条件:timeStamp={timeInt},读出 TimeCuoInfoCom 表数据为:{JsonConvert.SerializeObject(time)}");
                        if (ItemCode == "time" && time != null)
                        {
                            int TimeStamp = string.IsNullOrEmpty(timeStamp) ? time.timeStamp : int.Parse(timeStamp);
                            CMMLog.Info($"WMSIn-{startBit}:time获取时间戳数据为:{TimeStamp}!");
                            model.data.Add(new ItemData()
                            {
                                //1楼拆盘机调WMS获取入库终点,传输时间戳数据(暂定)
                                //isFold = time.isFold,
                                //packingMachineNumber = time.packingMachineNumber,
                                itemCode = time.productModel,
                                lotNo = time.batchNumber,
                                machineNo = time.packingMachineNumber.ToString(),
                                packageCode = time.bagNumber,
                                palletLayers = time.isNeedTray.ToString(),
                                foldingbag = time.isFold.ToString(),
                                //totalWeight = time.totalWeight,
                                timeStamp = TimeStamp,
                                photoURL = time.photoURL,
                                //productType = time.productModel,
                                //bagNumber = time.bagNumber,
                                //needWeight = time.needWeight,
                                realWeight = time.realWeight,
                                //batchNumber = time.batchNumber,
                                //totalCount = time.totalCount,
 
                                Jm_Item_Code = time.materialCode,
                                Jm_Item_Name = time.materialName,
                                Jm_Item_Model = time.productModel,
                                Jm_Item_Unit = time.measurementUnit,
                                Jm_Item_Staff = employee_id
                            });
 
                            // 非ERP变更流程可以直接删除 TimeCuoInfoCom 表
                            //if (ERPService.ERPSwitch01 == "0")
                            //{
                            //    CMMLog.Info($"WMSIn-{startBit}:查询条件:timeStamp={timeInt},非ERP变更流程直接删除 TimeCuoInfoCom 表!");
                            //}
                        }
                        else if (ItemCode == "拆盘变更")
                        {
                            model.data.Add(new ItemData()
                            {
                                //1楼拆盘机调WMS获取入库终点,传输时间戳数据(暂定)
                                //isFold = time.isFold,
                                //packingMachineNumber = time.packingMachineNumber,
                                itemCode = "1",
                                lotNo = "1",
                                machineNo = "1",
                                packageCode = "1",
                                palletLayers = "1",
 
                                foldingbag = "1",
                                //totalWeight = time.totalWeight,
                                timeStamp = 1,
                                photoURL = "1",
                                //productType = time.productModel,
                                //bagNumber = time.bagNumber,
                                //needWeight = time.needWeight,
                                realWeight = 1,
                                //batchNumber = time.batchNumber,
                                //totalCount = time.totalCount,
                            });
                        }
                    }
                    else
                    {
                        var info = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("trayCode", ItemCode), "MachineInfo");
                        // ERP变更新增数据直接从  ERP物料中间表-ERPItemTable 获取(根据物料编码)
                        CMMLog.Info($"WMSIn-{startBit}:查询条件:trayCode={ItemCode},读出 MachineInfo 表数据为:{JsonConvert.SerializeObject(info)}");
                        if (info != null)
                        {
                            //var erpItemTableInfo = MongoDBSingleton.Instance.FindOne<ERPItemTable>(Query.EQ("item_code", info.materialCode), "ERPItemTable");
                            //CMMLog.Info($"WMSIn-{startBit}:查询条件:item_code={info.materialCode},读出 ERPItemTable 表数据为:{JsonConvert.SerializeObject(erpItemTableInfo)}");
                            model.locationGear = info.secondNg.ToString();
                            model.trayCode = info.trayCode;
                            model.trayType = info.trayType == "1" ? "田字托" : info.trayType == "2" ? "川字托" : "";
                            //string product = !string.IsNullOrEmpty(info.productType) ? info.productType : erpItemTableInfo.item_spec;
                            model.data.Add(new ItemData()
                            {
                                itemCode = info.materialCode,
                                lotNo = info.lotNo,
                                machineNo = info.machineNo,
                                packageCode = info.packageCode,
                                palletLayers = info.palletLayers,
                                foldingbag = info.overlappingLayers,
                                itemState = info.secondNg,
                                productWeight = info.productWeight,
                                //trayCodeWeight = info.trayCodeWeight,
                                oneTrayWeight = info.oneTrayWeight,
                                //bagNo = info.bagNo,
                                //productType = info.productType,
                                //materialCode = info.materialCode,
                                //location = info.location,
                                //addWeight = info.addWeight,
                                //packNg = info.packNg,
                                //addState = info.addState,
 
                                //Jm_Item_Code = info.materialCode,
                                //Jm_Item_Name = erpItemTableInfo.item_name,
                                //Jm_Item_Model = erpItemTableInfo.item_spec,
                                //Jm_Item_Unit = erpItemTableInfo.item_uom,
                                //Jm_Item_Staff = info.trayCodeWeight
                            });
                        }
                    }
                }
                string msgData = JsonConvert.SerializeObject(model);
                string reqStr = "物料名=" + ItemCode + ";数量";
                string feedback = "";
 
                CMMLog.Info($"WMSIn-{startBit}:【调用wms获取入库货位】:发送数据:{JsonConvert.SerializeObject(msgData)},物料条件:ItemCode:{ItemCode},trayCode:{trayCode}");
                var url = hao.WebUrl + "GlmInSendTask";
                try {
                    //hao.WebUrl = "http://192.168.1.199:9001/api/";
                    feedback = helper.WebPost(url, msgData).Replace(@"\", "").Trim();
                    Console.WriteLine(feedback);
                    CMMLog.Info($"WMSIn-{startBit}:【调用wms获取入库货位】:接收数据:" + feedback);
                    if (!string.IsNullOrEmpty(feedback)) {
                        //var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback.Substring(1, feedback.Length - 2));
                        var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback);
                        //var res = feedback.Replace("{", "").Replace("}", "").Replace(":", "=").Replace(",", ";").Replace('"', ' ');
                        if (wmsResponse.Success == true) {
                            msg = $"WMSIn-{startBit}:【调用wms获取入库货位】成功!返回结果=" + wmsResponse.Success + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                            //if(!string.IsNullOrEmpty(trayCode)) MongoDBSingleton.Instance.Remove<TimeCuoInfoCom>(Query.EQ("timeStamp", int.Parse(trayCode)), RemoveFlags.Single);
                        }
                        else {
                            string errMsg = wmsResponse.Code + "-" + wmsResponse.Msg;
                            msg = $"WMSIn-{startBit}:【调用wms获取入库货位】失败!错误原因=" + errMsg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        }
                        result = wmsResponse.Success;
                    }
                    else {
                        string errMsg = "参数反馈空值";
                        msg = $"WMSIn-{startBit}:【调用wms获取入库货位】失败!错误原因=" + errMsg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                    }
                }
                catch (Exception e) {
                    CMMLog.Error(string.Format($"WMSIn-{startBit}:【调用wms获取入库货位失败】异常{0}", e.Message), e);
                    if(string.IsNullOrEmpty(msg)) msg = $"WMSIn-{startBit}:【调用wms获取入库货位】失败!解析返回值出错=" + feedback + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                }
            }
            else msg = string.Format($"WMSIn-{startBit}:调用wms失败,ams工作中心没有设置!");
            CMMLog.Info(msg);
            Console.WriteLine(msg);
            return result;
        }
 
        public static bool WMSOut(string endBit, string ItemCode) {
            //endBit = endBit.Substring(0, 9);
            bool result = false;
            HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_WMS);
            string msg = "";
            if (hao != null) {
                //http://192.168.1.199:9001/api/wmsapi/InWorkArea
                WMSOutModel model = new WMSOutModel();
                model.endBit = endBit;
                model.projectCode = "glm";
                model.needCreateAMSTask = "Y";
                //model.Data = new List<ItemData>();
                //if (!string.IsNullOrEmpty(ItemCode))
                //{
                //    model.Data.Add(new ItemData()
                //    {
                //        itemCode = ItemCode
                //    });
                //}
                string msgData = JsonConvert.SerializeObject(model);
                string reqStr = "物料名=" + ItemCode + ";";
                string feedback = "";
 
                var url = hao.WebUrl + "GlmOutSendAmsTask";
                try {
                    //hao.WebUrl = "http://192.168.1.199:9001/api/";
                    feedback = helper.WebPost(url, msgData).Replace(@"\", "").Trim();
                    CMMLog.Info($"WMSOut-{endBit}:WMS出库接口回报数据:{feedback}");
                    if (!string.IsNullOrEmpty(feedback)) 
                    {
                        //var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback.Substring(1, feedback.Length - 2));
                        var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback);
                        CMMLog.Info($"WMSOut-{endBit}:{wmsResponse.Success}");
                        if (wmsResponse.Success == true) msg = $"WMSOut-{endBit}:【调用wms获取出库货位】成功!返回结果=" + wmsResponse.Success + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        else msg = $"WMSOut-{endBit}:【调用wms获取出库货位】失败!错误原因=" + wmsResponse.Code + "-" + wmsResponse.Msg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        result = wmsResponse.Success;
                    }
                    else msg = $"WMSOut-{endBit}:【调用wms获取出库货位】失败!错误原因=参数反馈空值;调用WebUrl=" + url + ";输入参数=" + msgData;
                }
                catch (Exception e) {
                    CMMLog.Error(string.Format($"WMSOut-{endBit}:【调用wms获取出库货位失败】异常{0}", e.Message), e);
                    msg = $"WMSOut-{endBit}:【调用wms获取出库货位】失败!解析返回值出错=" + feedback + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                }
            }
            else msg = string.Format($"WMSOut-{endBit}:调用wms失败,ams工作中心没有设置!");
            CMMLog.Info(msg);
            Console.WriteLine(msg);
            return result;
        }
 
        public static bool WMSEmptyOut(string endBit, string ItemCode,string trayType, ref string taskNo, ref string trayCode)
        {
            bool result = false;
            HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_WMS);
            string msg = "";
            if (hao != null)
            {
                //http://192.168.1.199:9001/api/wmsapi/InWorkArea
                WMSEmptyOutModel model = new WMSEmptyOutModel();
                model.endBit = endBit;
                model.projectCode = "glm";
                model.needCreateAMSTask = "Y";
                model.trayType = trayType;
                string msgData = JsonConvert.SerializeObject(model);
                string reqStr = "物料名=" + ItemCode + ";";
                string feedback = "";
 
                var url = hao.WebUrl + "TrayOutCallTask";
                try
                {
                    //hao.WebUrl = "http://192.168.1.199:9001/api/";
                    feedback = helper.WebPost(url, msgData).Replace(@"\", "").Trim();
                    CMMLog.Info($"WMS回报数据:{feedback}");
                    if (!string.IsNullOrEmpty(feedback))
                    {
                        //var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback.Substring(1, feedback.Length - 2));
                        var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback);
 
                        CMMLog.Info($"{wmsResponse.Success}");
                        if (wmsResponse.Success == true)
                        {
                            var ok = wmsResponse.Data[0];
                            taskNo = ok.taskNo;
                            trayCode = ok.trayCode;
                            msg = "【调用wms获取出库货位】成功!返回结果=" + wmsResponse.Success + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        }
                        else
                        {
                            string errMsg = wmsResponse.Code + "-" + wmsResponse.Msg;
                            msg = "【调用wms获取出库货位】失败!错误原因=" + errMsg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        }
                        result = wmsResponse.Success;
                    }
                    else
                    {
                        string errMsg = "参数反馈空值";
                        msg = "【调用wms获取出库货位】失败!错误原因=" + errMsg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                    }
                }
                catch (Exception e)
                {
                    CMMLog.Error(string.Format("【调用wms获取出库货位失败】异常{0}", e.Message), e);
                    msg = "【调用wms获取出库货位】失败!解析返回值出错=" + feedback + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                }
            }
            else msg = string.Format("调用wms失败,ams工作中心没有设置!");
            CMMLog.Info(msg);
            Console.WriteLine(msg);
            return result;
        }
 
 
        /// <summary>
        /// 改道接口
        /// </summary>
        /// <param name="taskNo">WMS任务号</param>
        /// <param name="changeArea">改道库区</param>
        /// <returns></returns>
        internal static string WmsUpdateWay(string taskNo, string changeArea)
        {
            string result = "";
            string param = JsonConvert.SerializeObject(new
            {
                taskNo = taskNo,
                updateType = "终点",
                changeArea = changeArea
            });
            var req = WmsWebPost(param, "UpdateWay");
            if (req != null && req.success) result = req.location;
            return result;
        }
 
        /// <summary>
        /// WebPost
        /// </summary>
        /// <param name="param">发送内容</param>
        /// <param name="postName">接口名称</param>
        /// <param name="Par1">扩展参数</param>
        /// <returns></returns>
        private static WmsResModel WmsWebPost(string param, string postName, string Par1 = "")
        {
            string msg = ""; string feedback = "";
            WmsResModel response = new WmsResModel();
            response.success = false;
            try
            {
                HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_WMS);
                if (hao != null)
                {
                    string webAPIUrl = hao.WebUrl + postName;
                    feedback = new WebApiHelper().WebPost(webAPIUrl, param);
                    if (!string.IsNullOrEmpty(feedback))
                    {
                        msg = $"【WMS Post {postName}】WebUrl={webAPIUrl} ;param={param} ;return={feedback}";
                        response = JsonConvert.DeserializeObject<WmsResModel>(feedback.Substring(1, feedback.Length - 2).Replace(@"\", ""));
                        //response = JsonConvert.DeserializeObject<WmsResModel>(feedback);
 
                        if (response.success == true) msg = $"【WMS Post {postName}】success!;WebUrl={ webAPIUrl};param={param} ;return={feedback}";
                        else msg = $"【WMS Post {postName}】fail!WebUrl={ webAPIUrl} ;param={param} ;return={feedback}";
                    }
                    else
                    {
                        string errMsg = "can't find the address";
                        msg = $"【WMS Post {postName}】fail!err={errMsg};WebUrl={ webAPIUrl} ;param={param}";
                    }
                }
                else msg = $"调用wms失败,ams工作中心没有设置!";
                CMMLog.Info(msg);
            }
            catch (Exception ex)
            {
                CMMLog.Info($"【WMS Post {postName}】fail!err={ex.Message}");
            }
            return response;
        }
 
        internal static bool WMSEmptyUnbind(string extend)
        {
            bool result = false;
            string feedback = "";
            HardwareAccessObject hao = HardwareAccessHelper.Instance.GetEquipmentsHAO(Constants.WMS_DEVICE_TYPE_WMS);
            string msg = "";
            if (hao != null)
            {
                //http://192.168.1.199:9001/api/wmsapi/UnbindLocation
                WMSEmptyUnbindModel model = new WMSEmptyUnbindModel();
                model.locationCode = extend;
                string msgData = JsonConvert.SerializeObject(model);
 
                var url = hao.WebUrl + "UnbindLocation";
                try
                {
                    //hao.WebUrl = "http://192.168.1.199:9001/api/";
                    feedback = helper.WebGet(url + "?locationCode=" + extend).Replace(@"\", "").Trim();
                    CMMLog.Info($"调用解绑接口WMS回报数据:{feedback}");
                    if (!string.IsNullOrEmpty(feedback))
                    {
                        //var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel>(feedback.Substring(1, feedback.Length - 2));
 
                        var wmsResponse = JsonConvert.DeserializeObject<WMSResponseModel1>(feedback);
 
                        CMMLog.Info($"{wmsResponse.Success}");
                        if (wmsResponse.Success == true)
                        {
                            //var ok = wmsResponse.Data[0];
                            msg = "【调用wms解绑空托缓存位】成功!返回结果=" + wmsResponse.Success + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        }
                        else
                        {
                            string errMsg = wmsResponse.Msg;
                            msg = "【调用wms解绑空托缓存位】失败!错误原因=" + errMsg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                        }
                        result = wmsResponse.Success;
                    }
                    else
                    {
                        string errMsg = "参数反馈空值";
                        msg = "【调用wms解绑空托缓存位】失败!错误原因=" + errMsg + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                    }
                }
                catch (Exception e)
                {
                    CMMLog.Error(string.Format("【调用wms获取出库货位失败】异常{0}", e.Message), e);
                    msg = "【调用wms解绑空托缓存位】失败!解析返回值出错=" + feedback + ";调用WebUrl=" + url + ";输入参数=" + msgData;
                }
            }
            else msg = string.Format("调用wms失败,ams工作中心没有设置!");
            CMMLog.Info(msg);
            //Console.WriteLine(msg);
            return result;
        }
 
        public class WMSEmptyUnbindModel
        {
            //public string taskNo { get; set; }
            //public string endStock { get; set; }
            //public string endArea { get; set; }
            public string locationCode { get; set; }
        }
 
        public class WMSResponseModel1
        {
            //"{\"Success\":false,\"ErrCode\":null,\"ErrMsg\":\"\",\"OkList\":[],\"FailList\":[{\"Index\":0,\"ErrCode\":null,\"ErrMsg\":\"未找到aaa位置的所属仓库\"}],\"Tasks\":[]}"
            // "{\"Success\":true,\"ErrCode\":null,\"ErrMsg\":null,\"OkList\":[{\"Index\":0,\"TaskNo\":\"RKT00032\",\"StartStock\":\"XMZ001\",\"StartArea\":\"XMZ001005\",\"StartBit\":\"XMZ001Q71-1\",\"EndStock\":\"XMZ001\",\"EndArea\":\"XMZ001001\",\"EndBit\":\"XMZ001B61-2\",\"Priority\":0}],\"FailList\":[],\"Tasks\":[]}"
            public int Status { get; set; }
            //public string Code { get; set; }
            public string Msg { get; set; }
            public List<TaskInfo> Data { get; set; }
            public int AffectedRows { get; set; }
 
            public bool Success { get; set; }
            //public List<TaskInfo> failList { get; set; }
            //public List<TaskInfo> tasks { get; set; }
            //{"taskNo":"任务号","startStock":"起始仓库","startArea":"起始库区","startBit":"起始货位","endStock":"目的仓库","endArea":"目的库区","endBit":"目的货位"}
            public class TaskInfo
            {
                public string taskNo { get; set; }
                public string startStock { get; set; }
                public string startArea { get; set; }
                public string startBit { get; set; }
                public string endStock { get; set; }
                public string endArea { get; set; }
                public string endBit { get; set; }
                #region   设备通道信息
 
                /// <summary>
                /// 设备通道数据3——托盘码          
                /// </summary>
                public string trayCode { get; set; }
 
                #endregion
            }
        }
 
        public class WmsResModel
        {
            public bool success { get; set; }
            public string errCode { get; set; }
            public string location { get; set; }
 
        }
    }
}