using System; using System.Collections.Generic; using System.Linq; 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.DeliverNote; using Win_in.Sfs.Wms.Store.Application.Contracts; namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent.Outgoing; public class DeliverNoteConverter : IOutgoingConverter { private readonly string billtype = "1003"; private readonly IOutgoingFromWmsManager _outgoingFromWmsManager; private readonly IOutgoingToExternalManager _outgoingToExternalManager; // private readonly IUserDepartmentAppService _departmentAppService; private readonly IObjectMapper _objectMapper; public DeliverNoteConverter( 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.Deliver, EnumSystemType.ERP).ConfigureAwait(false); foreach (var outgoingFromWms in outgoingFromWmsList) { #region 主表 var wmsReceipt = JsonSerializer.Deserialize(outgoingFromWms.DataContent); var exchangeReceipt = _objectMapper.Map(wmsReceipt); var purchaseOrder = BuildDataInterface(exchangeReceipt); var outgoingToExternal = new OutgoingToExternal() { DataType = EnumOutgoingDataType.Deliver.ToString(), TableType = EnumExchangeTableType.MainTable, DataAction = outgoingFromWms.DataAction, SourceSystem = EnumSystemType.WMS.ToString(), SourceDataId = wmsReceipt.Id.ToString(), SourceDataGroupCode = wmsReceipt.Number, SourceDataDetailCode = wmsReceipt.Number, Writer = nameof(TyrpOutgoingBackgroundWorker), DestinationSystem = EnumSystemType.ERP.ToString(), DestinationDataId = "", }; outgoingToExternal.SetEffectiveDate(outgoingFromWms.EffectiveDate); outgoingToExternal.SourceDataContent = JsonSerializer.Serialize(exchangeReceipt); outgoingToExternal.DestinationDataContent = JsonSerializer.Serialize(purchaseOrder); outgoingToExternalList.Add(outgoingToExternal); #endregion #region 明细 var WipDetails = wmsReceipt.Details.GroupBy(r=>new { r.Number,r.ItemCode,r.FromLocationErpCode}).Select(p => new DeliverNoteDetailExchangeDto { Qty = p.Sum(itm => itm.Qty), Number = p.Key.Number, ItemCode = p.Key.ItemCode, FromLocationErpCode = p.Key.FromLocationErpCode }); foreach (var detail in WipDetails) { var outgoingDetailToExternal = new OutgoingToExternal() { DataType = EnumOutgoingDataType.Deliver.ToString(), TableType = EnumExchangeTableType.DetailTable, DataAction = outgoingFromWms.DataAction, SourceSystem = EnumSystemType.WMS.ToString(), SourceDataId = detail.Number.ToString(), SourceDataGroupCode = wmsReceipt.Number, SourceDataDetailCode = detail.ItemCode, Writer = nameof(TyrpOutgoingBackgroundWorker), DestinationSystem = EnumSystemType.ERP.ToString(), DestinationDataId = "", }; outgoingDetailToExternal.SetEffectiveDate(outgoingFromWms.EffectiveDate); outgoingDetailToExternal.SourceDataContent = JsonSerializer.Serialize(detail); var purchaseOrderDetail = BuildDataInterfaceDetail(exchangeReceipt, detail); outgoingDetailToExternal.DestinationDataContent = JsonSerializer.Serialize(purchaseOrderDetail); outgoingToExternalList.Add(outgoingDetailToExternal); } #endregion } //插入到中间表OutgoingToExternal await _outgoingToExternalManager.CreateManyAsync(outgoingToExternalList).ConfigureAwait(false); //将outgoingFromWms数据归档 await _outgoingFromWmsManager.ArchiveManyAsync(outgoingFromWmsList).ConfigureAwait(false); return outgoingToExternalList; } /// /// 构建主表 /// /// /// private Wmsoutm BuildDataInterface(DeliverNoteExchangeDto exchangeOrder) { var ret = new Wmsoutm() { wmsoutm_nbr = exchangeOrder.Number, wmsoutm_type = billtype, wmsoutm_dt_w = DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), wmsoutm_stat = "Y", wmsoutm_tyrp_dt = "", wmsoutm_user = "WMS", wmsoutm_dept = "", wmsoutm_date = exchangeOrder.ActiveDate.ToString("yyyyMMdd"), wmsoutm_cust = exchangeOrder.CustomerCode ?? "", wmsoutm_shm_nbr = "", wmsoutm_cust_loc = "", wmsoutm_stock_stat = "", wmsoutm_open_part = "", wmsoutm_open_loc = "", wmsoutm_open_hours = 0, wmsoutm_tyrp_k = "", wmsoutm_id = 0,//明细中最大scmsend_id wmsoutm_open_qty = 0, }; return ret; } /// /// 构建明细 /// /// /// /// private static Wmsoutd BuildDataInterfaceDetail(DeliverNoteExchangeDto exchangeOrder, DeliverNoteDetailExchangeDto exchangeDetailOrder) { var ret = new Wmsoutd() { wmsoutd_nbr = exchangeOrder.Number, wmsoutd_part = exchangeDetailOrder.ItemCode, wmsoutd_loc = exchangeDetailOrder.FromLocationErpCode ?? "", wmsoutd_qty = exchangeDetailOrder.Qty, wmsoutd_bcm_code = "", wmsoutd_projt_id = "", }; return ret; } }