You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.3 KiB
62 lines
2.3 KiB
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
|
|
{
|
|
/// <summary>
|
|
/// 修改安全库存命令处理程序
|
|
/// </summary>
|
|
public sealed class ModifySafetyCommandHandler : CommandHandler<ModifySafetyCommand>
|
|
{
|
|
private readonly ICommandBus _commandBus; // 命令总线
|
|
private readonly IMapper _mapper; // 模型映射
|
|
private readonly IMaterialRepository _materialRepository; // 角色仓库
|
|
|
|
/// <summary>
|
|
/// 依赖注入
|
|
/// </summary>
|
|
public ModifySafetyCommandHandler(
|
|
ICommandBus commandBus,
|
|
IMapper mapper,
|
|
IMaterialRepository materialRepository)
|
|
{
|
|
_commandBus = commandBus;
|
|
_mapper = mapper;
|
|
_materialRepository = materialRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理程序
|
|
/// </summary>
|
|
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<Safety>(command));
|
|
await _commandBus.Trigger(_mapper.Map<ModifySafetySuccessEvent>(command), cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
await _commandBus.Notify(command.ValidationResult,
|
|
cancellationToken: cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
}
|