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 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("amount"), //CreateTime = DateTime.Now, Date = j.Value("createTime").StampToDateTime().Date, ItemName = j.Value("orderType"), ShopId = shopId }); var group = sourceList.GroupBy(s => new { s.Date, s.ItemName }); var insertList = new List(); 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>(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 shops, DateTime startDate, DateTime endDate) { var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList(); fsql.Delete().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); } } } }