10 changed files with 650 additions and 601 deletions
@ -1,31 +0,0 @@ |
|||||
using BBWY.Common.Http; |
|
||||
using BBWY.Common.Models; |
|
||||
using BBWY.Server.Model; |
|
||||
using Microsoft.Extensions.Options; |
|
||||
using NLog; |
|
||||
using Yitter.IdGenerator; |
|
||||
|
|
||||
namespace BBWY.Server.Business.Sync |
|
||||
{ |
|
||||
public class JDPopularizeAdSkuReportFormSyncBusiness: BaseSyncBusiness, IDenpendency |
|
||||
{ |
|
||||
public JDPopularizeAdSkuReportFormSyncBusiness(RestApiService restApiService, |
|
||||
IOptions<GlobalConfig> options, |
|
||||
ILogger logger, |
|
||||
IFreeSql fsql, |
|
||||
IIdGenerator idGenerator, |
|
||||
TaskSchedulerManager taskSchedulerManager, |
|
||||
VenderBusiness venderBusiness) : base(restApiService, |
|
||||
options, |
|
||||
logger, |
|
||||
fsql, |
|
||||
idGenerator, |
|
||||
taskSchedulerManager, |
|
||||
venderBusiness) |
|
||||
{ |
|
||||
|
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
||||
} |
|
@ -0,0 +1,165 @@ |
|||||
|
using BBWY.Common.Http; |
||||
|
using BBWY.Common.Models; |
||||
|
using BBWY.Server.Model; |
||||
|
using BBWY.Server.Model.Db; |
||||
|
using BBWY.Server.Model.Dto; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Newtonsoft.Json; |
||||
|
using Newtonsoft.Json.Linq; |
||||
|
using NLog; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Net.Http; |
||||
|
using System.Text.RegularExpressions; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Yitter.IdGenerator; |
||||
|
|
||||
|
namespace BBWY.Server.Business.Sync |
||||
|
{ |
||||
|
public class JDPopularizeReportFormAdLevelSyncBusiness : BaseSyncBusiness, IDenpendency |
||||
|
{ |
||||
|
public JDPopularizeReportFormAdLevelSyncBusiness(RestApiService restApiService, |
||||
|
IOptions<GlobalConfig> options, |
||||
|
ILogger logger, |
||||
|
IFreeSql fsql, |
||||
|
IIdGenerator idGenerator, |
||||
|
TaskSchedulerManager taskSchedulerManager, |
||||
|
VenderBusiness venderBusiness) : base(restApiService, |
||||
|
options, |
||||
|
logger, |
||||
|
fsql, |
||||
|
idGenerator, |
||||
|
taskSchedulerManager, |
||||
|
venderBusiness) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除推广报表-sku维度-京速推业务线数据
|
||||
|
/// </summary>
|
||||
|
/// <param name="shops"></param>
|
||||
|
/// <param name="startDate"></param>
|
||||
|
/// <param name="endDate"></param>
|
||||
|
private void DeleteOldData(IList<ShopResponse> shops, DateTime startDate, DateTime endDate) |
||||
|
{ |
||||
|
var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList(); |
||||
|
fsql.Delete<JDPopularizeAdSku>().Where(s => shopIds.Contains(s.ShopId.Value) && |
||||
|
s.Date >= startDate && s.Date <= endDate && |
||||
|
s.BusinessType == 2).ExecuteAffrows(); |
||||
|
} |
||||
|
|
||||
|
public void SyncAllShopPopularizeReportFormAdLevel() |
||||
|
{ |
||||
|
var shopList = venderBusiness.GetShopList(shopId: null, Enums.Platform.京东); |
||||
|
var date = DateTime.Now.Date.AddDays(-1); |
||||
|
DeleteOldData(shopList, date, date); |
||||
|
foreach (var shop in shopList) |
||||
|
{ |
||||
|
Task.Factory.StartNew(() => SyncShopPopularizeReportFormAdLevelByDate(shop, date, date), |
||||
|
System.Threading.CancellationToken.None, |
||||
|
TaskCreationOptions.LongRunning, |
||||
|
taskSchedulerManager.JDPopularizeTaskScheduler); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void SyncShopPopularizeReportFormAdLevelByDate(long? shopId, DateTime startDate, DateTime endDate) |
||||
|
{ |
||||
|
startDate = startDate.Date; |
||||
|
endDate = endDate.Date; |
||||
|
var shopList = venderBusiness.GetShopList(shopId, Enums.Platform.京东); |
||||
|
DeleteOldData(shopList, startDate, endDate); |
||||
|
foreach (var shop in shopList) |
||||
|
{ |
||||
|
Task.Factory.StartNew(() => SyncShopPopularizeReportFormAdLevelByDate(shop, startDate, endDate), |
||||
|
System.Threading.CancellationToken.None, |
||||
|
TaskCreationOptions.LongRunning, |
||||
|
taskSchedulerManager.JDPopularizeTaskScheduler); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SyncShopPopularizeReportFormAdLevelByDate(ShopResponse shop, DateTime startDate, DateTime endDate) |
||||
|
{ |
||||
|
var pageIndex = 1; |
||||
|
while (true) |
||||
|
{ |
||||
|
SyncShopPopularizeReportFormAdLevel(shop, startDate, endDate, pageIndex, out int count); |
||||
|
if (count < 100) |
||||
|
break; |
||||
|
pageIndex++; |
||||
|
Thread.Sleep(2000); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SyncShopPopularizeReportFormAdLevel(ShopResponse shop, DateTime startDate, DateTime endDate, int pageIndex, out int currentCount) |
||||
|
{ |
||||
|
currentCount = 0; |
||||
|
try |
||||
|
{ |
||||
|
var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId); |
||||
|
var httpResult = restApiService.SendRequest(relayAPIHost, "Api/PlatformSDK/GetJDSopularizeReportFormByAdLevel", new SyncJDPopularizeReportFormRequest() |
||||
|
{ |
||||
|
AppKey = shop.AppKey, |
||||
|
AppSecret = shop.AppSecret, |
||||
|
AppToken = shop.AppToken, |
||||
|
EndDate = endDate, |
||||
|
StartDate = startDate, |
||||
|
Platform = shop.PlatformId, |
||||
|
PageIndex = pageIndex, |
||||
|
Business = 2 |
||||
|
}, null, HttpMethod.Post); |
||||
|
if (httpResult.StatusCode != System.Net.HttpStatusCode.OK) |
||||
|
throw new Exception($"获取JD推广报表-创意维度失败 {httpResult.Content}"); |
||||
|
|
||||
|
var presponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(httpResult.Content); |
||||
|
if (!presponse.Success) |
||||
|
throw new Exception($"获取JD推广报表-创意维度失败 {presponse.Msg}"); |
||||
|
|
||||
|
SyncShopPopularizeReportFormAdLevel(long.Parse(shop.ShopId), presponse.Data); |
||||
|
currentCount = presponse.Data?.Count() ?? 0; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
var data = JsonConvert.SerializeObject(new { shop, startDate, endDate, pageIndex }); |
||||
|
logger.Error(ex, $"SyncShopPopularizeReportFormAdLevel Data:{data}"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SyncShopPopularizeReportFormAdLevel(long shopId, JArray jArray) |
||||
|
{ |
||||
|
if (jArray == null || !jArray.HasValues) |
||||
|
return; |
||||
|
var insertList = new List<JDPopularizeAdSku>(); |
||||
|
foreach (var j in jArray) |
||||
|
{ |
||||
|
var adName = j.Value<string>("adName"); |
||||
|
var skuMatch = Regex.Match(adName, @"^(.*-)?(\d+)-(.*)$"); |
||||
|
if (!skuMatch.Success) |
||||
|
continue; |
||||
|
var sku = skuMatch.Groups[2].Value; |
||||
|
insertList.Add(new JDPopularizeAdSku() |
||||
|
{ |
||||
|
Id = idGenerator.NewLong(), |
||||
|
BusinessType = 2, |
||||
|
ShopId = shopId, |
||||
|
CreateTime = DateTime.Now, |
||||
|
CampaignId = j.Value<long>("campaignId"), |
||||
|
AdGroupId = j.Value<long>("adGroupId"), |
||||
|
AdId = j.Value<long>("adId"), |
||||
|
AdName = adName, |
||||
|
Date = DateTime.ParseExact(j.Value<string>("date"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture), |
||||
|
Cost = j["retrievalType0"].Value<decimal>("cost"), |
||||
|
Clicks = j["retrievalType0"].Value<int?>("clicks") ?? 0, |
||||
|
Impressions = j["retrievalType0"].Value<int?>("impressions") ?? 0, |
||||
|
TotalCartCnt = j["retrievalType0"].Value<int?>("totalCartCnt") ?? 0, |
||||
|
TotalOrderCnt = j["retrievalType0"].Value<int?>("totalOrderCnt") ?? 0, |
||||
|
Sku = sku |
||||
|
}); |
||||
|
} |
||||
|
if (insertList.Count > 0) |
||||
|
fsql.Insert(insertList).ExecuteAffrows(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,151 @@ |
|||||
|
using BBWY.Common.Http; |
||||
|
using BBWY.Common.Models; |
||||
|
using BBWY.Server.Model; |
||||
|
using BBWY.Server.Model.Db; |
||||
|
using BBWY.Server.Model.Dto; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Newtonsoft.Json; |
||||
|
using Newtonsoft.Json.Linq; |
||||
|
using NLog; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Net.Http; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Yitter.IdGenerator; |
||||
|
|
||||
|
namespace BBWY.Server.Business.Sync |
||||
|
{ |
||||
|
public class JDPopularizeReportFormSkuLevelSyncBusiness : BaseSyncBusiness, IDenpendency |
||||
|
{ |
||||
|
public JDPopularizeReportFormSkuLevelSyncBusiness(RestApiService restApiService, |
||||
|
IOptions<GlobalConfig> options, |
||||
|
ILogger logger, |
||||
|
IFreeSql fsql, |
||||
|
IIdGenerator idGenerator, |
||||
|
TaskSchedulerManager taskSchedulerManager, |
||||
|
VenderBusiness venderBusiness) : base(restApiService, |
||||
|
options, |
||||
|
logger, |
||||
|
fsql, |
||||
|
idGenerator, |
||||
|
taskSchedulerManager, |
||||
|
venderBusiness) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 删除推广报表-sku维度-京速推业务线数据
|
||||
|
/// </summary>
|
||||
|
/// <param name="shops"></param>
|
||||
|
/// <param name="startDate"></param>
|
||||
|
/// <param name="endDate"></param>
|
||||
|
private void DeleteOldData(IList<ShopResponse> shops, DateTime startDate, DateTime endDate) |
||||
|
{ |
||||
|
var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList(); |
||||
|
fsql.Delete<JDPopularizeAdSku>().Where(s => shopIds.Contains(s.ShopId.Value) && |
||||
|
s.Date >= startDate && s.Date <= endDate && |
||||
|
s.BusinessType == 134217728).ExecuteAffrows(); |
||||
|
} |
||||
|
|
||||
|
public void SyncAllShopPopularizeReportFormSkuLevel() |
||||
|
{ |
||||
|
var shopList = venderBusiness.GetShopList(shopId: null, Enums.Platform.京东); |
||||
|
var date = DateTime.Now.Date.AddDays(-1); |
||||
|
DeleteOldData(shopList, date, date); |
||||
|
foreach (var shop in shopList) |
||||
|
{ |
||||
|
Task.Factory.StartNew(() => SyncShopPopularizeReportFormSkuLevelByDate(shop, date, date), |
||||
|
System.Threading.CancellationToken.None, |
||||
|
TaskCreationOptions.LongRunning, |
||||
|
taskSchedulerManager.JDPopularizeTaskScheduler); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void SyncShopPopularizeReportFormSkuLevelByDate(long? shopId, DateTime startDate, DateTime endDate) |
||||
|
{ |
||||
|
startDate = startDate.Date; |
||||
|
endDate = endDate.Date; |
||||
|
var shopList = venderBusiness.GetShopList(shopId, Enums.Platform.京东); |
||||
|
DeleteOldData(shopList, startDate, endDate); |
||||
|
foreach (var shop in shopList) |
||||
|
{ |
||||
|
Task.Factory.StartNew(() => SyncShopPopularizeReportFormSkuLevelByDate(shop, startDate, endDate), |
||||
|
System.Threading.CancellationToken.None, |
||||
|
TaskCreationOptions.LongRunning, |
||||
|
taskSchedulerManager.JDPopularizeTaskScheduler); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SyncShopPopularizeReportFormSkuLevelByDate(ShopResponse shop, DateTime startDate, DateTime endDate) |
||||
|
{ |
||||
|
var pageIndex = 1; |
||||
|
while (true) |
||||
|
{ |
||||
|
SyncShopPopularizeReportFormSkuLevel(shop, startDate, endDate, pageIndex, out int count); |
||||
|
if (count < 100) |
||||
|
break; |
||||
|
pageIndex++; |
||||
|
Thread.Sleep(2000); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SyncShopPopularizeReportFormSkuLevel(ShopResponse shop, DateTime startDate, DateTime endDate, int pageIndex, out int currentCount) |
||||
|
{ |
||||
|
currentCount = 0; |
||||
|
try |
||||
|
{ |
||||
|
var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId); |
||||
|
var httpResult = restApiService.SendRequest(relayAPIHost, "Api/PlatformSDK/GetJDSopularizeReportFormBySkuLevel", new SyncJDPopularizeReportFormRequest() |
||||
|
{ |
||||
|
AppKey = shop.AppKey, |
||||
|
AppSecret = shop.AppSecret, |
||||
|
AppToken = shop.AppToken, |
||||
|
EndDate = endDate, |
||||
|
StartDate = startDate, |
||||
|
Platform = shop.PlatformId, |
||||
|
PageIndex = pageIndex, |
||||
|
Business = 134217728 |
||||
|
}, null, HttpMethod.Post); |
||||
|
if (httpResult.StatusCode != System.Net.HttpStatusCode.OK) |
||||
|
throw new Exception($"获取JD推广报表-sku维度失败 {httpResult.Content}"); |
||||
|
|
||||
|
var presponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(httpResult.Content); |
||||
|
if (!presponse.Success) |
||||
|
throw new Exception($"获取JD推广报表-sku维度失败 {presponse.Msg}"); |
||||
|
|
||||
|
SyncShopPopularizeReportFormSkuLevel(long.Parse(shop.ShopId), presponse.Data); |
||||
|
currentCount = presponse.Data?.Count() ?? 0; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
var data = JsonConvert.SerializeObject(new { shop, startDate, endDate, pageIndex }); |
||||
|
logger.Error(ex, $"SyncShopPopularizeReportFormSkuLevel Data:{data}"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void SyncShopPopularizeReportFormSkuLevel(long shopId, JArray jArray) |
||||
|
{ |
||||
|
if (jArray == null || !jArray.HasValues) |
||||
|
return; |
||||
|
var insertList = jArray.Select(j => new JDPopularizeAdSku() |
||||
|
{ |
||||
|
Id = idGenerator.NewLong(), |
||||
|
BusinessType = 134217728, |
||||
|
ShopId = shopId, |
||||
|
CreateTime = DateTime.Now, |
||||
|
CampaignId = j.Value<long>("campaignId"), |
||||
|
Cost = j.Value<decimal>("cost"), |
||||
|
Date = DateTime.ParseExact(j.Value<string>("date"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture), |
||||
|
Clicks = j.Value<int?>("clicks") ?? 0, |
||||
|
Impressions = j.Value<int?>("impressions") ?? 0, |
||||
|
Sku = j.Value<string>("skuId"), |
||||
|
TotalCartCnt = j.Value<int?>("totalCartCnt") ?? 0, |
||||
|
TotalOrderCnt = j.Value<int?>("totalOrderCnt") ?? 0 |
||||
|
}).ToList(); |
||||
|
fsql.Insert(insertList).ExecuteAffrows(); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue