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