huanghongfeng
2025-09-08 093c2580c7a274b4339a1f6305e91d4e69ea2c5a
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_TaskInfoService
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using Autofac.Core;
using AutoMapper;
using Magicodes.ExporterAndImporter.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NetTaste;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Reflection.Metadata;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm.Http;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Utilities;
using WIDESEAWCS_DTO.Enum;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;
using static Microsoft.IO.RecyclableMemoryStreamManager;
 
namespace WIDESEAWCS_TaskInfoService
{
    public class TaskService : ServiceBase<Dt_Task, ITaskRepository>, ITaskService
    {
        private readonly IRouterService _routerService;
        private readonly IRouterRepository _routerRepository;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskExecuteDetailRepository _taskExecuteDetailRepository;
        private readonly ITask_HtyService _task_HtyService;
        private readonly IMapper _mapper;
 
 
        private Dictionary<string, OrderByType> _taskOrderBy = new()
            {
                {nameof(Dt_Task.Grade),OrderByType.Asc },
                {nameof(Dt_Task.CreateDate),OrderByType.Asc},
            };
 
        public Dictionary<string, OrderByType> TaskOrderBy { get { return _taskOrderBy; } set { _taskOrderBy = value; } }
 
        public List<int> TaskInboundTypes => typeof(TaskInboundTypeEnum).GetEnumIndexList();
 
        public List<int> TaskOutboundTypes => typeof(TaskOutboundTypeEnum).GetEnumIndexList();
 
        public TaskService(ITaskRepository BaseDal, IRouterService routerService, ITaskExecuteDetailService taskExecuteDetailService, ITaskExecuteDetailRepository taskExecuteDetailRepository, IMapper mapper, ITask_HtyService task_HtyService, IRouterRepository routerRepository) : base(BaseDal)
        {
            _routerService = routerService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskExecuteDetailRepository = taskExecuteDetailRepository;
            _task_HtyService = task_HtyService;
            _mapper = mapper;
            _routerRepository = routerRepository;
        }
 
        /// <summary>
        /// 接收任务信息
        /// </summary>
        /// <param name="taskDTOs">WMS任务对象集合</param>
        /// <returns>返回处理结果</returns>
        public ExpoReverse TaskInstruction(ExpoTaskDTO expoTask)
        {
            ExpoReverse content = new ExpoReverse();
            try
            {
                foreach (var item in expoTask.data)
                {
                    if (item.type == 1)     //入库
                    {
                        Dt_Task dt_Task = new Dt_Task();
                        dt_Task.TaskNum = item.taskNo;
                        dt_Task.PalletCode = item.containerNo;    //暂无
                        dt_Task.Roadway = "RGV101";
                        dt_Task.TaskType = (int)TaskInboundTypeEnum.Inbound;
                        dt_Task.RGVTaskType = (int)RGVTaskTypeEnum.PickingUp;      //取货
                        dt_Task.RGVCurrentexecution = "0";      //目前暂无
                        dt_Task.TaskState = (int)TaskInStatusEnum.RGV_InNew;      //目前暂无
                        dt_Task.SourceAddress = item.startAddress;
                        dt_Task.TargetAddress = item.endAddress;
                        dt_Task.CurrentAddress = item.startAddress;
                        dt_Task.NextAddress = "0";   //目前暂无
                        dt_Task.Grade = item.priority;
                        dt_Task.WMSId = 0;   //目前暂无
                        BaseDal.AddData(dt_Task);
                        content.code = 0;
                    }
                    else if (item.type == 2)    //出库
                    {
                        string roadway = "";
                        string CurrentAddress = "";
                        switch (item.startAddress)
                        {
                            case "1":
                                roadway = "RGV110";
                                CurrentAddress = "1101";
                                break;
                            case "2":
                                roadway = "RGV112";
                                CurrentAddress = "1121";
                                break;
                            case "3":
                                roadway = "RGV114";
                                CurrentAddress = "1141";
                                break;
                            case "4":
                                roadway = "RGV115";
                                CurrentAddress = "1151";
                                break;
                        }
 
                        Dt_Task dt_Task = new Dt_Task();
                        dt_Task.TaskNum = item.taskNo;
                        dt_Task.PalletCode = item.containerNo;    //暂无
                        dt_Task.Roadway = roadway;
                        dt_Task.TaskType = (int)TaskOutboundTypeEnum.Outbound;
                        dt_Task.RGVTaskType = (int)RGVTaskTypeEnum.TravelingOnly;      //取货
                        dt_Task.RGVCurrentexecution = "0";      //目前暂无
                        dt_Task.TaskState = (int)TaskOutStatusEnum.OutNew;      //目前暂无
                        dt_Task.SourceAddress = item.startAddress;
                        dt_Task.TargetAddress = "";
                        dt_Task.CurrentAddress = CurrentAddress;
                        dt_Task.NextAddress = "0";   //目前暂无
                        dt_Task.Grade = item.priority;
                        dt_Task.WMSId = 0;   //目前暂无
                        BaseDal.AddData(dt_Task);
                        content.code = 0;
                    }
                    else
                    {
                        content.code = 1;
                        content.msg = $"任务类型错误,wcs接收到的类型为:{item.type}";
                    }
                }
            }
            catch (Exception ex)
            {
                content.code = 1;
                content.msg = $"数据错误,原因:{ex.Message}";
            }
            return content;
        }
 
 
        /// <summary>
        /// 根据设备编号、当前地址按照优先级以及创建时间排序查询任务池新增的任务
        /// </summary>
        /// <param name="deviceNo">设备编号</param>
        /// <returns>返回任务实体对象,可能为null</returns>
        public Dt_Task QueryStackerCraneTask(string DeviceCode)
        {
            return BaseDal.QueryFirst(x => x.Roadway == DeviceCode, TaskOrderBy);
        }
 
