步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
12 KiB

3 years ago
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,
NLogManager nLogManager,
3 years ago
IFreeSql fsql,
IIdGenerator idGenerator,
TaskSchedulerManager taskSchedulerManager,
3 years ago
VenderBusiness venderBusiness,
YunDingBusiness yunDingBusiness) : base(restApiService,
3 years ago
options,
nLogManager,
3 years ago
fsql,
idGenerator,
taskSchedulerManager,
3 years ago
venderBusiness,
yunDingBusiness)
3 years ago
{
}
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.CookieTime >= startDate && s.CookieTime <= endDate).ExecuteAffrows();
3 years ago
}
public void SyncAllShopPopularizeReportFormOrderLevel()
{
var shopList = venderBusiness.GetShopList(shopId: null, Enums.Platform.);
var startDate = DateTime.Now.Date.AddDays(-15);
var endDate = DateTime.Now;
DeleteOldData(shopList, startDate, endDate);
3 years ago
foreach (var shop in shopList)
{
Task.Factory.StartNew(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, startDate, endDate, 2),
3 years ago
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.JDPopularizeTaskScheduler);
Task.Factory.StartNew(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, startDate, endDate, 134217728),
3 years ago
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)
{
3 years ago
Task.Factory.StartNew(() => SyncShopPopularizeReportFormOrderLevelByDate(shop, startDate, endDate, 2),
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.JDPopularizeTaskScheduler);
3 years ago
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
}, GetYunDingRequestHeader(), HttpMethod.Post);
3 years ago
nLogManager.GetLogger($"订单维度-{shop.ShopName}-{(businessType == 2 ? "" : "")}").Info(httpResult.Content);
3 years ago
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 });
3 years ago
nLogManager.GetLogger($"订单维度-{shop.ShopName}-{(businessType == 2 ? "" : "")}").Error(ex, $"SyncShopPopularizeReportFormOrderLevel Data:{data}");
3 years ago
}
}
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 });
3 years ago
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"));
3 years ago
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"),
3 years ago
OrderTime = DateTime.ParseExact(j.Value<string>("orderTime"), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture),
CookieTime = DateTime.ParseExact(j.Value<string>("cookieTime"), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture),
3 years ago
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,
3 years ago
OrderTime = DateTime.ParseExact(j.Value<string>("orderTime"), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture),
CookieTime = DateTime.ParseExact(j.Value<string>("cookieTime"), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture),
3 years ago
OrderId = j.Value<string>("orderId"),
PlaceOrderSku = j.Value<string>("skuId"),
PopularizeSku = j.Value<string>("clickSkuId")
});
}
if (insertList.Count > 0)
fsql.Insert(insertList).ExecuteAffrows();
}
}
}