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.
184 lines
6.4 KiB
184 lines
6.4 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.Wms.DataExchange.Domain;
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Fawtyg.EOS;
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Shared;
|
|
using Win_in.Sfs.Wms.DataExchange.WMS.SuppplierAsn;
|
|
|
|
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.EosAgent;
|
|
|
|
public class ShipReader : IReader
|
|
{
|
|
|
|
private readonly IShipManager _shipManager;
|
|
private readonly IIncomingFromExternalManager _incomingFromExternalManager;
|
|
private readonly ILogger<ShipReader> _logger;
|
|
private readonly ISupplierItemAppService _supplierItemAppService;
|
|
|
|
public ShipReader(
|
|
IShipManager shipManager
|
|
, IIncomingFromExternalManager incomingFromExternalManager
|
|
, ILogger<ShipReader> logger,
|
|
ISupplierItemAppService supplierItemAppService
|
|
)
|
|
{
|
|
_shipManager = shipManager;
|
|
_incomingFromExternalManager = incomingFromExternalManager;
|
|
_logger = logger;
|
|
_supplierItemAppService = supplierItemAppService;
|
|
}
|
|
|
|
public virtual async Task<List<IncomingFromExternal>> ReadAsync()
|
|
{
|
|
//从EOS读取待处理Ship
|
|
var toBeProcessedShips = await _shipManager.GetToBeProcessedListAsync().ConfigureAwait(false);
|
|
if (!toBeProcessedShips.Any())
|
|
{
|
|
_logger.LogInformation("no ships");
|
|
return new List<IncomingFromExternal>();
|
|
}
|
|
|
|
var shipbillno = await GetNoSupplierItemNum(toBeProcessedShips).ConfigureAwait(false);
|
|
if (shipbillno.Count > 0)
|
|
{
|
|
toBeProcessedShips = toBeProcessedShips.Where(r => !shipbillno.Contains(r.ShipBillNo)).ToList();
|
|
}
|
|
//Ship逐一转换为SupplierAsn
|
|
var incomingDataList = await BuildIncomingFromExternalFromShipAsync(toBeProcessedShips).ConfigureAwait(false);
|
|
await _incomingFromExternalManager.CreateManyAsync(incomingDataList).ConfigureAwait(false);
|
|
//更新EOS数据状态
|
|
await _shipManager.UpdateProcessedListAsync(toBeProcessedShips).ConfigureAwait(false);
|
|
|
|
return incomingDataList;
|
|
}
|
|
|
|
private async Task<List<IncomingFromExternal>> BuildIncomingFromExternalFromShipAsync(List<Ship> toBeProcessedShips)
|
|
{
|
|
var incomingDataList = new List<IncomingFromExternal>();
|
|
foreach (var ship in toBeProcessedShips)
|
|
{
|
|
var isHistory = await _shipManager.CheckIsHistoryShip(ship.ShipBillNo).ConfigureAwait(false);
|
|
|
|
if (isHistory)
|
|
{
|
|
ship.WmsState = 2;
|
|
ship.WmsDate = DateTime.Now;
|
|
continue;
|
|
}
|
|
|
|
var incomingData = BuildIncomingFromExternal(ship);
|
|
|
|
incomingData.SetEffectiveDate(ship.EosDate);
|
|
incomingData.SetSuccess();
|
|
try
|
|
{
|
|
var supplierAsn = BuildSupplierAsnCreateInput(ship);
|
|
incomingData.DestinationDataContent = JsonSerializer.Serialize(supplierAsn);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
incomingData.SetError(EnumExchangeDataErrorCode.Exception, ex.Message, ex.ToString());
|
|
}
|
|
|
|
incomingDataList.Add(incomingData);
|
|
|
|
}
|
|
|
|
return incomingDataList;
|
|
}
|
|
|
|
private async Task<List<string>> GetNoSupplierItemNum(List<Ship> toBeProcessedShips)
|
|
{
|
|
List<string> shipbillno = new List<string>();
|
|
|
|
foreach (var item in toBeProcessedShips)
|
|
{
|
|
if (shipbillno.Contains(item.ShipBillNo))
|
|
{
|
|
continue;
|
|
}
|
|
var supplierItemDTO = await _supplierItemAppService.GetBySupplierCodeAndItemCodeAsync(item.SupplierCode, item.ERP).ConfigureAwait(false);
|
|
if (supplierItemDTO == null)
|
|
{
|
|
shipbillno.Add(item.ShipBillNo);
|
|
}
|
|
}
|
|
return shipbillno;
|
|
}
|
|
|
|
|
|
private static IncomingFromExternal BuildIncomingFromExternal(Ship ship)
|
|
{
|
|
var incomingData = new IncomingFromExternal()
|
|
{
|
|
DataType = EnumIncomingDataType.SupplierAsn.ToString(),
|
|
DataAction = ship.ShipBillState switch
|
|
{
|
|
0 => EnumExchangeDataAction.Add,
|
|
1 => EnumExchangeDataAction.Delete,
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
},
|
|
SourceSystem = EnumSystemType.SCP.ToString(),
|
|
SourceDataId = ship.Id.ToString(),
|
|
SourceDataGroupCode = ship.ShipBillNo,
|
|
SourceDataDetailCode = ship.DetailMatNo,
|
|
SourceDataContent = JsonSerializer.Serialize(ship),
|
|
WriteTime = DateTime.Now,
|
|
Writer = nameof(EosIncomingBackgroundWorker),
|
|
|
|
DestinationSystem = EnumSystemType.WMS.ToString(),
|
|
};
|
|
return incomingData;
|
|
}
|
|
|
|
private static SupplierAsnExchangeDto BuildSupplierAsnCreateInput(Ship ship)
|
|
{
|
|
var supplierAsn = new SupplierAsnExchangeDto()
|
|
{
|
|
Number = ship.ShipBillNo,
|
|
PoNumber = ship.ShipBillNo,
|
|
SupplierCode = ship.SupplierCode,
|
|
ContactName = ship.PlanUserCode,
|
|
ShipDate = ship.ShipDate,
|
|
DueDate = ship.PlanArriveDate,
|
|
PlanArriveDate = ship.PlanArriveDate,
|
|
ActiveDate = ship.EosDate,
|
|
Worker = ship.PlanUserCode,
|
|
// RpNumber = "",
|
|
// DockCode = ship.Loc,
|
|
// WarehouseCode = "",
|
|
// Remark = ship.ShipMemo,
|
|
};
|
|
var supplierAsnDetail = new SupplierAsnDetailExchangeDto()
|
|
{
|
|
Number = ship.ShipBillNo,
|
|
PoNumber = ship.ShipBillNo,
|
|
LocationErpCode = ship.Loc,
|
|
PackingCode = ship.DetailMatNo,
|
|
Uom = ship.Unit ?? "",
|
|
Qty = ship.Qty,
|
|
Lot = ship.PrtductBatch,
|
|
ProduceDate = ship.ProductDate,
|
|
ItemCode = ship.ERP,
|
|
ProjectCode = ship.XmCode,
|
|
Remark = ship.ShipMemo,
|
|
// PoLine = "",
|
|
// SupplierPackUom = ship.Unit,
|
|
// SupplierPackQty = ship.Qty,
|
|
// ConvertRate = 1,
|
|
// StdPackUom = ship.Unit,
|
|
// StdPackQty = ship.Qty,
|
|
SupplierBatch = ship.PrtductBatch,
|
|
ArriveDate = ship.PlanArriveDate,
|
|
// ExpireDate = DateTime.MaxValue,
|
|
};
|
|
supplierAsn.Detail = supplierAsnDetail;
|
|
return supplierAsn;
|
|
}
|
|
|
|
}
|
|
|