using AutoMapper; using Kean.Domain.Material.Commands; using Kean.Domain.Material.Events; using Kean.Domain.Material.Repositories; using System.Threading; using System.Threading.Tasks; namespace Kean.Domain.Material.CommandHandlers { /// /// 创建物料命令处理程序 /// public sealed class CreateMaterialCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly IMaterialRepository _materialRepository; // 物料仓库 /// /// 依赖注入 /// public CreateMaterialCommandHandler( ICommandBus commandBus, IMapper mapper, IMaterialRepository materialRepository) { _commandBus = commandBus; _mapper = mapper; _materialRepository = materialRepository; } /// /// 处理程序 /// public override async Task Handle(CreateMaterialCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { if (command.Category.HasValue && !await _materialRepository.IsCategoryExist(command.Category.Value)) { await _commandBus.Notify(nameof(command.Category), "品类不存在", command.Category, nameof(ErrorCode.Gone), cancellationToken: cancellationToken); return; } if (await _materialRepository.IsMaterialCodeExist(command.Code, null)) { await _commandBus.Notify(nameof(command.Code), "料号重复", command.Code, nameof(ErrorCode.Conflict), cancellationToken: cancellationToken); return; } //if (await _materialRepository.IsMaterialNameExist(command.Name, null)) //{ // await _commandBus.Notify(nameof(command.Name), "名称重复", command.Name, nameof(ErrorCode.Conflict), // cancellationToken: cancellationToken); // return; //} Output(nameof(command.Id), await _materialRepository.CreateMaterial(_mapper.Map(command))); await _commandBus.Trigger(_mapper.Map(command), cancellationToken); } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }