|  |  | 
 |  |  | using AutoMapper; | 
 |  |  | using HslCommunication; | 
 |  |  | using Mapster; | 
 |  |  | using Microsoft.AspNetCore.Components.Routing; | 
 |  |  | using Newtonsoft.Json; | 
 |  |  | using SqlSugar; | 
 |  |  | using System.Diagnostics.CodeAnalysis; | 
 |  |  | using System.Threading.Tasks; | 
 |  |  | using WIDESEAWCS_BasicInfoRepository; | 
 |  |  | using WIDESEAWCS_BasicInfoService; | 
 |  |  | using WIDESEAWCS_Common; | 
 |  |  | 
 |  |  | using WIDESEAWCS_Core; | 
 |  |  | using WIDESEAWCS_Core.BaseServices; | 
 |  |  | using WIDESEAWCS_Core.Helper; | 
 |  |  | using WIDESEAWCS_Core.LogHelper; | 
 |  |  | using WIDESEAWCS_DTO.TaskInfo; | 
 |  |  | using WIDESEAWCS_DTO.WMS; | 
 |  |  | using WIDESEAWCS_ISystemServices; | 
 |  |  | 
 |  |  | using WIDESEAWCS_ITaskInfoRepository; | 
 |  |  | using WIDESEAWCS_ITaskInfoService; | 
 |  |  | using WIDESEAWCS_Model.Models; | 
 |  |  | using WIDESEAWCS_QuartzJob; | 
 |  |  | using WIDESEAWCS_QuartzJob.DeviceBase; | 
 |  |  | using WIDESEAWCS_QuartzJob.Models; | 
 |  |  | using WIDESEAWCS_QuartzJob.Repository; | 
 |  |  | using WIDESEAWCS_QuartzJob.Service; | 
 |  |  | using WIDESEAWCS_TaskInfoRepository; | 
 |  |  |  | 
 |  |  | namespace WIDESEAWCS_TaskInfoService | 
 |  |  | { | 
 |  |  | 
 |  |  |                     if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup) | 
 |  |  |                     { | 
 |  |  |                         // 判断任务目标地址和路径是否满足特定条件 | 
 |  |  |                         if (task.TargetAddress == "002-021-001" && task.Roadway.Contains("JZ") && task.TaskType == (int)TaskOutboundTypeEnum.OutTray) | 
 |  |  |                         if (((task.TargetAddress == "002-021-001" || task.TargetAddress == "001-021-001") && task.Roadway.Contains("JZ"))|| task.TaskType == (int)TaskOutboundTypeEnum.OutFireAlarm) | 
 |  |  |                         { | 
 |  |  |                             // 设置任务状态为出库新建 | 
 |  |  |                             task.TaskState = (int)TaskOutStatusEnum.OutNew; | 
 |  |  | 
 |  |  |                     // 判断任务类型是否为入库任务 | 
 |  |  |                     else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) | 
 |  |  |                     { | 
 |  |  |                         var value = _sys_ConfigService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStation, SysConfigKeyConst.JZNGInBoundStation).ConfigValue; | 
 |  |  |                         var valueList = value.Split(',').ToList(); | 
 |  |  |                         // 判断源地址是否不等于"1359-4" | 
 |  |  |                         if (task.SourceAddress != "1359-4") | 
 |  |  |                         //if (task.SourceAddress != "1359-4" && task.SourceAddress != "1357-4") | 
 |  |  |                         if (!valueList.Contains(task.SourceAddress)) | 
 |  |  |                         { | 
 |  |  |                             // 查询从源地址到目标地址的路由 | 
 |  |  |                             List<Dt_Router> routers = _routerService.QueryNextRoutes(item.SourceAddress, item.TargetAddress); | 
 |  |  | 
 |  |  |                         } | 
 |  |  |                         else | 
 |  |  |                         { | 
 |  |  |                             // 设置当前地址为"002-021-001" | 
 |  |  |                             task.CurrentAddress = "002-021-001"; | 
 |  |  |                             task.CurrentAddress = string.Empty; | 
 |  |  |                             if (task.SourceAddress == "1325-6") | 
 |  |  |                                 // 设置当前地址为"001-021-001" | 
 |  |  |                                 task.CurrentAddress = "001-021-001"; | 
 |  |  |                             else | 
 |  |  |                                 // 设置当前地址为"002-021-001" | 
 |  |  |                                 task.CurrentAddress = "002-021-001"; | 
 |  |  |                             // 设置下一个地址为目标地址 | 
 |  |  |                             task.NextAddress = item.TargetAddress; | 
 |  |  |                         } | 
 |  |  | 
 |  |  |                     wmsIpAddrss = GetIpAddress(SysConfigKeyConst.WMSIP_BASE, SysConfigKeyConst.RequestTask); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 var result = await HttpHelper.PostAsync(wmsIpAddrss, new { Position = sourceAddress, PalletCode = palletCode }.ToJsonString()); | 
 |  |  |                 var result = await HttpHelper.PostAsync(wmsIpAddrss, new { Position = sourceAddress, PalletCode = palletCode, stationManager.productLine }.ToJsonString()); | 
 |  |  |                 content = JsonConvert.DeserializeObject<WebResponseContent>(result); | 
 |  |  |  | 
 |  |  |                 if (!content.Status) | 
 |  |  | 
 |  |  |         /// <param name="deviceNo">设备编号</param> | 
 |  |  |         /// <param name="currentAddress">当前地址</param> | 
 |  |  |         /// <returns></returns> | 
 |  |  |         public Dt_Task QueryConveyorLineTask(string deviceNo, string currentAddress,string Barcode) | 
 |  |  |         { | 
 |  |  |             return BaseDal.QueryFirst(x => ((TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskInStatusEnum.InNew) || (TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish)) && x.CurrentAddress == currentAddress && x.PalletCode == Barcode, TaskOrderBy); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         /// <summary> | 
 |  |  |         /// 根据设备编号、当前地址查询输送线未执行的任务 | 
 |  |  |         /// </summary> | 
 |  |  |         /// <param name="deviceNo">设备编号</param> | 
 |  |  |         /// <param name="currentAddress">当前地址</param> | 
 |  |  |         /// <returns></returns> | 
 |  |  |         public Dt_Task QueryNextConveyorLineTask(string deviceNo, string currentAddress) | 
 |  |  |         { | 
 |  |  |             return BaseDal.QueryFirst(x => ((TaskInboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskInStatusEnum.InNew) || (TaskOutboundTypes.Contains(x.TaskType) && x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish)) && x.NextAddress == currentAddress, TaskOrderBy); | 
 |  |  | 
 |  |  |         /// <param name="nextAddress">下一地址</param> | 
 |  |  |         /// <returns></returns> | 
 |  |  |         public Dt_Task QueryExecutingConveyorLineTask(int taskNum, string nextAddress) | 
 |  |  |         { | 
 |  |  |             return BaseDal.QueryFirst(x => x.TaskNum == taskNum && x.NextAddress == nextAddress && (x.TaskState == (int)TaskInStatusEnum.Line_InExecuting || x.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting), TaskOrderBy); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         /// <summary> | 
 |  |  |         /// 根据任务号、下一地址查询输送线执行中的任务 | 
 |  |  |         /// </summary> | 
 |  |  |         /// <param name="taskNum">任务号</param> | 
 |  |  |         /// <param name="nextAddress">下一地址</param> | 
 |  |  |         /// <returns></returns> | 
 |  |  |         public Dt_Task QueryExecutingConveyorLineTask(int taskNum, string nextAddress,string Barcode) | 
 |  |  |         { | 
 |  |  |             return BaseDal.QueryFirst(x => x.TaskNum == taskNum && x.NextAddress == nextAddress && (x.TaskState == (int)TaskInStatusEnum.Line_InExecuting || x.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting), TaskOrderBy); | 
 |  |  |         } | 
 |  |  | 
 |  |  |                         _taskHtyRepository.AddData(task_Hty); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                     if (task.TaskState == (int)TaskOutStatusEnum.SC_OutFinish) | 
 |  |  |                     { | 
 |  |  |                         #region WMS同步任务完成 | 
 |  |  |  | 
 |  |  |                         var keys = new Dictionary<string, object>() | 
 |  |  |                         { | 
 |  |  |                             {"taskNum", task.TaskNum} | 
 |  |  |                         }; | 
 |  |  |                         // 获取WMSip地址 | 
 |  |  |                         var configs = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress); | 
 |  |  |                         var wmsBasex = configs.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue; | 
 |  |  |                         var completeTask = configs.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.CompleteTask)?.ConfigValue; | 
 |  |  |                         if (wmsBasex == null || completeTask == null) | 
 |  |  |                         { | 
 |  |  |                             throw new InvalidOperationException("WMS IP 未配置"); | 
 |  |  |                         } | 
 |  |  |                         var wmsIpAddressx = wmsBasex + completeTask; | 
 |  |  |  | 
 |  |  |                         var results = HttpHelper.GetAsync(wmsIpAddressx, keys).Result; | 
 |  |  |                         content = JsonConvert.DeserializeObject<WebResponseContent>(results); | 
 |  |  |                         if (!content.Data.IsNullOrEmpty()) | 
 |  |  |                         { | 
 |  |  |                             if (content.Data.ToString() == "NG") | 
 |  |  |                             { | 
 |  |  |                                 task.Remark = "NG"; | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                         task.ExceptionMessage += "   " + content.Message; | 
 |  |  |  | 
 |  |  |                         #endregion WMS同步任务完成 | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                         if (!task.Roadway.Contains("GW")) | 
 |  |  |                         { | 
 |  |  |                             var routers = _routerService.QueryNextRoutes(task.NextAddress, task.TargetAddress); | 
 |  |  |                             if (!routers.Any()) return WebResponseContent.Instance.Error($"未找到设备路由信息"); | 
 |  |  |                             task.CurrentAddress = task.NextAddress; | 
 |  |  |                             task.NextAddress = routers.FirstOrDefault().ChildPosi; | 
 |  |  |                             task.ModifyDate = DateTime.Now; | 
 |  |  |                             task.Modifier = "System"; | 
 |  |  |                             BaseDal.UpdateData(task); | 
 |  |  |                             _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"堆垛机出库完成"); | 
 |  |  |                         } | 
 |  |  |                         else | 
 |  |  |                         { | 
 |  |  |                             task.TaskState = (int)TaskOutStatusEnum.OutFinish; | 
 |  |  |                             //如高温出库后 NG 则将任务标记为NG 并在出库申请后将任务出至NG口 | 
 |  |  |                             if (task.Remark == "NG") | 
 |  |  |                             { | 
 |  |  |                                 BaseDal.UpdateData(task); | 
 |  |  |                             } | 
 |  |  |                             else | 
 |  |  |                             { | 
 |  |  |                                 BaseDal.DeleteData(task); | 
 |  |  |                                 ConsoleHelper.WriteWarningLine($"高温出库目的地址{task.TargetAddress}"); | 
 |  |  |                             } | 
 |  |  |  | 
 |  |  |                             var taskHty = task.Adapt<Dt_Task_Hty>(); | 
 |  |  |                             _taskHtyRepository.AddData(taskHty); | 
 |  |  |                             _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"堆垛机出库完成"); | 
 |  |  |                         } | 
 |  |  |  | 
 |  |  |                         //暂不考虑多个出库口 | 
 |  |  |  | 
 |  |  |                         if (task.Roadway.Contains("CH") && task.TaskType == (int)TaskOutboundTypeEnum.Outbound) | 
 |  |  |                         { | 
 |  |  |                             Dt_Task? newTask = UpdatePosition(task.TaskNum, task.CurrentAddress); | 
 |  |  |                             if (newTask != null) | 
 |  |  |                             { | 
 |  |  |                                 task = newTask; | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                         content = StackCraneTaskCompleted(task.TaskNum); | 
 |  |  |                         return content; | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } | 
 |  |  |                 else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup) | 
 |  |  |                 { | 
 |  |  | 
 |  |  |                     } | 
 |  |  |                     else if (task.TaskState == (int)TaskInStatusEnum.SC_InFinish) | 
 |  |  |                     { | 
 |  |  |                         if (App.User.UserId > 0) | 
 |  |  |                         { | 
 |  |  |                             content = StackCraneTaskCompleted(task.TaskNum); | 
 |  |  |                             return content; | 
 |  |  |                         } | 
 |  |  |  | 
 |  |  |                         task.ModifyDate = DateTime.Now; | 
 |  |  |                         task.Modifier = "System"; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |                 task.ModifyDate = DateTime.Now; | 
 |  |  |                 task.Modifier = "System"; | 
 |  |  |                 BaseDal.UpdateData(task); | 
 |  |  |                  | 
 |  |  |  | 
 |  |  |                 _taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"系统自动流程,更新当前位置【{oldCurrentPos} ----> {task.CurrentAddress}】和下一位置【{oldNextPos} ----> {task.NextAddress}】"); | 
 |  |  |                 return task; | 
 |  |  | 
 |  |  |                 var wmsIpAddress = wmsBase + completeTask; | 
 |  |  |  | 
 |  |  |                 var result = HttpHelper.GetAsync(wmsIpAddress, keys).Result; | 
 |  |  |                 content = JsonConvert.DeserializeObject<WebResponseContent>(result); | 
 |  |  |                 if (!content.Data.IsNullOrEmpty()) | 
 |  |  |                 { | 
 |  |  |                     if (content.Data.ToString() == "NG") | 
 |  |  |                     { | 
 |  |  |                         task.Remark = "NG"; | 
 |  |  |                         //BaseDal.UpdateData(task); | 
 |  |  |                         //_taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"堆垛机出库完成,MOM返回NG"); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 task.ExceptionMessage += "   " + content.Message; | 
 |  |  |  | 
 |  |  |                 QuartzLogger.WriteLogToFile($"Info_任务完成", $"任务完成,任务号:【{taskNum}】返回参数【{JsonConvert.SerializeObject(result)}】{Environment.NewLine}{Environment.NewLine}"); | 
 |  |  |                 if(result != null) | 
 |  |  |                 { | 
 |  |  |  | 
 |  |  |                     content = JsonConvert.DeserializeObject<WebResponseContent>(result); | 
 |  |  |                     if (!content.Data.IsNullOrEmpty()) | 
 |  |  |                     { | 
 |  |  |                         if (content.Data.ToString() == "NG") | 
 |  |  |                         { | 
 |  |  |                             task.Remark = "NG"; | 
 |  |  |                             //BaseDal.UpdateData(task); | 
 |  |  |                             //_taskExecuteDetailService.AddTaskExecuteDetail(task.TaskId, $"堆垛机出库完成,MOM返回NG"); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     task.ExceptionMessage += "   " + content.Message; | 
 |  |  |  | 
 |  |  |                 } | 
 |  |  |                 #endregion WMS同步任务完成 | 
 |  |  |  | 
 |  |  |                 if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup && task.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting) | 
 |  |  | 
 |  |  |             catch (Exception ex) | 
 |  |  |             { | 
 |  |  |                 content = WebResponseContent.Instance.Error($"任务完成异常,任务号:【{taskNum}】"); | 
 |  |  |                 QuartzLogger.WriteLogToFile($"Info_任务完成", $"任务完成异常,任务号:【{taskNum}】异常信息【{ex.StackTrace}】{Environment.NewLine}{Environment.NewLine}"); | 
 |  |  |             } | 
 |  |  |             QuartzLogger.WriteLogToFile($"Info_任务完成", $"任务完成,任务号:【{taskNum}】返回参数【{JsonConvert.SerializeObject(content)}】{Environment.NewLine}{Environment.NewLine}"); | 
 |  |  |             return content; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | 
 |  |  |         { | 
 |  |  |             return BaseDal.QueryFirst(x => x.Roadway == deviceNo && x.TaskType == (int)TaskRelocationTypeEnum.Relocation && x.TaskState == (int)TaskStatus.Created); | 
 |  |  |         } | 
 |  |  |         public Dt_Task QueryOutFireAlarmTask(string deviceNo) | 
 |  |  |         { | 
 |  |  |             return BaseDal.QueryFirst(x => x.Roadway == deviceNo && x.TaskType == (int)TaskOutboundTypeEnum.OutFireAlarm && x.TaskState == (int)TaskOutStatusEnum.OutNew); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public Dt_Task QueryExecutingTaskByBarcode(string barcode, string nextAddress) | 
 |  |  |         { | 
 |  |  | 
 |  |  |             } | 
 |  |  |             return wcsBasez + address; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         #region 重写方法 | 
 |  |  |  | 
 |  |  |         public override WebResponseContent DeleteData(object[] key) | 
 |  |  |         { | 
 |  |  |             foreach (var item in key) | 
 |  |  |             { | 
 |  |  |                 var task = BaseDal.QueryFirst(x => x.TaskId == Convert.ToInt32(item)); | 
 |  |  |                 var taskHty = task.Adapt<Dt_Task_Hty>(); | 
 |  |  |                 taskHty.Creater = App.User.UserName != null ? App.User.UserName : "System"; | 
 |  |  |                 _taskHtyRepository.AddData(taskHty); | 
 |  |  |             } | 
 |  |  |             return base.DeleteData(key); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         #endregion 重写方法 | 
 |  |  |     } | 
 |  |  | } |