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 ModifyItemCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IInterfaceRecordRepository _interfaceRecordRepository;
///
/// 依赖注入
///
public ModifyItemCommandHandler(
ICommandBus commandBus,
IInterfaceRecordRepository interfaceRecordRepository,
IMapper mapper)
{
_commandBus = commandBus;
_mapper = mapper;
_interfaceRecordRepository = interfaceRecordRepository;
}
///
/// 处理程序
///
public override async Task Handle(ModifyItemCommand command, CancellationToken cancellationToken)
{
if (await _interfaceRecordRepository.IsItemListCodeExist(command.Code, command.ItemName))
{
await _commandBus.Notify(nameof(command.Code), "物料类型重复", command.Code, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
if (await _interfaceRecordRepository.IsItemListNameExist(command.Name, command.Name))
{
await _commandBus.Notify(nameof(command.Name), "物料类型名称重复", command.Name, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
await _interfaceRecordRepository.ModifyItem(_mapper.Map- (command));
}
}
}