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

107 lines
4.0 KiB

using AutoMapper;
using Kean.Domain.Stock.Commands;
using Kean.Domain.Stock.Enums;
using Kean.Domain.Stock.Events;
using Kean.Domain.Stock.Repositories;
using Kean.Domain.Stock.Strategies;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Stock.CommandHandlers
{
/// <summary>
/// 重定位命令处理程序
/// </summary>
public sealed class RelocateCommandHandler : CommandHandler<RelocateCommand>
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly IStockRepository _stockRepository; // 存储仓库
private readonly IWarehouseRepository _warehouseRepository; // 库房仓库
private readonly StateContext<StateStrategy_Default> _stateContext; // 库存状态判定策略
/// <summary>
/// 依赖注入
/// </summary>
public RelocateCommandHandler(
IServiceProvider serviceProvider,
IWarehouseRepository warehouseRepository,
ICommandBus commandBus,
IMapper mapper,
IStockRepository stockRepository)
{
_commandBus = commandBus;
_mapper = mapper;
_stockRepository = stockRepository;
_warehouseRepository = warehouseRepository;
_stateContext = new(serviceProvider);
}
/// <summary>
/// 处理程序
/// </summary>
public override async Task Handle(RelocateCommand command, CancellationToken cancellationToken)
{
var transfer = true;
// 当前位置
switch (await _warehouseRepository.IsCell(command.Original))
{
// 货位
case true:
// 置空
await _warehouseRepository.UpdateCell(command.Original, CellState.Empty);
break;
}
var state = await _stateContext.GetState(command.Barcode);
// 目标位置
switch (await _warehouseRepository.IsCell(command.Destination))
{
// 货位
case true:
// 空,认为是空托盘,创建库存
if (state == CellState.Empty)
{
transfer = false;
await _stockRepository.CreateStock(new()
{
Barcode = command.Barcode,
Cell = command.Destination,
Spec = command.Spec
});
await _warehouseRepository.UpdateCell(command.Destination, CellState.Pallet);
}
// 非空托盘,更新目标状态
else
{
await _stockRepository.CleanTag(command.Barcode);
await _warehouseRepository.UpdateCell(command.Destination, state);
}
break;
// 站台
case false:
// 空托盘,删除库存
if (state == CellState.Pallet)
{
//transfer = false;
//await _stockRepository.DeleteStock(new()
//{
// Barcode = command.Barcode
//});
}
// 空,发生在站台到站台的空托盘移动,不做操作
else if (state == CellState.Empty)
{
transfer = false;
}
break;
}
if (transfer)
{
await _stockRepository.TransferStock(command.Barcode, command.Spec, command.Destination);
}
await _commandBus.Trigger(_mapper.Map<RelocateSuccessEvent>(command), cancellationToken);
}
}
}