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

159 lines
7.9 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 JDPopularizeReportFormAdGroupLevelSyncBusiness : BaseSyncBusiness, IDenpendency
{
public JDPopularizeReportFormAdGroupLevelSyncBusiness(RestApiService restApiService,
IOptions<GlobalConfig> options,
3 years ago
NLogManager nLogManager,
3 years ago
ILogger logger,
IFreeSql fsql,
IIdGenerator idGenerator,
TaskSchedulerManager taskSchedulerManager,
3 years ago
VenderBusiness venderBusiness, YunDingBusiness yunDingBusiness) : base(restApiService,
3 years ago
options,
logger,
3 years ago
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();
3 years ago
fsql.Delete<JDPopularizeAdGroup>().Where(s => shopIds.Contains(s.ShopId.Value) &&
3 years ago
s.Date >= startDate && s.Date <= endDate &&
s.BusinessType == 2).ExecuteAffrows();
}
public void SyncAllShopPopularizeReportFormAdGroupLevel()
{
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(() => SyncShopPopularizeReportFormAdGroupLevelByDate(shop, date, date),
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.JDPopularizeTaskScheduler);
}
}
public void SyncShopPopularizeReportFormAdGroupLevelByDate(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(() => SyncShopPopularizeReportFormAdGroupLevelByDate(shop, startDate, endDate),
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.JDPopularizeTaskScheduler);
}
}
private void SyncShopPopularizeReportFormAdGroupLevelByDate(ShopResponse shop, DateTime startDate, DateTime endDate)
{
var pageIndex = 1;
while (true)
{
SyncShopPopularizeReportFormAdGroupLevel(shop, startDate, endDate, pageIndex, out int count);
if (count < 100)
break;
pageIndex++;
Thread.Sleep(2000);
}
}
private void SyncShopPopularizeReportFormAdGroupLevel(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/GetJDSopularizeReportFormByAdGroupLevel", 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);
3 years ago
nLogManager.GetLogger($"单元维度-{shop.ShopName}").Info(httpResult.Content);
3 years ago
if (httpResult.StatusCode != System.Net.HttpStatusCode.OK)
3 years ago
throw new Exception($"获取JD推广报表-单元维度失败 {httpResult.Content}");
3 years ago
var presponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(httpResult.Content);
if (!presponse.Success)
3 years ago
throw new Exception($"获取JD推广报表-单元维度失败 {presponse.Msg}");
3 years ago
SyncShopPopularizeReportFormAdGroupLevel(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}").Error(ex, $"SyncShopPopularizeReportFormAdGroupLevel Data:{data}");
3 years ago
}
}
private void SyncShopPopularizeReportFormAdGroupLevel(long shopId, JArray jArray)
{
if (jArray == null || !jArray.HasValues)
return;
3 years ago
var insertList = new List<JDPopularizeAdGroup>();
3 years ago
foreach (var j in jArray)
{
3 years ago
insertList.Add(new JDPopularizeAdGroup()
3 years ago
{
Id = idGenerator.NewLong(),
BusinessType = 2,
ShopId = shopId,
CreateTime = DateTime.Now,
CampaignId = j.Value<long>("campaignId"),
AdGroupId = j.Value<long>("adGroupId"),
3 years ago
AdGroupName = j.Value<string>("adGroupName"),
3 years ago
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,
3 years ago
TotalOrderCnt = j["retrievalType0"].Value<int?>("totalOrderCnt") ?? 0
3 years ago
});
}
if (insertList.Count > 0)
fsql.Insert(insertList).ExecuteAffrows();
}
}
}