mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 22:47:15 +00:00
Migration from Amazon MWS to Selling Partner API
This commit is contained in:
29
src/bnhtrade.Core/Logic/Product/AmazonCompetitivePrice.cs
Normal file
29
src/bnhtrade.Core/Logic/Product/AmazonCompetitivePrice.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Product
|
||||
{
|
||||
public class AmazonCompetitivePrice
|
||||
{
|
||||
private Data.Amazon.ProductPricing.GetCompetitivePricing apiRead;
|
||||
|
||||
public AmazonCompetitivePrice()
|
||||
{
|
||||
apiRead = new Data.Amazon.ProductPricing.GetCompetitivePricing();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Function adapted (for use with SP-API) from VBA to retrive a product price from Amazon MWS and return a competivite price
|
||||
/// </summary>
|
||||
/// <param name="asin"></param>
|
||||
/// <param name="lastPrice"></param>
|
||||
public void GetForSpreadsheetApplication(string asin, decimal lastPrice = 0m)
|
||||
{
|
||||
var result = apiRead.ByAsin(asin);
|
||||
//result = new Data.Amazon.SellingPartnerAPI.SDK.Models.ProductPricing.Price();
|
||||
}
|
||||
}
|
||||
}
|
||||
67
src/bnhtrade.Core/Logic/Product/AmazonEstimateFee.cs
Normal file
67
src/bnhtrade.Core/Logic/Product/AmazonEstimateFee.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Product
|
||||
{
|
||||
public class AmazonEstimateFee
|
||||
{
|
||||
public void UpdateDatabase(List<(string asin, decimal listingPrice)> inputList)
|
||||
{
|
||||
int inputCount = inputList.Count;
|
||||
var log = new Log.LogEvent();
|
||||
log.LogInformation("Starting update Amazon Fee Estimation, " + inputCount + " ASIN(s) to do...");
|
||||
|
||||
//check input list
|
||||
if (!inputList.Any())
|
||||
return;
|
||||
|
||||
// retrive product ids from db
|
||||
var asinList = new List<string>();
|
||||
foreach (var item in inputList)
|
||||
{
|
||||
asinList.Add(item.asin);
|
||||
}
|
||||
var productList = new Data.Database.Product.ReadProductId().ProductIdByAsin(asinList);
|
||||
|
||||
// request info from SP-API and update database
|
||||
var amazon = new Data.Amazon.ProductFee.GetFeeEstimate();
|
||||
var db = new Data.Database.Amazon.FeeEstimate();
|
||||
var progress = new UI.ConsoleProgressBar(productList.Count, "Processing " + productList.Count + " records...");
|
||||
int doneCount = 0;
|
||||
try
|
||||
{
|
||||
foreach (var item in productList)
|
||||
{
|
||||
doneCount++;
|
||||
progress.Report(doneCount);
|
||||
|
||||
string asin = item.Key;
|
||||
int productId = item.Value;
|
||||
|
||||
decimal listingPrice = 0m;
|
||||
foreach (var input in inputList)
|
||||
{
|
||||
if (asin == input.asin)
|
||||
{
|
||||
listingPrice = input.listingPrice;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var result = amazon.GetFba(asin, productId, listingPrice);
|
||||
db.UpdateProductFeeEstimate(result);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
progress.Dispose();
|
||||
|
||||
}
|
||||
|
||||
log.LogInformation("Completed update Amazon Fee Estimation, " + doneCount + " of " + inputCount + " done.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace bnhtrade.Core.Logic.Product
|
||||
public GetCompetitivePrice(string sqlConnectionString)
|
||||
{
|
||||
this.sqlConnectionString = sqlConnectionString;
|
||||
dbRead = new Data.Database.Product.ReadCompetitivePrice(sqlConnectionString);
|
||||
dbRead = new Data.Database.Product.ReadCompetitivePrice();
|
||||
newConditionMultipier = new Dictionary<int, decimal>();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace bnhtrade.Core.Logic.Product
|
||||
DistinctResponceCount = 0;
|
||||
}
|
||||
|
||||
public Dictionary<string, Model.Product.CompetitivePrice> Execute(List<Model.Sku.Price.SkuPriceParameter> skuParamList)
|
||||
public Dictionary<string, Model.Product.CompetitivePrice> Execute(List<Model.Sku.Price.SkuRepriceInfo> skuParamList)
|
||||
{
|
||||
var requestList = new List<(int ProductId, int ConditionId)>();
|
||||
for (int i = 0; i < skuParamList.Count; i++)
|
||||
@@ -218,44 +218,46 @@ namespace bnhtrade.Core.Logic.Product
|
||||
else
|
||||
{
|
||||
DistinctResponceCount = 1;
|
||||
return result.First();
|
||||
return result.First();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateConditionMultipers()
|
||||
{
|
||||
var conditionInfo = new List<Model.Sku.SkuConditionInfo>();
|
||||
throw new NotImplementedException();
|
||||
|
||||
if (newConditionMultipier.Any())
|
||||
{
|
||||
conditionInfo = new Logic.Sku.GetSkuConditionInfo(sqlConnectionString)
|
||||
.GetByConditionId(newConditionMultipier.Keys.ToList());
|
||||
}
|
||||
else
|
||||
{
|
||||
conditionInfo = new Logic.Sku.GetSkuConditionInfo(sqlConnectionString).GetAll();
|
||||
}
|
||||
|
||||
for(int i = 0; i < conditionInfo.Count; i++)
|
||||
{
|
||||
if (newConditionMultipier.ContainsKey(conditionInfo[i].SkuConditionId))
|
||||
{
|
||||
newConditionMultipier[conditionInfo[i].SkuConditionId] = conditionInfo[i].CompetitivePriceMultiplier;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Condition Id mismatch");
|
||||
}
|
||||
}
|
||||
//var conditionInfo = new List<Model.Sku.SkuConditionInfo>();
|
||||
|
||||
// check all multipilers have been set
|
||||
for (int i = 0; i < newConditionMultipier.Count; i++)
|
||||
{
|
||||
if (newConditionMultipier.ElementAt(i).Value <= 0)
|
||||
{
|
||||
throw new Exception("Invalid price multiplier");
|
||||
}
|
||||
}
|
||||
//if (newConditionMultipier.Any())
|
||||
//{
|
||||
// conditionInfo = new Logic.Sku.GetSkuConditionInfo(sqlConnectionString)
|
||||
// .GetByConditionId(newConditionMultipier.Keys.ToList());
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// conditionInfo = new Logic.Sku.GetSkuConditionInfo(sqlConnectionString).GetAll();
|
||||
//}
|
||||
|
||||
//for(int i = 0; i < conditionInfo.Count; i++)
|
||||
//{
|
||||
// if (newConditionMultipier.ContainsKey(conditionInfo[i].SkuConditionId))
|
||||
// {
|
||||
// newConditionMultipier[conditionInfo[i].SkuConditionId] = conditionInfo[i].CompetitivePriceMultiplier;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new Exception("Condition Id mismatch");
|
||||
// }
|
||||
//}
|
||||
|
||||
//// check all multipilers have been set
|
||||
//for (int i = 0; i < newConditionMultipier.Count; i++)
|
||||
//{
|
||||
// if (newConditionMultipier.ElementAt(i).Value <= 0)
|
||||
// {
|
||||
// throw new Exception("Invalid price multiplier");
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
111
src/bnhtrade.Core/Logic/Product/GetProductInfo.cs
Normal file
111
src/bnhtrade.Core/Logic/Product/GetProductInfo.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Product
|
||||
{
|
||||
public class GetProductInfo
|
||||
{
|
||||
private Data.Database.Product.ReadProduct dbRead;
|
||||
|
||||
public GetProductInfo()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
dbRead = new Data.Database.Product.ReadProduct();
|
||||
}
|
||||
|
||||
public Model.Product.ProductInfo GetByProductId(int productId)
|
||||
{
|
||||
var temp = GetByProductId(new List<int> { productId });
|
||||
if (temp.Any()) { return temp[0]; }
|
||||
else { return null; }
|
||||
}
|
||||
|
||||
public List<Model.Product.ProductInfo> GetByProductId(List<int> productIdList)
|
||||
{
|
||||
dbRead.Innit();
|
||||
dbRead.FilterByProductId = productIdList;
|
||||
return dbRead.ExecuteQuery();
|
||||
}
|
||||
|
||||
public Model.Product.ProductInfo GetBySkuNumber(string skuNumber)
|
||||
{
|
||||
var temp = GetBySkuNumber(new List<string> { skuNumber });
|
||||
|
||||
if (temp.ContainsKey(skuNumber))
|
||||
{
|
||||
return temp[skuNumber];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets product info for given list of SKU numbers
|
||||
/// </summary>
|
||||
/// <param name="skuNumberList">SKU Number list</param>
|
||||
/// <returns>Dictionary with SKU number as key and Product Info as value</returns>
|
||||
public Dictionary<string, Model.Product.ProductInfo> GetBySkuNumber(List<string> skuNumberList)
|
||||
{
|
||||
var returnDictionary = new Dictionary<string, Model.Product.ProductInfo>();
|
||||
|
||||
if (skuNumberList == null || !skuNumberList.Any())
|
||||
{
|
||||
return returnDictionary;
|
||||
}
|
||||
|
||||
var skuDictionary = dbRead.ProductIdBySkuNumber(skuNumberList);
|
||||
var productList = GetByProductId(skuDictionary.Values.ToList());
|
||||
|
||||
// build return dictionary
|
||||
foreach (var skuNumber in skuDictionary)
|
||||
{
|
||||
foreach (var product in productList)
|
||||
{
|
||||
if (product.ProductID == skuNumber.Value)
|
||||
{
|
||||
returnDictionary.Add(skuNumber.Key, product);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return returnDictionary;
|
||||
}
|
||||
|
||||
public bool AddToSkuInfo(List<Model.Sku.Sku> skuList)
|
||||
{
|
||||
bool missionSuccess = true;
|
||||
|
||||
if (skuList == null || !skuList.Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// get list of sku numbers to condition codes
|
||||
var lookupDictionary = GetBySkuNumber(skuList.Select(x => x.SkuNumber).ToList());
|
||||
|
||||
for (int i = 0; i < skuList.Count; i++)
|
||||
{
|
||||
if (lookupDictionary.ContainsKey(skuList[i].SkuNumber))
|
||||
{
|
||||
skuList[i].ProductInfo = lookupDictionary[skuList[i].SkuNumber];
|
||||
}
|
||||
else
|
||||
{
|
||||
missionSuccess = false;
|
||||
}
|
||||
}
|
||||
|
||||
return missionSuccess;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user