山东雷驰
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.8 KiB

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
{
/// <summary>
/// 删除安全库存命令处理程序
/// </summary>
public sealed class DeleteSafetyCommandHandler : CommandHandler<DeleteSafetyCommand>
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IMaterialRepository _materialRepository; // 物料仓库
/// <summary>
/// 依赖注入
/// </summary>
public DeleteSafetyCommandHandler(
ICommandBus commandBus,
IMapper mapper,
IMaterialRepository materialRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_materialRepository = materialRepository;
}
/// <summary>
/// 处理程序
/// </summary>
public override async Task Handle(DeleteSafetyCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
List<int> result = new();
foreach (var item in command.Id)
{
await _materialRepository.DeleteSafety(item);
result.Add(item);
}
command.Id = result;
await _commandBus.Trigger(_mapper.Map<DeleteSafetySuccessEvent>(command), cancellationToken);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}