You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
6.1 KiB
150 lines
6.1 KiB
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.Shared;
|
|
using Win_in.Sfs.Wms.DataExchange.WMS.ProductReceiptNote;
|
|
|
|
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.MesAgent;
|
|
|
|
public class MesOutReader : IReader
|
|
{
|
|
private readonly IMesOutManager _mesOutManager;
|
|
private readonly IIncomingFromExternalManager _incomingFromExternalManager;
|
|
private readonly ILogger<MesOutReader> _logger;
|
|
private readonly ILocationAppService _locationAppService;
|
|
public MesOutReader(
|
|
IMesOutManager mesOutManager
|
|
, IIncomingFromExternalManager incomingFromExternalManager
|
|
, ILogger<MesOutReader> logger
|
|
, ILocationAppService locationAppService
|
|
)
|
|
{
|
|
_mesOutManager = mesOutManager;
|
|
_incomingFromExternalManager = incomingFromExternalManager;
|
|
_logger = logger;
|
|
_locationAppService = locationAppService;
|
|
}
|
|
|
|
public virtual async Task<List<IncomingFromExternal>> 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<IncomingFromExternal>();
|
|
}
|
|
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 = BuildIncomingFromExternalFromShipAsync(toBeProcessedMwsOuts);
|
|
await _incomingFromExternalManager.CreateManyAsync(incomingDataList).ConfigureAwait(false);
|
|
//更新MES数据状态
|
|
await _mesOutManager.UpdateProcessedListAsync(toBeProcessedMwsOuts).ConfigureAwait(false);
|
|
|
|
return incomingDataList;
|
|
}
|
|
|
|
private static List<IncomingFromExternal> BuildIncomingFromExternalFromShipAsync(List<MesOut> toBeProcessedMesOuts)
|
|
{
|
|
var incomingDataList = new List<IncomingFromExternal>();
|
|
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);
|
|
|
|
}
|
|
|
|
return incomingDataList;
|
|
}
|
|
private async Task<List<string>> GetListByTypesAndErpCodeAsync(List<MesOut> toBeProcessedShips)
|
|
{
|
|
List<string> mesoutnbr = new List<string>();
|
|
|
|
foreach (var item in toBeProcessedShips)
|
|
{
|
|
if (mesoutnbr.Contains(item.Mesout_ref_nbr))
|
|
{
|
|
continue;
|
|
}
|
|
List<EnumLocationType> types = new List<EnumLocationType> { 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
|
|
};
|
|
productReceiptNote.Detail = productReceiptNoteDetail;
|
|
return productReceiptNote;
|
|
}
|
|
|
|
}
|
|
|