using AutoMapper;
using Kean.Domain.Material.Commands;
using Kean.Domain.Material.Events;
using Kean.Domain.Material.Models;
using Kean.Domain.Material.Repositories;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Material.CommandHandlers
{
///
/// 修改安全库存命令处理程序
///
public sealed class ModifySafetyCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IMaterialRepository _materialRepository; // 角色仓库
///
/// 依赖注入
///
public ModifySafetyCommandHandler(
ICommandBus commandBus,
IMapper mapper,
IMaterialRepository materialRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_materialRepository = materialRepository;
}
///
/// 处理程序
///
public override async Task Handle(ModifySafetyCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
if (!await _materialRepository.IsSafetyExist(command.Id))
{
await _commandBus.Notify(nameof(command.Id), "安全库存不存在", command.Id, nameof(ErrorCode.Gone),
cancellationToken: cancellationToken);
return;
}
if (await _materialRepository.IsSafetyExist(command.Material, command.Warehouse, command.Id))
{
await _commandBus.Notify(nameof(command.Material), "物料仓库信息重复", command.Material, nameof(ErrorCode.Conflict),
cancellationToken: cancellationToken);
return;
}
await _materialRepository.ModifySafety(_mapper.Map(command));
await _commandBus.Trigger(_mapper.Map(command), cancellationToken);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}