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.
195 lines
11 KiB
195 lines
11 KiB
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 JDPopularizeReportFormCampaignLevelSyncBusiness : BaseSyncBusiness, IDenpendency
|
|
{
|
|
public JDPopularizeReportFormCampaignLevelSyncBusiness(RestApiService restApiService,
|
|
IOptions<GlobalConfig> options,
|
|
NLogManager nLogManager,
|
|
IFreeSql fsql,
|
|
IIdGenerator idGenerator,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
VenderBusiness venderBusiness,
|
|
YunDingBusiness yunDingBusiness) : base(restApiService,
|
|
options,
|
|
nLogManager,
|
|
fsql,
|
|
idGenerator,
|
|
taskSchedulerManager,
|
|
venderBusiness,
|
|
yunDingBusiness)
|
|
{
|
|
|
|
}
|
|
|
|
private void DeleteOldData(IList<ShopResponse> shops, DateTime startDate, DateTime endDate)
|
|
{
|
|
var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList();
|
|
fsql.Delete<JDPopularizeCampaign>().Where(s => shopIds.Contains(s.ShopId.Value) &&
|
|
s.Date >= startDate && s.Date <= endDate).ExecuteAffrows();
|
|
}
|
|
|
|
public void SyncAllShopPopularizeReportFormCampaignLevel()
|
|
{
|
|
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(() => SyncShopPopularizeReportFormCampaignLevelByDate(shop, date, date, 2),
|
|
System.Threading.CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.JDPopularizeTaskScheduler);
|
|
|
|
Task.Factory.StartNew(() => SyncShopPopularizeReportFormCampaignLevelByDate(shop, date, date, 134217728),
|
|
System.Threading.CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.JDPopularizeTaskScheduler);
|
|
}
|
|
}
|
|
|
|
public void SyncShopPopularizeReportFormCampaignLevelByDate(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(() => SyncShopPopularizeReportFormCampaignLevelByDate(shop, startDate, endDate, 2),
|
|
System.Threading.CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.JDPopularizeTaskScheduler);
|
|
|
|
Task.Factory.StartNew(() => SyncShopPopularizeReportFormCampaignLevelByDate(shop, startDate, endDate, 134217728),
|
|
System.Threading.CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.JDPopularizeTaskScheduler);
|
|
}
|
|
}
|
|
|
|
private void SyncShopPopularizeReportFormCampaignLevelByDate(ShopResponse shop, DateTime startDate, DateTime endDate, int businessType)
|
|
{
|
|
var pageIndex = 1;
|
|
while (true)
|
|
{
|
|
SyncShopPopularizeReportFormCampaignLevel(shop, startDate, endDate, pageIndex, businessType, out int count);
|
|
if (count < 100)
|
|
break;
|
|
pageIndex++;
|
|
Thread.Sleep(2000);
|
|
}
|
|
}
|
|
|
|
private void SyncShopPopularizeReportFormCampaignLevel(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/GetJDSopularizeReportFormByCampaignLevel", 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);
|
|
|
|
nLogManager.GetLogger($"计划维度-{shop.ShopName}-{(businessType == 2 ? "快车" : "京速推")}").Info(httpResult.Content);
|
|
|
|
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}");
|
|
|
|
SyncShopPopularizeReportFormCampaignLevel(long.Parse(shop.ShopId), presponse.Data, businessType);
|
|
currentCount = presponse.Data?.Count() ?? 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var data = JsonConvert.SerializeObject(new { shop, startDate, endDate, pageIndex });
|
|
nLogManager.GetLogger($"计划维度-{shop.ShopName}-{(businessType == 2 ? "快车" : "京速推")}").Error(ex, $"SyncShopPopularizeReportFormCampaignLevel Data:{data}");
|
|
}
|
|
}
|
|
|
|
private void SyncShopPopularizeReportFormCampaignLevel(long shopId, JArray jArray, int businessType)
|
|
{
|
|
if (jArray == null || !jArray.HasValues)
|
|
return;
|
|
var insertList = new List<JDPopularizeCampaign>();
|
|
foreach (var j in jArray)
|
|
{
|
|
insertList.Add(new JDPopularizeCampaign()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
BusinessType = businessType,
|
|
ShopId = shopId,
|
|
CreateTime = DateTime.Now,
|
|
CampaignId = j.Value<long>("campaignId"),
|
|
CampaignName = j.Value<string>("campaignName"),
|
|
Date = DateTime.ParseExact(j.Value<string>("date"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture),
|
|
Cost = j.Value<decimal>("cost"),
|
|
Clicks = j.Value<int?>("clicks") ?? 0,
|
|
Impressions = j.Value<int?>("impressions") ?? 0,
|
|
TotalCartCnt = j.Value<int?>("totalCartCnt") ?? 0,
|
|
TotalOrderCnt = j.Value<int?>("totalOrderCnt") ?? 0,
|
|
CampaignPutType = j.Value<int?>("campaignPutType"),
|
|
CampaignType = j.Value<int?>("campaignType"),
|
|
CampaignTypeExpand = j.Value<int?>("campaignTypeExpand"),
|
|
CouponCnt = j.Value<int?>("couponCnt") ?? 0,
|
|
CPA = j.Value<decimal?>("CPA") ?? 0.00M,
|
|
CPC = j.Value<decimal?>("CPC") ?? 0.00M,
|
|
CPM = j.Value<decimal?>("CPM") ?? 0.00M,
|
|
CTR = j.Value<decimal?>("CTR") ?? 0.00M,
|
|
DepthPassengerCnt = j.Value<int?>("depthPassengerCnt") ?? 0,
|
|
DirectCartCnt = j.Value<int?>("directCartCnt") ?? 0,
|
|
DirectOrderCnt = j.Value<int?>("directOrderCnt") ?? 0,
|
|
DirectOrderSum = j.Value<decimal?>("directOrderSum") ?? 0M,
|
|
GoodsAttentionCnt = j.Value<int?>("goodsAttentionCnt") ?? 0,
|
|
IndirectCartCnt = j.Value<int?>("indirectCartCnt") ?? 0,
|
|
IndirectOrderCnt = j.Value<int?>("indirectOrderCnt") ?? 0,
|
|
IndirectOrderSum = j.Value<decimal?>("indirectOrderSum") ?? 0,
|
|
NewCustomersCnt = j.Value<int?>("newCustomersCnt") ?? 0,
|
|
OrderDate = DateTime.ParseExact(j.Value<string>("orderDate"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture),
|
|
Pin = j.Value<string>("pin"),
|
|
PreorderCnt = j.Value<int?>("preorderCnt") ?? 0,
|
|
PutType = j.Value<string>("putType"),
|
|
ShopAttentionCnt = j.Value<int?>("shopAttentionCnt") ?? 0,
|
|
TotalCartCost = j.Value<decimal?>("totalCartCost") ?? 0M,
|
|
TotalOrderCVS = j.Value<decimal?>("totalOrderCVS") ?? 0M,
|
|
TotalOrderROI = j.Value<decimal?>("totalOrderROI") ?? 0M,
|
|
TotalOrderSum = j.Value<decimal?>("totalOrderSum") ?? 0M,
|
|
TotalPresaleOrderCnt = j.Value<int?>("totalPresaleOrderCnt") ?? 0,
|
|
TotalPresaleOrderSum = j.Value<decimal?>("totalPresaleOrderSum") ?? 0M,
|
|
VisitorCnt = j.Value<int?>("visitorCnt") ?? 0,
|
|
VisitPageCnt = j.Value<int?>("visitPageCnt") ?? 0,
|
|
VisitTimeAverage = j.Value<decimal?>("visitTimeAverage") ?? 0M
|
|
});
|
|
}
|
|
if (insertList.Count > 0)
|
|
fsql.Insert(insertList).ExecuteAffrows();
|
|
}
|
|
}
|
|
}
|
|
|