Commit 36ef030e by lizefeng

新增亚马逊仓储费抓取

parent b9029aa3
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.Models
{
public class dc_profit_business_amazon_storage_fee
{
public int id { get; set; }
public string _unique_id { get; set; }
public DateTime _create_date { get; set; }
public DateTime _update_date { get; set; }
public string _project { get; set; }
public int _is_delete { get; set; }
public DateTime _date { get; set; }
public DateTime report_end_date { get; set; }
public string account { get; set; }
public string asin { get; set; }
public string site { get; set; }
public decimal fee { get; set; }
public string fee_type { get; set; }
public string currency { get; set; }
public string fulfillment_center { get; set; }
}
}
......@@ -75,7 +75,7 @@ namespace AutoTurnOver.Services
var now = DateTime.Now;
var fees = GetFinancialEventGroups(item, new_task_synchro_log.start_time, new_task_synchro_log.end_time);
//var json = fees.ToJson();
foreach (var feeItem in fees.Where(s => s.FundTransferStatus == "Succeeded" || (s.ProcessingStatus== "Open" && s.FinancialEventGroupEnd==null)) )
foreach (var feeItem in fees.Where(s => s.FundTransferStatus == "Succeeded" || (s.ProcessingStatus == "Open" && s.FinancialEventGroupEnd == null)))
{
......@@ -106,11 +106,11 @@ namespace AutoTurnOver.Services
transferFee.type_cn = "放款 预估";
transferFee.description = $" {transferFee.currency_code}${transferFee.total} 转账计划于以下日期发起:{end_date_utc.ToString("yyyy/MM/dd")}。";
}
transferFee.settlement_date_str = $"{transferFee.start_date.Value.ToString("yyyy-MM-ddTHH:mm:ss")}";
transferFee.other = transferFee.total;
transferFee.data_id = $"{transferFee.type}_{transferFee.account}-{transferFee.settlement_date_str}";
var oldData = MyMySqlConnection._connection.QueryFirstOrDefault<dc_base_amazon_fee>(" select * from dc_base_amazon_fee where data_id=@data_id ", new { data_id = transferFee.data_id }) ;
var oldData = MyMySqlConnection._connection.QueryFirstOrDefault<dc_base_amazon_fee>(" select * from dc_base_amazon_fee where data_id=@data_id ", new { data_id = transferFee.data_id });
if (oldData != null)
{
transferFee.id = oldData.id;
......@@ -163,7 +163,7 @@ namespace AutoTurnOver.Services
/// <summary>
/// 拉取报表
/// </summary>
public void SynchroReportIds(ReportTypes reportTypeEnum, int version = 8, string account = "",string site = "")
public void SynchroReportIds(ReportTypes reportTypeEnum, int version = 8, string account = "", string site = "")
{
// 拉取亚马逊账号
......@@ -237,8 +237,8 @@ namespace AutoTurnOver.Services
report_par_json = reportsItem.ToJson(),
btime = reportsItem.DataStartTime,
etime = reportsItem.DataEndTime,
site_code = item.SiteEn,
account= item.Account
site_code = String.Join(",", reportsItem.MarketplaceIds),
account = item.Account
};
reportsDb.id = MyMySqlConnection._connection.QueryFirstOrDefault<int?>(" select id from dc_base_amazon_fee_report_log where report_id=@report_id ", new
......@@ -251,7 +251,8 @@ namespace AutoTurnOver.Services
}
else
{
MyMySqlConnection._connection.Execute(" update dc_base_amazon_fee_report_log set account=@account where id=@id ",new {
MyMySqlConnection._connection.Execute(" update dc_base_amazon_fee_report_log set account=@account where id=@id ", new
{
id = reportsDb.id,
account = reportsDb.account
});
......@@ -281,20 +282,29 @@ namespace AutoTurnOver.Services
}
}
public void SynchroReportIdByCreate(ReportTypes reportTypeEnum)
public void SynchroReportIdByCreate(ReportTypes reportTypeEnum, int version = 8, string account = "", string site = "")
{
// 拉取亚马逊账号
var amazonAccounts = ApiServices.GetAmazonAccountList();
if (!string.IsNullOrWhiteSpace(account))
{
amazonAccounts = amazonAccounts.Where(s => s.Account == account).ToList();
}
if (!string.IsNullOrWhiteSpace(site))
{
amazonAccounts = amazonAccounts.Where(s => s.SiteEn == site).ToList();
}
//amazonAccounts = amazonAccounts.Where(s => s.Account == "Natural Daisy Encens" && s.SiteEn == "UK").ToList();
foreach (var itemV in amazonAccounts)
{
try
{
if (itemV.AmznSpDevAccountObj != null && itemV.AmznSpTokenObj != null )
if (itemV.AmznSpDevAccountObj != null && itemV.AmznSpTokenObj != null)
{
// 由于token1小时刷新一次,所以个账号要重新从账号系统拉取token
var item = ApiServices.GetAmazonAccountList().FirstOrDefault(s => s.Id == itemV.Id);
var task_name = $"SynchroAmazonReport_{reportTypeEnum.ToString()}_v9_{item.Id}";
var task_name = $"SynchroAmazonReport_{reportTypeEnum.ToString()}_v{version}_{item.Id}";
// 查询最后一次成功抓取的记录
var last_task_synchro_log = MyMySqlConnection._connection.QuerySingleOrDefault<task_synchro_log>(" select * from task_synchro_log where task_name=@task_name and status=1 order by end_time desc limit 1 ", new { task_name = task_name });
......@@ -317,7 +327,7 @@ namespace AutoTurnOver.Services
}
else
{
new_task_synchro_log.start_time = new DateTime(2023, 1, 1);
new_task_synchro_log.start_time = DateTime.Now.AddDays(-30);
}
new_task_synchro_log.id = MyMySqlConnection._connection.Insert(new_task_synchro_log) ?? 0;
......@@ -790,6 +800,108 @@ namespace AutoTurnOver.Services
}
/// <summary>
/// 仓储超量费
/// </summary>
/// <param name="status"></param>
/// <param name="data_id"></param>
public void AnaFbaStorageFeeReport(int status = 0, string data_id = "")
{
var reportTypeEnum = ReportTypes.GET_FBA_STORAGE_FEE_CHARGES_DATA;
var ana_task = MyMySqlConnection._connection.QueryFirstOrDefault<dc_base_amazon_fee_report_log>(" select * from dc_base_amazon_fee_report_log where report_type=@report_type and ana_status=@status order by id desc limit 1 ", new
{
report_type = reportTypeEnum.ToString(),
status = status
});
if (!string.IsNullOrWhiteSpace(data_id))
{
ana_task = MyMySqlConnection._connection.QueryFirstOrDefault<dc_base_amazon_fee_report_log>(" select * from dc_base_amazon_fee_report_log where report_type=@report_type and data_id=@data_id order by id desc limit 1 ", new
{
report_type = reportTypeEnum.ToString(),
data_id = data_id
});
}
if (ana_task == null)
{
return;
}
try
{
var pamsAccount = ApiServices.GetAmazonAccountList().FirstOrDefault(s => s.Id == ana_task.pams_account_id);
var filePath = DownloadReportData(pamsAccount, ana_task.report_id);
StringBuilder response_str = new StringBuilder() { };
using (StreamReader sr = new StreamReader(filePath.ReportDocumentId))
{
response_str.AppendLine(sr.ReadToEnd());
sr.Close();
}
string jsonText = response_str.ToString();
ana_task.content = jsonText;
var table = CsvFileHelper.ReadFromCSV(filePath.ReportDocumentId, true, '\t');
foreach (DataRow row in table.Rows)
{
try
{
dc_profit_business_amazon_storage_fee storageFee = new dc_profit_business_amazon_storage_fee
{
currency = row.DataRowToString("currency"),
fulfillment_center = row.DataRowToString("fulfillment_center"),
fee_type = "月度仓储费",
account = pamsAccount.Account,
asin = row.DataRowToString("asin"),
report_end_date = filePath.DataEndTime ?? new DateTime(1991, 1, 1)
};
storageFee.fee = row.DataRowToNumber("estimated_monthly_storage_fee") ?? 0;
storageFee._date = DateTime.Parse(row.DataRowToString("month_of_charge"));
storageFee.site = row.DataRowToString("country_code");
if ("GB".Equals(storageFee.site, StringComparison.CurrentCultureIgnoreCase))
{
storageFee.site = "UK";
}
storageFee._create_date = DateTime.Now;
storageFee._update_date = DateTime.Now;
storageFee._unique_id = $"{storageFee.asin}-{storageFee._date.ToString("yyyy-MM")}-{storageFee.site}-{storageFee.account}-{storageFee.fulfillment_center}";
var oldData = MyMySqlConnection._connection.QueryFirstOrDefault<dc_profit_business_amazon_storage_fee>(" select * from dc_profit_business_amazon_storage_fee where _unique_id=@unique_id ", new { unique_id = storageFee._unique_id });
if (oldData != null)
{
// 多份报表都出现同一份数据。已报表实际较晚的为准
if (oldData.report_end_date <= storageFee.report_end_date)
{
MyMySqlConnection._connection.Update(storageFee);
}
}
else
{
MyMySqlConnection._connection.Insert(storageFee);
}
}
catch (Exception ex)
{
throw;
}
}
ana_task.ana_error = "";
ana_task.ana_stack_trace = "";
ana_task.ana_status = 1;
}
catch (Exception ex)
{
ana_task.ana_error = ex.Message;
ana_task.ana_stack_trace = ex.StackTrace;
ana_task.ana_status = -1;
}
MyMySqlConnection._connection.Update(ana_task);
}
/// <summary>
/// 根据站点推算币种
/// </summary>
/// <param name="currency"></param>
......@@ -857,8 +969,8 @@ namespace AutoTurnOver.Services
feeShipmentEvent.shipping_credits_tax = item.ItemPrice.Component.Where(s => s.Type.Equals("ShippingTax", StringComparison.InvariantCultureIgnoreCase)).Sum(s => s.Amount.Text ?? 0);
feeShipmentEvent.product_sales_tax = item.ItemPrice.Component.Where(s => s.Type.Equals("Tax", StringComparison.InvariantCultureIgnoreCase)).Sum(s => s.Amount.Text ?? 0);
// 比利时站合并两个税
if ("Amazon.com.be".Equals(feeShipmentEvent.MarketplaceName,StringComparison.CurrentCultureIgnoreCase) || "Amazon.nl".Equals(feeShipmentEvent.MarketplaceName,StringComparison.InvariantCulture))
if ("Amazon.com.be".Equals(feeShipmentEvent.MarketplaceName, StringComparison.CurrentCultureIgnoreCase) || "Amazon.nl".Equals(feeShipmentEvent.MarketplaceName, StringComparison.InvariantCulture))
{
feeShipmentEvent.product_sales_tax = feeShipmentEvent.shipping_credits_tax + feeShipmentEvent.product_sales_tax;
feeShipmentEvent.shipping_credits_tax = 0;
......@@ -1226,6 +1338,26 @@ namespace AutoTurnOver.Services
}
return filePath;
}
public Report DownloadReportData(AmazonAccountDto account, string reportId)
{
AmazonConnection amazonConnection = MapAmazonConnection(account);
var filePath = string.Empty;
string ReportDocumentId = string.Empty;
var reportData = amazonConnection.Reports.GetReport(reportId);
if (!string.IsNullOrEmpty(reportData.ReportDocumentId))
{
reportData.ReportDocumentId = amazonConnection.Reports.GetReportFile(reportData.ReportDocumentId);
return reportData;
}
else
{
return null;
}
}
public AmazonConnection MapAmazonConnection(AmazonAccountDto account)
{
......@@ -1366,9 +1498,9 @@ namespace AutoTurnOver.Services
settlement_date_str = item.settlement_date_str,
account = item.account
}).ToList();
if (feeSingleDatas != null && feeSingleDatas.Count>0)
if (feeSingleDatas != null && feeSingleDatas.Count > 0)
{
if(feeSingleDatas.Count == 1)
if (feeSingleDatas.Count == 1)
{
item.settlement_id = feeSingleDatas[0].settlement_id;
MyMySqlConnection._connection.Execute(" update dc_base_amazon_fee set settlement_id=@settlement_id,_ts=now() where id=@id ", new
......@@ -1392,7 +1524,7 @@ namespace AutoTurnOver.Services
id = item.id
});
}
}
}
}
......@@ -1406,7 +1538,7 @@ namespace AutoTurnOver.Services
//datas = datas.Where(s=>s.id== 263627).ToList();
foreach (var item in datas)
{
//自己的开始时间,是上一个报表的结束时间
var feeSingleData2 = MyMySqlConnection._connection.QueryFirstOrDefault<dc_base_amazon_fee>(" select * from dc_base_amazon_fee where settlement_date_end_str=@settlement_date_str and account=@account and settlement_id!='-' limit 1 ", new
{
......
......@@ -42,6 +42,18 @@ namespace AutoTurnOver.Utility
}
public static string DataRowToString(this DataRow row,string colName)
{
if (row[colName] == null)
{
return null;
}
else
{
return row[colName].ToString();
}
}
public static string DataRowToString(this DataRow row,int index)
{
if (row[index] == null)
......@@ -66,6 +78,29 @@ namespace AutoTurnOver.Utility
return DateTime.Parse(row[index].ToString());
}
}
public static decimal? DataRowToNumber(this DataRow row,string colName)
{
if (row[colName] == null)
{
return null;
}
else
{
var strData = row[colName].ToString();
if (strData.Contains("E"))
{
return Convert.ToDecimal(Decimal.Parse(strData.ToString(), System.Globalization.NumberStyles.Float));
}
else
{
return decimal.Parse(strData);
}
}
}
public static decimal? DataRowToNumber(this DataRow row,int index)
{
if (row[index] == null)
......
......@@ -60,6 +60,20 @@ namespace FikaAmazonAPI.Utils
list.Add(Singapore); list.Add(Australia); list.Add(Japan);
return list.FirstOrDefault(a => a.Country.Code == countryCode);
}
public static MarketPlace GetMarketplaceById(string id)
{
var list = new List<MarketPlace>();
//NorthAmerica
list.Add(US); list.Add(Canada); list.Add(Mexico); list.Add(Brazil);
//Europe
list.Add(Spain); list.Add(UnitedKingdom); list.Add(France); list.Add(Belgium);
list.Add(Netherlands); list.Add(Germany); list.Add(Italy); list.Add(Sweden);
list.Add(Egypt); list.Add(Poland); list.Add(Turkey); list.Add(UnitedArabEmirates);
list.Add(India); list.Add(SaudiArabia);
//FarEast
list.Add(Singapore); list.Add(Australia); list.Add(Japan);
return list.FirstOrDefault(a => a.ID == id);
}
//https://developer-docs.amazon.com/sp-api/docs/marketplace-ids
......
......@@ -51,15 +51,17 @@ namespace ResetOutofstock
//report_invest_return_dao.SynchBtmOrderRefund();
//report_invest_return_dao.CalculationStockScore("962073701");
//dc_ana_deviation_dao.PushAnaTask();
new AmazonDataSynchroService().SynchroFinancialEventGroups();
//new AmazonDataSynchroService().SynchroFinancialEventGroups();
//new AmazonDataSynchroService().SetMarketplaceName(DateTime.Now);
//new AmazonDataSynchroService().SetSettlementByDate();
//new AmazonDataSynchroService().SetMarketplaceNameByDate();
//new AmazonDataSynchroService().SynchroReportIds();
//new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_XML, 13, "Siqidzi","BE");
//new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_FBA_INVENTORY_PLANNING_DATA, 15, "Elite99 Nail Art", "US");
//new AmazonDataSynchroService().SynchroReportIdByCreate(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_FBA_OVERAGE_FEE_CHARGES_DATA, 14, "Siqidzi","BE");
//new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL, 13);
//new AmazonDataSynchroService().AnaFbaShipmentReport();
//new AmazonDataSynchroService().AnaFbaStorageFeeReport();
//while (true)
//{
// new AmazonDataSynchroService().AnaReport(status:-3);
......
......@@ -658,6 +658,71 @@ namespace ResetOutofstock
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
try
{
Console.WriteLine($"开始 拉取亚马逊 仓储超量费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_FBA_OVERAGE_FEE_CHARGES_DATA, 15);
Console.WriteLine($"结束 拉取亚马逊 仓储超量费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
Thread.Sleep(1000 * 60 * 3 );
}
catch (Exception ex)
{
Console.WriteLine(" 拉取亚马逊 仓储超量费:" + ex.Message);
Console.WriteLine(" 拉取亚马逊 仓储超量费 :" + ex.StackTrace);
}
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
try
{
Console.WriteLine($"开始 拉取亚马逊 长期仓储费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_FBA_INVENTORY_PLANNING_DATA, 15);
Console.WriteLine($"结束 拉取亚马逊 长期仓储费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
Thread.Sleep(1000 * 60 * 3 );
}
catch (Exception ex)
{
Console.WriteLine(" 拉取亚马逊 长期仓储费:" + ex.Message);
Console.WriteLine(" 拉取亚马逊 长期仓储费 :" + ex.StackTrace);
}
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
try
{
Console.WriteLine($"开始 拉取亚马逊 月度仓储费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_FBA_STORAGE_FEE_CHARGES_DATA, 15);
Console.WriteLine($"结束 拉取亚马逊 月度仓储费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
Thread.Sleep(1000 * 60 * 3 );
}
catch (Exception ex)
{
Console.WriteLine(" 拉取亚马逊 月度仓储费:" + ex.Message);
Console.WriteLine(" 拉取亚马逊 月度仓储费 :" + ex.StackTrace);
}
}
});
Task.Factory.StartNew(() =>
......@@ -705,6 +770,28 @@ namespace ResetOutofstock
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
try
{
Console.WriteLine($"开始 解析月度仓储费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new AmazonDataSynchroService().AnaFbaStorageFeeReport();
new AmazonDataSynchroService().AnaFbaStorageFeeReport(-1);
Console.WriteLine($"结束 解析月度仓储费,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
Thread.Sleep(1000 * 3 );
}
catch (Exception ex)
{
Console.WriteLine(" 解析亚马逊报表(发货流水):" + ex.Message);
Console.WriteLine(" 解析亚马逊报表(发货流水) :" + ex.StackTrace);
}
}
});
Task.Factory.StartNew(() =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment