using Kean.Domain.Task.Commands; using Kean.Domain.Task.Enums; using Kean.Domain.Task.Repositories; using System.Collections.Generic; using System.Threading; namespace Kean.Domain.Task.CommandHandlers { /// /// 创建启用命令处理程序 /// public sealed class EnableCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IWarehouseRepository _warehouseRepository; // 库房仓库 /// /// 依赖注入 /// public EnableCommandHandler( ICommandBus commandBus, IWarehouseRepository warehouseRepository) { _commandBus = commandBus; _warehouseRepository = warehouseRepository; } /// /// 处理程序 /// public override async System.Threading.Tasks.Task Handle(EnableCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { var states = new Queue(); foreach (var item in command.Cells) { var cell = await _warehouseRepository.GetCellByName(item, command.Warehouse); switch (cell?.State) { case CellState.Enabled: states.Enqueue(true); break; case CellState.Selected: states.Enqueue(true); break; case CellState.Disabled: await _warehouseRepository.ReleaseCell(cell.Id); states.Enqueue(true); break; default: states.Enqueue(null); break; } } Output(nameof(command.States), states); } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }