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.

112 lines
4.5 KiB

2 years ago
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.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.InterfaceCalendar;
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent.Incoming;
public class InterfaceCalendarReader : IReader
{
private readonly IInterfaceCalendarManager _interfaceCalendarManager;
private readonly IIncomingFromExternalManager _incomingFromExternalManager;
private readonly ILogger<InterfaceCalendarReader> _logger;
public InterfaceCalendarReader(
IInterfaceCalendarManager interfaceCalendarManager
, IIncomingFromExternalManager incomingFromExternalManager
, ILogger<InterfaceCalendarReader> logger
)
{
_interfaceCalendarManager = interfaceCalendarManager;
_incomingFromExternalManager = incomingFromExternalManager;
_logger = logger;
}
public virtual async Task<List<IncomingFromExternal>> ReadAsync()
{
//从MES读取待处理mes_period
var toBeProcessedDatas = await _interfaceCalendarManager.GetToBeProcessedListAsync().ConfigureAwait(false);
if (!toBeProcessedDatas.Any())
{
_logger.LogInformation("no interfaceCalendars");
return new List<IncomingFromExternal>();
}
//mes_period逐一转换为InterfaceCalendar
var incomingDataList = BuildIncomingFromExternalFromShipAsync(toBeProcessedDatas);
await _incomingFromExternalManager.CreateManyAsync(incomingDataList).ConfigureAwait(false);
return incomingDataList;
}
private static List<IncomingFromExternal> BuildIncomingFromExternalFromShipAsync(List<mes_period> toBeProcessedDatas)
{
var incomingDataList = new List<IncomingFromExternal>();
foreach (var item in toBeProcessedDatas)
{
var incomingData = BuildIncomingFromExternal(item);
incomingData.SetEffectiveDate(DateTime.ParseExact(item.mes_period_dt, "yyyyMMdd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture));
incomingData.SetSuccess();
try
{
var productReceiptNote = BuildInterfaceCalendarCreateInput(item);
incomingData.DestinationDataContent = JsonSerializer.Serialize(productReceiptNote);
}
catch (Exception ex)
{
incomingData.SetError(EnumExchangeDataErrorCode.Exception, ex.Message, ex.ToString());
}
incomingDataList.Add(incomingData);
}
return incomingDataList;
}
private static IncomingFromExternal BuildIncomingFromExternal(mes_period mes_period)
{
var incomingData = new IncomingFromExternal()
{
DataType = EnumIncomingDataType.InterfaceCalendar.ToString(),
DataAction = EnumExchangeDataAction.Add,
SourceSystem = EnumSystemType.ERP.ToString(),
SourceDataId = mes_period.mes_period_ym,
SourceDataGroupCode = mes_period.mes_period_ym,
SourceDataDetailCode = mes_period.mes_period_ym,
SourceDataContent = JsonSerializer.Serialize(mes_period),
WriteTime = DateTime.Now,
Writer = nameof(TyrpIncomingBackgroundWorker),
DestinationSystem = EnumSystemType.WMS.ToString(),
};
return incomingData;
}
private static InterfaceCalendarExchangeDto BuildInterfaceCalendarCreateInput(mes_period mes_period)
{
var period = DateTime.ParseExact(mes_period.mes_period_ym, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
var interfaceCalendar = new InterfaceCalendarExchangeDto()
{
Code = mes_period.mes_period_ym,
Name = mes_period.mes_period_ym,
Year = period.Year.ToString(),
Month = period.Month.ToString(),
BeginTime = DateTime.ParseExact(mes_period.mes_period_start, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture),
EndTime = DateTime.ParseExact(mes_period.mes_period_end, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture),
LastModificationTime = mes_period.mes_period_dt == null ? null : DateTime.ParseExact(mes_period.mes_period_dt, "yyyyMMdd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture),
ConvertToTime = DateTime.Now,
Enabled = true,
};
return interfaceCalendar;
}
}