diff --git a/SiNan.API/Controllers/GOIController.cs b/SiNan.API/Controllers/GOIController.cs index 9613491..c98ed7d 100644 --- a/SiNan.API/Controllers/GOIController.cs +++ b/SiNan.API/Controllers/GOIController.cs @@ -15,7 +15,7 @@ namespace SiNan.API.Controllers } /// - /// 产品维度分析-查询产品综合GOI + /// 产品维度分析-查询SPU综合GOI /// /// /// @@ -25,6 +25,17 @@ namespace SiNan.API.Controllers return goiBusiness.QueryProductGOI(request); } + /// + /// 产品维度分析-查询SKU综合GOI + /// + /// + /// + [HttpPost] + public ListResponse QueryProductSkuGOI([FromBody] QueryProductSkuGOIRequest request) + { + return goiBusiness.QueryProductSkuGOI(request); + } + /// /// 产品360推广分析 /// diff --git a/SiNan.Business/AggregationBusiness.cs b/SiNan.Business/AggregationBusiness.cs index baa4fda..bf057d7 100644 --- a/SiNan.Business/AggregationBusiness.cs +++ b/SiNan.Business/AggregationBusiness.cs @@ -1,6 +1,7 @@ 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; @@ -25,7 +26,7 @@ namespace SiNan.Business this.taskSchedulerManager = taskSchedulerManager; } - #region SPU聚合任务 + #region SPU/SKU聚合任务 public void StartSpuAggregationTask() { StartSpuAggregationTaskByCondition(new SpuAggregationRequest() @@ -45,16 +46,18 @@ namespace SiNan.Business } } - private void SpuAggregation(long shopId, string spu, DateTime aggregationDate) + private void SpuAggregation(long shopId, string querySpu, DateTime aggregationDate) { - var aggregationStartDate = aggregationDate.Date; - var aggregationEndDate = aggregationDate.Date.AddDays(1).AddSeconds(-1); - var spuIdList = fsql.Select().Where(p => p.ShopId == shopId && p.State == 8) - .WhereIf(!string.IsNullOrEmpty(spu), p => p.Id == spu) + 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(spu), ps => ps.ProductId == spu) + 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 { @@ -65,7 +68,13 @@ namespace SiNan.Business var dbAggregationJDPopularizeSpuList = fsql.Select() .InnerJoin((aspu, p) => aspu.Id == p.Id) .Where((aspu, p) => p.ShopId == shopId && p.State == 8) - .WhereIf(!string.IsNullOrEmpty(spu), (aspu, p) => p.Id == spu) + .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; @@ -83,24 +92,546 @@ namespace SiNan.Business List insertAggregationSkuDailyList = new List(); List insertAggregationSpuList = new List(); List insertAggregationSkuList = new List(); - IDictionary> updateAggregationSpuDictionary = new Dictionary>(); - IDictionary> updateAggregationSkuDictionary = new Dictionary>(); + 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 ((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_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), + }; - var aggregationDate_ProductLevelList = goiBusiness.StatisticsProductLevelGOI(currentGroupSkuIdList, aggregationStartDate, aggregationEndDate); - var aggregationDate_PopularizeLevelList = goiBusiness.StatisticsPopularizeLevelGOI(currentGroupSkuIdList, aggregationStartDate, aggregationEndDate); + 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); - if ((DateTime.Now.Date - aggregationStartDate).TotalDays <= 31) + 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() + { + AggregationDate = 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(long.Parse(shop.ShopId), request.CampaignId, request.AggregationDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); + } + } + + private void CampaignAggregation(long shopId, long? campaignId, DateTime aggregationDate) + { + 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 ((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); + } + + foreach (var campaign in campaignList) + { + #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 处理sku + #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() + { + AggregationDate = 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(long.Parse(shop.ShopId), request.AdGroupId, request.AggregationDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); + } } + + private void AdGroupAggregation(long shopId, long? adGroupId, DateTime aggregationDate) + { + 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 ((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); + } + + foreach (var adGroup in adGroupList) + { + #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 } } diff --git a/SiNan.Business/GOIBusiness.cs b/SiNan.Business/GOIBusiness.cs index 48c3d65..a66327e 100644 --- a/SiNan.Business/GOIBusiness.cs +++ b/SiNan.Business/GOIBusiness.cs @@ -193,157 +193,156 @@ namespace SiNan.Business return list; } + //public ListResponse QueryProductGOI(QueryProductGOIRequest request) + //{ + // if (request.ShopId == 0) + // throw new BusinessException("缺少店铺Id"); + + // if (request.PageSize > 5) + // request.PageSize = 5; + + // var productList = fsql.Select().Where(p => p.ShopId == request.ShopId) + // .WhereIf(!string.IsNullOrEmpty(request.SpuTitle), p => p.Title.Contains(request.SpuTitle)) + // .WhereIf(!string.IsNullOrEmpty(request.Spu), p => p.Id == request.Spu) + // .Where(p => fsql.Select() + // .InnerJoin((ps, jas) => ps.Id == jas.Sku) + // .WhereIf(!string.IsNullOrEmpty(request.Sku), (ps, jas) => ps.Id == request.Sku) + // .Where((ps, jas) => jas.ShopId == request.ShopId && ps.ProductId == p.Id) + // .Any()) + // .OrderByDescending(p => p.CreateTime) + // .Page(request.PageIndex, request.PageSize) + // .Count(out var productCount) + // .ToList(); + + + // if (productList.Count == 0) + // return new ListResponse() { ItemList = new List() }; + + // var productIdList = productList.Select(p => p.Id).ToList(); + // var skuList = fsql.Select().Where(ps => productIdList.Contains(ps.ProductId)).ToList(); + // var skuIdList = skuList.Select(s => s.Id).ToList(); + + // var startDate_yestoday = DateTime.Now.Date.AddDays(-1); + // var endDate_yestoday = DateTime.Now.Date.AddSeconds(-1); + + // 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); + + // #region 商品维度 + + // //昨天 + // var yestodayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_yestoday, endDate_yestoday); + + // //近7天 + // var recent7dayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_Recent7day, endDate_Recent7day); + + // //近30天 + // var recent30dayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_Recent30day, endDate_Recent30day); + + // #endregion + + // #region 推广维度 + // //昨天 + // var yestodayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_yestoday, endDate_yestoday); + + // //近7天 + // var recent7dayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_Recent7day, endDate_Recent7day); + + // //近30天 + // var recent30dayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_Recent30day, endDate_Recent30day); + // #endregion + + // #region 累计花费/累计亏损 + // var historyPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, null, null); + // #endregion + + // List productGOIList = new List(); + // foreach (var product in productList) + // { + // var productGoi = product.Map(); + // productGOIList.Add(productGoi); + + // productGoi.ProductSkuGOIList = skuList.Where(ps => ps.ProductId == product.Id).Map>(); + + // productGoi.Logo = productGoi.ProductSkuGOIList.FirstOrDefault()?.Logo; + // foreach (var productSku in productGoi.ProductSkuGOIList) + // { + // productSku.ProductGOI_Yestoday = yestodayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + // productSku.ProductGOI_Recent7Day = recent7dayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + // productSku.ProductGOI_Recent30Day = recent30dayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + // productSku.PromotionGOI_Yestoday = yestodayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + // productSku.PromotionGOI_Recent7Day = recent7dayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + // productSku.PromotionGOI_Recent30Day = recent30dayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + + // var historyPopularizeLevelGOI = historyPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); + // productSku.TotalCost = historyPopularizeLevelGOI?.Cost ?? 0M; + // productSku.TotalDeficit = (historyPopularizeLevelGOI?.Profit ?? 0M) - (historyPopularizeLevelGOI?.Cost ?? 0M); + // } + + // productGoi.ProductGOI_Yestoday = new GOIResponse() + // { + // Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Yestoday?.Cost ?? 0M), + // Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Yestoday?.Profit ?? 0M) + // }; + // productGoi.ProductGOI_Recent7Day = new GOIResponse() + // { + // Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent7Day?.Cost ?? 0M), + // Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent7Day?.Profit ?? 0M) + // }; + // productGoi.ProductGOI_Recent30Day = new GOIResponse() + // { + // Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent30Day?.Cost ?? 0M), + // Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent30Day?.Profit ?? 0M) + // }; + + // productGoi.PromotionGOI_Yestoday = new GOIResponse() + // { + // Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Yestoday?.Cost ?? 0M), + // Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Yestoday?.Profit ?? 0M) + // }; + // productGoi.PromotionGOI_Recent7Day = new GOIResponse() + // { + // Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent7Day?.Cost ?? 0M), + // Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent7Day?.Profit ?? 0M) + // }; + // productGoi.PromotionGOI_Recent30Day = new GOIResponse() + // { + // Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent30Day?.Cost ?? 0M), + // Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent30Day?.Profit ?? 0M) + // }; + + // productGoi.TotalCost = productGoi.ProductSkuGOIList.Sum(x => x.TotalCost); + // productGoi.TotalDeficit = productGoi.ProductSkuGOIList.Sum(x => x.TotalDeficit); + + // for (var i = 0; i < productGoi.ProductSkuGOIList.Count(); i++) + // { + // var productSku = productGoi.ProductSkuGOIList[i]; + // if (productSku.TotalCost == 0) + // { + // productGoi.ProductSkuGOIList.RemoveAt(i); + // i--; + // } + // } + // } + + // return new ListResponse() + // { + // Count = productCount, + // ItemList = productGOIList + // }; + //} + public ListResponse QueryProductGOI(QueryProductGOIRequest request) { - if (request.ShopId == 0) - throw new BusinessException("缺少店铺Id"); - - if (request.PageSize > 5) - request.PageSize = 5; - - //ISelect? skuChildSelect = string.IsNullOrEmpty(request.Sku) ? - // null : - // fsql.Select().As("ps").Where(ps => ps.ShopId == request.ShopId && ps.Id == request.Sku); - //var productList = fsql.Select().Where(p => p.ShopId == request.ShopId) - // .WhereIf(!string.IsNullOrEmpty(request.Spu), p => p.Id == request.Spu) - // .WhereIf(skuChildSelect != null, p => skuChildSelect.Where(ps => ps.ProductId == p.Id).Any()) - // .OrderByDescending(p => p.CreateTime) - // .Page(request.PageIndex, request.PageSize) - // .Count(out var productCount) - // .ToList(); - - var productList = fsql.Select().Where(p => p.ShopId == request.ShopId) - .WhereIf(!string.IsNullOrEmpty(request.SpuTitle), p => p.Title.Contains(request.SpuTitle)) - .WhereIf(!string.IsNullOrEmpty(request.Spu), p => p.Id == request.Spu) - .Where(p => fsql.Select() - .InnerJoin((ps, jas) => ps.Id == jas.Sku) - .WhereIf(!string.IsNullOrEmpty(request.Sku), (ps, jas) => ps.Id == request.Sku) - .Where((ps, jas) => jas.ShopId == request.ShopId && ps.ProductId == p.Id) - .Any()) - .OrderByDescending(p => p.CreateTime) - .Page(request.PageIndex, request.PageSize) - .Count(out var productCount) - .ToList(); - - - if (productList.Count == 0) - return new ListResponse() { ItemList = new List() }; - - var productIdList = productList.Select(p => p.Id).ToList(); - var skuList = fsql.Select().Where(ps => productIdList.Contains(ps.ProductId)).ToList(); - var skuIdList = skuList.Select(s => s.Id).ToList(); - - var startDate_yestoday = DateTime.Now.Date.AddDays(-1); - var endDate_yestoday = DateTime.Now.Date.AddSeconds(-1); - - 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); - - #region 商品维度 - - //昨天 - var yestodayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_yestoday, endDate_yestoday); - - //近7天 - var recent7dayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_Recent7day, endDate_Recent7day); - - //近30天 - var recent30dayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_Recent30day, endDate_Recent30day); - - #endregion - - #region 推广维度 - //昨天 - var yestodayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_yestoday, endDate_yestoday); - - //近7天 - var recent7dayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_Recent7day, endDate_Recent7day); - - //近30天 - var recent30dayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_Recent30day, endDate_Recent30day); - #endregion - - #region 累计花费/累计亏损 - var historyPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, null, null); - #endregion - - List productGOIList = new List(); - foreach (var product in productList) - { - var productGoi = product.Map(); - productGOIList.Add(productGoi); - - productGoi.ProductSkuGOIList = skuList.Where(ps => ps.ProductId == product.Id).Map>(); - - productGoi.Logo = productGoi.ProductSkuGOIList.FirstOrDefault()?.Logo; - foreach (var productSku in productGoi.ProductSkuGOIList) - { - productSku.ProductGOI_Yestoday = yestodayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - productSku.ProductGOI_Recent7Day = recent7dayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - productSku.ProductGOI_Recent30Day = recent30dayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - productSku.PromotionGOI_Yestoday = yestodayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - productSku.PromotionGOI_Recent7Day = recent7dayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - productSku.PromotionGOI_Recent30Day = recent30dayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - - var historyPopularizeLevelGOI = historyPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id); - productSku.TotalCost = historyPopularizeLevelGOI?.Cost ?? 0M; - productSku.TotalDeficit = (historyPopularizeLevelGOI?.Profit ?? 0M) - (historyPopularizeLevelGOI?.Cost ?? 0M); - } - - productGoi.ProductGOI_Yestoday = new GOIResponse() - { - Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Yestoday?.Cost ?? 0M), - Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Yestoday?.Profit ?? 0M) - }; - productGoi.ProductGOI_Recent7Day = new GOIResponse() - { - Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent7Day?.Cost ?? 0M), - Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent7Day?.Profit ?? 0M) - }; - productGoi.ProductGOI_Recent30Day = new GOIResponse() - { - Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent30Day?.Cost ?? 0M), - Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent30Day?.Profit ?? 0M) - }; - - productGoi.PromotionGOI_Yestoday = new GOIResponse() - { - Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Yestoday?.Cost ?? 0M), - Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Yestoday?.Profit ?? 0M) - }; - productGoi.PromotionGOI_Recent7Day = new GOIResponse() - { - Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent7Day?.Cost ?? 0M), - Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent7Day?.Profit ?? 0M) - }; - productGoi.PromotionGOI_Recent30Day = new GOIResponse() - { - Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent30Day?.Cost ?? 0M), - Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent30Day?.Profit ?? 0M) - }; - - productGoi.TotalCost = productGoi.ProductSkuGOIList.Sum(x => x.TotalCost); - productGoi.TotalDeficit = productGoi.ProductSkuGOIList.Sum(x => x.TotalDeficit); - - for (var i = 0; i < productGoi.ProductSkuGOIList.Count(); i++) - { - var productSku = productGoi.ProductSkuGOIList[i]; - if (productSku.TotalCost == 0) - { - productGoi.ProductSkuGOIList.RemoveAt(i); - i--; - } - } - } + return new ListResponse(); + } - return new ListResponse() - { - Count = productCount, - ItemList = productGOIList - }; + public ListResponse QueryProductSkuGOI(QueryProductSkuGOIRequest request) + { + return new ListResponse(); } public ListResponse QueryProduct360PopularizeGOI(Product360PopularizeAnalysisRequest request) diff --git a/SiNan.Business/SiNan.Business.csproj b/SiNan.Business/SiNan.Business.csproj index 5ece7a4..f1cf71f 100644 --- a/SiNan.Business/SiNan.Business.csproj +++ b/SiNan.Business/SiNan.Business.csproj @@ -7,6 +7,14 @@ True + + 1701;1702;8600 + + + + 1701;1702;8600 + + diff --git a/SiNan.Business/TaskSchedulerManager.cs b/SiNan.Business/TaskSchedulerManager.cs index 4cddd88..155e243 100644 --- a/SiNan.Business/TaskSchedulerManager.cs +++ b/SiNan.Business/TaskSchedulerManager.cs @@ -7,9 +7,15 @@ namespace SiNan.Business public LimitedConcurrencyLevelTaskScheduler AggregationSpuGOIScheduler { get; private set; } + public LimitedConcurrencyLevelTaskScheduler AggregationCampaignGOIScheduler { get; private set; } + + public LimitedConcurrencyLevelTaskScheduler AggregationAdGroupGOIScheduler { get; private set; } + public TaskSchedulerManager() { AggregationSpuGOIScheduler = new LimitedConcurrencyLevelTaskScheduler(5); + AggregationCampaignGOIScheduler = new LimitedConcurrencyLevelTaskScheduler(5); + AggregationAdGroupGOIScheduler = new LimitedConcurrencyLevelTaskScheduler(5); } } } diff --git a/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs b/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs new file mode 100644 index 0000000..6ff70b0 --- /dev/null +++ b/SiNan.Model/Dto/Request/Aggregation/AdGroupAggregationRequest.cs @@ -0,0 +1,17 @@ +namespace SiNan.Model.Dto +{ + public class AdGroupAggregationRequest + { + /// + /// ShopId可空 + /// + public long? ShopId { get; set; } + + public DateTime AggregationDate { get; set; } + + /// + /// 单元Id可空 + /// + public long? AdGroupId { get; set; } + } +} diff --git a/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs b/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs new file mode 100644 index 0000000..9eda466 --- /dev/null +++ b/SiNan.Model/Dto/Request/Aggregation/CampaignAggregationRequest.cs @@ -0,0 +1,17 @@ +namespace SiNan.Model.Dto +{ + public class CampaignAggregationRequest + { + /// + /// ShopId可空 + /// + public long? ShopId { get; set; } + + public DateTime AggregationDate { get; set; } + + /// + /// 计划Id可空 + /// + public long? CampaignId { get; set; } + } +} diff --git a/SiNan.Model/Dto/Request/GOI/QueryProductGOIRequest.cs b/SiNan.Model/Dto/Request/GOI/QueryProductGOIRequest.cs index 02a009a..26ebaf2 100644 --- a/SiNan.Model/Dto/Request/GOI/QueryProductGOIRequest.cs +++ b/SiNan.Model/Dto/Request/GOI/QueryProductGOIRequest.cs @@ -32,5 +32,5 @@ public long ShopId { get; set; } } - + } \ No newline at end of file diff --git a/SiNan.Model/Dto/Request/GOI/QueryProductSkuGOIRequest.cs b/SiNan.Model/Dto/Request/GOI/QueryProductSkuGOIRequest.cs index df86995..ccf6a80 100644 --- a/SiNan.Model/Dto/Request/GOI/QueryProductSkuGOIRequest.cs +++ b/SiNan.Model/Dto/Request/GOI/QueryProductSkuGOIRequest.cs @@ -7,5 +7,7 @@ public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } + + public long? ShopId { get; set; } } }