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 { /// /// 入库命令处理程序 /// public sealed class PalletInCommandHandler : CommandHandler { 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; // 物料服务 /// /// 依赖注入 /// 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; } /// /// 处理程序 /// 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(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(command); event0.Cell = command.Destination ?? original; await _commandBus.Trigger(event0, cancellationToken); if (!_notifications.Any()) { // 允许创建库存 var stock = _mapper.Map(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(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); } } } }