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 DisableCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IWarehouseRepository _warehouseRepository; // 库房仓库
///
/// 依赖注入
///
public DisableCommandHandler(
ICommandBus commandBus,
IWarehouseRepository warehouseRepository)
{
_commandBus = commandBus;
_warehouseRepository = warehouseRepository;
}
///
/// 处理程序
///
public override async System.Threading.Tasks.Task Handle(DisableCommand 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:
await _warehouseRepository.DisableCell(command.CellTag, cell.Id);
states.Enqueue(false);
break;
case CellState.Selected:
states.Enqueue(true);
break;
case CellState.Disabled:
states.Enqueue(false);
break;
default:
states.Enqueue(null);
break;
}
}
Output(nameof(command.States), states);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}