using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_ITaskInfoService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Service; using WIDESEAWCS_Tasks.StackerCraneJob; namespace WIDESEAWCS_Tasks { /// /// 分容堆垛机命令构建器:负责任务到命令对象的转换。 /// public class FormationStackerCraneCommandBuilder { private readonly ITaskService _taskService; private readonly IRouterService _routerService; public FormationStackerCraneCommandBuilder(ITaskService taskService, IRouterService routerService) { _taskService = taskService; _routerService = routerService; } public FormationStackerCraneTaskCommand? ConvertToStackerCraneTaskCommand(Dt_Task task) { FormationStackerCraneTaskCommand command = new() { Barcode = task.PalletCode, TaskNum = task.TaskNum, WorkType = 4, WorkAction = 1, FireAlarm = 0, FieldName = string.Empty }; TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup(); return taskTypeGroup switch { TaskTypeGroup.InboundGroup => BuildInboundCommand(task, command), TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, command), TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, command), _ => command }; } private FormationStackerCraneTaskCommand? BuildInboundCommand(Dt_Task task, FormationStackerCraneTaskCommand command) { Dt_Router? router = _routerService.QueryNextRoute(task.CurrentAddress, task.Roadway, task.TaskType); if (router == null) { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.CurrentAddress}】信息,无法获取对应的堆垛机取货站台信息"); return null; } command.StartRow = Convert.ToInt16(router.SrmRow); command.StartColumn = Convert.ToInt16(router.SrmColumn); command.StartLayer = Convert.ToInt16(router.SrmLayer); if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer)) { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,终点:【{task.NextAddress}】"); return null; } command.EndRow = endRow; command.EndColumn = endColumn; command.EndLayer = endLayer; return command; } private FormationStackerCraneTaskCommand? BuildOutboundCommand(Dt_Task task, FormationStackerCraneTaskCommand command) { Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.TargetAddress, task.TaskType); if (router == null) { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"未找到站台【{task.TargetAddress}】信息,无法获取对应的堆垛机放货站台信息"); return null; } command.EndRow = Convert.ToInt16(router.SrmRow); command.EndColumn = Convert.ToInt16(router.SrmColumn); command.EndLayer = Convert.ToInt16(router.SrmLayer); if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer)) { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】"); return null; } command.StartRow = startRow; command.StartColumn = startColumn; command.StartLayer = startLayer; return command; } private FormationStackerCraneTaskCommand? BuildRelocationCommand(Dt_Task task, FormationStackerCraneTaskCommand command) { if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer)) { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务终点错误,终点:【{task.NextAddress}】"); return null; } command.EndRow = endRow; command.EndColumn = endColumn; command.EndLayer = endLayer; if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer)) { _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】"); return null; } command.StartRow = startRow; command.StartColumn = startColumn; command.StartLayer = startLayer; return command; } private static bool TryParseAddress(string address, out short row, out short column, out short layer) { row = column = layer = 0; string[] parts = address.Split("-"); if (parts.Length != 3) { return false; } return short.TryParse(parts[0], out row) && short.TryParse(parts[1], out column) && short.TryParse(parts[2], out layer); } } }