using AutoMapper;
using Kean.Domain.Material.Commands;
using Kean.Domain.Material.Events;
using Kean.Domain.Material.Repositories;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Material.CommandHandlers
{
///
/// 删除物料命令处理程序
///
public sealed class DeleteMaterialCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IMaterialRepository _materialRepository; // 物料仓库
///
/// 依赖注入
///
public DeleteMaterialCommandHandler(
ICommandBus commandBus,
IMapper mapper,
IMaterialRepository materialRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_materialRepository = materialRepository;
}
///
/// 处理程序
///
public override async Task Handle(DeleteMaterialCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
List result = new();
foreach (var item in command.Id)
{
await _materialRepository.DeleteMaterial(item);
result.Add(item);
}
command.Id = result;
await _commandBus.Trigger(_mapper.Map(command), cancellationToken);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}