mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 22:47:15 +00:00
Feature: Sync MWS Shipment with Database
Various restructuring and misc. features added. Removed reliance on ABrain Amazon MWS NuGet package, added Amazon's own C# lib
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
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.AmazonFBAInbound.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.AmazonShipmentId, false);
|
||||
}
|
||||
|
||||
// build list of shipmentId that do not exist in database
|
||||
int complete = 0;
|
||||
using (TransactionScope scope = new TransactionScope())
|
||||
{
|
||||
List<Model.AmazonFBAInbound.ShipmentInfo> newShipmentInfoList = null;
|
||||
if (dicShipExistsInDb.Any())
|
||||
{
|
||||
var newShipmentId = new List<string>();
|
||||
|
||||
// query db for shipment header info
|
||||
var requestHeader = new Data.Database.FBAInbound.GetShipmentHeaderInfo(sqlConnectionString);
|
||||
requestHeader.ShipmentIdList = dicShipExistsInDb.Keys.ToList();
|
||||
var resultHeader = requestHeader.Execute();
|
||||
|
||||
// compare db and mws result
|
||||
foreach (var item in resultHeader)
|
||||
{
|
||||
dicShipExistsInDb[item.AmazonShipmentId] = 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.AmazonShipmentId);
|
||||
}
|
||||
}
|
||||
|
||||
// write to db
|
||||
if (newShipmentInfoList != null)
|
||||
{
|
||||
foreach (var item in newShipmentInfoList)
|
||||
{
|
||||
// add the update date
|
||||
item.LastUpdatedUtc = dateTimeBefore;
|
||||
|
||||
// write to db
|
||||
var dbWrite = new Data.Database.FBAInbound.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user