using AutoMapper; using Kean.Domain.Basic.Commands; using Kean.Domain.Basic.Events; using Kean.Domain.Basic.Models; using Kean.Domain.Basic.Repositories; using System.Threading; using System.Threading.Tasks; namespace Kean.Domain.Basic.CommandHandlers { /// /// 创建物料命令处理程序 /// public sealed class CreateItemCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly IInterfaceRecordRepository _interfaceRecordRepository; /// /// 依赖注入 /// public CreateItemCommandHandler( ICommandBus commandBus, IInterfaceRecordRepository interfaceRecordRepository, IMapper mapper) { _commandBus = commandBus; _mapper = mapper; _interfaceRecordRepository = interfaceRecordRepository; } /// /// 处理程序 /// public override async Task Handle(CreateItemCommand command, CancellationToken cancellationToken) { if (await _interfaceRecordRepository.IsItemListCodeExist(command.Code, string.Empty)) { await _commandBus.Notify(nameof(command.Code), "物料类型重复", command.Code, nameof(ErrorCode.Conflict), cancellationToken: cancellationToken); return; } if (await _interfaceRecordRepository.IsItemListNameExist(command.Name, string.Empty)) { await _commandBus.Notify(nameof(command.Name), "物料类型名称重复", command.Name, nameof(ErrorCode.Conflict), cancellationToken: cancellationToken); return; } Output(nameof(command.ItemListId), await _interfaceRecordRepository.CreateItem(_mapper.Map(command))); } } }