using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Win_in.Sfs.Basedata.Application.Contracts; using Win_in.Sfs.Shared.Domain.Shared; using Win_in.Sfs.Wms.DataExchange.Domain; using Win_in.Sfs.Wms.DataExchange.Domain.Fawtyg.MesOut; using Win_in.Sfs.Wms.DataExchange.Domain.Fawtyg.Qtyrfe; using Win_in.Sfs.Wms.DataExchange.Domain.Shared; using Win_in.Sfs.Wms.DataExchange.WMS.ProductReceiptNote; using Win_in.Sfs.Wms.DataExchange.WMS.TransferNote; namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.MesAgent; public class MesOutReader : IReader { private readonly IMesOutManager _mesOutManager; private readonly IIncomingFromExternalManager _incomingFromExternalManager; private readonly ILogger _logger; private readonly ILocationAppService _locationAppService; private readonly ICustomerItemAppService _customerItemAppService; public MesOutReader( IMesOutManager mesOutManager , IIncomingFromExternalManager incomingFromExternalManager , ILogger logger , ILocationAppService locationAppService , ICustomerItemAppService customerItemAppService ) { _mesOutManager = mesOutManager; _incomingFromExternalManager = incomingFromExternalManager; _logger = logger; _locationAppService = locationAppService; _customerItemAppService = customerItemAppService; } public virtual async Task> ReadAsync() { //从MES读取待处理MesOut var toBeProcessedMwsOuts = await _mesOutManager.GetToBeProcessedListAsync().ConfigureAwait(false); toBeProcessedMwsOuts = toBeProcessedMwsOuts.OrderBy(r => r.Mesout_dt).Take(3000).ToList(); if (!toBeProcessedMwsOuts.Any()) { _logger.LogInformation("no productreceipts"); return new List(); } var mesoutnbr = await GetListByTypesAndErpCodeAsync(toBeProcessedMwsOuts).ConfigureAwait(false); if (mesoutnbr.Count > 0) { toBeProcessedMwsOuts = toBeProcessedMwsOuts.Where(r => !mesoutnbr.Contains(r.Mesout_ref_nbr)).ToList(); var toBeProcessedEroMwsOuts = toBeProcessedMwsOuts.Where(r => mesoutnbr.Contains(r.Mesout_ref_nbr)).ToList(); await _mesOutManager.UpdateProcesseErrordListAsync(toBeProcessedEroMwsOuts).ConfigureAwait(false); } //MesOut逐一转换为ProductReceiptNote var incomingDataList =await BuildIncomingFromExternalFromShipAsync(toBeProcessedMwsOuts).ConfigureAwait(false); await _incomingFromExternalManager.CreateManyAsync(incomingDataList).ConfigureAwait(false); //更新MES数据状态 await _mesOutManager.UpdateProcessedListAsync(toBeProcessedMwsOuts).ConfigureAwait(false); return incomingDataList; } private async Task> BuildIncomingFromExternalFromShipAsync(List toBeProcessedMesOuts) { var incomingDataList = new List(); foreach (var mesOut in toBeProcessedMesOuts) { var incomingData = BuildIncomingFromExternal(mesOut); incomingData.SetEffectiveDate(DateTime.ParseExact(mesOut.Mesout_date, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture)); incomingData.SetSuccess(); try { var productReceiptNote = BuildProductReceiptNoteCreateInput(mesOut); incomingData.DestinationDataContent = JsonSerializer.Serialize(productReceiptNote); } catch (Exception ex) { incomingData.SetError(EnumExchangeDataErrorCode.Exception, ex.Message, ex.ToString()); } incomingDataList.Add(incomingData); if (mesOut.Mesout_quality == "1") { //查询物品对应的客户库位 string locationCode = await _customerItemAppService.GetFirstLocationCodeByItemCode(mesOut.Mesout_part).ConfigureAwait(false); if (!string.IsNullOrEmpty(locationCode)) { var incomingData_TransferNote = BuildIncomingFromExternal_TransferNote(mesOut); incomingData_TransferNote.SetEffectiveDate(DateTime.ParseExact(mesOut.Mesout_date, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture)); incomingData_TransferNote.SetSuccess(); try { var transferNote = BuildTransferNoteCreateInput(mesOut, locationCode); incomingData_TransferNote.DestinationDataContent = JsonSerializer.Serialize(transferNote); } catch (Exception ex) { incomingData.SetError(EnumExchangeDataErrorCode.Exception, ex.Message, ex.ToString()); } incomingDataList.Add(incomingData_TransferNote); } } } return incomingDataList; } private async Task> GetListByTypesAndErpCodeAsync(List toBeProcessedShips) { List mesoutnbr = new List(); foreach (var item in toBeProcessedShips) { if (mesoutnbr.Contains(item.Mesout_ref_nbr)) { continue; } List types = new List { EnumLocationType.FG, EnumLocationType.WIP }; var loc = await _locationAppService.GetListByTypesAndErpCodeAsync(types, item.Mesout_loc, item.Mesout_loc).ConfigureAwait(false); if (loc.Count == 0) { mesoutnbr.Add(item.Mesout_ref_nbr); } } return mesoutnbr; } private static IncomingFromExternal BuildIncomingFromExternal(MesOut mesOut) { var incomingData = new IncomingFromExternal() { DataType = EnumIncomingDataType.ProductReceipt.ToString(), DataAction = EnumExchangeDataAction.Add, SourceSystem = EnumSystemType.MES.ToString(), SourceDataId = mesOut.Mesout_ref_nbr.ToString(), SourceDataGroupCode = mesOut.Mesout_loc.ToString(), SourceDataDetailCode = mesOut.Mesout_part, SourceDataContent = JsonSerializer.Serialize(mesOut), WriteTime = DateTime.Now, Writer = nameof(MesIncomingBackgroundWorker), DestinationSystem = EnumSystemType.WMS.ToString(), }; //返喷数不为零用tabletype标志出来,用于后续汇总 if (mesOut.Mesout_bad != 0) { incomingData.TableType = EnumExchangeTableType.MainTable; } return incomingData; } private static ProductReceiptNoteExchangeDto BuildProductReceiptNoteCreateInput(MesOut mesOut) { var productReceiptNote = new ProductReceiptNoteExchangeDto() { CompleteTime = DateTime.ParseExact(mesOut.Mesout_dt, "yyyyMMdd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture), ActiveDate = DateTime.ParseExact(mesOut.Mesout_date, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture) }; var productReceiptNoteDetail = new ProductReceiptNoteDetailExchangeDto() { RawLocationCode = mesOut.Mesout_id, ItemCode = mesOut.Mesout_part, Qty = mesOut.Mesout_move, LocationErpCode = mesOut.Mesout_loc, Remark = mesOut.memo, ReturnQty = mesOut.Mesout_bad, MesBarCode = mesOut.Mesout_barcode, MesQuality = mesOut.Mesout_quality }; productReceiptNote.Detail = productReceiptNoteDetail; return productReceiptNote; } private static IncomingFromExternal BuildIncomingFromExternal_TransferNote(MesOut mesOut) { var incomingData = new IncomingFromExternal() { DataType = EnumIncomingDataType.TransferNote.ToString(), DataAction = EnumExchangeDataAction.Add, SourceSystem = EnumSystemType.MES.ToString(), SourceDataId = mesOut.Mesout_ref_nbr.ToString(), SourceDataGroupCode = mesOut.Mesout_ref_nbr, SourceDataDetailCode = mesOut.Mesout_part, SourceDataContent = JsonSerializer.Serialize(mesOut), WriteTime = DateTime.Now, Writer = nameof(MesIncomingBackgroundWorker), DestinationSystem = EnumSystemType.WMS.ToString(), }; return incomingData; } private static TransferNoteExchangeDto BuildTransferNoteCreateInput(MesOut mesOut,string toLocationCode) { var transferNote = new TransferNoteExchangeDto() { Worker = "MesJK", Remark="质量补移库" }; var transferNoteDetail = new TransferNoteDetailExchangeDto() { ItemCode = mesOut.Mesout_part, Qty = mesOut.Mesout_move, ToLocationErpCode = toLocationCode, FromLocationErpCode = mesOut.Mesout_loc, PackingCode="RFE", Lot = "RFE", }; transferNote.Detail = transferNoteDetail; return transferNote; } }