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

155 lines
6.6 KiB

3 years ago
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;
3 years ago
using System.Linq;
3 years ago
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;
3 years ago
using BBWY.Common.Extensions;
using BBWY.Server.Model.Db;
3 years ago
using System.Threading;
3 years ago
namespace BBWY.Server.Business.Sync
{
public class JDPopularizeSyncBusiness : BaseSyncBusiness, IDenpendency
{
public JDPopularizeSyncBusiness(RestApiService restApiService,
IOptions<GlobalConfig> options,
NLogManager nLogManager,
3 years ago
IFreeSql fsql,
IIdGenerator idGenerator,
TaskSchedulerManager taskSchedulerManager,
3 years ago
VenderBusiness venderBusiness,
YunDingBusiness yunDingBusiness) : base(restApiService,
3 years ago
options,
nLogManager,
3 years ago
fsql,
idGenerator,
taskSchedulerManager,
3 years ago
venderBusiness,
yunDingBusiness)
3 years ago
{
}
private void SyncShopPopularizeRecord(long shopId, JArray jArray)
{
3 years ago
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();
3 years ago
}
3 years ago
private void SyncShopPopularizeRecord(ShopResponse shop, DateTime startDate, DateTime endDate, int pageIndex, out int currentCount)
3 years ago
{
3 years ago
currentCount = 0;
3 years ago
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,
3 years ago
PageIndex = pageIndex
}, GetYunDingRequestHeader(), HttpMethod.Post);
3 years ago
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);
3 years ago
currentCount = presponse.Data?.Count() ?? 0;
3 years ago
}
catch (Exception ex)
{
var shopData = JsonConvert.SerializeObject(shop);
nLogManager.Default().Error(ex, $"SyncShopPopularizeRecord ShopData:{shopData}");
3 years ago
}
}
3 years ago
private void SyncAllShopPopularizeRecordByDate(ShopResponse shop, DateTime startDate, DateTime endDate)
{
3 years ago
var pageIndex = 1;
3 years ago
while (true)
{
3 years ago
SyncShopPopularizeRecord(shop, startDate, endDate, pageIndex, out int count);
if (count < 100)
3 years ago
break;
3 years ago
pageIndex++;
Thread.Sleep(3000);
3 years ago
}
}
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();
3 years ago
}
3 years ago
public void SyncAllShopPopularizeRecord()
{
3 years ago
var shopList = venderBusiness.GetShopList(shopId: null, Enums.Platform.);
var date = DateTime.Now.Date.AddDays(-1);
DeleteOldData(shopList, date, date);
3 years ago
foreach (var shop in shopList)
{
3 years ago
Task.Factory.StartNew(() => SyncShopPopularizeRecord(shop, date, date, 1, out _),
3 years ago
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.JDPopularizeTaskScheduler);
}
}
3 years ago
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);
}
}
3 years ago
}
}