using AutoMapper; using Kean.Domain.Stock.Commands; using Kean.Domain.Stock.Enums; using Kean.Domain.Stock.Models; using Kean.Domain.Stock.Repositories; using Kean.Domain.Stock.SharedServices.Proxies; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Kean.Domain.Stock.CommandHandlers { /// /// 命令处理程序 /// public sealed class SetRollResultCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly INotification _notifications; // 总线通知 private readonly IMapper _mapper; // 模型映射 private readonly IStockRepository _stockRepository; // 存储仓库 private readonly TaskProxy _taskProxy; // 任务服务 /// /// 依赖注入 /// public SetRollResultCommandHandler( ICommandBus commandBus, INotification notifications, IMapper mapper, TaskProxy taskProxy, IStockRepository stockRepository) { _commandBus = commandBus; _notifications = notifications; _mapper = mapper; _taskProxy = taskProxy; _stockRepository = stockRepository; } /// /// 处理程序 /// public override async Task Handle(SetRollResultCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { Models.Stock stock = null; if (!string.IsNullOrEmpty(command.Barcode)) { stock = await this._stockRepository.GetStock(command.Barcode); } if (!string.IsNullOrEmpty(command.BatchNo)) { stock = await this._stockRepository.GetStorageBill(command.BatchNo); } if (stock == null) { await _commandBus.Notify(nameof(command.BatchNo), $"膜卷码({command.BatchNo})未找到库存", command.BatchNo, cancellationToken: cancellationToken); return; } List updateLines = new List(); foreach (var r in stock.Lines) { //if (r.Supplier == "ok") //{ //} //else //{ // if (command.Code=="0") // { // //r.QualityState = "ok"; // r.Supplier = "ok"; // JObject jo = new JObject(); // jo.Add("property", "Supplier"); // jo.Add("message", command.Message); // r.Remark = jo; // updateLines.Add(r); // } //} } if (command.Code=="0") { if (updateLines.Count > 0) { var updateCommand = _mapper.Map(stock); updateCommand.Transaction = nameof(StockType.Update); updateCommand.Lines = updateLines; updateCommand.Operator = command.Operator; updateCommand.Tag = JsonConvert.SerializeObject(new { Transaction = "SetRollResult" }); await _commandBus.Execute(updateCommand, cancellationToken); } } else { await _taskProxy.SetTaskState(0, stock.Barcode, "WmsResultFail", $"{command.Message}", "Task"); } //判断是否托盘上所有膜卷号已经过账 var stockUnload = await this._stockRepository.GetStorageUnupload(stock.Barcode); if (stockUnload.Lines.Count() > 0) { return; } else { //全部上传完,删除任务 await _taskProxy.DeleteTask(stock.Barcode); } } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }