using AutoMapper;
using Kean.Domain.Task.Commands;
using Kean.Domain.Task.Enums;
using Kean.Domain.Task.Events;
using Kean.Domain.Task.Repositories;
using System;
using System.Threading;
using Kean.Domain.Task.SharedServices.Proxies;
namespace Kean.Domain.Task.CommandHandlers
{
///
/// 任务中转命令处理程序
///
public sealed class TransferRelocateCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly ITaskRepository _taskRepository; // 任务仓库
private readonly IWarehouseRepository _warehouseRepository; // 库房仓库
private readonly StockProxy _stockProxy; // 仓储域
///
/// 依赖注入
///
public TransferRelocateCommandHandler(
ICommandBus commandBus,
IMapper mapper,
ITaskRepository taskRepository,
IWarehouseRepository warehouseRepository,
StockProxy stockProxy)
{
_commandBus = commandBus;
_taskRepository = taskRepository;
_warehouseRepository = warehouseRepository;
_stockProxy = stockProxy;
}
///
/// 处理程序
///
public override async System.Threading.Tasks.Task Handle(TransferRelocateCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
var task = await _taskRepository.GetTask(command.Id);
await _taskRepository.UpdateStatus(command.Id, TaskState.Running);
if (task != null && (task.Type == TaskType.PalletOut || task.Type == TaskType.ApplyOut || task.Type == TaskType.Outfeed))
{
var outStation = await _warehouseRepository.GetStationById(task.Destination);
await _stockProxy.Relocate(
"Transfer",
task.Barcode,
null,
task.Original,
Convert.ToInt32(outStation.Group),
//task.Operator,
-1,
task.Tag,
(task.Timestamp,DateTime.Now));
await _taskRepository.UpdateTransitPlace(command.Id, Convert.ToInt32(outStation.Group));
await _warehouseRepository.ReleaseCell(task.Original, task.Destination);
}
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}