2 changed files with 231 additions and 0 deletions
@ -0,0 +1,228 @@ |
|||
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 JDPopularizeReportFormOrderLevelSyncBusiness : BaseSyncBusiness, IDenpendency |
|||
{ |
|||
public JDPopularizeReportFormOrderLevelSyncBusiness(RestApiService restApiService, |
|||
IOptions<GlobalConfig> options, |
|||
ILogger logger, |
|||
IFreeSql fsql, |
|||
IIdGenerator idGenerator, |
|||
TaskSchedulerManager taskSchedulerManager, |
|||
VenderBusiness venderBusiness) : base(restApiService, |
|||
options, |
|||
logger, |
|||
fsql, |
|||
idGenerator, |
|||
taskSchedulerManager, |
|||
venderBusiness) |
|||
{ |
|||
|
|||
} |
|||
|
|||
private void DeleteOldData(IList<ShopResponse> shops, DateTime startDate, DateTime endDate) |
|||
{ |
|||
var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList(); |
|||
fsql.Delete<JDOrderPopularizeRelation>().Where(s => shopIds.Contains(s.ShopId.Value) && |
|||
s.OrderTime >= startDate && s.OrderTime <= endDate).ExecuteAffrows(); |
|||
} |
|||
|
|||
public void SyncAllShopPopularizeReportFormOrderLevel() |
|||
{ |
|||
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(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, date, date, 2), |
|||
System.Threading.CancellationToken.None, |
|||
TaskCreationOptions.LongRunning, |
|||
taskSchedulerManager.JDPopularizeTaskScheduler); |
|||
|
|||
Task.Factory.StartNew(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, date, date, 134217728), |
|||
System.Threading.CancellationToken.None, |
|||
TaskCreationOptions.LongRunning, |
|||
taskSchedulerManager.JDPopularizeTaskScheduler); |
|||
} |
|||
} |
|||
|
|||
public void SyncShopPopularizeReportFormOrderLevelByDate(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(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, startDate, endDate, 2), |
|||
System.Threading.CancellationToken.None, |
|||
TaskCreationOptions.LongRunning, |
|||
taskSchedulerManager.JDPopularizeTaskScheduler); |
|||
|
|||
Task.Factory.StartNew(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, startDate, endDate, 134217728), |
|||
System.Threading.CancellationToken.None, |
|||
TaskCreationOptions.LongRunning, |
|||
taskSchedulerManager.JDPopularizeTaskScheduler); |
|||
} |
|||
} |
|||
|
|||
private void SyncShopPopularizeReportFormOrderLevelByDate(ShopResponse shop, DateTime startDate, DateTime endDate, int businessType) |
|||
{ |
|||
var pageIndex = 1; |
|||
while (true) |
|||
{ |
|||
SyncShopPopularizeReportFormOrderLevel(shop, startDate, endDate, pageIndex, businessType, out int count); |
|||
if (count < 100) |
|||
break; |
|||
pageIndex++; |
|||
Thread.Sleep(2000); |
|||
} |
|||
} |
|||
|
|||
private void SyncShopPopularizeReportFormOrderLevel(ShopResponse shop, DateTime startDate, DateTime endDate, int pageIndex, int businessType, out int currentCount) |
|||
{ |
|||
currentCount = 0; |
|||
try |
|||
{ |
|||
var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId); |
|||
var httpResult = restApiService.SendRequest(relayAPIHost, "Api/PlatformSDK/GetJDSopularizeReportFormByOrderLevel", new SyncJDPopularizeReportFormRequest() |
|||
{ |
|||
AppKey = shop.AppKey, |
|||
AppSecret = shop.AppSecret, |
|||
AppToken = shop.AppToken, |
|||
EndDate = endDate, |
|||
StartDate = startDate, |
|||
Platform = shop.PlatformId, |
|||
PageIndex = pageIndex, |
|||
Business = businessType |
|||
}, 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}"); |
|||
|
|||
if (businessType == 2) |
|||
SyncShopPopularizeReportFormOrderLevelByKuaiChe(long.Parse(shop.ShopId), presponse.Data); |
|||
else if (businessType == 134217728) |
|||
SyncShopPopularizeReportFormOrderLevelByJST(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, $"SyncShopPopularizeReportFormOrderLevel Data:{data}"); |
|||
} |
|||
} |
|||
|
|||
private void SyncShopPopularizeReportFormOrderLevelByKuaiChe(long shopId, JArray jArray) |
|||
{ |
|||
if (jArray == null || !jArray.HasValues) |
|||
return; |
|||
|
|||
var campaignNames = jArray.Select(j => j.Value<string>("campaignName")).Distinct().ToList(); |
|||
var adGroupNames = jArray.Select(j => j.Value<string>("adGroupName")).Distinct().ToList(); |
|||
var adNames = jArray.Select(j => j.Value<string>("adName")).Distinct().ToList(); |
|||
|
|||
var campaigns = fsql.Select<JDPopularizeCampaign>() |
|||
.Where(c => c.ShopId == shopId && campaignNames.Contains(c.CampaignName)) |
|||
.GroupBy(c => new { c.CampaignId, c.CampaignName }) |
|||
.ToList(g => new { g.Key.CampaignId, g.Key.CampaignName }); |
|||
|
|||
var adGroups = fsql.Select<JDPopularizeAdGroup>() |
|||
.Where(c => c.ShopId == shopId && adGroupNames.Contains(c.AdGroupName)) |
|||
.GroupBy(c => new { c.AdGroupId, c.AdGroupName }) |
|||
.ToList(g => new { g.Key.AdGroupId, g.Key.AdGroupName }); |
|||
|
|||
var ads = fsql.Select<JDPopularizeAdSku>() |
|||
.Where(c => c.ShopId == shopId && adNames.Contains(c.AdName)) |
|||
.GroupBy(c => new { c.AdId, c.AdName }) |
|||
.ToList(g => new { g.Key.AdId, g.Key.AdName }); |
|||
|
|||
var insertList = new List<JDOrderPopularizeRelation>(); |
|||
foreach (var j in jArray) |
|||
{ |
|||
var campaign = campaigns.FirstOrDefault(c => c.CampaignName == j.Value<string>("campaignName")); |
|||
var adGroup = adGroups.FirstOrDefault(g => g.AdGroupName == j.Value<string>("adGroupName")); |
|||
var ad = ads.FirstOrDefault(a => a.AdName == j.Value<string>("adName")); |
|||
|
|||
var popularizeSku = string.Empty; |
|||
if (ad != null) |
|||
{ |
|||
var skuMatch = Regex.Match(ad.AdName, @"^(.*-)?(\d+)-(.*)$"); |
|||
if (skuMatch.Success) |
|||
{ |
|||
popularizeSku = skuMatch.Groups[2].Value; |
|||
} |
|||
} |
|||
|
|||
insertList.Add(new JDOrderPopularizeRelation() |
|||
{ |
|||
Id = idGenerator.NewLong(), |
|||
BusinessType = 2, |
|||
ShopId = shopId, |
|||
CreateTime = DateTime.Now, |
|||
CampaignId = campaign?.CampaignId, |
|||
AdGroupId = adGroup?.AdGroupId, |
|||
AdId = ad?.AdId, |
|||
OrderId = j.Value<string>("orderId"), |
|||
OrderTime = DateTime.ParseExact(j.Value<string>("date"), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture), |
|||
PlaceOrderSku = j.Value<string>("skuId"), |
|||
PopularizeSku = popularizeSku |
|||
}); |
|||
} |
|||
if (insertList.Count > 0) |
|||
fsql.Insert(insertList).ExecuteAffrows(); |
|||
} |
|||
|
|||
private void SyncShopPopularizeReportFormOrderLevelByJST(long shopId, JArray jArray) |
|||
{ |
|||
if (jArray == null || !jArray.HasValues) |
|||
return; |
|||
|
|||
var campaignNames = jArray.Select(j => j.Value<string>("campaignName")).Distinct().ToList(); |
|||
var campaigns = fsql.Select<JDPopularizeCampaign>() |
|||
.Where(c => c.ShopId == shopId && campaignNames.Contains(c.CampaignName)) |
|||
.GroupBy(c => new { c.CampaignId, c.CampaignName }) |
|||
.ToList(g => new { g.Key.CampaignId, g.Key.CampaignName }); |
|||
|
|||
var insertList = new List<JDOrderPopularizeRelation>(); |
|||
foreach (var j in jArray) |
|||
{ |
|||
insertList.Add(new JDOrderPopularizeRelation() |
|||
{ |
|||
Id = idGenerator.NewLong(), |
|||
BusinessType = 134217728, |
|||
ShopId = shopId, |
|||
CreateTime = DateTime.Now, |
|||
CampaignId = campaigns.FirstOrDefault(c => c.CampaignName == j.Value<string>("campaignName"))?.CampaignId, |
|||
OrderTime = DateTime.ParseExact(j.Value<string>("date"), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture), |
|||
OrderId = j.Value<string>("orderId"), |
|||
PlaceOrderSku = j.Value<string>("skuId"), |
|||
PopularizeSku = j.Value<string>("clickSkuId") |
|||
}); |
|||
} |
|||
if (insertList.Count > 0) |
|||
fsql.Insert(insertList).ExecuteAffrows(); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue