dengjunjie
2 天以前 10c3dd5f5f3e5224029072074efc326dc38d9daf
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
using Autofac.Core;
using Magicodes.IE.Core;
using NetTaste;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection.Metadata;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.AGV.HIKROBOT;
using WIDESEAWCS_DTO.RGV.FOURBOT;
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
 
namespace WIDESEAWCS_TaskInfoService
{
    public partial class TaskService
    {
 
        #region 更新任务状态
        /// <summary>
        /// 更新任务状态
        /// </summary>
        /// <param name="TaskDTO"></param>
        /// <param name="deviceTypeEnum"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent UpdateTaskStatus(object TaskDTO, DeviceTypeEnum deviceTypeEnum)
        {
 
            WebResponseContent content = new WebResponseContent();
            GALAXISUpdateTaskStatus gALAXIS = new();
            RGVReturnInfo Rgv = new();//四向车任务状态通知
            HIKROBOTTaskProcessReport hIKROBOT = new();//海康机器人
            string deviceName = "";
            try
            {
                if (deviceTypeEnum == DeviceTypeEnum.GALAXIS)
                {
                    deviceName = "凯乐士";
                    gALAXIS = TaskDTO.Serialize().DeserializeObject<GALAXISUpdateTaskStatus>();
                    if (gALAXIS == null) throw new Exception("参数转换失败!");
                    var task = BaseDal.QueryFirst(x => x.WMSTaskNum == gALAXIS.taskId);
                    if (task == null) throw new Exception($"未找到任务,任务编号【{gALAXIS.taskId}】");
                    switch (gALAXIS.taskStatus)
                    {
                        case 0:
                            TaskCompleted(task, deviceTypeEnum);
                            break;
                        case 4:
                            TaskFromCompleted(task, deviceTypeEnum);
                            break;
                        default:
                            task.TaskState = ReturnTaskStatus(gALAXIS.taskStatus, deviceTypeEnum);
                            if (gALAXIS.taskStatus == 10) TaskFeedback(task, 1);
                            else BaseDal.UpdateData(task);
                            break;
                    }
                }
                else if (deviceTypeEnum == DeviceTypeEnum.HIKROBOT)
                {
                    deviceName = "海康";
                    hIKROBOT = TaskDTO.Serialize().DeserializeObject<HIKROBOTTaskProcessReport>();
                    if (hIKROBOT == null) throw new Exception("参数转换失败!");
                    var task = BaseDal.QueryFirst(x => x.WMSTaskNum == hIKROBOT.robotTaskCode);
                    if (task == null) throw new Exception($"未找到任务,任务编号【{hIKROBOT.robotTaskCode}】");
                    switch (hIKROBOT.extra.values.method)
                    {
                        case "start":
                            if (task.TaskState == (int)TaskStatusEnum.CallingHKtransportation && task.TaskType == (int)TaskTypeEnum.MOVE)
                            {
                                task.TaskState = (int)TaskStatusEnum.HktransportIng;
                                BaseDal.UpdateData(task);
                            }
                            else if (task.TaskState == (int)TaskStatusEnum.Execut)
                            {
                                task.TaskState = (int)TaskStatusEnum.Takeing;
                                TaskFeedback(task, 1);
                            }
                            break;
                        case "outbin"://离开起点
                            {
                                TaskFromCompleted(task, deviceTypeEnum); //任务状体改为3取货完成,更新起点货位状态,并反馈WMS起点完成
                            }
                            break;
                        case "finish"://离开提升机
                            {
                                //入提升机
                                if (task.TaskType == TaskTypeEnum.RK3F.ObjToInt() && task.TaskState == (int)TaskStatusEnum.CheckPalletCodeFinish)
                                {
                                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                                    if (device == null) task.ExceptionMessage = "未找到提升机信息";
                                    else
                                    {
                                        bool Work = false;
                                        if (device.IsConnected)
                                        {
                                            int i = 0;
                                            while (!Work && i <= 3)
                                            {
                                                i++;
                                                Work = device.SetValue(HoistEnum.Inboundboxon, 1, task.TargetAddress);
                                                Thread.Sleep(500);
                                            }
                                        }
                                        if (!Work)
                                        {
                                            task.ExceptionMessage = $"【{task.TargetAddress}】写入3楼入库箱放到位失败";
                                            task.Remark = $"{device.DeviceCode}_Inboundboxon_{1}_{task.TargetAddress}";
                                        }
                                    }
                                    BaseDal.UpdateData(task);
                                }
 
                                //3 出提升机 之前是AGV_ToExecute
                                else if (task.TaskType == TaskTypeEnum.CK3F.ObjToInt() && task.TaskState == (int)TaskStatusEnum.TakeFinish)
                                {
                                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                                    if (device == null) task.ExceptionMessage = "未找到提升机信息";
                                    else
                                    {
                                        bool Work = false;
                                        if (device.IsConnected)
                                        {
                                            int i = 0;
                                            while (!Work && i <= 3)
                                            {
                                                i++;
                                                Work = device.SetValue(HoistEnum.Outboundboxon, 1, task.SourceAddress);//出库完成
                                                Thread.Sleep(500);
                                            }
                                        }
                                        if (!Work)
                                        {
                                            task.ExceptionMessage = $"【{task.SourceAddress}】写入3楼出库箱放到位失败";
                                            task.Remark = $"{device.DeviceCode}_Outboundboxon_{1}_{task.SourceAddress}";
                                        }
                                    }
                                    BaseDal.UpdateData(task);
                                }
                                //四楼 出库  
                                else if ((task.TaskType == TaskTypeEnum.CHUKU1.ObjToInt() || task.TaskType == TaskTypeEnum.F04.ObjToInt()) && task.TaskState == (int)TaskStatusEnum.AGV_ToExecute)
                                {
                                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                                    if (device == null) task.ExceptionMessage = "未找到提升机信息";
                                    else
                                    {
                                        bool Work = false;
                                        if (device.IsConnected)
                                        {
                                            int i = 0;
                                            while (!Work && i <= 3)
                                            {
                                                i++;
                                                Work = device.SetValue(HoistEnum.Emptyboxplacedinposition, 1, task.TargetAddress);
                                                Thread.Sleep(500);
                                            }
                                        }
                                        if (!Work)
                                        {
                                            task.ExceptionMessage = $"【{task.TargetAddress}】写入4楼空箱放到位失败";
                                            task.Remark = $"{device.DeviceCode}_Emptyboxplacedinposition_{1}_{task.TargetAddress}";
                                        }
                                    }
                                    BaseDal.UpdateData(task);
                                }
                                // //四楼出提升机 入库 之前是AGV_ToExecute  //要写成取货完成状态TakeFinish
                                else if ((task.TaskType == TaskTypeEnum.Q1TSJ4.ObjToInt() || task.TaskType == TaskTypeEnum.F03.ObjToInt()) && task.TaskState == (int)TaskStatusEnum.TakeFinish)
                                {
                                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                                    if (device == null) task.ExceptionMessage = "未找到提升机信息";
                                    else
                                    {
                                        bool Work = false;
                                        if (device.IsConnected)
                                        {
                                            int i = 0;
                                            while (!Work && i <= 3)
                                            {
                                                i++;
                                                Work = device.SetValue(HoistEnum.Outboundboxretrievalcompleted, 1, task.SourceAddress);
                                                Thread.Sleep(500);
                                            }
                                        }
                                        if (!Work)
                                        {
                                            task.ExceptionMessage = $"【{task.SourceAddress}】写入4楼出库取箱完成失败";
                                            task.Remark = $"{device.DeviceCode}_Outboundboxretrievalcompleted_{1}_{task.SourceAddress}";
                                        }
                                    }
                                    BaseDal.UpdateData(task);
                                }
                                //跨楼层
                                else if (task.TaskType == TaskTypeEnum.Q3RK.ObjToInt() || task.TaskType == TaskTypeEnum.Q3CK.ObjToInt()/* && task.TaskState == (int)TaskStatusEnum.TakeFinish*/)
                                {
                                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "F1") as OtherDevice;
                                    if (device == null) task.ExceptionMessage = "未找到质检门信息";
                                    else
                                    {
                                        bool Wok = false;
                                        var Address = task.TaskType == TaskTypeEnum.Q3RK.ObjToInt() ? task.TargetAddress : task.SourceAddress;
                                        if (device.IsConnected)
                                        {
                                            int i = 0;
                                            while (!Wok && i <= 3)
                                            {
                                                i++;
                                                Wok = device.SetValue(QualityInspectionCommandEnum.RequestToEnter, false, Address);
                                                Thread.Sleep(500);
                                            }
                                        }
                                        if (!Wok)
                                        {
                                            task.ExceptionMessage = $"【{Address}】关闭请求进入失败!";
                                            task.Remark = $"{device.DeviceCode}_RequestToEnter_{false}_{Address}";
                                        }
                                    }
                                    BaseDal.UpdateData(task);
                                }
                            }
                            break;
                        case "into"://申请进入提升机
                            {
                                task.TaskState = (int)TaskStatusEnum.AGV_WaitToExecute;
                                BaseDal.UpdateData(task);
                            }
                            break;
                        case "wait"://扫码等待
                            {
                                //要先开启扫码信号
                                var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                                if (device == null) task.ExceptionMessage = "未找到提升机信息";
                                else
                                {
                                    bool Wok = false;
                                    if (device.IsConnected)
                                    {
                                        int i = 0;
                                        while (!Wok && i <= 3)
                                        {
                                            i++;
                                            Wok = device.SetValue<HoistEnum, short>(HoistEnum.Codereadingtriggered, 1, "TSJR3F");
                                            Thread.Sleep(500);
                                        }
                                    }
                                    if (!Wok)
                                    {
                                        task.ExceptionMessage = $"【TSJR3F】3楼读码触发失败!";
                                        task.Remark = $"{device.DeviceCode}_Codereadingtriggered_{1}_TSJR3F";
                                    }
                                }
                                task.TaskState = (int)TaskStatusEnum.CheckPalletCodeing;
                                BaseDal.UpdateData(task);
                            }
                            break;
                        case "end"://任务完成通知
                            {
                                if (task.TaskType == (int)TaskTypeEnum.MOVE)
                                {
                                    task.TaskState = TaskStatusEnum.Finish.ObjToInt();
                                    BaseDal.DeleteAndMoveIntoHty(task, OperateTypeEnum.自动完成);
                                    var Task = BaseDal.QueryFirst(x => task.WMSTaskNum.Contains(x.WMSTaskNum));
                                    Task.TaskState = TaskStatusEnum.HkTransportCompleted.ObjToInt();
                                    BaseDal.UpdateData(Task);
                                }
                                else
                                {
                                    TaskCompleted(task, deviceTypeEnum);
                                }
                            }
                            break;
                        case "intolift": //进入电梯
                            {
                                //六楼到一楼的入库任务
                                if (task.TaskType == (int)TaskTypeEnum.Q3RK || task.TaskType == (int)TaskTypeEnum.MOVE || task.TaskType == (int)TaskTypeEnum.F01 || task.TaskType == (int)TaskTypeEnum.Q3CK)
                                {
                                    task.TaskState = TaskStatusEnum.Hkintolift.ObjToInt();
                                    BaseDal.UpdateData(task);
                                }
                            }
                            break;
                        case "leavelift": //离开电梯
                            {
                                if (task.TaskType == (int)TaskTypeEnum.Q3RK || task.TaskType == (int)TaskTypeEnum.MOVE || task.TaskType == (int)TaskTypeEnum.F01 || task.TaskType == (int)TaskTypeEnum.Q3CK)
                                {
                                    task.TaskState = TaskStatusEnum.HKleavelift.ObjToInt();
                                    BaseDal.UpdateData(task);
                                }
                            }
                            break;
                        default:
                            throw new Exception("未定义的方法名!");
                            //task.TaskState = ReturnTaskStatus(hIKROBOT.extra.values.method, deviceTypeEnum);
                            //BaseDal.UpdateData(task);
                            //break;
                    }
                }
                else //四向车
                {
                    deviceName = "四向车";
                    Rgv = TaskDTO.Serialize().DeserializeObject<RGVReturnInfo>();
                    if (Rgv == null) throw new Exception("参数转换失败!");
                    //var task = BaseDal.QueryFirst(x => x.RGVTaskId == Rgv.content.taskID);
                    var task = BaseDal.QueryFirst(x => x.PalletCode == Rgv.content.podID);
                    if (task == null) throw new Exception($"未找到任务,任务编号【{Rgv.content.taskID}】");
                    //移库
                    if (task.TaskType == TaskTypeEnum.CPMoveInventory.ObjToInt() && Rgv.messageType == 10 && Rgv.content.status == 4)
                    {
                        deviceName += "移库";
                        WMSMoveLocationFeedback wMSMoveLocationFeedback = new WMSMoveLocationFeedback()
                        {
                            containerCode = task.PalletCode,
                            fromStationCode = task.SourceAddress,
                            toLocationCode = task.TargetAddress
                        };
                        Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSMoveLocationFeedback));
                        WMSReturn agvContent = null;
                        if (apiInfo == null) task.ExceptionMessage = "未找到移库完成反馈WMS接口配置信息!请检查接口配置";
                        else
                        {
                            task.TaskState = TaskStatusEnum.Finish.ObjToInt();
                            string response = HttpHelper.Post(apiInfo.ApiAddress, wMSMoveLocationFeedback.Serialize());
                            agvContent = response.DeserializeObject<WMSReturn>();
                            content.OK(data: agvContent);
                            if (agvContent.code != 200)
                            {
                                task.ExceptionMessage = agvContent.message;
                                content.Error(agvContent.message);
                            }
                            UpdateLocationStatus(task.TargetAddress, task.PalletCode, deviceTypeEnum, LocationStatusEnum.InStock);
                            BaseDal.DeleteAndMoveIntoHty(task, OperateTypeEnum.自动完成);
                        }
                    }
                    else
                    {
                        //站台到质检台
                        if (Rgv.messageType == 72)//小车顶起货物
                        {
                            if (task.TaskType == (int)TaskTypeEnum.CPInbound && task.TaskState == (int)TaskStatusEnum.Execut)
                            {
                                task.TaskState = (int)TaskStatusEnum.WaiCheckShape;
                                //BaseDal.UpdateData(task);//给质检门写入启动信号
                                var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "F1") as OtherDevice;
                                if (device == null) task.ExceptionMessage = "未找到1楼质检门设备信息";
                                else
                                {
                                    if (device.IsConnected)
                                        device.SetValue(QualityInspectionCommandEnum.StartqualityInspection, true, task.SourceAddress);
                                    else
                                        task.ExceptionMessage = "1楼质检门设备连接失败";
                                }
                                TaskFeedback(task, 4);
                            }
                            else
                            {
                                //这里出库,这个接口中也返回给了wms状态
                                TaskFromCompleted(task, deviceTypeEnum);
                            }
                        }
                        else if (Rgv.messageType == 10)//质检台到目标货位
                        {
                            if (Rgv.content.status == 2) TaskFeedback(task, 1);
                            if (Rgv.content.status == 4)
                            {
                                if (task.TaskType == (int)TaskTypeEnum.CPInbound && task.TaskState == (int)TaskStatusEnum.TakeFinish)
                                {
                                    if (!string.IsNullOrEmpty(task.ExceptionMessage)) task.TaskState = TaskStatusEnum.CheckShapeingNG.ObjToInt();
                                    else task.TaskState = (int)TaskStatusEnum.CheckShapeing;
                                    BaseDal.UpdateData(task);
                                }
                                else if (task.TaskType == (int)TaskTypeEnum.CPInbound && task.TaskState == (int)TaskStatusEnum.Puting)
                                {
                                    if (!string.IsNullOrEmpty(task.ExceptionMessage) && !task.ExceptionMessage.Contains("由于目标计算机积极拒绝,无法连接"))
                                        ErrorTaskFeedback(task, true/*!task.ExceptionMessage.Contains("外检失败")*/);
                                    else
                                        TaskCompleted(task, deviceTypeEnum);
                                }
                                else if (task.TaskType == (int)TaskTypeEnum.CPOutbound)//出库完成
                                {
                                    TaskCompleted(task, deviceTypeEnum);
                                }
                            }
                            if (Rgv.content.status == 5)
                            {
                                task.ExceptionMessage = Rgv.content.errorReason;
                                var Error = _taskErrorMessageService.Repository.QueryFirst(x => x.ErrorCode == Rgv.content.errorCode && x.DeviceType == (int)deviceTypeEnum);
                                if (Error != null)
                                {
                                    task.ExceptionMessage = Error.ExceptionMessage;
                                    MatchCollection matches = Regex.Matches(Rgv.content.errorReason, @"\(([^)]+)\)");
                                    if (matches.Count > 0)
                                        task.ExceptionMessage = ReplacePlaceholders(Rgv.content.errorReason, Error.ExceptionMessage);
                                    ErrorTaskFeedback(task, true, Error.ErrorCode.ToString());
                                }
                                else
                                    ErrorTaskFeedback(task, true);
                            }
                        }
                    }
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
                _trackloginfoService.AddTrackLog(TaskDTO, content, $"{deviceName}任务状态反馈", "", "");
            }
            return content;
        }
        #endregion
 
        #region 异常信息转换
        /// <summary>
        /// 异常信息()内容转换
        /// </summary>
        /// <param name="source"></param>
        /// <param name="targetTemplate"></param>
        /// <returns></returns>
        public string ReplacePlaceholders(string source, string targetTemplate)
        {
            // 提取源字符串中的所有括号值
            var values = Regex.Matches(source, @"\(([^)]+)\)")
                              .Cast<Match>()
                              .Select(m => m.Groups[1].Value)
                              .ToList();
 
            // 替换目标模板中的所有 (%v)
            int idx = 0;
            string result = Regex.Replace(targetTemplate, @"\(\%v\)", _ =>
                idx < values.Count ? values[idx++] : "");
 
            return result;
        }
        #endregion
 
        #region 任务状态转换
        public int ReturnTaskStatus(object status, DeviceTypeEnum deviceTypeEnum)
        {
            try
            {
                if (deviceTypeEnum == DeviceTypeEnum.GALAXIS)
                {
                    TaskStatusEnum taskStatus = status switch
                    {
                        1 => TaskStatusEnum.FromOutOfStock,
                        2 => TaskStatusEnum.ToaddInStock,
                        3 => TaskStatusEnum.TaskNumRepetition,
                        5 => TaskStatusEnum.RCSDataError,
                        6 => TaskStatusEnum.TaskTypeError,
                        7 => TaskStatusEnum.ParameterError,
                        8 => TaskStatusEnum.RCSError,
                        10 => TaskStatusEnum.Takeing,
                        13 => TaskStatusEnum.Cancel,
                        _ => throw new ArgumentOutOfRangeException(
                            nameof(status),
                            status,
                            $"未知的任务状态码: {status}"
                        )
                    };
                    return (int)taskStatus;
                }
                else if (deviceTypeEnum == DeviceTypeEnum.HIKROBOT)
                {
                    return 0;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
 
        #region 任务起点完成
        /// <summary>
        /// 任务起点完成
        /// </summary>
        /// <param name="dt_Task"></param>
        /// <param name="deviceTypeEnum"></param>
        /// <returns></returns>
        public WebResponseContent TaskFromCompleted(Dt_Task dt_Task, DeviceTypeEnum deviceTypeEnum)
        {
            try
            {
                dt_Task.TaskState = TaskStatusEnum.TakeFinish.ObjToInt();
                TaskFeedback(dt_Task, 4);
                //Db.Ado.BeginTran();
                UpdateLocationStatus(dt_Task.SourceAddress, dt_Task.PalletCode, deviceTypeEnum, LocationStatusEnum.Free);
                //BaseDal.UpdateData(dt_Task);
                //Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
        #endregion
 
        #region 任务完成
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name=""></param>
        /// <param name="deviceTypeEnum"></param>
        /// <returns></returns>
        public WebResponseContent TaskCompleted(Dt_Task dt_Task, DeviceTypeEnum deviceTypeEnum)
        {
            try
            {
                dt_Task.TaskState = TaskStatusEnum.Finish.ObjToInt();
                TaskFeedback(dt_Task, 2, string.IsNullOrEmpty(dt_Task.Remark));
                //Db.Ado.BeginTran();
                UpdateLocationStatus(dt_Task.TargetAddress, dt_Task.PalletCode, deviceTypeEnum, LocationStatusEnum.InStock);
 
                //Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                //Db.Ado.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
        #endregion
 
        #region 更新货位状态
        /// <summary>
        /// 更新货位状态
        /// </summary>
        /// <param name="LocationCode"></param>
        /// <param name="deviceTypeEnum"></param>
        /// <param name="locationStatusEnum"></param>
        /// <returns></returns>
        public WebResponseContent UpdateLocationStatus(string locationCode, string palletCode, DeviceTypeEnum deviceTypeEnum, LocationStatusEnum locationStatusEnum)
        {
            try
            {
                if (deviceTypeEnum == DeviceTypeEnum.GALAXIS)
                {
                    Dt_KLSLocationInfo? dt_KLSLocationInfo = _kLSLocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
                    if (dt_KLSLocationInfo != null)
                    {
                        if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
                        {
                            dt_KLSLocationInfo.PalletCode = "";
                        }
                        else
                        {
                            dt_KLSLocationInfo.PalletCode = palletCode;
                        }
                        dt_KLSLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
                        _kLSLocationInfoService.Repository.UpdateData(dt_KLSLocationInfo);
                    }
                }
                else if (deviceTypeEnum == DeviceTypeEnum.HIKROBOT)
                {
                    Dt_HKLocationInfo? dt_HKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
                    if (dt_HKLocationInfo != null)
                    {
                        if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
                        {
                            dt_HKLocationInfo.PalletCode = "";
                        }
                        else
                        {
                            dt_HKLocationInfo.PalletCode = palletCode;
                        }
                        dt_HKLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
                        _hKLocationInfoService.Repository.UpdateData(dt_HKLocationInfo);
                    }
                    Dt_StationInfo? stationInfo = _stationInfo.Repository.QueryFirst(x => x.StationCode == locationCode);
                    if (stationInfo != null)
                    {
                        if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
                        {
                            stationInfo.PalletCode = "";
                        }
                        else
                        {
                            stationInfo.PalletCode = palletCode;
                        }
                        stationInfo.StationStatus = locationStatusEnum.ObjToInt();
                        _stationInfo.UpdateData(stationInfo);
                    }
                    Dt_StationManger stationManger = _stationMangerService.Repository.QueryFirst(x => x.StationCode == locationCode);
                    if (stationManger != null)
                    {
                        if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
                        {
                            stationManger.Remark = "";
                            stationManger.IsOccupied = LocationStatusEnum.Free.ObjToInt();
                        }
                        else
                        {
                            stationManger.Remark = palletCode;
                            stationManger.IsOccupied = LocationStatusEnum.Lock.ObjToInt();
                        }
                        _stationMangerService.UpdateData(stationManger);
                    }
                }
                else if (deviceTypeEnum == DeviceTypeEnum.YuanLiJuHe)
                {
                    Dt_RGVLocationInfo dt_RGVLocationInfo = _rGVLocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
                    //这里将任务的托盘号给货位表中的PalletCode字段。
 
                    if (dt_RGVLocationInfo != null)
                    {
                        if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
                        {
                            dt_RGVLocationInfo.PalletCode = "";
                        }
                        else
                        {
                            dt_RGVLocationInfo.PalletCode = palletCode;
                        }
                        dt_RGVLocationInfo.LocationStatus = locationStatusEnum.ObjToInt();
                        _rGVLocationInfoService.Repository.UpdateData(dt_RGVLocationInfo);
                    }
 
                    Dt_StationManger stationManger = _stationMangerService.Repository.QueryFirst(x => x.StationCode == locationCode);
                    if (stationManger != null)
                    {
                        if (locationStatusEnum == LocationStatusEnum.Free) //如果起点完成就给货位赋值托盘号
                        {
                            stationManger.Remark = "";
                            stationManger.IsOccupied = LocationStatusEnum.Free.ObjToInt();
                        }
                        else
                        {
                            stationManger.Remark = palletCode;
                            stationManger.IsOccupied = LocationStatusEnum.Lock.ObjToInt();
                        }
                        _stationMangerService.UpdateData(stationManger);
                    }
                }
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
 
        #region 上报WMS
        public WebResponseContent TaskFeedback(Dt_Task dt_Task, int Status, bool Del = false)
        {
            WebResponseContent content = new WebResponseContent();
            WMSInOutBoundCompleteFeedback boundCompleteFeedback = new WMSInOutBoundCompleteFeedback();
            WMSReturn agvContent = null;
            try
            {
                Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSInOutBoundCompleteFeedback));
                if (apiInfo == null) dt_Task.ExceptionMessage = "未找到出入库完成反馈WMS接口配置信息!请检查接口配置";
                else
                {
                    boundCompleteFeedback.taskCode = dt_Task.WMSTaskNum;
                    boundCompleteFeedback.containerCode = dt_Task.PalletCode;
                    boundCompleteFeedback.fromStationCode = dt_Task.SourceAddress;
                    boundCompleteFeedback.toLocationCode = dt_Task.TargetAddress;
                    boundCompleteFeedback.status = Status;
                    boundCompleteFeedback.custStatus = "";
                    string response = HttpHelper.Post(apiInfo.ApiAddress, boundCompleteFeedback.Serialize());
                    agvContent = response.DeserializeObject<WMSReturn>();
                    content.OK(data: agvContent);
                    if (agvContent.code != 200)
                    {
                        dt_Task.ExceptionMessage = agvContent.message;
                        content.Error(agvContent.message);
                    }
                }
            }
            catch (Exception ex)
            {
                dt_Task.ExceptionMessage = ex.Message;
            }
            finally
            {
                _trackloginfoService.AddTrackLog(boundCompleteFeedback, content, "出入库任务状态反馈WMS", "", "");
                if (agvContent != null && agvContent.code == 200 && Del)
                    BaseDal.DeleteAndMoveIntoHty(dt_Task, OperateTypeEnum.自动完成);
                else
                    BaseDal.UpdateData(dt_Task);
            }
            return WebResponseContent.Instance.OK();
        }
        #endregion
        #region 失败任务上报WMS并删除任务
        public WebResponseContent ErrorTaskFeedback(Dt_Task dt_Task, bool Del = false, string ErrorCode = "")
        {
            WebResponseContent content = new WebResponseContent();
            WMSInOutBoundCompleteFeedback boundCompleteFeedback = new WMSInOutBoundCompleteFeedback();
            WMSReturn agvContent = null;
            try
            {
                Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(WMSInOutBoundCompleteFeedback));
                if (apiInfo == null) dt_Task.ExceptionMessage = "未找到出入库完成反馈WMS接口配置信息!请检查接口配置";
                else
                {
                    boundCompleteFeedback.taskCode = dt_Task.WMSTaskNum;
                    boundCompleteFeedback.containerCode = dt_Task.PalletCode;
                    boundCompleteFeedback.fromStationCode = dt_Task.SourceAddress;
                    boundCompleteFeedback.toLocationCode = dt_Task.TargetAddress;
                    boundCompleteFeedback.status = 3;
                    boundCompleteFeedback.custStatus = ErrorCode;
                    boundCompleteFeedback.memo = dt_Task.ExceptionMessage;
                    string response = HttpHelper.Post(apiInfo.ApiAddress, boundCompleteFeedback.Serialize());
                    agvContent = response.DeserializeObject<WMSReturn>();
                    content.OK(data: agvContent);
                    content.Message = dt_Task.ExceptionMessage;
                    if (agvContent.code != 200)
                    {
                        dt_Task.ExceptionMessage = agvContent.message;
                        content.Error(agvContent.message);
                    }
                }
            }
            catch (Exception ex)
            {
                dt_Task.ExceptionMessage = ex.Message;
                content.Error(ex.Message);
            }
            finally
            {
                _trackloginfoService.AddTrackLog(boundCompleteFeedback, content, "出入库任务状态反馈WMS", "", "");
                if (agvContent != null && agvContent.code == 200 && Del)
                    BaseDal.DeleteAndMoveIntoHty(dt_Task, OperateTypeEnum.自动删除);
                else BaseDal.UpdateData(dt_Task);
            }
            return content;
        }
        #endregion
    }
}