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

156 lines
6.7 KiB

using BBWY.Common.Http;
using BBWY.Common.Models;
using BBWY.Server.Model;
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;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using BBWY.Common.Extensions;
using BBWY.Server.Model.Db;
using System.Threading;
namespace BBWY.Server.Business.Sync
{
public class JDPopularizeSyncBusiness : BaseSyncBusiness, IDenpendency
{
public JDPopularizeSyncBusiness(RestApiService restApiService,
IOptions<GlobalConfig> options,
ILogger logger,
NLogManager nlogManager,
IFreeSql fsql,
IIdGenerator idGenerator,
TaskSchedulerManager taskSchedulerManager,
VenderBusiness venderBusiness,
YunDingBusiness yunDingBusiness) : base(restApiService,
options,
logger,
nlogManager,
fsql,
idGenerator,
taskSchedulerManager,
venderBusiness,
yunDingBusiness)
{
}
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 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), presponse.Data);
currentCount = presponse.Data?.Count() ?? 0;
}
catch (Exception ex)
{
var shopData = JsonConvert.SerializeObject(shop);
logger.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);
}
}
}
}