步步为盈
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.

218 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.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace BBWY.Server.Business.Sync
{
public class JDPopularizeReportFormAdLevelSyncBusiness : BaseSyncBusiness, IDenpendency
{
private char[] separator_dx = new char[] { '-' };
public JDPopularizeReportFormAdLevelSyncBusiness(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<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
}, GetYunDingRequestHeader(), HttpMethod.Post);
nLogManager.GetLogger($"创意维度-{shop.ShopName}").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}");
SyncShopPopularizeReportFormAdLevel(shop.ShopName, long.Parse(shop.ShopId), presponse.Data);
currentCount = presponse.Data?.Count() ?? 0;
}
catch (Exception ex)
{
var data = JsonConvert.SerializeObject(new { shop, startDate, endDate, pageIndex });
nLogManager.GetLogger($"创意维度-{shop.ShopName}").Error(ex, $"SyncShopPopularizeReportFormAdLevel Data:{data}");
}
}
private void SyncShopPopularizeReportFormAdLevel(string shopName, 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");
if (string.IsNullOrEmpty(adName))
continue;
var adId = j.Value<string>("adId");
var skuMatch = Regex.Match(adName, @"^(.*-)?(\d+)-(.*)$");
string sku;
if (skuMatch.Success)
sku = skuMatch.Groups[2].Value;
else
{
skuMatch = Regex.Match(adName, @"^(.*)-(\d+)$");
if (!skuMatch.Success)
{
nLogManager.GetLogger($"创意维度-{shopName}").Info($"创意名称识别失败 adId {adId} adName {adName} 名称格式错误");
continue;
}
sku = skuMatch.Groups[2].Value;
}
if (sku == adId)
{
nLogManager.GetLogger($"创意维度-{shopName}").Info($"创意名称识别失败 adId {adId} adName {adName} 提取的[sku]与创意Id相同");
continue;
}
var r0 = j["retrievalType0"];
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 = long.Parse(adId),
AdName = adName,
Date = DateTime.ParseExact(r0.Value<string>("date"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture),
Cost = r0.Value<decimal>("cost"),
Clicks = r0.Value<int?>("clicks") ?? 0,
Impressions = r0.Value<int?>("impressions") ?? 0,
TotalCartCnt = r0.Value<int?>("totalCartCnt") ?? 0,
TotalOrderCnt = r0.Value<int?>("totalOrderCnt") ?? 0,
Sku = sku,
VisitorCnt = r0.Value<int?>("visitorCnt") ?? 0,
TotalOrderSum = r0.Value<decimal>("totalOrderSum"),
TotalOrderCVS = r0.Value<decimal?>("totalOrderCVS") ?? 0M,
CouponCnt = r0.Value<int?>("couponCnt") ?? 0,
CPA = r0.Value<decimal?>("CPA") ?? 0.00M,
CPC = r0.Value<decimal?>("CPC") ?? 0.00M,
CPM = r0.Value<decimal?>("CPM") ?? 0.00M,
CTR = r0.Value<decimal?>("CTR") ?? 0.00M,
DepthPassengerCnt = r0.Value<int?>("depthPassengerCnt") ?? 0,
DirectCartCnt = r0.Value<int?>("directCartCnt") ?? 0,
DirectOrderCnt = r0.Value<int?>("directOrderCnt") ?? 0,
DirectOrderSum = r0.Value<decimal?>("directOrderSum") ?? 0M,
GoodsAttentionCnt = r0.Value<int?>("goodsAttentionCnt") ?? 0,
IndirectCartCnt = r0.Value<int?>("indirectCartCnt") ?? 0,
IndirectOrderCnt = r0.Value<int?>("indirectOrderCnt") ?? 0,
IndirectOrderSum = r0.Value<decimal?>("indirectOrderSum") ?? 0,
NewCustomersCnt = r0.Value<int?>("newCustomersCnt") ?? 0,
OrderDate = DateTime.ParseExact(r0.Value<string>("orderDate"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture),
Pin = r0.Value<string>("pin"),
PreorderCnt = r0.Value<int?>("preorderCnt") ?? 0,
ShopAttentionCnt = r0.Value<int?>("shopAttentionCnt") ?? 0,
TotalCartCost = r0.Value<decimal?>("totalCartCost") ?? 0M,
TotalOrderROI = r0.Value<decimal?>("totalOrderROI") ?? 0M,
TotalPresaleOrderCnt = r0.Value<int?>("totalPresaleOrderCnt") ?? 0,
TotalPresaleOrderSum = r0.Value<decimal?>("totalPresaleOrderSum") ?? 0M,
VisitPageCnt = r0.Value<int?>("visitPageCnt") ?? 0,
VisitTimeAverage = r0.Value<decimal?>("visitTimeAverage") ?? 0M,
AdCreativeType = r0.Value<string>("adCreativeType"),
AdCustomTitle = r0.Value<string>("adCustomTitle"),
MaterialId = r0.Value<long?>("MaterialId") ?? 0,
MaterialSize = r0.Value<string>("materialSize"),
});
//Console.WriteLine(insertList.Count());
}
if (insertList.Count > 0)
fsql.Insert(insertList).ExecuteAffrows();
}
}
}