diff --git a/SiNan.API/Controllers/AggregationController.cs b/SiNan.API/Controllers/AggregationController.cs new file mode 100644 index 0000000..4d72b86 --- /dev/null +++ b/SiNan.API/Controllers/AggregationController.cs @@ -0,0 +1,53 @@ +using Microsoft.AspNetCore.Mvc; +using SiNan.Business; +using SiNan.Model.Dto; + +namespace SiNan.API.Controllers +{ + + public class AggregationController : BaseApiController + { + private AggregationBusiness aggregationBusiness; + + public AggregationController(IHttpContextAccessor httpContextAccessor, AggregationBusiness aggregationBusiness) : base(httpContextAccessor) + { + this.aggregationBusiness = aggregationBusiness; + } + + [HttpPost] + public void StartSpuAggregationTask() + { + aggregationBusiness.StartSpuAggregationTask(); + } + + [HttpPost] + public void StartSpuAggregationTaskByCondition([FromBody]SpuAggregationRequest request) + { + aggregationBusiness.StartSpuAggregationTaskByCondition(request); + } + + [HttpPost] + public void StartCampaignAggregationTask() + { + aggregationBusiness.StartCampaignAggregationTask(); + } + + [HttpPost] + public void StartCampaginAggregationTaskByCondition([FromBody]CampaignAggregationRequest request) + { + aggregationBusiness.StartCampaginAggregationTaskByCondition(request); + } + + [HttpPost] + public void StartAdGroupAggregationTask() + { + aggregationBusiness.StartAdGroupAggregationTask(); + } + + [HttpPost] + public void StartAdGroupAggregationTaskByCondition([FromBody]AdGroupAggregationRequest request) + { + aggregationBusiness.StartAdGroupAggregationTaskByCondition(request); + } + } +} diff --git a/SiNan.Business/AggregationBusiness.cs b/SiNan.Business/AggregationBusiness.cs index bf057d7..278a089 100644 --- a/SiNan.Business/AggregationBusiness.cs +++ b/SiNan.Business/AggregationBusiness.cs @@ -31,7 +31,8 @@ namespace SiNan.Business { StartSpuAggregationTaskByCondition(new SpuAggregationRequest() { - AggregationDate = DateTime.Now.Date.AddDays(-1), + AggregationStartDate = DateTime.Now.Date.AddDays(-1), + AggregationEndDate = DateTime.Now.Date.AddDays(-1), ShopId = null, Spu = string.Empty }); @@ -42,11 +43,30 @@ namespace SiNan.Business var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { - Task.Factory.StartNew(() => SpuAggregation(long.Parse(shop.ShopId), request.Spu, request.AggregationDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationSpuGOIScheduler); + Task.Factory.StartNew(() => SpuAggregation(shop.ShopName, long.Parse(shop.ShopId), request.Spu, request.AggregationStartDate, request.AggregationEndDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationSpuGOIScheduler); } } - private void SpuAggregation(long shopId, string querySpu, DateTime aggregationDate) + private void SpuAggregation(string shopName, long shopId, string querySpu, DateTime aggregationStartDate, DateTime aggregationEndDate) + { + DateTime startDate = aggregationStartDate.Date; + aggregationEndDate = aggregationEndDate.Date; + try + { + while (startDate <= aggregationEndDate) + { + Console.WriteLine($"{DateTime.Now} {shopName} SPU聚合 聚合日期{startDate}"); + SpuAggregation(shopName, shopId, querySpu, startDate, startDate == aggregationEndDate); + startDate = startDate.AddDays(1); + } + } + catch (Exception ex) + { + nLogManager.Default().Error(ex.Message, $"ShopId {shopId} SPU聚合失败"); + } + } + + private void SpuAggregation(string shopName, long shopId, string querySpu, DateTime aggregationDate, bool isLastDate) { aggregationDate = aggregationDate.Date; var startDate_aggregationDate = aggregationDate; @@ -83,8 +103,13 @@ namespace SiNan.Business group s by num / 10 into g select g.ToArray(); //10个spu为一组 + var spuGroupCount = spuGroups.Count(); + var spuGroupIndex = 0; + foreach (var spuGroup in spuGroups) { + spuGroupIndex++; + Console.WriteLine($"{DateTime.Now} {shopName} SPU聚合 {spuGroupIndex}/{spuGroupCount}"); var currentGroupSkuList = skuList.Where(s => spuGroup.Contains(s.ProductId)).ToList(); var currentGroupSkuIdList = currentGroupSkuList.Select(ps => ps.Id).ToList(); @@ -103,8 +128,10 @@ namespace SiNan.Business IList recent30d_PopularizeLevelList = null; - if ((DateTime.Now.Date - startDate_aggregationDate).TotalDays <= 31) + if (isLastDate && (DateTime.Now.Date - startDate_aggregationDate).TotalDays <= 31) { + Console.WriteLine($"{DateTime.Now} {shopName} SPU聚合 最后最日且小于31天,聚合近7天和近30天数据"); + var startDate_Recent7day = DateTime.Now.Date.AddDays(-7); var endDate_Recent7day = DateTime.Now.Date.AddSeconds(-1); var startDate_Recent30day = DateTime.Now.Date.AddDays(-30); @@ -350,7 +377,8 @@ namespace SiNan.Business { StartCampaginAggregationTaskByCondition(new CampaignAggregationRequest() { - AggregationDate = DateTime.Now.Date.AddDays(-1), + AggregationStartDate = DateTime.Now.Date.AddDays(-1), + AggregationEndDate = DateTime.Now.Date.AddDays(-1), ShopId = null, CampaignId = null }); @@ -361,11 +389,30 @@ namespace SiNan.Business var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { - Task.Factory.StartNew(() => CampaignAggregation(long.Parse(shop.ShopId), request.CampaignId, request.AggregationDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); + Task.Factory.StartNew(() => CampaignAggregation(shop.ShopName, long.Parse(shop.ShopId), request.CampaignId, request.AggregationStartDate, request.AggregationEndDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); } } - private void CampaignAggregation(long shopId, long? campaignId, DateTime aggregationDate) + private void CampaignAggregation(string shopName, long shopId, long? campaignId, DateTime aggregationStartDate, DateTime aggregationEndDate) + { + DateTime startDate = aggregationStartDate.Date; + aggregationEndDate = aggregationEndDate.Date; + try + { + while (startDate <= aggregationEndDate) + { + Console.WriteLine($"{DateTime.Now} {shopName} 计划聚合 聚合日期{startDate}"); + CampaignAggregation(shopName, shopId, campaignId, startDate, startDate == aggregationEndDate); + startDate = startDate.AddDays(1); + } + } + catch (Exception ex) + { + nLogManager.Default().Error(ex.Message, $"ShopId {shopId} Campaign聚合失败"); + } + } + + private void CampaignAggregation(string shopName, long shopId, long? campaignId, DateTime aggregationDate, bool isLastDate) { aggregationDate = aggregationDate.Date; var startDate_aggregationDate = aggregationDate; @@ -391,7 +438,7 @@ namespace SiNan.Business IList recent7d_PopularizeLevelList = null; IList recent30d_PopularizeLevelList = null; - if ((DateTime.Now.Date - startDate_aggregationDate).TotalDays <= 31) + if (isLastDate && (DateTime.Now.Date - startDate_aggregationDate).TotalDays <= 31) { var startDate_Recent7day = DateTime.Now.Date.AddDays(-7); var endDate_Recent7day = DateTime.Now.Date.AddSeconds(-1); @@ -402,8 +449,12 @@ namespace SiNan.Business recent30d_PopularizeLevelList = goiBusiness.CalculationCampaignLevelGOI(campaignIdList, startDate_Recent30day, endDate_Recent30day); } + var campaignIndex = 0; foreach (var campaign in campaignList) { + campaignIndex++; + Console.WriteLine($"{DateTime.Now} {shopName} 计划聚合 {campaignIndex}/{campaignList.Count()}"); + #region 处理计划每日聚合 var campaginGoi_AggregationDate_PopularizeLevel = aggregationDate_PopularizeLevelList.FirstOrDefault(x => x.LevelId == campaign.CampaignId); if (campaginGoi_AggregationDate_PopularizeLevel != null) @@ -491,7 +542,8 @@ namespace SiNan.Business { StartAdGroupAggregationTaskByCondition(new AdGroupAggregationRequest() { - AggregationDate = DateTime.Now.Date.AddDays(-1), + AggregationStartDate = DateTime.Now.Date.AddDays(-1), + AggregationEndDate = DateTime.Now.Date.AddDays(-1), ShopId = null, AdGroupId = null }); @@ -502,11 +554,30 @@ namespace SiNan.Business var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { - Task.Factory.StartNew(() => AdGroupAggregation(long.Parse(shop.ShopId), request.AdGroupId, request.AggregationDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); + Task.Factory.StartNew(() => AdGroupAggregation(shop.ShopName, long.Parse(shop.ShopId), request.AdGroupId, request.AggregationStartDate, request.AggregationEndDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); } } - private void AdGroupAggregation(long shopId, long? adGroupId, DateTime aggregationDate) + private void AdGroupAggregation(string shopName, long shopId, long? adGroupId, DateTime aggregationStartDate, DateTime aggregationEndDate) + { + DateTime startDate = aggregationStartDate.Date; + aggregationEndDate = aggregationEndDate.Date; + try + { + while (startDate <= aggregationEndDate) + { + Console.WriteLine($"{DateTime.Now} {shopName} 单元聚合 聚合日期{startDate}"); + AdGroupAggregation(shopName, shopId, adGroupId, startDate, startDate == aggregationEndDate); + startDate = startDate.AddDays(1); + } + } + catch (Exception ex) + { + nLogManager.Default().Error(ex.Message, $"ShopId {shopId} AdGroup聚合失败"); + } + } + + private void AdGroupAggregation(string shopName, long shopId, long? adGroupId, DateTime aggregationDate, bool isLastDate) { aggregationDate = aggregationDate.Date; var startDate_aggregationDate = aggregationDate; @@ -532,7 +603,7 @@ namespace SiNan.Business IList recent7d_PopularizeLevelList = null; IList recent30d_PopularizeLevelList = null; - if ((DateTime.Now.Date - startDate_aggregationDate).TotalDays <= 31) + if (isLastDate && (DateTime.Now.Date - startDate_aggregationDate).TotalDays <= 31) { var startDate_Recent7day = DateTime.Now.Date.AddDays(-7); var endDate_Recent7day = DateTime.Now.Date.AddSeconds(-1); @@ -543,8 +614,12 @@ namespace SiNan.Business recent30d_PopularizeLevelList = goiBusiness.CalculationAdGroupLevelGOI(adGroupIdList, startDate_Recent30day, endDate_Recent30day); } + var adGroupIndex = 0; foreach (var adGroup in adGroupList) { + adGroupIndex++; + Console.WriteLine($"{DateTime.Now} {shopName} 单元聚合 {adGroupIndex}/{adGroupList.Count()}"); + #region 处理计划每日聚合 var adGroupGoi_AggregationDate_PopularizeLevel = aggregationDate_PopularizeLevelList.FirstOrDefault(x => x.LevelId == adGroup.CampaignId); if (adGroupGoi_AggregationDate_PopularizeLevel != null) diff --git a/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs b/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs index 6ff70b0..293f8e1 100644 --- a/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs +++ b/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs @@ -7,7 +7,9 @@ /// public long? ShopId { get; set; } - public DateTime AggregationDate { get; set; } + public DateTime AggregationStartDate { get; set; } + + public DateTime AggregationEndDate { get; set; } /// /// 单元Id可空 diff --git a/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs b/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs index 9eda466..857e2f3 100644 --- a/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs +++ b/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs @@ -7,7 +7,9 @@ /// public long? ShopId { get; set; } - public DateTime AggregationDate { get; set; } + public DateTime AggregationStartDate { get; set; } + + public DateTime AggregationEndDate { get; set; } /// /// 计划Id可空 diff --git a/SiNan.Model/Dto/Request/Aggregation/SpuAggregationRequest.cs b/SiNan.Model/Dto/Request/Aggregation/SpuAggregationRequest.cs index 126da2b..7287385 100644 --- a/SiNan.Model/Dto/Request/Aggregation/SpuAggregationRequest.cs +++ b/SiNan.Model/Dto/Request/Aggregation/SpuAggregationRequest.cs @@ -7,7 +7,9 @@ /// public long? ShopId { get; set; } - public DateTime AggregationDate { get; set; } + public DateTime AggregationStartDate { get; set; } + + public DateTime AggregationEndDate { get; set; } /// /// Spu可空