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; namespace BBWY.Server.Business.Sync { public class JDPopularizeSyncBusiness : BaseSyncBusiness, IDenpendency { public JDPopularizeSyncBusiness(RestApiService restApiService, IOptions options, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager, VenderBusiness venderBusiness) : base(restApiService, options, logger, fsql, idGenerator, taskSchedulerManager, 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("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) { 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, }, null, 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); } catch (Exception ex) { var shopData = JsonConvert.SerializeObject(shop); logger.Error(ex, $"SyncOrder ShopData:{shopData}"); } } /// /// 俺也书 /// /// /// /// private void SyncAllShopPopularizeRecordByDate(ShopResponse shop, DateTime startDate, DateTime endDate) { var currentDate = startDate; while (true) { SyncShopPopularizeRecord(shop, currentDate, currentDate); currentDate = currentDate.AddDays(1); if (currentDate > endDate) break; } } 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), 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); } } } }