using AutoMapper; using Masuit.Tools; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.Const; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_DTO.WMS; using WIDESEA_IServices; using WIDESEA_IStorageTaskRepository; using WIDESEA_IStorageTaskServices; using WIDESEA_Model.Models; using WIDESEA_StorageTaskRepository; using WIDESEAWCS_BasicInfoRepository; using WIDESEAWCS_Model.Models; namespace WIDESEA_Tasks { public class OutNGTask : IJob { private readonly IDt_TaskRepository _taskRepository; private readonly IDt_StationManagerRepository _stationManagerRepository; private readonly ISys_ConfigService _configService; private readonly IDt_TaskService _taskService; private readonly IDt_Task_HtyRepository _task_HtyRepository; private readonly IMapper _mapper; public OutNGTask(IDt_TaskRepository taskRepository, IDt_StationManagerRepository stationManagerRepository, ISys_ConfigService configService, IDt_TaskService taskService, IDt_Task_HtyRepository task_HtyRepository, IMapper mapper) { _taskRepository = taskRepository; _stationManagerRepository = stationManagerRepository; _configService = configService; _taskService = taskService; _task_HtyRepository = task_HtyRepository; _mapper = mapper; } public Task Execute(IJobExecutionContext context) { try { var task = _taskRepository.QueryFirst(x => x.TaskType == (int)TaskInboundTypeEnum.Inbound && x.TaskState == (int)TaskInStatusEnum.SC_InExecuting && x.ErrorMessage != null && (x.Roadway.Contains("GW") || x.Roadway.Contains("CW"))); if (task != null) { Dt_StationManager stationManager = null; string position = null; if (task.Roadway.Contains("CW")) { stationManager = _stationManagerRepository.QueryFirst(x => (x.stationPLC == "1017" || x.stationPLC == "1024") && x.stationType == 10 && x.Roadway == task.Roadway); position = task.Roadway switch { var s when s.StartsWith("CWSC") => int.TryParse(s.Substring(4), out var num) ? num switch { >= 1 and <= 9 => "001-035-001", >= 11 and <= 17 => "001-036-001" } : throw new Exception("未找到该巷道信息"), _ => throw new Exception("未找到该巷道信息") }; } else { stationManager = _stationManagerRepository.QueryFirst(x => x.stationType == 1 && (x.stationPLC == "1015" || x.stationPLC == "2025") && x.remark == task.Roadway); position = "002-000-002"; } var taskNew = CreateTask(task, stationManager, position); WMSTaskDTO taskDTO = CreateTaskDTO(taskNew); var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress); var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue; var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveByWMSNGTask)?.ConfigValue; if (wmsBase == null || ipAddress == null) { throw new InvalidOperationException("WMS IP 未配置"); } var wmsIpAddress = wmsBase + ipAddress; var result = HttpHelper.PostAsync(wmsIpAddress, taskDTO.ToJsonString()).Result; var content = JsonConvert.DeserializeObject(result); if (content.Status) { var taskHtyNG = CreateHistoricalTask(task); _task_HtyRepository.AddData(taskHtyNG); _taskRepository.DeleteData(task); _taskRepository.AddData(taskNew); } return Task.CompletedTask; } } catch (Exception ex) { ConsoleHelper.WriteWarningLine($"执行 NG 任务出库时发生异常: {ex.Message}{ex.StackTrace}"); } return Task.CompletedTask; } private Dt_Task CreateTask(Dt_Task task,Dt_StationManager stationManager,string position) { return new Dt_Task { Grade = 1, Roadway = task.Roadway, TargetAddress = stationManager.stationChildCode, Dispatchertime = DateTime.Now, MaterialNo = "", NextAddress = position, OrderNo = null, PalletCode = task.PalletCode, SourceAddress = task.TargetAddress, CurrentAddress = task.TargetAddress, TaskState = (int)TaskOutStatusEnum.OutNew, TaskType = (int)TaskOutboundTypeEnum.OutNG, TaskNum = _taskRepository.GetTaskNo().Result, Creater = "System", CreateDate = DateTime.Now, TaskId = 0, ProductionLine = task.ProductionLine, ProcessCode = task.ProcessCode, ErrorMessage = task.ErrorMessage, }; } private WMSTaskDTO CreateTaskDTO(Dt_Task task) { return new WMSTaskDTO { TaskNum = task.TaskNum.Value, Grade = 1, PalletCode = task.PalletCode, RoadWay = task.Roadway, SourceAddress = task.SourceAddress, TargetAddress = task.TargetAddress, NextAddress = task.NextAddress, TaskState = task.TaskState.Value, Id = 0, TaskType = task.TaskType, ProductionLine = task.ProductionLine, }; } private Dt_Task_Hty CreateHistoricalTask(Dt_Task task, bool isHand = false) { // 更新任务状态 task.TaskState = task.TaskType > 199 ? (int)TaskInStatusEnum.InFinish : (int)TaskOutStatusEnum.OutFinish; task.CurrentAddress = task.NextAddress; // 创建历史任务 var taskHty = _mapper.Map(task); taskHty.FinishTime = DateTime.Now; taskHty.TaskId = 0; taskHty.OperateType = (int)OperateTypeEnum.NG任务删除; taskHty.SourceId = task.TaskId; if (isHand) { taskHty.Creater = App.User.UserName != null ? App.User.UserName : "System"; } return taskHty; } } }