using Kean.Domain.Task.Commands; using Kean.Domain.Task.Events; using Kean.Domain.Task.Models; using Kean.Domain.Task.Repositories; using Kean.Domain.Task.SharedServices.Proxies; using Kean.Infrastructure.Soap.Hithium.Models; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Threading; namespace Kean.Domain.Task.EventHandlers { /// /// 上架后上传信息 /// [EventHandlerIndex(8)] public sealed class CompleteSuccessEventHandler_WMS : EventHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IStockRepository _stockRepository; // 存储仓库 private readonly MaterialProxy _materialProxy; // 物料服务 private readonly IWarehouseRepository _warehouseRepository; // 仓库 private readonly Kean.Infrastructure.Soap.Hithium.WmsAPIService _wmsService; // MES private readonly ILogger _logger; // 日志 /// /// 依赖注入 /// public CompleteSuccessEventHandler_WMS( IStockRepository stockRepository, MaterialProxy materialProxy, IWarehouseRepository warehouseRepository, Kean.Infrastructure.Soap.Hithium.WmsAPIService wmsService, ILogger logger, ICommandBus commandBus) { _stockRepository = stockRepository; _materialProxy = materialProxy; _wmsService = wmsService; _warehouseRepository = warehouseRepository; _logger = logger; _commandBus = commandBus; } /// /// 处理程序 /// public override async System.Threading.Tasks.Task Handle(CompleteSuccessEvent @event, CancellationToken cancellationToken) { //获取目标位置 Warehouse warehouse; if (@event.Tag.StartsWith("Warehouse:")) { warehouse = await _warehouseRepository.GetWarehouse(Convert.ToInt32(@event.Tag[10..])); } else { await _commandBus.Notify(nameof(@event.Tag), $"无法解析库房信息", @event.Tag, cancellationToken: cancellationToken); return; } //获取stock var stockLines = await _stockRepository.GetLines(@event.Barcode); foreach (var r in stockLines) { //校验本地库存 1-本地 2-联机 if (r.IS_LOCAL.Equals("1")) { _logger.LogInformation($"上架后上传信息:托盘条码[{@event.Barcode}],膜卷号[{r.Bill}],本地库存[{r.IS_LOCAL}]不上传信息"); return; } //校验是否入海辰WMS 1-是;0-否 if (r.IS_WMS.Equals("1")) { _logger.LogInformation($"上架后上传信息:托盘条码[{@event.Barcode}],膜卷号[{r.Bill}],已经入库海辰WMS[{r.IS_WMS}]不上传信息"); return; } //@=@ 上传次数 //if (Convert.ToInt32(r.UPLOAD_NUM) < 2) //{ //} var sGoods = await _materialProxy.GetMaterial(r.Id, string.Empty, false); Models.Material goods = JsonConvert.DeserializeObject(sGoods); WMSParamIn wmsParamIn = JsonConvert.DeserializeObject(warehouse.Remark); wmsParamIn.batchNo = r.Bill; wmsParamIn.materialCode = goods.Code; wmsParamIn.materialName = goods.Name; wmsParamIn.unit = r.MES_UNIT; wmsParamIn.quantity = Convert.ToDecimal(r.QUALIFIED_NUM); wmsParamIn.workOrderNo = r.WORKORDER_NO; //wmsParamIn.whlCode //wmsParamIn.factory wmsParamIn.lineCode = r.LINE_CODE; wmsParamIn.lineName = r.LINE_NAME; wmsParamIn.productDate = Convert.ToDateTime(r.ManufacturingDate).ToString("yyyy-MM-dd HH:mm:ss"); wmsParamIn.postUserName = r.POSTUSER; var sResult = await _wmsService.Invoke("CreateSemiProductStock", JsonConvert.SerializeObject(wmsParamIn), @event.Barcode, string.Empty); Kean.Infrastructure.Soap.Hithium.Models.Result result = JsonConvert.DeserializeObject(sResult); if (!result.Success) { } } ////@==@ 调用MES 库存调拨 //InventoryTransfer inventoryTransfer = JsonConvert.DeserializeObject(@event.Tag); ////所在的站点 site ////目标库位 //if (@event.Cell == 190000) //{ // inventoryTransfer.slTarget = "C3VF(阳极)"; //} //if (@event.Cell == 290000) //{ // inventoryTransfer.slTarget = "C3VG(阴极)"; //} ////过账人 ////inventoryTransfer.postUser = @event.Tag; //foreach (var m in @event.Lines) //{ // ////源库位 // //inventoryTransfer.slSource = m.Supplier; // //库存列表 // inventoryTransfer.inventoryList.Add(m.Bill); //} //var startTimestamp = DateTime.Now; //string paramIn = JsonConvert.SerializeObject(inventoryTransfer); //var sOut = await _mesService.Invoke("WarehouseInventoryTransfer", paramIn, @event.Barcode, @event.RequestNo); //var endTimestamp = DateTime.Now; //Result result = JsonConvert.DeserializeObject(sOut); //if (!result.code.Equals("0")) //{ // await _commandBus.Notify("InterfaceRecord", $"{result.message}", JsonConvert.SerializeObject(new // { // DIRECTION = "WMS->MES", // METHOD = "WarehouseInventoryTransfer", // BARCODE = @event.Barcode, // REQUEST_NO = @event.RequestNo, // START_TIME = startTimestamp, // END_TIME = endTimestamp, // CREATE_TIME = endTimestamp, // PARAM_IN = paramIn, // PARAM_OUT = sOut, // RESULT = result.code // }), cancellationToken: cancellationToken); // return; //} } } }