山东雷驰
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.

133 lines
5.4 KiB

3 months ago
using AutoMapper;
using Kean.Domain.Stock.Commands;
using Kean.Domain.Stock.Events;
using Kean.Domain.Stock.Repositories;
using Kean.Domain.Stock.SharedServices.Proxies;
using Kean.Infrastructure.Configuration;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Stock.CommandHandlers
{
/// <summary>
/// 出库命令处理程序
/// </summary>
public sealed class OutboundCommandHandler : CommandHandler<OutboundCommand>
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly INotification _notifications; // 总线通知
private readonly IMapper _mapper; // 模型映射
private readonly IStockRepository _stockRepository; // 存储仓库
private readonly IWarehouseRepository _warehouseRepository; // 库房仓库
private readonly BarcodeValidator _barcodeValidator; // 条码验证器
private readonly TaskProxy _taskProxy; // 任务域
/// <summary>
/// 依赖注入
/// </summary>
public OutboundCommandHandler(
ICommandBus commandBus,
INotification notifications,
IMapper mapper,
IStockRepository stockRepository,
IWarehouseRepository warehouseRepository,
BarcodeValidator barcodeValidator,
TaskProxy taskProxy)
{
_commandBus = commandBus;
_notifications = notifications;
_mapper = mapper;
_stockRepository = stockRepository;
_warehouseRepository = warehouseRepository;
_barcodeValidator = barcodeValidator;
_taskProxy = taskProxy;
}
/// <summary>
/// 处理程序
/// </summary>
public override async Task Handle(OutboundCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid || !string.IsNullOrEmpty(command.Tag))
{
// 条码格式校验
if (!await _barcodeValidator.Validate(command.Barcode))
{
await _commandBus.Notify(nameof(command.Barcode), "条码格式不正确", command.Barcode,
cancellationToken: cancellationToken);
return;
}
var original = await _stockRepository.GetCell(command.Barcode);
// 托盘不存在
if (!original.HasValue)
{
await _commandBus.Notify(nameof(command.Barcode), $"托盘({command.Barcode})不存在", command.Barcode,
cancellationToken: cancellationToken);
return;
}
// 托盘有任务
if (await _taskProxy.HasTask(command.Barcode))
{
await _commandBus.Notify(nameof(command.Barcode), "托盘有任务", command.Barcode,
cancellationToken: cancellationToken);
return;
}
// 不能在立库货位中操作
if (await _warehouseRepository.IsCell(original.Value) != false)
{
await _commandBus.Notify(nameof(command.Barcode), "在立库货位中不能取消,请联系立库管理员出库取消", command.Barcode,
cancellationToken: cancellationToken);
return;
}
// 时间戳
command.Timestamp ??= DateTime.Now;
command.Lines = await _stockRepository.GetLines(command.Barcode);
foreach (var r in command.Lines)
{
r.Quantity = -r.Quantity;
}
// 切点事件
var event0 = _mapper.Map<OutboundExecutingEvent>(command);
event0.Cell = original.Value;
await _commandBus.Trigger(event0, cancellationToken);
if (!_notifications.Any())
{
// 允许删除库存
var stock = _mapper.Map<Models.Stock>(command);
stock.Cell = original.Value;
stock.Timestamp = command.Timestamp.Value;
if (await _stockRepository.DeleteStock(stock).ContinueWith(task =>
{
if (task.Exception?.InnerException is RepositoryException ex)
{
task.Exception.Handle(_ => true);
_commandBus.Notify(ex.Member.Key, ex.Message, ex.Member.Value,
cancellationToken: cancellationToken).Wait();
return true;
}
return false;
}))
{
return;
}
// 成功事件
var event1 = _mapper.Map<OutboundSuccessEvent>(command);
event1.Cell = stock.Cell;
event1.Timestamp = command.Timestamp.Value;
await _commandBus.Trigger(event1, cancellationToken);
}
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}