| using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEA_DTO.Agv; | 
| using WIDESEAWCS_Common.APIEnum; | 
| using WIDESEAWCS_Common.TaskEnum; | 
| using WIDESEAWCS_Core; | 
| using WIDESEAWCS_Core.Helper; | 
| using WIDESEAWCS_DTO.Agv; | 
| using WIDESEAWCS_Model.Models; | 
| using WIDESEAWCS_QuartzJob; | 
|   | 
| namespace WIDESEAWCS_Tasks | 
| { | 
|     public partial class AGV_CPJob | 
|     { | 
|         public void SendAGVTask() | 
|         { | 
|             try | 
|             { | 
|                 var newTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_Execute.ObjToInt()|| x.TaskState == TaskStatusEnum.New.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode) && x.TaskType!=999).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList(); | 
|   | 
|                 if (newTasks.Count>0) | 
|                 { | 
|                     foreach (var task in newTasks) | 
|                     { | 
|                         try | 
|                         { | 
|                             if (task.TaskType == TaskTypeEnum.OutProduct.ObjToInt()) | 
|                                 task.CurrentAddress = GetAGVAddress(task.CurrentAddress); | 
|                             else | 
|                                 task.NextAddress = GetAGVAddress(task.NextAddress); | 
|                             AgvTaskDTO taskDTO = new AgvTaskDTO() | 
|                             { | 
|                                 TaskCode = task.AgvTaskNum, | 
|                                 ReqCode = DateTime.Now.ToString("yyyyMMdd") + task.AgvTaskNum, | 
|                                 TaskTyp = task.TaskType < TaskTypeEnum.Inbound.ObjToInt() ? "CPC" : "CPR", | 
|                                 ctnrCode = task.PalletCode, | 
|                                 PositionCodePath = new List<CodePath>() | 
|                                 { | 
|                                     new CodePath() | 
|                                     { | 
|                                         type = "05", | 
|                                         positionCode = task.CurrentAddress | 
|                                     }, | 
|                                     new CodePath() | 
|                                     { | 
|                                         type = "05", | 
|                                         positionCode = task.NextAddress | 
|                                     } | 
|                                 } | 
|                             }; | 
|                             if (taskDTO.TaskTyp== "CPC") | 
|                             { | 
|                                 taskDTO.Priority = task.Grade.ToString(); | 
|                             } | 
|                             if (task.GroupId.IsNotEmptyOrNull()) | 
|                             { | 
|                                 taskDTO.GroupId=task.GroupId; | 
|                             } | 
|                             //发送AGV任务 | 
|                             WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask); | 
|                             if (!content.Status) | 
|                                 throw new Exception(content.Message); | 
|                             if (taskDTO.TaskTyp == "CPC") | 
|                             { | 
|                                 task.Dispatchertime = DateTime.Now; | 
|                                 _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing); | 
|                             } | 
|                             else | 
|                             { | 
|                                 task.Dispatchertime = DateTime.Now; | 
|                                 _taskService.UpdateTask(task, TaskStatusEnum.AGV_Takeing); | 
|                             } | 
|                             Thread.Sleep(500); | 
|                         } | 
|                         catch (Exception ex) | 
|                         { | 
|                             task.TaskState = TaskStatusEnum.Exception.ObjToInt(); | 
|                             task.ExceptionMessage = ex.Message; | 
|                         } | 
|                     } | 
|                     if (newTasks.Count > 0) | 
|                     { | 
|                         _taskService.UpdateData(newTasks); | 
|                     } | 
|                 } | 
|                 var newTasksLocations = _taskService.Db.Queryable<Dt_Task>().Where(x => x.TaskState == TaskStatusEnum.New.ObjToInt() && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode) && x.Remark == "测试空框").ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList(); | 
|                 if (newTasksLocations.Count>0) | 
|                 { | 
|                     foreach (var task in newTasksLocations) | 
|                     { | 
|                         try | 
|                         { | 
|                             string CurrentAddress = GetAGVAddress(task.CurrentAddress); | 
|                             string NextAddress = GetAGVAddress(task.NextAddress); | 
|                             AgvTaskDTO taskDTO = new AgvTaskDTO() | 
|                             { | 
|                                 TaskCode = task.AgvTaskNum, | 
|                                 ReqCode = DateTime.Now.ToString("yyyyMMdd") + task.AgvTaskNum, | 
|                                 TaskTyp = "CP", | 
|                                 ctnrCode = task.PalletCode, | 
|                                 PositionCodePath = new List<CodePath>() | 
|                                 { | 
|                                     new CodePath() | 
|                                     { | 
|                                         type="05", | 
|                                         positionCode=CurrentAddress | 
|                                     }, | 
|                                     new CodePath() | 
|                                     { | 
|                                         type="05", | 
|                                         positionCode=NextAddress | 
|                                     } | 
|                                 }, | 
|                             }; | 
|                             //发送AGV任务 | 
|                             WebResponseContent content = _taskService.AgvSendTask(taskDTO, APIEnum.Agv_CPSendTask); | 
|                             if (!content.Status) | 
|                                 throw new Exception(content.Message); | 
|                             task.TaskState = TaskStatusEnum.AGV_Takeing.ObjToInt(); | 
|                             task.CurrentAddress = CurrentAddress; | 
|                             task.NextAddress = NextAddress; | 
|                             _taskService.UpdateTask(task, TaskStatusEnum.AGV_Executing); | 
|                         } | 
|                         catch (Exception ex) | 
|                         { | 
|                             task.TaskState = TaskStatusEnum.Exception.ObjToInt(); | 
|                             task.ExceptionMessage = ex.Message; | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 WriteError(nameof(AGV_CPJob), ex.Message, ex); | 
|             } | 
|         } | 
|         /// <summary> | 
|         /// AGV取放货回调 | 
|         /// </summary> | 
|         public void ContinueAGVTask() | 
|         { | 
|             try | 
|             { | 
|                 //获取是否有安全申请中的任务 | 
|                 var continueTasks = _taskService.Db.Queryable<Dt_Task>().Where(x => (x.TaskState == TaskStatusEnum.AGV_WaitToExecute.ObjToInt()) && nameof(AGV_CPJob).Contains(x.DeviceCode) && !string.IsNullOrEmpty(x.DeviceCode)).ToList().OrderBy(x => x.Grade).ThenBy(x => x.CreateDate).ToList(); | 
|                 foreach (var continueTask in continueTasks) | 
|                 { | 
|                     //取货回调 | 
|                     if (continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) | 
|                     { | 
|                         Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.CurrentAddress || x.StationCode == continueTask.CurrentAddress); | 
|                         if (stationManger == null) | 
|                         { | 
|                             continue; | 
|                         } | 
|                         IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); | 
|                         if (device == null) | 
|                         { | 
|                             continue; | 
|                         } | 
|                         OtherDevice otherDevice = (OtherDevice)device; | 
|                         short canTake = otherDevice.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanTake, stationManger.StationCode); | 
|                         if (canTake != 1) | 
|                         { | 
|                             continue; | 
|                         } | 
|                     } | 
|                     else//放货回调 | 
|                     { | 
|                         Dt_StationManger stationManger = _stationMangerRepository.QueryFirst(x => x.AGVStationCode == continueTask.NextAddress); | 
|                         if (stationManger == null) | 
|                         { | 
|                             continue; | 
|                         } | 
|                         IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManger.StationDeviceCode); | 
|                         if (device == null) | 
|                         { | 
|                             continue; | 
|                         } | 
|                         OtherDevice otherDevice = (OtherDevice)device; | 
|                         short canPut = otherDevice.GetValue<GroundStationDBName, short>(GroundStationDBName.R_IsCanPut, stationManger.StationCode); | 
|                         if (canPut != 1) | 
|                         { | 
|                             continue; | 
|                         } | 
|                     } | 
|                     //获取调入参数 | 
|                     AGVBoxApplyPassDTO boxApplyPassDTO = new AGVBoxApplyPassDTO() | 
|                     { | 
|                         ReqCode = Guid.NewGuid().ToString().Replace("-", ""), | 
|                         ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), | 
|                         TaskCode = continueTask.AgvTaskNum | 
|                     }; | 
|                     if (continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) | 
|                     { | 
|                         boxApplyPassDTO.Type = "1"; | 
|                     } | 
|                     else | 
|                     { | 
|                         boxApplyPassDTO.Type = "2"; | 
|                     } | 
|                     //请求料箱回调接口 | 
|                     WebResponseContent content = _taskService.AgvBoxApplyPass(boxApplyPassDTO); | 
|                     if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup && boxApplyPassDTO.TaskCode == continueTask.AgvTaskNum) | 
|                     { | 
|                         _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Executing); | 
|                     } | 
|                     else if (content.Status && continueTask.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && boxApplyPassDTO.TaskCode== continueTask.AgvTaskNum) | 
|                     { | 
|                         _taskService.UpdateTask(continueTask, TaskStatusEnum.AGV_Puting); | 
|                     } | 
|                     else | 
|                     { | 
|                         continueTask.ExceptionMessage = content.Message; | 
|                         _taskService.UpdateTask(continueTask, TaskStatusEnum.Exception); | 
|                     } | 
|                 } | 
|                      | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 WriteError(nameof(AGV_CPJob), ex.Message, ex); | 
|             } | 
|         } | 
|         /// <summary> | 
|         /// 获取AGV地址 | 
|         /// </summary> | 
|         /// <param name="Address"></param> | 
|         /// <returns></returns> | 
|         public static string GetAGVAddress(string Address) | 
|         { | 
|             string[] targetCodes = Address.Split("-"); | 
|             if (targetCodes.Length == 5) | 
|             { | 
|                 var Row = Convert.ToInt16(targetCodes[1]); | 
|                 var Column = Convert.ToInt16(targetCodes[2]); | 
|                 var Layer = Convert.ToInt16(targetCodes[3]); | 
|                 var a = Row switch | 
|                 { | 
|                     1 => "A", | 
|                     2 => "B", | 
|                     3 => "C", | 
|                     4 => "D", | 
|                     5 => "E", | 
|                     _ => throw new Exception($"未定义的排,地址:【{Address}】"), | 
|                 }; | 
|                 var b = Layer > 9 ? "" + Layer : "0" + Layer; | 
|                 var c = Column > 9 ? "" + Column : "0" + Column; | 
|                 if (Column == 10) c = "010"; | 
|                 Address = a + b + c; | 
|             } | 
|             else | 
|             { | 
|                 throw new Exception($"地址有误,地址:【{Address}】"); | 
|             } | 
|             return Address; | 
|         } | 
|     } | 
| } |