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.
158 lines
6.9 KiB
158 lines
6.9 KiB
using BBWY.Common.Extensions;
|
|
using BBWY.Common.Models;
|
|
using BBWY.Server.Model;
|
|
using BBWY.Server.Model.Db;
|
|
using BBWY.Server.Model.Dto;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Yitter.IdGenerator;
|
|
|
|
namespace BBWY.Server.Business.Sync
|
|
{
|
|
public class JDPopularizeSyncBusiness : BaseSyncBusiness, IDenpendency
|
|
{
|
|
public JDPopularizeSyncBusiness(
|
|
NLogManager nLogManager,
|
|
IFreeSql fsql,
|
|
IIdGenerator idGenerator,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
VenderBusiness venderBusiness,
|
|
IEnumerable<PlatformSDKBusiness> platformSDKBusinessList) : base(
|
|
nLogManager,
|
|
fsql,
|
|
idGenerator,
|
|
taskSchedulerManager,
|
|
platformSDKBusinessList,
|
|
venderBusiness)
|
|
{
|
|
|
|
}
|
|
|
|
private void SyncShopPopularizeRecord(long shopId, JArray jArray)
|
|
{
|
|
if (jArray == null || !jArray.HasValues)
|
|
return;
|
|
var sourceList = jArray.Select(j => new Shoppopularize()
|
|
{
|
|
//Id = idGenerator.NewLong(),
|
|
Cost = j.Value<decimal>("amount"),
|
|
//CreateTime = DateTime.Now,
|
|
Date = j.Value<long>("createTime").StampToDateTime().Date,
|
|
ItemName = j.Value<string>("orderType"),
|
|
ShopId = shopId
|
|
});
|
|
var group = sourceList.GroupBy(s => new { s.Date, s.ItemName });
|
|
var insertList = new List<Shoppopularize>();
|
|
foreach (var g in group)
|
|
{
|
|
insertList.Add(new Shoppopularize()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
Cost = g.Sum(s => s.Cost),
|
|
CreateTime = DateTime.Now,
|
|
Date = g.Key.Date,
|
|
ItemName = g.Key.ItemName,
|
|
ShopId = shopId
|
|
});
|
|
}
|
|
fsql.Insert(insertList).ExecuteAffrows();
|
|
}
|
|
|
|
private void SyncShopPopularizeRecord(ShopResponse shop, DateTime startDate, DateTime endDate, int pageIndex, out int currentCount)
|
|
{
|
|
currentCount = 0;
|
|
try
|
|
{
|
|
var response = GetPlatformSDKBusiness(shop.PlatformId).GetJDShopSopularizeRecordList(
|
|
new SyncShopPopularizeRequest()
|
|
{
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
EndDate = endDate,
|
|
StartDate = startDate,
|
|
Platform = shop.PlatformId,
|
|
PageIndex = pageIndex
|
|
});
|
|
//var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId);
|
|
//var httpResult = restApiService.SendRequest(relayAPIHost, "Api/PlatformSDK/GetJDShopSopularizeRecordList", new SyncShopPopularizeRequest()
|
|
//{
|
|
// AppKey = shop.AppKey,
|
|
// AppSecret = shop.AppSecret,
|
|
// AppToken = shop.AppToken,
|
|
// EndDate = endDate,
|
|
// StartDate = startDate,
|
|
// Platform = shop.PlatformId,
|
|
// PageIndex = pageIndex
|
|
//}, GetYunDingRequestHeader(), 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}");
|
|
|
|
SyncShopPopularizeRecord(long.Parse(shop.ShopId), response);
|
|
currentCount = response?.Count() ?? 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var shopData = JsonConvert.SerializeObject(shop);
|
|
nLogManager.Default().Error(ex, $"SyncShopPopularizeRecord ShopData:{shopData}");
|
|
}
|
|
}
|
|
|
|
private void SyncAllShopPopularizeRecordByDate(ShopResponse shop, DateTime startDate, DateTime endDate)
|
|
{
|
|
var pageIndex = 1;
|
|
while (true)
|
|
{
|
|
SyncShopPopularizeRecord(shop, startDate, endDate, pageIndex, out int count);
|
|
if (count < 100)
|
|
break;
|
|
pageIndex++;
|
|
Thread.Sleep(3000);
|
|
}
|
|
}
|
|
|
|
private void DeleteOldData(IList<ShopResponse> shops, DateTime startDate, DateTime endDate)
|
|
{
|
|
var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList();
|
|
fsql.Delete<Shoppopularize>().Where(s => shopIds.Contains(s.ShopId.Value) && s.Date >= startDate && s.Date <= endDate).ExecuteAffrows();
|
|
}
|
|
|
|
public void SyncAllShopPopularizeRecord()
|
|
{
|
|
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(() => SyncShopPopularizeRecord(shop, date, date, 1, out _),
|
|
System.Threading.CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.JDPopularizeTaskScheduler);
|
|
}
|
|
}
|
|
|
|
public void SyncShopPopularizeRecordByDate(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(() => SyncAllShopPopularizeRecordByDate(shop, startDate, endDate),
|
|
System.Threading.CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.JDPopularizeTaskScheduler);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|