Files
bnhtrade/src/bnhtrade.Core/Logic/AmazonFBAInbound/UpdateDatabaseShipmentInfo.cs
Bobbie Hodgetts 43d61c2ef8 Feature repricing min max (#10)
amazon settlement import/export improvements
2020-05-01 09:08:23 +01:00

113 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using bnhtrade.Core.Data;
namespace bnhtrade.Core.Logic.AmazonFBAInbound
{
public class UpdateDatabaseShipmentInfo
{
private string sqlConnectionString;
private readonly string logDateTimeId = "FbaInboundShipmentNewCheck";
public int TotalUpdated { get; private set; } = 0;
public UpdateDatabaseShipmentInfo(string sqlConnectionString)
{
this.sqlConnectionString = sqlConnectionString;
}
public void GetNew()
{
TotalUpdated = 0;
// get time frame to check
DateTime dateTimeBefore = new Data.AmazonMWS.CurrentDateTime().GetUtc();
DateTime dateTimeAfter = new Data.Database.Log.DateTimeLog(sqlConnectionString).GetDateTimeUtc(logDateTimeId);
//var objGetUtc = new Data.AmazonMWS.CurrentDateTime();
//DateTime dateTimeBefore = objGetUtc.GetUtc();
//var objDateTimeCheck = new Data.Database.Log.DateTimeLog(sqlConnectionString);
//DateTime dateTimeAfter = objDateTimeCheck.GetDateTimeUtc(logDateTimeId);
//
var shipmentRequest = new Data.AmazonMWS.FBAInbound.ListInboundShipments();
shipmentRequest.LastUpdatedAfter = dateTimeAfter.AddDays(-14);
shipmentRequest.LastUpdatedBefore = dateTimeBefore;
List<Model.AmazonFba.ShipmentInfo> shipmentInfoList = shipmentRequest.GetShipmentInfo();
// build list of shipments returned from mws
var dicShipExistsInDb = new Dictionary<string, bool>();
foreach (var item in shipmentInfoList)
{
dicShipExistsInDb.Add(item.FbaShipmentId, false);
}
// build list of shipmentId that do not exist in database
int complete = 0;
using (TransactionScope scope = new TransactionScope())
{
List<Model.AmazonFba.ShipmentInfo> newShipmentInfoList = null;
if (dicShipExistsInDb.Any())
{
var newShipmentId = new List<string>();
// query db for shipment header info
var resultHeader = new Data.Database.AmazonShipment.ReadShipmentInfo(sqlConnectionString)
.HeaderByFbaShipmentId(dicShipExistsInDb.Keys.ToList());
// compare db and mws result
foreach (var item in resultHeader)
{
dicShipExistsInDb[item.FbaShipmentId] = true;
}
foreach (var item in dicShipExistsInDb)
{
if (item.Value == false)
{
newShipmentId.Add(item.Key);
}
}
// query mws for new shipment info
if (newShipmentId.Any())
{
shipmentRequest.ShipmentIdList = newShipmentId;
newShipmentInfoList = shipmentRequest.GetShipmentInfo();
foreach (var item in newShipmentInfoList)
{
var shipmentItemInfoRequest = new Data.AmazonMWS.FBAInbound.ListInboundShipmentItems();
item.ShipmentItemInfoList = shipmentItemInfoRequest.GetByAmazonShipmentId(item.FbaShipmentId);
}
}
// write to db
if (newShipmentInfoList != null)
{
foreach (var item in newShipmentInfoList)
{
// add the update date
item.LastUpdated = dateTimeBefore;
// write to db
var dbWrite = new Data.Database.AmazonShipment.SetShipmentInfo(sqlConnectionString);
dbWrite.Excecute(item);
complete = complete + 1;
}
}
}
// update datetime log
new Data.Database.Log.DateTimeLog(sqlConnectionString).SetDateTimeUtc(logDateTimeId, dateTimeBefore);
//objDateTimeCheck.SetDateTimeUtc(logDateTimeId, dateTimeBefore);
scope.Complete();
}
TotalUpdated = complete;
}
}
}