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.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Yitter.IdGenerator; namespace BBWY.Server.Business.Sync { public class JDPopularizeReportFormAdLevelSyncBusiness : BaseSyncBusiness, IDenpendency { private char[] separator_dx = new char[] { '-' }; public JDPopularizeReportFormAdLevelSyncBusiness( NLogManager nLogManager, IFreeSql fsql, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager, VenderBusiness venderBusiness, IEnumerable platformSDKBusinessList) : base( nLogManager, fsql, idGenerator, taskSchedulerManager, platformSDKBusinessList, venderBusiness) { } 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 && s.BusinessType == 2).ExecuteAffrows(); } public void SyncAllShopPopularizeReportFormAdLevel() { 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(() => SyncShopPopularizeReportFormAdLevelByDate(shop, date, date), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.JDPopularizeTaskScheduler); } } public void SyncShopPopularizeReportFormAdLevelByDate(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(() => SyncShopPopularizeReportFormAdLevelByDate(shop, startDate, endDate), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.JDPopularizeTaskScheduler); } } private void SyncShopPopularizeReportFormAdLevelByDate(ShopResponse shop, DateTime startDate, DateTime endDate) { var pageIndex = 1; while (true) { SyncShopPopularizeReportFormAdLevel(shop, startDate, endDate, pageIndex, out int count); if (count < 100) break; pageIndex++; Thread.Sleep(2000); } } private void SyncShopPopularizeReportFormAdLevel(ShopResponse shop, DateTime startDate, DateTime endDate, int pageIndex, out int currentCount) { currentCount = 0; try { var response = GetPlatformSDKBusiness(Enums.Platform.京东).GetJDSopularizeReportFormByAdLevel(new SyncJDPopularizeReportFormRequest() { AppKey = shop.AppKey, AppSecret = shop.AppSecret, AppToken = shop.AppToken, EndDate = endDate, StartDate = startDate, Platform = shop.PlatformId, PageIndex = pageIndex, Business = 2 }); //var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId); //var httpResult = restApiService.SendRequest(relayAPIHost, "Api/PlatformSDK/GetJDSopularizeReportFormByAdLevel", new SyncJDPopularizeReportFormRequest() //{ // AppKey = shop.AppKey, // AppSecret = shop.AppSecret, // AppToken = shop.AppToken, // EndDate = endDate, // StartDate = startDate, // Platform = shop.PlatformId, // PageIndex = pageIndex, // Business = 2 //}, GetYunDingRequestHeader(), HttpMethod.Post); nLogManager.GetLogger($"创意维度-{shop.ShopName}").Info(JsonConvert.SerializeObject(response)); //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}"); SyncShopPopularizeReportFormAdLevel(shop.ShopName, long.Parse(shop.ShopId), response); currentCount = response?.Count() ?? 0; } catch (Exception ex) { var data = JsonConvert.SerializeObject(new { shop, startDate, endDate, pageIndex }); nLogManager.GetLogger($"创意维度-{shop.ShopName}").Error(ex, $"SyncShopPopularizeReportFormAdLevel Data:{data}"); } } private void SyncShopPopularizeReportFormAdLevel(string shopName, long shopId, JArray jArray) { if (jArray == null || !jArray.HasValues) return; var insertList = new List(); foreach (var j in jArray) { var adName = j.Value("adName"); if (string.IsNullOrEmpty(adName)) continue; var adId = j.Value("adId"); var skuMatch = Regex.Match(adName, @"^(.*-)?(\d+)-(.*)$"); string sku; if (skuMatch.Success) sku = skuMatch.Groups[2].Value; else { skuMatch = Regex.Match(adName, @"^(.*)-(\d+)$"); if (!skuMatch.Success) { nLogManager.GetLogger($"创意维度-{shopName}").Info($"创意名称识别失败 adId {adId} adName {adName} 名称格式错误"); continue; } sku = skuMatch.Groups[2].Value; } if (sku == adId) { nLogManager.GetLogger($"创意维度-{shopName}").Info($"创意名称识别失败 adId {adId} adName {adName} 提取的[sku]与创意Id相同"); continue; } insertList.Add(new JDPopularizeAdSku() { Id = idGenerator.NewLong(), BusinessType = 2, ShopId = shopId, CreateTime = DateTime.Now, CampaignId = j.Value("campaignId"), AdGroupId = j.Value("adGroupId"), AdId = long.Parse(adId), AdName = adName, Date = DateTime.ParseExact(j.Value("date"), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture), Cost = j["retrievalType0"].Value("cost"), Clicks = j["retrievalType0"].Value("clicks") ?? 0, Impressions = j["retrievalType0"].Value("impressions") ?? 0, TotalCartCnt = j["retrievalType0"].Value("totalCartCnt") ?? 0, TotalOrderCnt = j["retrievalType0"].Value("totalOrderCnt") ?? 0, Sku = sku, VisitorCnt = j["retrievalType0"].Value("visitorCnt") ?? 0, TotalOrderSum = j["retrievalType0"].Value("totalOrderSum") }); //Console.WriteLine(insertList.Count()); } if (insertList.Count > 0) fsql.Insert(insertList).ExecuteAffrows(); } } }