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.Auth.Application.Contracts; 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.Tyrp; using Win_in.Sfs.Wms.DataExchange.Domain.Shared; using Win_in.Sfs.Wms.DataExchange.WMS.CountAdjustNote; using Win_in.Sfs.Wms.Store.Application.Contracts; namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent.Outgoing; public class CountAdjustNoteConverter : IOutgoingConverter { private readonly IOutgoingFromWmsManager _outgoingFromWmsManager; private readonly IOutgoingToExternalManager _outgoingToExternalManager; private readonly ISupplierAsnAppService _supplierAsnAppService; private readonly IDepartmentAppService _departmentAppService; private readonly IObjectMapper _objectMapper; private readonly ILocationAppService _locationAppService; public CountAdjustNoteConverter( IOutgoingFromWmsManager outgoingFromWmsManager , IOutgoingToExternalManager outgoingToExternalManager , ISupplierAsnAppService supplierAsnAppService , IDepartmentAppService departmentAppService , IObjectMapper objectMapper , ILocationAppService locationAppService ) { _outgoingFromWmsManager = outgoingFromWmsManager; _outgoingToExternalManager = outgoingToExternalManager; _supplierAsnAppService = supplierAsnAppService; _departmentAppService = departmentAppService; _objectMapper = objectMapper; _locationAppService= locationAppService; } public virtual async Task> ConvertAsync() { var outgoingToExternalList = new List(); var outgoingFromWmsList = await _outgoingFromWmsManager.GetToBeProcessedListAsync(EnumOutgoingDataType.CountAdjust, EnumSystemType.ERP).ConfigureAwait(false); foreach (var outgoingFromWms in outgoingFromWmsList) { var wmsCountAdjust = JsonSerializer.Deserialize(outgoingFromWms.DataContent); var department = await _departmentAppService.GetByUsernameAsync(wmsCountAdjust.Worker).ConfigureAwait(false); var departmentCode = department == null ? "" : department.Code; var locs =await _locationAppService.GetListByTypesAsync(new List { EnumLocationType.WIP }).ConfigureAwait(false); var locationCodes = locs.Select(p => p.Code); var wipItems=wmsCountAdjust.Details.Where(p => locationCodes.Contains(p.LocationCode));//库位类型是线边,传线边仓调整 var Items = wmsCountAdjust.Details.Where(p => !locationCodes.Contains(p.LocationCode));//盘点 var WipDetails = wipItems.GroupBy(r => new { r.ItemCode, r.LocationErpCode }).Select(p => new CountAdjustNoteDetailExchangeDto { CountQty = p.Sum(itm => itm.CountQty), InventoryQty = p.Sum(itm => itm.InventoryQty), ItemCode = p.Key.ItemCode, LocationErpCode = p.Key.LocationErpCode }); foreach (var detail in WipDetails) { //判断盘点数与库存数是否相等 if (detail.CountQty != detail.InventoryQty) { var outgoingToExternal = new OutgoingToExternal() { DataType = outgoingFromWms.DataType, DataAction = outgoingFromWms.DataAction, SourceSystem = EnumSystemType.WMS.ToString(), SourceDataId = wmsCountAdjust.Number, SourceDataGroupCode = wmsCountAdjust.Number, SourceDataDetailCode = WipDetails.FirstOrDefault().ItemCode, Writer = nameof(TyrpOutgoingBackgroundWorker), DestinationSystem = EnumSystemType.ERP.ToString(), DestinationDataId = "", }; outgoingToExternal.SetEffectiveDate(outgoingFromWms.EffectiveDate); var exchangeIssue = await BuildPurchaseReceiptExchangeDtoAsync(wmsCountAdjust, detail).ConfigureAwait(false); outgoingToExternal.SourceDataContent = JsonSerializer.Serialize(exchangeIssue); var arrive = BuildWip(exchangeIssue, departmentCode); outgoingToExternal.DestinationDataContent = JsonSerializer.Serialize(arrive); outgoingToExternalList.Add(outgoingToExternal); } } var details = Items.GroupBy(r => new { r.ItemCode, r.LocationErpCode }).Select(p => new CountAdjustNoteDetailExchangeDto { CountQty = p.Sum(itm => itm.CountQty), InventoryQty = p.Sum(itm => itm.InventoryQty), ItemCode = p.Key.ItemCode, LocationErpCode = p.Key.LocationErpCode }); foreach (var detail in details) { //判断盘点数与库存数是否相等 if (detail.CountQty != detail.InventoryQty) { var outgoingToExternal = new OutgoingToExternal() { DataType = outgoingFromWms.DataType, DataAction = outgoingFromWms.DataAction, SourceSystem = EnumSystemType.WMS.ToString(), SourceDataId = wmsCountAdjust.Number, SourceDataGroupCode = wmsCountAdjust.Number, SourceDataDetailCode = details.FirstOrDefault().ItemCode, Writer = nameof(TyrpOutgoingBackgroundWorker), DestinationSystem = EnumSystemType.ERP.ToString(), DestinationDataId = "", }; outgoingToExternal.SetEffectiveDate(outgoingFromWms.EffectiveDate); var exchangeIssue = await BuildPurchaseReceiptExchangeDtoAsync(wmsCountAdjust, detail).ConfigureAwait(false); outgoingToExternal.SourceDataContent = JsonSerializer.Serialize(exchangeIssue); var arrive = BuildIssue(exchangeIssue, departmentCode); outgoingToExternal.DestinationDataContent = JsonSerializer.Serialize(arrive); outgoingToExternalList.Add(outgoingToExternal); } } } await _outgoingToExternalManager.CreateManyAsync(outgoingToExternalList).ConfigureAwait(false); //将outgoingFromWms数据归档 await _outgoingFromWmsManager.ArchiveManyAsync(outgoingFromWmsList).ConfigureAwait(false); return outgoingToExternalList; //插入到中间表OutgoingToExternal } private CountAdjust BuildIssue(CountAdjustNoteExchangeDto exchangeCountAdjust, string departmentCode) { var detail = exchangeCountAdjust.Detail; var counta = new CountAdjust() { mesout_asd_refc = departmentCode, mesout_asd_dt_w = DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), mesout_asd_type = "4003", mesout_asd_part = detail.ItemCode, mesout_asd_date = exchangeCountAdjust.ActiveDate.ToString("yyyyMMdd"), mesout_asd_loc = string.Empty, mesout_asd_code = detail.ReasonCode, mesout_asd_qty = detail.CountQty + detail.InventoryQty, mesout_asd_user = "WMS", mesout_asd_k = string.Empty, mesout_asd_stat = "Y" }; return counta; } private CountAdjust BuildWip(CountAdjustNoteExchangeDto exchangeCountAdjust, string departmentCode) { var detail = exchangeCountAdjust.Detail; var counta = new CountAdjust() { mesout_asd_refc = departmentCode, mesout_asd_dt_w = DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), mesout_asd_type = "4041", mesout_asd_part = detail.ItemCode, mesout_asd_date = exchangeCountAdjust.ActiveDate.ToString("yyyyMMdd"), mesout_asd_loc = string.Empty, mesout_asd_code = detail.ReasonCode, mesout_asd_qty = detail.CountQty + detail.InventoryQty, mesout_asd_user = "WMS", mesout_asd_k = string.Empty, mesout_asd_stat = "Y" }; return counta; } private async Task BuildPurchaseReceiptExchangeDtoAsync( CountAdjustNoteDTO wmsCountAdjust, CountAdjustNoteDetailExchangeDto wmsCountAdjustDetail) { var exchangeCountAdjust = _objectMapper.Map(wmsCountAdjust); exchangeCountAdjust.Detail = wmsCountAdjustDetail; return exchangeCountAdjust; } }