Commit 3a3291bb by jianshuqin

修改BUG:同步4K接口的Amazon广告费重复数据异常

parent 8b7e5c8a
......@@ -30,7 +30,7 @@ namespace Bailun.DC.SyncAmazonAdFee
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
//每60分钟执行一次任务
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(60));
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(60 * 3));
return Task.CompletedTask;
}
......@@ -39,16 +39,9 @@ namespace Bailun.DC.SyncAmazonAdFee
try
{
var now = DateTime.Now;
if (now.Hour == 1) //每天1点启动
{
Console.WriteLine("开始启动 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Init();
Console.WriteLine("任务运行完成 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
Console.WriteLine($"目前时间是{now.Hour}小时,还没到设定的同步时间");
}
Console.WriteLine("开始启动 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Init();
Console.WriteLine("任务运行完成 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
catch (Exception ex)
{
......@@ -61,55 +54,53 @@ namespace Bailun.DC.SyncAmazonAdFee
Dapper.SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
}
public void Init(DateTime? startDate = null, DateTime? endDate = null)
public void Init(DateTime? startSyncDate = null, DateTime? endSyncDate = null)
{
IList<dc_base_company_account> listAccount = this.GetListAccount();
if (listAccount?.Count > 0)
{
if (!startDate.HasValue)
{
startDate = this.GetSyncDate();
Console.WriteLine($"同步开始日期{startDate.Value.ToString("yyyy-MM-dd")}");
}
if (!endDate.HasValue)
{
endDate = DateTime.Today.AddDays(-1);
}
for (DateTime date = startDate.Value; date <= endDate.Value; date = date.AddDays(1))
foreach (dc_base_company_account account in listAccount)
{
Console.WriteLine($"同步日期{date.ToString("yyyy-MM-dd")}");
foreach (dc_base_company_account account in listAccount)
DateTime? startDate = startSyncDate;
DateTime? endDate = endSyncDate;
if (!startSyncDate.HasValue)
{
var page = 1;
var pagesize = 1000;
var count = 0;
while (page == 1 || (page > 1 && count > 0))
startDate = this.GetSyncDate(account.account_id);
}
if (!endSyncDate.HasValue)
{
endDate = DateTime.Today.AddDays(-1);
}
Console.WriteLine($"同步[{account.seller_id}]开始日期{startDate.Value.ToString("yyyy-MM-dd")}");
var page = 1;
var pagesize = 1000;
var count = 0;
while (page == 1 || (page > 1 && count > 0))
{
try
{
try
long timeStamp = (long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
string sign = CommonHelper.md5Encrypt($"{this.appkey}{timeStamp}{account.seller_id}{account.site_en.ToLower()}{this.appkey}").ToUpper();
string url = $"{this.url}?pageIndex={page}&pageSize={pagesize}&sellerId={account.seller_id}&site={account.site_en.ToLower()}&timeStamp={timeStamp}&updateDate={startDate.Value.ToString("yyyy-MM-dd")}&sign={sign}";
string result = HttpHelper.NetHelper.Request(url);
Console.WriteLine($"接口地址:{url}");
if (!string.IsNullOrEmpty(result))
{
long timeStamp = (long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
string sign = CommonHelper.md5Encrypt($"{this.appkey}{timeStamp}{account.seller_id}{account.site_en.ToLower()}{this.appkey}").ToUpper();
string url = $"{this.url}?pageIndex={page}&pageSize={pagesize}&sellerId={account.seller_id}&site={account.site_en.ToLower()}&timeStamp={timeStamp}&updateDate={date.ToString("yyyy-MM-dd")}&sign={sign}";
string result = HttpHelper.NetHelper.Request(url);
Console.WriteLine($"接口地址:{url}");
if (!string.IsNullOrEmpty(result))
var json = JsonConvert.DeserializeObject<response_result>(result);
count = json?.details?.list?.Count ?? 0;
if (json.code == 100 && count > 0)
{
var json = JsonConvert.DeserializeObject<response_result>(result);
count = json?.details?.list?.Count ?? 0;
if (json.code == 100 && count > 0)
{
//保存数据
this.Save(json.details.list, account);
Console.WriteLine($"已保存{count}条记录数据!");
}
page++;
//保存数据
this.Save(json.details.list, account);
Console.WriteLine($"已保存{count}条记录数据!");
}
page++;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Thread.Sleep(60 * 1000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Thread.Sleep(60 * 1000);
}
}
}
......@@ -134,7 +125,7 @@ namespace Bailun.DC.SyncAmazonAdFee
return list;
}
public DateTime GetSyncDate()
public DateTime GetSyncDate(int accountId)
{
DateTime date = DateTime.Today.AddDays(-1);
using (var db = new MySqlConnection(GlobalConfig.ConnectionString_Data))
......@@ -143,7 +134,7 @@ namespace Bailun.DC.SyncAmazonAdFee
{
db.Open();
}
date = db.QueryFirstOrDefault<DateTime?>("SELECT MAX(DATE_FORMAT(t1.operation_time,'%Y-%m-%d')) FROM dc_base_finance_amazon_ad_product t1") ?? DateTime.Today.AddDays(-1);
date = db.QueryFirstOrDefault<DateTime?>("SELECT MAX(t1.operation_time) FROM dc_base_finance_amazon_ad_product t1 where account_id = @account_id", new { account_id = accountId }) ?? DateTime.Today.AddDays(-1);
}
return date;
......@@ -255,7 +246,7 @@ namespace Bailun.DC.SyncAmazonAdFee
, exchange_rate = @exchange_rate
, exchange_rate_usd = @exchange_rate_usd
, data_id = @data_id
where (`company_id`=@company_id AND `account_id`=@account_id AND `campaign_id`=@campaign_id AND `ad_group_id`=@ad_group_id AND `sku`=@sku AND `report_date`=@report_date AND `type`=@type) OR (`data_id`=@data_id)", m);
where (`company_id`=@company_id AND `account_id`=@account_id AND `campaign_id`=@campaign_id AND `ad_group_id`=@ad_group_id AND `sku`=@sku AND `report_date`=@report_date AND `type`=@type) OR (`data_id`=@data_id)", m);
}
if (count == 0)
{
......
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