using AutoMapper; using Kean.Domain.Task.Commands; using Kean.Domain.Task.Enums; using Kean.Domain.Task.Events; using Kean.Domain.Task.Repositories; using Kean.Domain.Task.SharedServices.Proxies; using Newtonsoft.Json; using System; using System.Threading; namespace Kean.Domain.Task.CommandHandlers { /// /// 任务取消命令处理程序 /// public sealed class CancelCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly ITaskRepository _taskRepository; // 任务仓库 private readonly IWarehouseRepository _warehouseRepository; // 库房仓库 private readonly StockProxy _stockProxy; // 库房仓库 private readonly WcsProxy _wcsProxy; private readonly Kean.Infrastructure.Soap.LED.LedService _ledService; /// /// 依赖注入 /// public CancelCommandHandler( ICommandBus commandBus, IMapper mapper, ITaskRepository taskRepository, StockProxy stockProxy, WcsProxy wcsProxy, Kean.Infrastructure.Soap.LED.LedService ledService, IWarehouseRepository warehouseRepository) { _commandBus = commandBus; _mapper = mapper; _taskRepository = taskRepository; _stockProxy = stockProxy; _wcsProxy = wcsProxy; _ledService = ledService; _warehouseRepository = warehouseRepository; } /// /// 处理程序 /// public override async System.Threading.Tasks.Task Handle(CancelCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { var task = await _taskRepository.GetTask(command.Id); if (task != null) { await _stockProxy.Reupload(task.Type.ToString(), task.Barcode, task.RequestNo, "3"); int? cellId = await _stockProxy.GetCell(task.Barcode); int originalCellId = cellId.HasValue ? cellId.Value : task.Original; await _warehouseRepository.ReleaseCell(task.TransitPlace== 0? originalCellId : task.TransitPlace, task.Destination); await _taskRepository.DeleteTask(command.Id); var @event = _mapper.Map(task); @event.Id = command.Id; @event.Timestamp = DateTime.Now; await _commandBus.Trigger(@event, cancellationToken); await _stockProxy.Record("Cancel", task.Barcode, task.Original, task.Destination, command.Operator, JsonConvert.SerializeObject(new { ManualFlg = command.ManualFlg, Transaction = task.Type.ToString(), Operator = (command.Operator == 0 ? -1 : command.Operator) }), task.Timestamp, DateTime.Now); // 前端人工操作 if (command.ManualFlg == 1) { await _wcsProxy.DeleteWcs(task.Id, $"人工取消"); } //LED提示 string line1 = $"{task.Barcode}出库取消"; string line2 = $"请重新上架"; await _ledService.Invoke(task.Destination.ToString(), line1, line2); } } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }