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

137 lines
5.7 KiB

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 Newtonsoft.Json;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Stock.CommandHandlers
{
/// <summary>
/// 入库命令处理程序
/// </summary>
public sealed class PalletInCommandHandler : CommandHandler<PalletInCommand>
{
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; // 任务域
private readonly MaterialProxy _materialProxy; // 物料服务
/// <summary>
/// 依赖注入
/// </summary>
public PalletInCommandHandler(
ICommandBus commandBus,
INotification notifications,
IMapper mapper,
IStockRepository stockRepository,
IWarehouseRepository warehouseRepository,
BarcodeValidator barcodeValidator,
MaterialProxy materialProxy,
TaskProxy taskProxy)
{
_commandBus = commandBus;
_notifications = notifications;
_mapper = mapper;
_materialProxy = materialProxy;
_stockRepository = stockRepository;
_warehouseRepository = warehouseRepository;
_barcodeValidator = barcodeValidator;
_taskProxy = taskProxy;
}
/// <summary>
/// 处理程序
/// </summary>
public override async Task Handle(PalletInCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
// 条码格式校验
if (!await _barcodeValidator.Validate(command.Barcode))
{
await _commandBus.Notify(nameof(command.Barcode), "条码格式不正确", command.Barcode,
cancellationToken: cancellationToken);
return;
}
//校验托盘号与物料类型是否匹配
var sGoods = await _materialProxy.GetMaterial(command.Lines.First().Material, null, false);
Models.Material goods = JsonConvert.DeserializeObject<Models.Material>(sGoods);
if (!goods.Model.Contains(command.Barcode.Substring(0,3)))
{
await _commandBus.Notify(nameof(command.Barcode), "托盘类型与物料类型不匹配", command.Barcode,
cancellationToken: cancellationToken);
return;
}
// 托盘有任务
if (await _taskProxy.HasTask(command.Barcode))
{
await _commandBus.Notify(nameof(command.Barcode), "托盘有任务", command.Barcode,
cancellationToken: cancellationToken);
return;
}
var original = (command.Barcode.Substring(2,1) == "G"? 12001 : 12007);
// 时间戳
command.Timestamp ??= DateTime.Now;
// 切点事件
var event0 = _mapper.Map<PalletInExecutingEvent>(command);
event0.Cell = command.Destination ?? original;
await _commandBus.Trigger(event0, cancellationToken);
if (!_notifications.Any())
{
// 允许创建库存
var stock = _mapper.Map<Models.Stock>(command);
stock.Cell = command.Destination ?? original;
stock.Timestamp = command.Timestamp.Value;
if (await _stockRepository.CreateStock(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<PalletInSuccessEvent>(command);
event1.Cell = stock.Cell;
event1.Timestamp = command.Timestamp.Value;
await _commandBus.Trigger(event1, cancellationToken);
}
// //获取最佳入库口
// string startStation = await this._warehouseRepository.GetInStartDevice((int)command.Destination, null, command.Barcode.Substring(0, 5), command.Lines.First().Material, command.Barcode);
// if (string.IsNullOrEmpty(startStation))
// {
// await _commandBus.Notify(nameof(command.Tag), $"设备故障,请先排除故障", startStation,
//cancellationToken: cancellationToken);
// return;
// }
Output(nameof(command.Tag), "");
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}