using System.Collections.Generic; using System.Text.Json; using System.Threading.Tasks; using Volo.Abp.ObjectMapping; using Win_in.Sfs.Wms.DataExchange.Domain; using Win_in.Sfs.Wms.DataExchange.Domain.Fawtyg.Tyrp; using Win_in.Sfs.Wms.DataExchange.Domain.Shared; using Win_in.Sfs.Wms.DataExchange.WMS.ProductReceiptNote; using Win_in.Sfs.Wms.Store.Application.Contracts; namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent.Outgoing; public class ProductReceiptNoteConverter : IOutgoingConverter { private readonly IOutgoingFromWmsManager _outgoingFromWmsManager; private readonly IOutgoingToExternalManager _outgoingToExternalManager; private readonly IObjectMapper _objectMapper; public ProductReceiptNoteConverter( IOutgoingFromWmsManager outgoingFromWmsManager , IOutgoingToExternalManager outgoingToExternalManager , IObjectMapper objectMapper ) { _outgoingFromWmsManager = outgoingFromWmsManager; _outgoingToExternalManager = outgoingToExternalManager; _objectMapper = objectMapper; } public virtual async Task> ConvertAsync() { var outgoingToExternalList = new List(); var outgoingFromWmsList = await _outgoingFromWmsManager.GetToBeProcessedListAsync(EnumOutgoingDataType.ProductReceipt, EnumSystemType.ERP).ConfigureAwait(false); foreach (var outgoingFromWms in outgoingFromWmsList) { var wmsReceipt = JsonSerializer.Deserialize(outgoingFromWms.DataContent); foreach (var detail in wmsReceipt.Details) { var outgoingToExternal = new OutgoingToExternal() { DataType = outgoingFromWms.DataType, DataAction = outgoingFromWms.DataAction, SourceSystem = EnumSystemType.WMS.ToString(), SourceDataId = detail.Id.ToString(), SourceDataGroupCode = wmsReceipt.Number, SourceDataDetailCode = detail.PackingCode, Writer = nameof(TyrpOutgoingBackgroundWorker), DestinationSystem = EnumSystemType.ERP.ToString(), DestinationDataId = "", }; outgoingToExternal.SetEffectiveDate(outgoingFromWms.EffectiveDate); var exchangeReceipt = await BuildProductReceiptNoteExchangeDtoAsync(wmsReceipt, detail).ConfigureAwait(false); outgoingToExternal.SourceDataContent = JsonSerializer.Serialize(exchangeReceipt); var ret = BuildProductReceiptNote(exchangeReceipt); outgoingToExternal.DestinationDataContent = JsonSerializer.Serialize(ret); outgoingToExternalList.Add(outgoingToExternal); } } //插入到中间表OutgoingToExternal await _outgoingToExternalManager.CreateManyAsync(outgoingToExternalList).ConfigureAwait(false); //将outgoingFromWms数据归档 await _outgoingFromWmsManager.ArchiveManyAsync(outgoingFromWmsList).ConfigureAwait(false); return outgoingToExternalList; } private ProductReceiptNote BuildProductReceiptNote(ProductReceiptNoteExchangeDto exchangeProductReceiptNote) { var detail = exchangeProductReceiptNote.Detail; var ret = new ProductReceiptNote() { mesout_ref_nbr = exchangeProductReceiptNote.Number, mesout_id = string.IsNullOrEmpty(detail.RawLocationCode)?"": detail.RawLocationCode, mesout_patr = detail.ItemCode, mesout_date = exchangeProductReceiptNote.ActiveDate.ToString("yyyyMMdd"), mesout_bad = detail.ReturnQty, mesout_move = detail.ReturnQty != 0 ? 0 : detail.Qty, mesout_unable = 0, mesout_loc = detail.LocationErpCode, mesout_dt = exchangeProductReceiptNote.ActiveDate.ToString("yyyyMMdd"), memo = string.IsNullOrEmpty(detail.Remark)?"": detail.Remark, refno = "", }; return ret; } private async Task BuildProductReceiptNoteExchangeDtoAsync(ProductReceiptNoteDTO wmsReceipt, ProductReceiptNoteDetailDTO wmsReceiptDetail) { var exchangeReceipt = _objectMapper.Map(wmsReceipt); var exchangeReceiptDetail = _objectMapper.Map(wmsReceiptDetail); exchangeReceipt.Detail = exchangeReceiptDetail; return exchangeReceipt; } }