mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
71 lines
2.6 KiB
C#
71 lines
2.6 KiB
C#
using bnhtrade.Core.Data.Amazon.Report;
|
|
using System.Linq;
|
|
|
|
namespace bnhtrade.Core.Logic.Import
|
|
{
|
|
public class AmazonSettlement
|
|
{
|
|
private Data.Amazon.Report.SettlementReport amazonReport;
|
|
private Logic.Log.LogEvent log = new Log.LogEvent();
|
|
|
|
|
|
public AmazonSettlement()
|
|
{
|
|
amazonReport = new Data.Amazon.Report.SettlementReport();
|
|
}
|
|
|
|
public void SyncDatabase()
|
|
{
|
|
string operation = "Import Amazon Settlement Reports";
|
|
|
|
log.LogInformation("Started '" + operation + "' operation.");
|
|
|
|
// get avaiable reports from amazon api
|
|
var spapiReportIdList = amazonReport.ListAvaliableReports();
|
|
int reportCount = spapiReportIdList.Count();
|
|
|
|
if (reportCount == 0)
|
|
{
|
|
log.LogInformation("Exiting '" + operation + "' operation. No settlement reports availble on Amazon SP-API.");
|
|
return;
|
|
}
|
|
|
|
// query db and remove reports that have already been imported
|
|
var dbReportList = new Data.Database.Import.AmazonSettlementHeaderRead().BySpapiReportId(spapiReportIdList);
|
|
foreach (var dbReport in dbReportList)
|
|
{
|
|
if (dbReport.SpapiReportIdIsSet)
|
|
{
|
|
for (int i = 0; i < spapiReportIdList.Count; i++)
|
|
{
|
|
if (spapiReportIdList[i] == dbReport.SpapiReportId)
|
|
{
|
|
spapiReportIdList.RemoveAt(i);
|
|
i--;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!spapiReportIdList.Any())
|
|
{
|
|
log.LogInformation("Exiting '" + operation + "' operation. No new reports to import (" + reportCount + " avaibale).");
|
|
return;
|
|
}
|
|
|
|
// import into database
|
|
var dbInsert = new Data.Database.Import.AmazonSettlementInsert();
|
|
for (int i = 0; i < spapiReportIdList.Count(); i++)
|
|
{
|
|
UI.Console.WriteLine("Importing settlement report " + (i + 1) + " of " + spapiReportIdList.Count() + " (ReportID:" + spapiReportIdList[i] + ").");
|
|
var filePath = amazonReport.GetReportFile(spapiReportIdList[i]);
|
|
bool ack = dbInsert.ByFlatFile(filePath, spapiReportIdList[i]);
|
|
log.LogInformation("Settlment Report imported (ReportID:" + spapiReportIdList[i] + ").");
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|