        public WebResponseContent UpdateRGVTasktype(int taskNum, int RGVTasktype)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
                int RGvtype = 0;
                string rwlx = "无";
                switch (RGVTasktype)
                {
                    case (int)RGVTaskTypeEnum.PickingUp:
                        RGvtype = 1;
                        rwlx = "取货";
                        break;
                    case (int)RGVTaskTypeEnum.Placing:
                        RGvtype = 2;
                        rwlx = "放货";
                        break;
                    case (int)RGVTaskTypeEnum.TravelingOnly:
                        RGvtype = 3;
                        rwlx = "仅行走中";
                        break;
                }
                task.RGVTaskType = RGvtype;
                bool updatesta = BaseDal.UpdateData(task);
                string Meesgt = $"目标地址:{task.CurrentAddress},任务类型:{rwlx},修改状态:{updatesta}";
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, Meesgt);
 
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
                throw;
            }
 
            return content;
        }
 
 
        /// <summary>
        /// 更新任务异常信息显示
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <param name="message">异常信息</param>
        public WebResponseContent UpdateTaskExceptionMessage(int taskNum, string message)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
                if (task.TaskType == (int)TaskInboundTypeEnum.Inbound)
                {
                    task.TaskState = (int)TaskInStatusEnum.InPending;
                }
                else if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                {
                    task.TaskState = (int)TaskOutStatusEnum.OutPending;
                }
                task.ExceptionMessage = message;
                task.ModifyDate = DateTime.Now;
                BaseDal.UpdateData(task);
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, task.ExceptionMessage);
 
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 将任务状态修改为下一个状态
        /// </summary>
        /// <param name="task">任务实体对象</param>
        /// <returns></returns>
        public WebResponseContent UpdateTaskStatusToNext([NotNull] Dt_Task task)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int oldState = task.TaskState;
                if (task.TaskType == (int)TaskInboundTypeEnum.Inbound)
                {
                    if (task.TaskState >= (int)TaskInStatusEnum.RGV_InMothercarFinish)
                    {
                        return content = WebResponseContent.Instance.Error($"该任务状态不可跳转到下一步,任务号:【{task.TaskNum}】,任务状态:【{task.TaskState}】");
                    }
 
                    int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskInStatusEnum>();
 
                    task.TaskState = nextStatus;
                }
                else if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                {
                    if (task.TaskState >= (int)TaskOutStatusEnum.OutFinish)
                    {
                        return content = WebResponseContent.Instance.Error($"该任务状态不可跳转到下一步,任务号:【{task.TaskNum}】,任务状态:【{task.TaskState}】");
                    }
 
                    int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskOutStatusEnum>();
 
                    task.TaskState = nextStatus;
                }
                else
                {
                    throw new Exception($"任务类型错误,未找到该任务类型,任务号:【{task.TaskNum}】,任务类型:【{task.TaskType}】");
                }
 
                if (task.TaskState <= 0)
                {
                    return content = WebResponseContent.Instance.Error($"该任务状态不可跳转到下一步,任务号:【{task.TaskNum}】,任务状态:【{task.TaskState}】");
                }
 
 
                task.ModifyDate = DateTime.Now;
                task.Modifier = "System";
                BaseDal.UpdateData(task);
 
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, App.User.UserId > 0 ? $"人工手动将任务状态从【{oldState}】跳转到【{task.TaskState}】" : $"系统自动流程,任务状态从【{oldState}】转到【{task.TaskState}】");
 
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 恢复挂起任务
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent TaskStatusRecovery(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
                if (task.TaskState != (int)TaskInStatusEnum.InPending && task.TaskState != (int)TaskOutStatusEnum.OutPending)
                {
                    return content = WebResponseContent.Instance.Error($"该任务状态不可恢复,任务号:【{taskNum}】,任务状态:【{task.TaskState}】");
                }
 
                Dt_TaskExecuteDetail taskExecuteDetail = _taskExecuteDetailRepository.QueryFirst(x => x.TaskId == task.TaskId && x.IsNormal, new Dictionary<string, OrderByType> { { nameof(Dt_TaskExecuteDetail.TaskDetailId), OrderByType.Desc } });
                if (taskExecuteDetail != null)
                {
                    task.TaskState = taskExecuteDetail.TaskState;
                }
                else
                {
                    if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
                    {
                        task.TaskState = (int)TaskOutStatusEnum.OutNew;
                    }
                    else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                    {
                        task.TaskState = (int)TaskInStatusEnum.RGV_InNew;
                    }
                    //todo
                }
 
                task.ExceptionMessage = string.Empty;
 
                BaseDal.UpdateData(task);
 
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"人工恢复挂起任务,恢复挂起时任务状态【{task.TaskState}】");
 
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 回滚任务状态
        /// </summary>
        /// <param name="taskNum">任务号</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent RollbackTaskStatusToLast(int taskNum)
        {
            WebResponseContent content = new();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
 
                int oldState = task.TaskState;
                Dt_TaskExecuteDetail taskExecuteDetail = _taskExecuteDetailRepository.QueryFirst(x => x.TaskId == task.TaskId && x.TaskState < task.TaskState && x.TaskState > 0, new Dictionary<string, OrderByType> { { nameof(Dt_TaskExecuteDetail.TaskDetailId), OrderByType.Desc } });
                if (taskExecuteDetail != null)
                {
                    task.TaskState = taskExecuteDetail.TaskState;
                    task.CurrentAddress = taskExecuteDetail.CurrentAddress;
                    task.NextAddress = taskExecuteDetail.NextAddress;
                }
                else
                {
                    return content = WebResponseContent.Instance.Error($"未找到任务明细信息,该任务状态不可回滚到上一步,任务号:【{taskNum}】,任务状态:【{task.TaskState}】");
                }
 
                task.ExceptionMessage = string.Empty;
 
                BaseDal.UpdateData(task);
 
                _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"人工将任务状态从【{oldState}】回滚到【{task.TaskState}】");
 
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
 
 
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="keys">主键数组</param>
        /// <returns></returns>
        public virtual WebResponseContent DeleteData(object[] keys)
        {
            try
            {
                List<Dt_Task> tasks = BaseDal.QureyDataByIds(keys);
                foreach (Dt_Task task in tasks)
                {
                    _task_HtyService.AddTaskHty(task);
                }
 
 
                if (typeof(Dt_Task).GetNavigatePro() == null)
                    return BaseDal.DeleteDataByIds(keys) ? WebResponseContent.Instance.OK() : WebResponseContent.Instance.Error();
                else
                {
                    if (keys != null)
                    {
                        Type detailType = typeof(Dt_Task).GetDetailType();
                        string name = typeof(Dt_Task).GetMainIdByDetail();
                        List<object> dynamicDelKeys = new List<object>();
 
                        for (int i = 0; i < keys.Length; i++)
                        {
                            dynamicDelKeys.Add(keys[i]);
                        }
                        ((SqlSugarClient)BaseDal.Db).BeginTran();
 
                        if (dynamicDelKeys.Count > 0)
                            BaseDal.Db.Deleteable<object>().AS(detailType.Name).Where($"{name} in (@id)", new { id = dynamicDelKeys.ToArray() }).ExecuteCommandHasChange();
 
                        BaseDal.DeleteDataByIds(keys);
 
                        ((SqlSugarClient)BaseDal.Db).CommitTran();
 
                        return WebResponseContent.Instance.OK();
                    }
                    else
                    {
                        return WebResponseContent.Instance.Error("参数错误");
                    }
                }
            }
            catch (Exception ex)
            {
                ((SqlSugarClient)BaseDal.Db).RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 任务完成将状态修改至下一步(后续调整)
        /// </summary>
        /// <param name="taskNum">任务编号</param>
        /// <returns>返回处理结果</returns>
        public WebResponseContent UpdateTaskStatus(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
 
            Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
            if (task == null) return WebResponseContent.Instance.Error($"未找到该任务信息,任务号:【{taskNum}】");
 
            int oldState = task.TaskState;
            if (task.TaskType == (int)TaskInboundTypeEnum.Inbound)
            {
                if (task.TaskState == (int)TaskInStatusEnum.RGV_InPickupExecuting)    //由取货中==》取货完成
                {
                    task.TaskState = (int)TaskInStatusEnum.RGV_InPickupFinish;
                    task.CurrentAddress = "1021";  //放货的地址
                    task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;    //放货
                }
                else if (task.TaskState == (int)TaskInStatusEnum.RGV_InReleaseExecuting)
                {
                    task.TaskState = (int)TaskInStatusEnum.RGV_InReleaseFinish;
                    task.RGVTaskType = (int)RGVTaskTypeEnum.PickingUp;      //取货
                    task.Roadway = "RGV104";
                }
                else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZicheExecuting)
                {
                    Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.StartPosi == task.CurrentAddress && x.ChildPosi == task.TargetAddress && x.IsEnd == false);
                    if (dt_Router != null)
                    {
                        if (task.CurrentAddress == "1061")  //HCJ站台
                        {
                            task.Roadway = "RGV107";
                        }
                        task.TaskState = (int)TaskInStatusEnum.RGV_InZicheFinish;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;      //取货
                        task.CurrentAddress = dt_Router.NextPosi;
 
                    }
                    else
                    {
                        if (task.TargetAddress == "3" && task.TargetAddress == "4")
                        {
                            task.TaskState = (int)TaskInStatusEnum.RGV_InZicheFinish;
                            task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;      //取货
                            task.CurrentAddress = "1061";
                        }
                    }
                }
                else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZicheReleaseExecuting)
                {
                    task.TaskState = (int)TaskInStatusEnum.RGV_InZicheReleaseFinish;
                    task.RGVTaskType = (int)RGVTaskTypeEnum.TravelingOnly;
                    task.CurrentAddress = "1021";       //此次待修改,需要考虑2号子车移动
                }
                else if (task.TaskState == (int)TaskInStatusEnum.RGV_InZichemoveExecuting)
                {
                    Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.IsEnd == true && x.ChildPosi == task.TargetAddress && x.InOutType == 1);
 
                    task.TaskState = (int)TaskInStatusEnum.RGV_InZichemoveFinish;
                    task.RGVTaskType = (int)RGVTaskTypeEnum.TravelingOnly;
                    task.CurrentAddress = dt_Router.NextPosi;       //此次待修改,需要考虑2号子车移动
                    task.Roadway = dt_Router.ChildPosiDeviceCode;
                }
                else if (task.TaskState == (int)TaskInStatusEnum.RGV_InMothercarExecuting)
                {
                    //最总任务完成
                    task.TaskState = (int)TaskInStatusEnum.RGV_InMothercarFinish;
                }
            }
            else if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
            {
                if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutMothercarExecuting)
                {
                    Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.StartPosi == task.CurrentAddress && x.IsEnd == true && x.InOutType == 2);
                    if (dt_Router != null)
                    {
                        task.TaskState = (int)TaskOutStatusEnum.RGV_OutMothercarFinish;
                        task.CurrentAddress = dt_Router.NextPosi;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.PickingUp;
                        task.Roadway = dt_Router.ChildPosiDeviceCode;  //放货的地址
                    }
                    else
                    {
                        Dt_Router dt_Rout = _routerRepository.QueryFirst(x => x.StartPosi == task.CurrentAddress && x.IsEnd == false && x.InOutType == 2);
                        task.TaskState = (int)TaskOutStatusEnum.RGV_OutMothercarFinish;
                        task.CurrentAddress = dt_Rout.StartPosi;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.PickingUp;
                        task.Roadway = dt_Rout.ChildPosiDeviceCode;
                    }
                }
                else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheExecuting)
                {
                    if ((task.CurrentAddress == "1101" || task.CurrentAddress == "1121") && task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheExecuting)
                    {
                        Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.StartPosi == task.CurrentAddress && x.InOutType == 2);
                        task.TaskState = (int)TaskOutStatusEnum.RGV_OutZicheFinish;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;
                        task.Roadway = dt_Router.ChildPosiDeviceCode;
                        task.CurrentAddress = dt_Router.NextPosi;
 
                    }
                    else
                    {
                        task.TaskState = (int)TaskOutStatusEnum.RGV_OutZicheFinish;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;
                        task.CurrentAddress = "1171";
                        task.Roadway = "RGV116";
                    }
 
                    /*Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.StartPosi == task.CurrentAddress && x.InOutType == 2);
                    task.TaskState = (int)TaskOutStatusEnum.RGV_OutZicheFinish;
                    task.CurrentAddress = dt_Router.NextPosi;
                    task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;*/
                }
                else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutZicheReleaseExecuting)
                {
                    if (task.CurrentAddress == "1131")
                    {
                        Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.StartPosi == task.CurrentAddress && x.InOutType == 2);
                        task.TaskState = (int)TaskOutStatusEnum.RGV_OutMothercarFinish;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.PickingUp;
                        task.Roadway = dt_Router.ChildPosiDeviceCode;
                    }
                    else
                    {
                        task.TaskState = (int)TaskOutStatusEnum.RGV_OutZicheReleaseFinish;
                        task.RGVTaskType = (int)RGVTaskTypeEnum.PickingUp;
                        task.Roadway = "RGV118";
                    }
                }
                else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutPickupExecuting)
                {
                    task.TaskState = (int)TaskOutStatusEnum.RGV_OutPickupFinish;
                    task.RGVTaskType = (int)RGVTaskTypeEnum.Placing;
                    task.CurrentAddress = "";
                }
                else if (task.TaskState == (int)TaskOutStatusEnum.RGV_OutReleaseExecuting)
                {
                    task.TaskState = (int)TaskOutStatusEnum.OutFinish;      //任务完成
                }
                else
                {
                    return WebResponseContent.Instance.Error();
                }
            }
 
            task.ExceptionMessage = string.Empty;
 
            BaseDal.UpdateData(task);
 
            _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"自动任务状态从【{oldState}】进入到【{task.TaskState}】,");
 
            content = WebResponseContent.Instance.OK();
 
            return content;
 
        }
 
 
 
        /// <summary>
        /// 查询RGV状态
        /// </summary>
        /// <param name="statusquery"></param>
        /// <returns></returns>
        public RGVStoResponse RgvStatus(StatusqueryReception statusquery)
        {
            RGVStoResponse rGVstatus = new RGVStoResponse();
            try
            {
                foreach (var item in statusquery.data)
                {
                    if (statusquery == null)
                    {
                        rGVstatus.code = 1;
                        rGVstatus.msg = "明和WCS接收到的参数为空,请检查";
                        return rGVstatus;
                    }
 
                    if (item.rgvTag == null)
                    {
                        rGVstatus.code = 1;
                        rGVstatus.msg = "明和WCS接收到RGV编号为空,请检查";
                        return rGVstatus;
                    }
 
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == item.rgvTag);
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == item.rgvTag && x.DeviceProParamName == "RGVCurrentlocation" && x.DeviceProParamType == "ReadDeviceCommand");
                    if (item.type == "pick")   //取货
                    {
                        //获取设备
                        //获取取货站台货位站台
                        Dt_Router dt_Router = _routerRepository.QueryFirst(x => x.InOutType == 1 && x.ChildPosiDeviceCode == item.rgvTag && x.IsEnd == true);
 
 
                        int Currentlocation = conveyorLine.Communicator.Read<short>(deviceProDTO.DeviceProAddress);
                        if (Currentlocation == int.Parse(dt_Router.NextPosi))    //为无货
                        {
                            rGVstatus.code = 0;
                            rGVstatus.data = new List<RgvOperation>
                    {
                        new RgvOperation { rgvTag = item.rgvTag, type = "pick", status = 1 },
                    };
                        }
                        else
                        {
                            rGVstatus.code = 0;
                            rGVstatus.data = new List<RgvOperation>
                    {
                        new RgvOperation { rgvTag = item.rgvTag, type = "pick", status = 0 },
                    };
                        }
                    }
                    else if (item.type == "put")    //放货
                    {
 
                        int Currentlocation = conveyorLine.Communicator.Read<short>(deviceProDTO.DeviceProAddress);
                        switch (Currentlocation)
                        {
                            case 1101:
                            case 1121:
                            case 1141:
                            case 1151:
                                rGVstatus.code = 0;
                                rGVstatus.data = new List<RgvOperation>
                                {
                                    new RgvOperation { rgvTag = item.rgvTag, type = "put", status = 2 },
                                };
                                break;
                            default:
                                rGVstatus.code = 0;
                                rGVstatus.data = new List<RgvOperation>
                                {
                                    new RgvOperation { rgvTag = item.rgvTag, type = "put", status = 0 },
                                };
                                break;
                        }
 
                    }
                    else
                    {
                        rGVstatus.code = 1;
                        rGVstatus.msg = $"明和WCS接收的参数类型错误,实际接收值为:{item.type}";
 
                    }
                }
 
                return rGVstatus;
            }
            catch (Exception ex)
            {
                rGVstatus.code = 1;
                rGVstatus.msg = $"明和WCS接口错误:{ex.Message}";
                return rGVstatus;
            }
 
        }
 
 
        /// <summary>
        /// 堆垛机完成的接口
        /// </summary>
        /// <param name="sCstatus"></param>
        /// <returns></returns>
        public ExpoReverse StkPickPutStatus(SCstatusReception sCstatus)
        {
            ExpoReverse expoReverse = new ExpoReverse();
            try
            {
                foreach (var item in sCstatus.data)
                {
                    if (sCstatus == null)
                    {
                        expoReverse.code = 1;
                        expoReverse.msg = "明和WCS接收到的参数为空,请检查";
                        return expoReverse;
                    }
 
                    if (item.rgvTag == null)
                    {
                        expoReverse.code = 1;
                        expoReverse.msg = "明和WCS接收到RGV编号为空,请检查";
                        return expoReverse;
                    }
 
                    if (item.type == "pick")   //取货
                    {
                        expoReverse.code = 0;
                    }
                    else if (item.type == "put")    //放货
                    {
                        expoReverse.code = 0;
                    }
                    else
                    {
                        expoReverse.code = 1;
                        expoReverse.msg = $"明和WCS接收的参数类型错误,实际接收值为:{item.type}";
 
                    }
                }
 
                return expoReverse;
            }
            catch (Exception ex)
            {
                expoReverse.code = 1;
                expoReverse.msg = $"明和WCS接口错误:{ex.Message}";
                return expoReverse;
            }
        }
 
        /// <summary>
        /// 堆垛机故障信息
        /// </summary>
        /// <param name="sCErrorStatus"></param>
        /// <returns></returns>
        public ExpoReverse StkErrorStatus(SCErrorStatusReception sCErrorStatus)
        {
            ExpoReverse expoReverse = new ExpoReverse();
            try
            {
                foreach (var item in sCErrorStatus.data)
                {
                    if (sCErrorStatus == null)
                    {
                        expoReverse.code = 1;
                        expoReverse.msg = "明和WCS接收到的参数为空,请检查";
                        return expoReverse;
                    }
                    expoReverse.code = 0;
                }
 
                return expoReverse;
            }
            catch (Exception ex)
            {
                expoReverse.code = 1;
                expoReverse.msg = $"明和WCS接口错误:{ex.Message}";
                return expoReverse;
            }
        }
 
        /// <summary>
        /// 任务取消接口
        /// </summary>
        /// <param name="sCErrorStatus"></param>
        /// <returns></returns>
        public ExpoReverse TaskCancel(TaskcanceledReception taskcanceled)
        {
            ExpoReverse expoReverse = new ExpoReverse();
            try
            {
                List<Dt_Task> _TaskList = BaseDal.QueryData(x => x.TaskType == (int)TaskInboundTypeEnum.Inbound).ToList();
 
 
 
                foreach (var item in taskcanceled.data)
                {
                    if (taskcanceled == null)
                    {
                        expoReverse.code = 1;
                        expoReverse.msg = "明和WCS接收到的参数为空,请检查";
                        return expoReverse;
                    }
 
                    Dt_Task dt_Task = _TaskList.FirstOrDefault(x => x.TaskNum == item.taskNo);
                    if (dt_Task != null)
                    {
                        if (dt_Task.TaskState == (int)TaskInStatusEnum.RGV_InNew)
                        {
                            dt_Task.TaskState = (int)TaskInStatusEnum.InCancel; //任务取消后,则移入历史
                            _task_HtyService.AddTaskHty(dt_Task);
                            BaseDal.DeleteData(dt_Task);
                            expoReverse.code = 0;
                        }
                        else
                        {
                            expoReverse.code = 0;
                            expoReverse.msg = $"当前任务已在执行中,不可取消!任务号:{item.taskNo}";
                            return expoReverse;
                        }
                    }
                    else
                    {
                        expoReverse.code = 0;
                        expoReverse.msg = $"未找到入库任务,任务号:{item.taskNo}";
                        return expoReverse;
                    }
                }
 
                return expoReverse;
            }
            catch (Exception ex)
            {
                expoReverse.code = 1;
                expoReverse.msg = $"明和WCS接口错误:{ex.Message}";
                return expoReverse;
            }
        }
 
 
 
 
        /// <summary>
        /// 堆垛机运行状态接口
        /// </summary>
        /// <param name="sCErrorStatus"></param>
        /// <returns></returns>
        public WebResponseContent StkRunStatus()
        {
            WebResponseContent responseContent=new WebResponseContent();
            return responseContent;
        }
    }
}