Commit d60c2315 by lizefeng

发布fba发货数据的拉取服务

parent 7f8b8482
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoTurnOver.Models
{
public class dc_base_amazon_fulfilled_shipments
{
public int id { get; set; }
public string data_id { get; set; }
public string amazon_order_id { get; set; }
public string merchant_order_id { get; set; }
public string shipment_id { get; set; }
public string shipment_item_id { get; set; }
public string amazon_order_item_id { get; set; }
public string merchant_order_item_id { get; set; }
public DateTime? purchase_date { get; set; }
public DateTime? payments_date { get; set; }
public DateTime? shipment_date { get; set; }
public DateTime? reporting_date { get; set; }
public string buyer_email { get; set; }
public string buyer_name { get; set; }
public string buyer_phone_number { get; set; }
public string sku { get; set; }
public string product_name { get; set; }
public decimal? quantity_shipped { get; set; }
public string currency { get; set; }
public decimal? item_price { get; set; }
public decimal? item_tax { get; set; }
public decimal? shipping_price { get; set; }
public decimal? shipping_tax { get; set; }
public decimal? gift_wrap_price { get; set; }
public decimal? gift_wrap_tax { get; set; }
public string ship_service_level { get; set; }
public string recipient_name { get; set; }
public string ship_address_1 { get; set; }
public string ship_address_2 { get; set; }
public string ship_address_3 { get; set; }
public string ship_city { get; set; }
public string ship_state { get; set; }
public string ship_postal_code { get; set; }
public string ship_country { get; set; }
public string ship_phone_number { get; set; }
public string bill_address_1 { get; set; }
public string bill_address_2 { get; set; }
public string bill_address_3 { get; set; }
public string bill_city { get; set; }
public string bill_state { get; set; }
public string bill_postal_code { get; set; }
public string bill_country { get; set; }
public string item_promotion_discount { get; set; }
public string ship_promotion_discount { get; set; }
public string carrier { get; set; }
public string tracking_number { get; set; }
public DateTime? estimated_arrival_date { get; set; }
public string fulfillment_center_id { get; set; }
public string fulfillment_channel { get; set; }
public string sales_channel { get; set; }
}
}
......@@ -26,6 +26,7 @@ using System.IO;
using FikaAmazonAPI.AmazonSpApiSDK.Models.Reports;
using System.Text;
using FikaAmazonAPI.ReportGeneration;
using System.Data;
namespace AutoTurnOver.Services
{
......@@ -314,7 +315,7 @@ namespace AutoTurnOver.Services
/// <summary>
/// 拉取报表
/// </summary>
public void SynchroReportIds()
public void SynchroReportIds(ReportTypes reportTypeEnum)
{
// 拉取亚马逊账号
var amazonAccounts = ApiServices.GetAmazonAccountList();
......@@ -327,8 +328,6 @@ namespace AutoTurnOver.Services
{
// 由于token1小时刷新一次,所以个账号要重新从账号系统拉取token
var item = ApiServices.GetAmazonAccountList().FirstOrDefault(s => s.Id == itemV.Id);
var reportTypeEnum = ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_XML;
var task_name = $"SynchroAmazonReport_{reportTypeEnum.ToString()}_v8_{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 });
......@@ -364,6 +363,10 @@ namespace AutoTurnOver.Services
foreach (var reportsItem in reports)
{
if (reportsItem.ProcessingStatus == Report.ProcessingStatusEnum.CANCELLED || reportsItem.ProcessingStatus == Report.ProcessingStatusEnum.FATAL)
{
continue;
}
var reportsDb = new dc_base_amazon_fee_report_log
{
ana_error = null,
......@@ -538,6 +541,113 @@ namespace AutoTurnOver.Services
MyMySqlConnection._connection.Update(ana_task);
}
public void AnaFbaShipmentReport()
{
var reportTypeEnum = ReportTypes.GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL;
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=0 order by id desc limit 1 ", new
{
report_type = reportTypeEnum.ToString()
});
if (ana_task == null)
{
return;
}
try
{
var pamsAccount = ApiServices.GetAmazonAccountList().FirstOrDefault(s => s.Id == ana_task.pams_account_id);
var filePath = DownloadReport(pamsAccount, ana_task.report_id);
StringBuilder response_str = new StringBuilder() { };
using (StreamReader sr = new StreamReader(filePath))
{
response_str.AppendLine(sr.ReadToEnd());
sr.Close();
}
string jsonText = response_str.ToString();
ana_task.content = jsonText;
var table = CsvFileHelper.ReadFromCSV(filePath, true,'\t');
foreach (DataRow row in table.Rows)
{
dc_base_amazon_fulfilled_shipments fulfilledShipments = new dc_base_amazon_fulfilled_shipments {
amazon_order_id = row.DataRowToString("amazon-order-id"),
amazon_order_item_id = row.DataRowToString("amazon-order-item-id"),
bill_address_1 = row.DataRowToString("bill-address-1"),
bill_address_2 = row.DataRowToString("bill-address-2"),
bill_address_3 = row.DataRowToString("bill-address-3"),
bill_city = row.DataRowToString("bill-city"),
bill_country = row.DataRowToString("bill-country"),
bill_postal_code = row.DataRowToString("bill-postal-code"),
bill_state = row.DataRowToString("bill-state"),
buyer_email = row.DataRowToString("buyer-email"),
buyer_name = row.DataRowToString("buyer-name"),
buyer_phone_number = row.DataRowToString("buyer-phone-number"),
carrier = row.DataRowToString("carrier"),
currency = row.DataRowToString("currency"),
estimated_arrival_date = row.DataRowToDateTime("estimated-arrival-date"),
fulfillment_center_id = row.DataRowToString("fulfillment-center-id"),
fulfillment_channel = row.DataRowToString("fulfillment-channel"),
gift_wrap_price = row.DataRowToNumber("gift-wrap-price"),
gift_wrap_tax = row.DataRowToNumber("gift-wrap-tax"),
item_price = row.DataRowToNumber("item-price"),
item_promotion_discount = row.DataRowToString("item-promotion-discount"),
item_tax = row.DataRowToNumber("item-tax"),
merchant_order_id = row.DataRowToString("merchant-order-id"),
payments_date = row.DataRowToDateTime("payments-date"),
merchant_order_item_id = row.DataRowToString("merchant-order-item-id"),
product_name = row.DataRowToString("product-name"),
purchase_date = row.DataRowToDateTime("purchase-date"),
quantity_shipped = row.DataRowToNumber("quantity-shipped"),
recipient_name = row.DataRowToString("recipient-name"),
reporting_date = row.DataRowToDateTime("reporting-date"),
sales_channel = row.DataRowToString("sales-channel"),
shipment_date = row.DataRowToDateTime("shipment-date"),
shipment_id = row.DataRowToString("shipment-id"),
shipment_item_id = row.DataRowToString("shipment-item-id"),
shipping_price = row.DataRowToNumber("shipping-price"),
shipping_tax = row.DataRowToNumber("shipping-tax"),
ship_address_1 = row.DataRowToString("ship-address-1"),
ship_address_2 = row.DataRowToString("ship-address-2"),
ship_address_3 = row.DataRowToString("ship-address-3"),
ship_city = row.DataRowToString("ship-city"),
ship_country = row.DataRowToString("ship-country"),
ship_phone_number = row.DataRowToString("ship-phone-number"),
ship_postal_code = row.DataRowToString("ship-postal-code"),
ship_promotion_discount = row.DataRowToString("ship-promotion-discount"),
ship_service_level = row.DataRowToString("ship-service-level"),
ship_state = row.DataRowToString("ship-state"),
sku = row.DataRowToString("sku"),
tracking_number = row.DataRowToString("tracking-number")
};
fulfilledShipments.data_id = $"{fulfilledShipments.amazon_order_id}-{fulfilledShipments.amazon_order_item_id}-{fulfilledShipments.sku}-{fulfilledShipments.merchant_order_item_id}-{fulfilledShipments.shipment_id}-{fulfilledShipments.shipment_item_id}-end";
fulfilledShipments.id = MyMySqlConnection._connection.QueryFirstOrDefault<int?>(" select id from dc_base_amazon_fulfilled_shipments where data_id=@data_id ",new { data_id = fulfilledShipments.data_id })??0;
if (fulfilledShipments.id > 0)
{
MyMySqlConnection._connection.Update(fulfilledShipments);
}
else
{
MyMySqlConnection._connection.Insert(fulfilledShipments);
}
}
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);
}
public ApiAmazonSettlementDto MapSettlement(string json)
{
return json.ToObj<ApiAmazonSettlementDto>();
......
......@@ -11,6 +11,61 @@ namespace AutoTurnOver.Utility
{
public class CsvFileHelper
{
/// <summary>
/// 将CSV文件中内容读取到DataTable中
/// </summary>
/// <param name="path">CSV文件路径</param>
/// <param name="hasTitle">是否将CSV文件的第一行读取为DataTable的列名</param>
/// <returns></returns>
public static DataTable ReadFromCSV(string path, bool hasTitle = false,char separator = ',')
{
DataTable dt = new DataTable(); //要输出的数据表
StreamReader sr = new StreamReader(path); //文件读入流
bool bFirst = true; //指示是否第一次读取数据
//逐行读取
string line;
while ((line = sr.ReadLine()) != null)
{
string[] elements = line.Replace("\"", "").Split(separator);
//第一次读取数据时,要创建数据列
if (bFirst)
{
for (int i = 0; i < elements.Length; i++)
{
dt.Columns.Add();
}
bFirst = false;
}
//有标题行时,第一行当做标题行处理
if (hasTitle)
{
for (int i = 0; i < dt.Columns.Count && i < elements.Length; i++)
{
dt.Columns[i].ColumnName = elements[i];
}
hasTitle = false;
}
else //读取一行数据
{
if (elements.Length == dt.Columns.Count)
{
dt.Rows.Add(elements);
}
else
{
//throw new Exception("CSV格式错误:表格各行列数不一致");
}
}
}
sr.Close();
return dt;
}
/// <summary>
/// 将DataTable中数据写入到CSV文件中
/// </summary>
......
......@@ -42,6 +42,57 @@ namespace AutoTurnOver.Utility
}
public static string DataRowToString(this DataRow row, string key)
{
if (row[key] == null)
{
return null;
}
else
{
return row[key].ToString();
}
}
public static DateTime? DataRowToDateTime(this DataRow row, string key)
{
try
{
if (row[key] == null)
{
return null;
}
else
{
return DateTime.Parse( row[key].ToString());
}
}
catch (Exception ex)
{
return null;
}
}
public static decimal? DataRowToNumber(this DataRow row, string key)
{
try
{
if (row[key] == null)
{
return null;
}
else
{
return decimal.Parse( row[key].ToString());
}
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// DataTable 转换为List 集合
/// </summary>
......
......@@ -53,12 +53,12 @@ namespace ResetOutofstock
//dc_ana_deviation_dao.PushAnaTask();
//new AmazonDataSynchroService().SynchroFinancialEventGroups();
//new AmazonDataSynchroService().SynchroReportIds();
//new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL);
//while (true)
//{
// new AmazonDataSynchroService().AnaReport();
// new AmazonDataSynchroService().AnaFbaShipmentReport();
//}
}
catch (Exception ex)
{
......
......@@ -623,7 +623,7 @@ namespace ResetOutofstock
try
{
Console.WriteLine($"开始 拉取亚马逊费用流水,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new AmazonDataSynchroService().SynchroReportIds();
new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_XML);
Console.WriteLine($"结束 拉取亚马逊费用流水,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
Thread.Sleep(1000 * 60 * 3 );
}
......@@ -636,6 +636,28 @@ namespace ResetOutofstock
}
});
Task.Factory.StartNew(() =>
{
while (true)
{
try
{
Console.WriteLine($"开始 拉取亚马逊 fba发货,线程Id:{Thread.CurrentThread.ManagedThreadId}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
new AmazonDataSynchroService().SynchroReportIds(FikaAmazonAPI.Utils.Constants.ReportTypes.GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL);
Console.WriteLine($"结束 拉取亚马逊 fba发货,线程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(() =>
......@@ -659,6 +681,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().AnaFbaShipmentReport();
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