using FreeSql; using SiNan.Common.Log; using SiNan.Common.Models; using SiNan.Model.Core; using SiNan.Model.Db; using SiNan.Model.Dto; using Yitter.IdGenerator; namespace SiNan.Business { public class AggregationBusiness : BaseBusiness, IDenpendency { private VenderBusiness venderBusiness; private GOIBusiness goiBusiness; private TaskSchedulerManager taskSchedulerManager; public AggregationBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, VenderBusiness venderBusiness, GOIBusiness goiBusiness, TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator) { this.venderBusiness = venderBusiness; this.goiBusiness = goiBusiness; this.taskSchedulerManager = taskSchedulerManager; } #region SPU/SKU聚合任务 public void StartSpuAggregationTask() { StartSpuAggregationTaskByCondition(new SpuAggregationRequest() { AggregationStartDate = DateTime.Now.Date.AddDays(-1), AggregationEndDate = DateTime.Now.Date.AddDays(-1), ShopId = null, Spu = string.Empty }); } public void StartSpuAggregationTaskByCondition(SpuAggregationRequest request) { var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { 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(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; var endDate_aggregationDate = aggregationDate.AddDays(1).AddSeconds(-1); var IsAggregationDateEqualsYesterDay = (DateTime.Now.Date - aggregationDate).TotalDays == 1; var spuIdList = fsql.Select().Where(p => p.ShopId == shopId && p.State == 8) .WhereIf(!string.IsNullOrEmpty(querySpu), p => p.Id == querySpu) .OrderBy(p => p.CreateTime) .ToList(p => p.Id); var skuList = fsql.Select().Where(ps => ps.ShopId == shopId && ps.State == 1) .WhereIf(!string.IsNullOrEmpty(querySpu), ps => ps.ProductId == querySpu) .OrderBy(ps => ps.CreateTime) .ToList(ps => new { ps.ProductId, ps.Id }); var dbAggregationJDPopularizeSpuList = fsql.Select() .InnerJoin((aspu, p) => aspu.Id == p.Id) .Where((aspu, p) => p.ShopId == shopId && p.State == 8) .WhereIf(!string.IsNullOrEmpty(querySpu), (aspu, p) => p.Id == querySpu) .ToList(); var dbAggregationJDPopularizeSkuList = fsql.Select() .InnerJoin((asku, ps) => asku.Id == ps.Id) .Where((asku, ps) => ps.ShopId == shopId && ps.State == 1) .WhereIf(!string.IsNullOrEmpty(querySpu), (asku, ps) => ps.ProductId == querySpu) .ToList(); var i = 0; var spuGroups = (from s in spuIdList let num = i++ group s by num / 10 into g select g.ToArray()).ToList(); //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(); List insertAggregationSpuDailyList = new List(); List insertAggregationSkuDailyList = new List(); List insertAggregationSpuList = new List(); List insertAggregationSkuList = new List(); List> updateAggregationSpuList = new List>(); List> updateAggregationSkuList = new List>(); var aggregationDate_ProductLevelList = goiBusiness.StatisticsProductLevelGOI(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate); var aggregationDate_PopularizeLevelList = goiBusiness.StatisticsPopularizeLevelGOI(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate); IList recent7d_ProductLevelList = null; IList recent7d_PopularizeLevelList = null; IList recent30d_ProductLevelList = null; IList recent30d_PopularizeLevelList = null; 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); var endDate_Recent30day = DateTime.Now.Date.AddSeconds(-1); recent7d_ProductLevelList = goiBusiness.StatisticsProductLevelGOI(currentGroupSkuIdList, startDate_Recent7day, endDate_Recent7day); recent7d_PopularizeLevelList = goiBusiness.StatisticsPopularizeLevelGOI(currentGroupSkuIdList, startDate_Recent7day, endDate_Recent7day); recent30d_ProductLevelList = goiBusiness.StatisticsProductLevelGOI(currentGroupSkuIdList, startDate_Recent30day, endDate_Recent30day); ; recent30d_PopularizeLevelList = goiBusiness.StatisticsPopularizeLevelGOI(currentGroupSkuIdList, startDate_Recent30day, endDate_Recent30day); } foreach (var spuId in spuGroup) { var currentSpuSkuIdList = currentGroupSkuList.Where(ps => ps.ProductId == spuId).Select(ps => ps.Id).ToList(); var currentSpu_AggregationDate_ProductLevelList = aggregationDate_ProductLevelList.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList(); var currentSpu_AggregationDate_PopularizeLevelList = aggregationDate_PopularizeLevelList.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList(); IList currentSpu_Recent7d_ProductLevelList = recent7d_ProductLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList(); IList currentSpu_Recent7d_PopularizeLevelList = recent7d_PopularizeLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList(); IList currentSpu_Recent30d_ProductLevelList = recent30d_ProductLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList(); IList currentSpu_Recent30d_PopularizeLevelList = recent30d_PopularizeLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList(); #region 处理SPU每日聚合 var spugoi_AggregationDate_ProductLevel = new GOIBySpu() { Spu = spuId, Cost = currentSpu_AggregationDate_ProductLevelList.Sum(x => x.Cost), Profit = currentSpu_AggregationDate_ProductLevelList.Sum(x => x.Profit), }; var spugoi_AggregationDate_PopularizeLevel = new GOIBySpu() { Spu = spuId, Cost = currentSpu_AggregationDate_PopularizeLevelList.Sum(x => x.Cost), Profit = currentSpu_AggregationDate_PopularizeLevelList.Sum(x => x.Profit), }; var spuDailyAggregation = new AggregationJDPopularizeSpuDaily() { Id = idGenerator.NewLong(), CreateTime = DateTime.Now, Date = aggregationDate, ProductId = spuId, ShopId = shopId, Cost = spugoi_AggregationDate_ProductLevel?.Cost ?? 0M, ProductLevelProfit = spugoi_AggregationDate_ProductLevel?.Profit ?? 0M, ProductLevelGOI = spugoi_AggregationDate_ProductLevel?.GOI ?? 0M, PopularizeLevelProfit = spugoi_AggregationDate_PopularizeLevel?.Profit ?? 0M, PopularizeLevelGOI = spugoi_AggregationDate_PopularizeLevel?.GOI ?? 0M }; insertAggregationSpuDailyList.Add(spuDailyAggregation); #endregion #region 处理SPU聚合 GOIBySpu spugoi_Recent7d_ProductLevel = currentSpu_Recent7d_ProductLevelList == null ? null : new GOIBySpu() { Spu = spuId, Cost = currentSpu_Recent7d_ProductLevelList.Sum(x => x.Cost), Profit = currentSpu_Recent7d_ProductLevelList.Sum(x => x.Profit), }; GOIBySpu spugoi_Recent7d_PopularizeLevel = currentSpu_Recent7d_PopularizeLevelList == null ? null : new GOIBySpu() { Spu = spuId, Cost = currentSpu_Recent7d_PopularizeLevelList.Sum(x => x.Cost), Profit = currentSpu_Recent7d_PopularizeLevelList.Sum(x => x.Profit), }; GOIBySpu spugoi_Recent30d_ProductLevel = currentSpu_Recent30d_ProductLevelList == null ? null : new GOIBySpu() { Spu = spuId, Cost = currentSpu_Recent30d_ProductLevelList.Sum(x => x.Cost), Profit = currentSpu_Recent30d_ProductLevelList.Sum(x => x.Profit), }; GOIBySpu spugoi_Recent30d_PopularizeLevel = currentSpu_Recent30d_PopularizeLevelList == null ? null : new GOIBySpu() { Spu = spuId, Cost = currentSpu_Recent30d_PopularizeLevelList.Sum(x => x.Cost), Profit = currentSpu_Recent30d_PopularizeLevelList.Sum(x => x.Profit), }; if (!dbAggregationJDPopularizeSpuList.Any(x => x.Id == spuId)) { insertAggregationSpuList.Add(new AggregationJDPopularizeSpu() { Id = spuId, CreateTime = DateTime.Now, Date = DateTime.Now.Date, UpdateTime = DateTime.Now, ShopId = shopId, YestodayCost = IsAggregationDateEqualsYesterDay ? spugoi_AggregationDate_ProductLevel.Cost : 0M, YestodayProductLevelProfit = IsAggregationDateEqualsYesterDay ? spugoi_AggregationDate_ProductLevel.Profit : 0M, YestodayProductLevelGOI = IsAggregationDateEqualsYesterDay ? spugoi_AggregationDate_ProductLevel.GOI : 0M, YestodayPopularizeLevelProfit = IsAggregationDateEqualsYesterDay ? spugoi_AggregationDate_PopularizeLevel.Profit : 0M, YestodayPopularizeLevelGOI = IsAggregationDateEqualsYesterDay ? spugoi_AggregationDate_PopularizeLevel.GOI : 0M, Recent7dCost = spugoi_Recent7d_ProductLevel?.Cost ?? 0M, Recent7dProductLevelProfit = spugoi_Recent7d_ProductLevel?.Profit ?? 0M, Recent7dProductLevelGOI = spugoi_Recent7d_ProductLevel?.GOI ?? 0M, Recent7dPopularizeLevelProfit = spugoi_Recent7d_PopularizeLevel?.Profit ?? 0M, Recent7dPopularizeLevelGOI = spugoi_Recent7d_PopularizeLevel?.GOI ?? 0M, Recent30dCost = spugoi_Recent30d_ProductLevel?.Cost ?? 0M, Recent30dProductLevelProfit = spugoi_Recent30d_ProductLevel?.Profit ?? 0M, Recent30dProductLevelGOI = spugoi_Recent30d_ProductLevel?.GOI ?? 0M, Recent30dPopularizeLevelProfit = spugoi_Recent30d_PopularizeLevel?.Profit ?? 0M, Recent30dPopularizeLevelGOI = spugoi_Recent30d_PopularizeLevel?.GOI ?? 0M }); } else { var updateSpuAggregation = fsql.Update(spuId) .Set(s => s.Date, DateTime.Now.Date) .Set(s => s.UpdateTime, DateTime.Now) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayCost, spugoi_AggregationDate_ProductLevel?.Cost ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayProductLevelProfit, spugoi_AggregationDate_ProductLevel?.Profit ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayProductLevelGOI, spugoi_AggregationDate_ProductLevel?.GOI ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelProfit, spugoi_AggregationDate_PopularizeLevel?.Profit ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelGOI, spugoi_AggregationDate_PopularizeLevel?.GOI ?? 0M) .SetIf(spugoi_Recent7d_ProductLevel != null, s => s.Recent7dCost, spugoi_Recent7d_ProductLevel?.Cost ?? 0M) .SetIf(spugoi_Recent7d_ProductLevel != null, s => s.Recent7dProductLevelProfit, spugoi_Recent7d_ProductLevel?.Profit ?? 0M) .SetIf(spugoi_Recent7d_ProductLevel != null, s => s.Recent7dProductLevelGOI, spugoi_Recent7d_ProductLevel?.GOI ?? 0M) .SetIf(spugoi_Recent7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelProfit, spugoi_Recent7d_PopularizeLevel?.Profit ?? 0M) .SetIf(spugoi_Recent7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelGOI, spugoi_Recent7d_PopularizeLevel?.GOI ?? 0M) .SetIf(spugoi_Recent30d_ProductLevel != null, s => s.Recent30dCost, spugoi_Recent30d_ProductLevel?.Cost ?? 0M) .SetIf(spugoi_Recent30d_ProductLevel != null, s => s.Recent30dProductLevelProfit, spugoi_Recent30d_ProductLevel?.Profit ?? 0M) .SetIf(spugoi_Recent30d_ProductLevel != null, s => s.Recent30dProductLevelGOI, spugoi_Recent30d_ProductLevel?.GOI ?? 0M) .SetIf(spugoi_Recent30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelProfit, spugoi_Recent30d_PopularizeLevel?.Profit ?? 0M) .SetIf(spugoi_Recent30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelGOI, spugoi_Recent30d_PopularizeLevel?.GOI ?? 0M); updateAggregationSpuList.Add(updateSpuAggregation); } #endregion foreach (var skuId in currentSpuSkuIdList) { #region 处理SKU每日聚合 var skugoi_AggregationDate_ProductLevel = currentSpu_AggregationDate_ProductLevelList.FirstOrDefault(x => x.Sku == skuId); var skugoi_AggregationDate_PopularizeLevel = currentSpu_AggregationDate_PopularizeLevelList.FirstOrDefault(x => x.Sku == skuId); var skugoi_Recent7d_ProductLevel = currentSpu_Recent7d_ProductLevelList?.FirstOrDefault(x => x.Sku == skuId); var skugoi_Recent7d_PopularizeLevel = currentSpu_Recent7d_PopularizeLevelList?.FirstOrDefault(x => x.Sku == skuId); var skugoi_Recent30d_ProductLevel = currentSpu_Recent30d_ProductLevelList?.FirstOrDefault(x => x.Sku == skuId); var skugoi_Recent30d_PopularizeLevel = currentSpu_Recent30d_PopularizeLevelList?.FirstOrDefault(x => x.Sku == skuId); var skuDailyAggregation = new AggregationJDPopularizeSkuDaily() { Id = idGenerator.NewLong(), CreateTime = DateTime.Now, Date = aggregationDate, SkuId = skuId, ProductId = spuId, ShopId = shopId, Cost = skugoi_AggregationDate_ProductLevel?.Cost ?? 0M, ProductLevelProfit = skugoi_AggregationDate_ProductLevel?.Profit ?? 0M, ProductLevelGOI = skugoi_AggregationDate_ProductLevel?.GOI ?? 0M, PopularizeLevelProfit = skugoi_AggregationDate_PopularizeLevel?.Profit ?? 0M, PopularizeLevelGOI = skugoi_AggregationDate_PopularizeLevel?.GOI ?? 0M }; insertAggregationSkuDailyList.Add(skuDailyAggregation); #endregion #region 处理SKU聚合 if (!dbAggregationJDPopularizeSkuList.Any(x => x.Id == skuId)) { insertAggregationSkuList.Add(new AggregationJDPopularizeSku() { Id = skuId, ProductId = spuId, CreateTime = DateTime.Now, Date = DateTime.Now.Date, UpdateTime = DateTime.Now, ShopId = shopId, YestodayCost = IsAggregationDateEqualsYesterDay ? skugoi_AggregationDate_ProductLevel.Cost : 0M, YestodayProductLevelProfit = IsAggregationDateEqualsYesterDay ? skugoi_AggregationDate_ProductLevel.Profit : 0M, YestodayProductLevelGOI = IsAggregationDateEqualsYesterDay ? skugoi_AggregationDate_ProductLevel.GOI : 0M, YestodayPopularizeLevelProfit = IsAggregationDateEqualsYesterDay ? skugoi_AggregationDate_PopularizeLevel.Profit : 0M, YestodayPopularizeLevelGOI = IsAggregationDateEqualsYesterDay ? skugoi_AggregationDate_PopularizeLevel.GOI : 0M, Recent7dCost = skugoi_Recent7d_ProductLevel?.Cost ?? 0M, Recent7dProductLevelProfit = skugoi_Recent7d_ProductLevel?.Profit ?? 0M, Recent7dProductLevelGOI = skugoi_Recent7d_ProductLevel?.GOI ?? 0M, Recent7dPopularizeLevelProfit = skugoi_Recent7d_PopularizeLevel?.Profit ?? 0M, Recent7dPopularizeLevelGOI = skugoi_Recent7d_PopularizeLevel?.GOI ?? 0M, Recent30dCost = skugoi_Recent30d_ProductLevel?.Cost ?? 0M, Recent30dProductLevelProfit = skugoi_Recent30d_ProductLevel?.Profit ?? 0M, Recent30dProductLevelGOI = skugoi_Recent30d_ProductLevel?.GOI ?? 0M, Recent30dPopularizeLevelProfit = skugoi_Recent30d_PopularizeLevel?.Profit ?? 0M, Recent30dPopularizeLevelGOI = skugoi_Recent30d_PopularizeLevel?.GOI ?? 0M }); } else { var updateSkuAggregation = fsql.Update(skuId) .Set(s => s.Date, DateTime.Now.Date) .Set(s => s.UpdateTime, DateTime.Now) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayCost, skugoi_AggregationDate_ProductLevel?.Cost ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayProductLevelProfit, skugoi_AggregationDate_ProductLevel?.Profit ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayProductLevelGOI, skugoi_AggregationDate_ProductLevel?.GOI ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelProfit, skugoi_AggregationDate_PopularizeLevel?.Profit ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelGOI, skugoi_AggregationDate_PopularizeLevel?.GOI ?? 0M) .SetIf(skugoi_Recent7d_ProductLevel != null, s => s.Recent7dCost, skugoi_Recent7d_ProductLevel?.Cost ?? 0M) .SetIf(skugoi_Recent7d_ProductLevel != null, s => s.Recent7dProductLevelProfit, skugoi_Recent7d_ProductLevel?.Profit ?? 0M) .SetIf(skugoi_Recent7d_ProductLevel != null, s => s.Recent7dProductLevelGOI, skugoi_Recent7d_ProductLevel?.GOI ?? 0M) .SetIf(skugoi_Recent7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelProfit, skugoi_Recent7d_PopularizeLevel?.Profit ?? 0M) .SetIf(skugoi_Recent7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelGOI, skugoi_Recent7d_PopularizeLevel?.GOI ?? 0M) .SetIf(skugoi_Recent30d_ProductLevel != null, s => s.Recent30dCost, skugoi_Recent30d_ProductLevel?.Cost ?? 0M) .SetIf(skugoi_Recent30d_ProductLevel != null, s => s.Recent30dProductLevelProfit, skugoi_Recent30d_ProductLevel?.Profit ?? 0M) .SetIf(skugoi_Recent30d_ProductLevel != null, s => s.Recent30dProductLevelGOI, skugoi_Recent30d_ProductLevel?.GOI ?? 0M) .SetIf(skugoi_Recent30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelProfit, skugoi_Recent30d_PopularizeLevel?.Profit ?? 0M) .SetIf(skugoi_Recent30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelGOI, skugoi_Recent30d_PopularizeLevel?.GOI ?? 0M); updateAggregationSkuList.Add(updateSkuAggregation); } #endregion } } fsql.Transaction(() => { fsql.Delete().Where(s => s.Date == aggregationDate && spuGroup.Contains(s.ProductId)).ExecuteAffrows(); fsql.Delete().Where(s => s.Date == aggregationDate && spuGroup.Contains(s.ProductId)).ExecuteAffrows(); if (insertAggregationSpuDailyList.Count() > 0) fsql.Insert(insertAggregationSpuDailyList).ExecuteAffrows(); if (insertAggregationSkuDailyList.Count() > 0) fsql.Insert(insertAggregationSkuDailyList).ExecuteAffrows(); if (insertAggregationSpuList.Count() > 0) fsql.Insert(insertAggregationSpuList).ExecuteAffrows(); if (insertAggregationSkuList.Count() > 0) fsql.Insert(insertAggregationSkuList).ExecuteAffrows(); if (updateAggregationSpuList.Count() > 0) { foreach (var update in updateAggregationSpuList) update.ExecuteAffrows(); } if (updateAggregationSkuList.Count() > 0) { foreach (var update in updateAggregationSkuList) update.ExecuteAffrows(); } }); } } #endregion #region 计划聚合任务 public void StartCampaignAggregationTask() { StartCampaginAggregationTaskByCondition(new CampaignAggregationRequest() { AggregationStartDate = DateTime.Now.Date.AddDays(-1), AggregationEndDate = DateTime.Now.Date.AddDays(-1), ShopId = null, CampaignId = null }); } public void StartCampaginAggregationTaskByCondition(CampaignAggregationRequest request) { var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { 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(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; var endDate_aggregationDate = aggregationDate.AddDays(1).AddSeconds(-1); var IsAggregationDateEqualsYesterDay = (DateTime.Now.Date - aggregationDate).TotalDays == 1; var campaignList = fsql.Select().Where(c => c.ShopId == shopId && c.Date == aggregationDate) .WhereIf(campaignId != null, c => c.CampaignId == campaignId) //.GroupBy(c => new { c.CampaignId, c.BusinessType }) .ToList(); var campaignIdList = campaignList.Select(j => j.CampaignId).Distinct().ToArray(); if (campaignIdList.Count() == 0) return; var dbAggregationJDPopularizeCampaignList = fsql.Select(campaignIdList).ToList(); List insertAggregationCampaignDailyList = new List(); List insertAggregationCampaignList = new List(); List> updateAggregationCampaignList = new List>(); var aggregationDate_PopularizeLevelList = goiBusiness.CalculationCampaignLevelGOI(campaignIdList, startDate_aggregationDate, endDate_aggregationDate); IList recent7d_PopularizeLevelList = null; IList recent30d_PopularizeLevelList = null; 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); var startDate_Recent30day = DateTime.Now.Date.AddDays(-30); var endDate_Recent30day = DateTime.Now.Date.AddSeconds(-1); recent7d_PopularizeLevelList = goiBusiness.CalculationCampaignLevelGOI(campaignIdList, startDate_Recent7day, endDate_Recent7day); 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) { var campaignDailyAggregation = new AggregationJDPopularizeCampaignDaily() { Id = idGenerator.NewLong(), CreateTime = DateTime.Now, Date = aggregationDate, ShopId = shopId, Cost = campaginGoi_AggregationDate_PopularizeLevel?.Cost ?? 0M, PopularizeLevelProfit = campaginGoi_AggregationDate_PopularizeLevel?.Profit ?? 0M, PopularizeLevelGOI = campaginGoi_AggregationDate_PopularizeLevel?.GOI ?? 0M, BusinessType = campaign.BusinessType, CampaignId = campaign.CampaignId, CampaignName = campaign.CampaignName }; insertAggregationCampaignDailyList.Add(campaignDailyAggregation); } #endregion #region 处理计划聚合 var campaginGoi_7d_PopularizeLevel = recent7d_PopularizeLevelList?.FirstOrDefault(x => x.LevelId == campaign.CampaignId); var campaginGoi_30d_PopularizeLevel = recent30d_PopularizeLevelList?.FirstOrDefault(x => x.LevelId == campaign.CampaignId); if (!dbAggregationJDPopularizeCampaignList.Any(c => c.Id == campaign.CampaignId)) { insertAggregationCampaignList.Add(new AggregationJDPopularizeCampaign() { Id = campaign.CampaignId.Value, BusinessType = campaign.BusinessType.Value, CampaignName = campaign.CampaignName, CreateTime = DateTime.Now, Date = DateTime.Now.Date, ShopId = shopId, UpdateTime = DateTime.Now, YestodayCost = IsAggregationDateEqualsYesterDay ? campaginGoi_AggregationDate_PopularizeLevel.Cost : 0M, YestodayPopularizeLevelProfit = IsAggregationDateEqualsYesterDay ? campaginGoi_AggregationDate_PopularizeLevel.Profit : 0M, YestodayPopularizeLevelGOI = IsAggregationDateEqualsYesterDay ? campaginGoi_AggregationDate_PopularizeLevel.GOI : 0M, Recent7dCost = campaginGoi_7d_PopularizeLevel?.Cost ?? 0M, Recent7dPopularizeLevelProfit = campaginGoi_7d_PopularizeLevel?.Profit ?? 0M, Recent7dPopularizeLevelGOI = campaginGoi_7d_PopularizeLevel?.GOI ?? 0M, Recent30dCost = campaginGoi_30d_PopularizeLevel?.Cost ?? 0M, Recent30dPopularizeLevelProfit = campaginGoi_30d_PopularizeLevel?.Profit ?? 0M, Recent30dPopularizeLevelGOI = campaginGoi_30d_PopularizeLevel?.GOI ?? 0M }); } else { var updateCampaignAggregation = fsql.Update(campaign.CampaignId.Value) .Set(s => s.Date, DateTime.Now.Date) .Set(s => s.UpdateTime, DateTime.Now) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayCost, campaginGoi_AggregationDate_PopularizeLevel?.Cost ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelProfit, campaginGoi_AggregationDate_PopularizeLevel?.Profit ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelGOI, campaginGoi_AggregationDate_PopularizeLevel?.GOI ?? 0M) .SetIf(campaginGoi_7d_PopularizeLevel != null, s => s.Recent7dCost, campaginGoi_7d_PopularizeLevel?.Cost ?? 0M) .SetIf(campaginGoi_7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelProfit, campaginGoi_7d_PopularizeLevel?.Profit ?? 0M) .SetIf(campaginGoi_7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelGOI, campaginGoi_7d_PopularizeLevel?.GOI ?? 0M) .SetIf(campaginGoi_30d_PopularizeLevel != null, s => s.Recent30dCost, campaginGoi_30d_PopularizeLevel?.Cost ?? 0M) .SetIf(campaginGoi_30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelProfit, campaginGoi_30d_PopularizeLevel?.Profit ?? 0M) .SetIf(campaginGoi_30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelGOI, campaginGoi_30d_PopularizeLevel?.GOI ?? 0M); updateAggregationCampaignList.Add(updateCampaignAggregation); } #endregion } fsql.Transaction(() => { fsql.Delete().Where(s => s.Date == aggregationDate && campaignIdList.Contains(s.CampaignId)).ExecuteAffrows(); if (insertAggregationCampaignDailyList.Count() > 0) fsql.Insert(insertAggregationCampaignDailyList).ExecuteAffrows(); if (insertAggregationCampaignList.Count() > 0) fsql.Insert(insertAggregationCampaignList).ExecuteAffrows(); if (updateAggregationCampaignList.Count() > 0) { foreach (var update in updateAggregationCampaignList) update.ExecuteAffrows(); } }); } #endregion #region 单元聚合任务 public void StartAdGroupAggregationTask() { StartAdGroupAggregationTaskByCondition(new AdGroupAggregationRequest() { AggregationStartDate = DateTime.Now.Date.AddDays(-1), AggregationEndDate = DateTime.Now.Date.AddDays(-1), ShopId = null, AdGroupId = null }); } public void StartAdGroupAggregationTaskByCondition(AdGroupAggregationRequest request) { var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { 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(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; var endDate_aggregationDate = aggregationDate.AddDays(1).AddSeconds(-1); var IsAggregationDateEqualsYesterDay = (DateTime.Now.Date - aggregationDate).TotalDays == 1; var adGroupList = fsql.Select().Where(c => c.ShopId == shopId && c.Date == aggregationDate) .WhereIf(adGroupId != null, c => c.AdGroupId == adGroupId) //.GroupBy(c => new { c.CampaignId, c.BusinessType }) .ToList(); var adGroupIdList = adGroupList.Select(j => j.AdGroupId).Distinct().ToArray(); if (adGroupIdList.Count() == 0) return; var dbAggregationJDPopularizeAdGroupList = fsql.Select(adGroupIdList).ToList(); List insertAggregationAdGroupDailyList = new List(); List insertAggregationAdGroupList = new List(); List> updateAggregationAdGroupList = new List>(); var aggregationDate_PopularizeLevelList = goiBusiness.CalculationAdGroupLevelGOI(adGroupIdList, startDate_aggregationDate, endDate_aggregationDate); IList recent7d_PopularizeLevelList = null; IList recent30d_PopularizeLevelList = null; 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); var startDate_Recent30day = DateTime.Now.Date.AddDays(-30); var endDate_Recent30day = DateTime.Now.Date.AddSeconds(-1); recent7d_PopularizeLevelList = goiBusiness.CalculationAdGroupLevelGOI(adGroupIdList, startDate_Recent7day, endDate_Recent7day); 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) { var adGroupDailyAggregation = new AggregationJDPopularizeAdGroupDaily() { Id = idGenerator.NewLong(), CreateTime = DateTime.Now, Date = aggregationDate, ShopId = shopId, Cost = adGroupGoi_AggregationDate_PopularizeLevel?.Cost ?? 0M, PopularizeLevelProfit = adGroupGoi_AggregationDate_PopularizeLevel?.Profit ?? 0M, PopularizeLevelGOI = adGroupGoi_AggregationDate_PopularizeLevel?.GOI ?? 0M, AdGroupId = adGroup.AdGroupId, CampaignId = adGroup.CampaignId, AdGroupName = adGroup.AdGroupName }; insertAggregationAdGroupDailyList.Add(adGroupDailyAggregation); } #endregion #region 处理计划聚合 var adGroupGoi_7d_PopularizeLevel = recent7d_PopularizeLevelList?.FirstOrDefault(x => x.LevelId == adGroup.AdGroupId); var adGroupGoi_30d_PopularizeLevel = recent30d_PopularizeLevelList?.FirstOrDefault(x => x.LevelId == adGroup.AdGroupId); if (!dbAggregationJDPopularizeAdGroupList.Any(c => c.Id == adGroup.AdGroupId)) { insertAggregationAdGroupList.Add(new AggregationJDPopularizeAdGroup() { Id = adGroup.AdGroupId.Value, AdGroupName = adGroup.AdGroupName, CreateTime = DateTime.Now, Date = DateTime.Now.Date, ShopId = shopId, UpdateTime = DateTime.Now, YestodayCost = IsAggregationDateEqualsYesterDay ? (adGroupGoi_AggregationDate_PopularizeLevel?.Cost ?? 0M) : 0M, YestodayPopularizeLevelProfit = IsAggregationDateEqualsYesterDay ? (adGroupGoi_AggregationDate_PopularizeLevel?.Profit ?? 0M) : 0M, YestodayPopularizeLevelGOI = IsAggregationDateEqualsYesterDay ? (adGroupGoi_AggregationDate_PopularizeLevel?.GOI ?? 0M) : 0M, Recent7dCost = adGroupGoi_7d_PopularizeLevel?.Cost ?? 0M, Recent7dPopularizeLevelProfit = adGroupGoi_7d_PopularizeLevel?.Profit ?? 0M, Recent7dPopularizeLevelGOI = adGroupGoi_7d_PopularizeLevel?.GOI ?? 0M, Recent30dCost = adGroupGoi_30d_PopularizeLevel?.Cost ?? 0M, Recent30dPopularizeLevelProfit = adGroupGoi_30d_PopularizeLevel?.Profit ?? 0M, Recent30dPopularizeLevelGOI = adGroupGoi_30d_PopularizeLevel?.GOI ?? 0M }); } else { var updateAdGroupAggregation = fsql.Update(adGroup.AdGroupId.Value) .Set(s => s.Date, DateTime.Now.Date) .Set(s => s.UpdateTime, DateTime.Now) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayCost, adGroupGoi_AggregationDate_PopularizeLevel?.Cost ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelProfit, adGroupGoi_AggregationDate_PopularizeLevel?.Profit ?? 0M) .SetIf(IsAggregationDateEqualsYesterDay, s => s.YestodayPopularizeLevelGOI, adGroupGoi_AggregationDate_PopularizeLevel?.GOI ?? 0M) .SetIf(adGroupGoi_7d_PopularizeLevel != null, s => s.Recent7dCost, adGroupGoi_7d_PopularizeLevel?.Cost ?? 0M) .SetIf(adGroupGoi_7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelProfit, adGroupGoi_7d_PopularizeLevel?.Profit ?? 0M) .SetIf(adGroupGoi_7d_PopularizeLevel != null, s => s.Recent7dPopularizeLevelGOI, adGroupGoi_7d_PopularizeLevel?.GOI ?? 0M) .SetIf(adGroupGoi_30d_PopularizeLevel != null, s => s.Recent30dCost, adGroupGoi_30d_PopularizeLevel?.Cost ?? 0M) .SetIf(adGroupGoi_30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelProfit, adGroupGoi_30d_PopularizeLevel?.Profit ?? 0M) .SetIf(adGroupGoi_30d_PopularizeLevel != null, s => s.Recent30dPopularizeLevelGOI, adGroupGoi_30d_PopularizeLevel?.GOI ?? 0M); updateAggregationAdGroupList.Add(updateAdGroupAggregation); } #endregion } fsql.Transaction(() => { fsql.Delete().Where(s => s.Date == aggregationDate && adGroupIdList.Contains(s.AdGroupId)).ExecuteAffrows(); if (insertAggregationAdGroupDailyList.Count() > 0) fsql.Insert(insertAggregationAdGroupDailyList).ExecuteAffrows(); if (insertAggregationAdGroupList.Count() > 0) fsql.Insert(insertAggregationAdGroupList).ExecuteAffrows(); if (updateAggregationAdGroupList.Count() > 0) { foreach (var update in updateAggregationAdGroupList) update.ExecuteAffrows(); } }); } #endregion #region SKU创意维度聚合任务 #endregion #region SKU智能投放维度聚合任务 #endregion } }