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 shipmentInfoList = shipmentRequest.GetShipmentInfo(); // build list of shipments returned from mws var dicShipExistsInDb = new Dictionary(); 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 newShipmentInfoList = null; if (dicShipExistsInDb.Any()) { var newShipmentId = new List(); // 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; } } }