From 76f6503a4d681d7146e287b589e54b01e3f65ad0 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Sun, 26 Nov 2023 15:42:50 +0800 Subject: [PATCH] =?UTF-8?q?SKU=E8=81=9A=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AggregationController.cs | 13 +++ SiNan.Business/AggregationBusiness.cs | 96 ++++++++++++++++++- SiNan.Business/GOIBusiness.cs | 56 +++++++++++ SiNan.Model/Core/GOI/GOIByAdSku.cs | 7 ++ .../Aggregation/AdSkuAggregationRequest.cs | 16 ++++ 5 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 SiNan.Model/Core/GOI/GOIByAdSku.cs create mode 100644 SiNan.Model/Dto/Request/Aggregation/AdSkuAggregationRequest.cs diff --git a/SiNan.API/Controllers/AggregationController.cs b/SiNan.API/Controllers/AggregationController.cs index 4d72b86..5560d45 100644 --- a/SiNan.API/Controllers/AggregationController.cs +++ b/SiNan.API/Controllers/AggregationController.cs @@ -49,5 +49,18 @@ namespace SiNan.API.Controllers { aggregationBusiness.StartAdGroupAggregationTaskByCondition(request); } + + [HttpPost] + public void StartAdSkuAggregationTask() + { + aggregationBusiness.StartAdSkuAggregationTask(); + } + + + [HttpPost] + public void StartAdSkuAggregationTaskByCondition([FromBody]AdSkuAggregationRequest request) + { + aggregationBusiness.StartAdSkuAggregationTaskByCondition(request); + } } } diff --git a/SiNan.Business/AggregationBusiness.cs b/SiNan.Business/AggregationBusiness.cs index 33828a3..d02ad63 100644 --- a/SiNan.Business/AggregationBusiness.cs +++ b/SiNan.Business/AggregationBusiness.cs @@ -574,7 +574,7 @@ namespace SiNan.Business 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); + Task.Factory.StartNew(() => AdGroupAggregation(shop.ShopName, long.Parse(shop.ShopId), request.AdGroupId, request.AggregationStartDate, request.AggregationEndDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationAdGroupGOIScheduler); } } @@ -721,12 +721,100 @@ namespace SiNan.Business } #endregion - #region SKU创意维度聚合任务 + #region SKU创意/推广维度聚合任务 + public void StartAdSkuAggregationTask() + { + StartAdSkuAggregationTaskByCondition(new AdSkuAggregationRequest() + { + AggregationStartDate = DateTime.Now.Date.AddDays(-1), + AggregationEndDate = DateTime.Now.Date.AddDays(-1), + ShopId = null + }); + } - #endregion + public void StartAdSkuAggregationTaskByCondition(AdSkuAggregationRequest request) + { + var shopList = venderBusiness.GetShopList(request.ShopId); + foreach (var shop in shopList) + { + Task.Factory.StartNew(() => AdSkuAggregation(shop.ShopName, long.Parse(shop.ShopId), request.SkuId, request.AggregationStartDate, request.AggregationEndDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationCampaignGOIScheduler); + } + } + + private void AdSkuAggregation(string shopName, long shopId, string skuId, DateTime aggregationStartDate, DateTime aggregationEndDate) + { + DateTime startDate = aggregationStartDate.Date; + aggregationEndDate = aggregationEndDate.Date; + try + { + while (startDate <= aggregationEndDate) + { + Console.WriteLine($"{DateTime.Now} {shopName} SKU聚合 聚合日期{startDate}"); + AdSkuAggregation(shopName, shopId, skuId, startDate, startDate == aggregationEndDate); + startDate = startDate.AddDays(1); + } + } + catch (Exception ex) + { + nLogManager.Default().Error(ex.Message, $"ShopId {shopId} AdGroup聚合失败"); + } + } - #region SKU智能投放维度聚合任务 + private void AdSkuAggregation(string shopName, long shopId, string skuId, 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 adSkuList = fsql.Select().Where(c => c.ShopId == shopId && c.Date == aggregationDate) + .WhereIf(skuId != null, c => c.Sku == skuId) + //.GroupBy(c => new { c.Sku, c.BusinessType }) + .ToList(); + + var adSkuIdList = adSkuList.Select(j => j.Sku).Distinct().ToArray(); + if (adSkuIdList.Count() == 0) + return; + List insertAggregationAdSkuDailyList = new List(); + var aggregationDate_PopularizeLevelList = goiBusiness.CalculationAdSkuLevelGOI(adSkuIdList, startDate_aggregationDate, endDate_aggregationDate); + + var adSkuIndex = 0; + foreach (var adSku in adSkuList) + { + adSkuIndex++; + Console.WriteLine($"{DateTime.Now} {shopName} SKU聚合 {adSkuIndex}/{adSkuIdList.Count()}"); + + #region 处理计划每日聚合 + var adSkuGoi_AggregationDate_PopularizeLevel = aggregationDate_PopularizeLevelList.FirstOrDefault(x => x.Sku == adSku.Sku && x.BusinessType == adSku.BusinessType); + + if (adSkuGoi_AggregationDate_PopularizeLevel != null) + { + var adSkuDailyAggregation = new AggregationJDPopularizeAdSkuDaily() + { + Id = idGenerator.NewLong(), + CreateTime = DateTime.Now, + Date = aggregationDate, + ShopId = shopId, + Cost = adSkuGoi_AggregationDate_PopularizeLevel?.Cost ?? 0M, + PopularizeLevelProfit = adSkuGoi_AggregationDate_PopularizeLevel?.Profit ?? 0M, + PopularizeLevelGOI = adSkuGoi_AggregationDate_PopularizeLevel?.GOI ?? 0M, + AdGroupId = adSku.AdGroupId, + CampaignId = adSku.CampaignId, + BusinessType = adSku.BusinessType + }; + insertAggregationAdSkuDailyList.Add(adSkuDailyAggregation); + } + #endregion + } + + fsql.Transaction(() => + { + fsql.Delete().Where(s => s.Date == aggregationDate && adSkuIdList.Contains(s.SkuId)).ExecuteAffrows(); + fsql.Insert(insertAggregationAdSkuDailyList).ExecuteAffrows(); + + }); + } #endregion } } diff --git a/SiNan.Business/GOIBusiness.cs b/SiNan.Business/GOIBusiness.cs index 200d232..4620f20 100644 --- a/SiNan.Business/GOIBusiness.cs +++ b/SiNan.Business/GOIBusiness.cs @@ -197,6 +197,62 @@ namespace SiNan.Business return list; } + public IList CalculationAdSkuLevelGOI(IList skuIdList, DateTime startDate, DateTime endDate) + { + var costs = fsql.Select() + .Where(jas => skuIdList.Contains(jas.Sku) && jas.Date >= startDate && jas.Date <= endDate) + .GroupBy(jas => new { jas.Sku, jas.BusinessType }) + .ToList(g => new + { + Cost = g.Sum(g.Value.Cost), + Sku = g.Key.Sku, + BusinessType = g.Key.BusinessType + }); + + var profits = fsql.Select() + .InnerJoin((jr, ocd, o) => jr.OrderId == ocd.OrderId) + .InnerJoin((jr, ocd, o) => jr.OrderId == o.Id) + .Where((jr, ocd, o) => skuIdList.Contains(jr.PopularizeSku) && + jr.CookieTime >= startDate && jr.CookieTime <= endDate && + ocd.IsEnabled == true && + o.OrderState != Enums.OrderState.已取消) + .GroupBy((jr, ocd, o) => new { jr.PopularizeSku, jr.BusinessType }) + .ToList(g => new + { + Profit = g.Sum(g.Value.Item2.SkuGrossProfit), + Sku = g.Key.PopularizeSku, + BusinessType = g.Key.BusinessType + }); + + IList list = new List(); + foreach (var skuId in skuIdList) + { + { + var cost = costs.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 2)?.Cost ?? 0M; + var profit = profits.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 2)?.Profit ?? 0M; + + if (cost != 0 || profit != 0) + { + var adskuGoi = new GOIByAdSku() { Sku = skuId, Cost = cost, Profit = profit, BusinessType = 2 }; + list.Add(adskuGoi); + } + + } + + { + var cost = costs.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 134217728)?.Cost ?? 0M; + var profit = profits.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 134217728)?.Profit ?? 0M; + if (cost != 0 || profit != 0) + { + var adskuGoi = new GOIByAdSku() { Sku = skuId, Cost = cost, Profit = profit, BusinessType = 134217728 }; + list.Add(adskuGoi); + } + } + + } + return list; + } + //public ListResponse QueryProductGOI(QueryProductGOIRequest request) //{ // if (request.ShopId == 0) diff --git a/SiNan.Model/Core/GOI/GOIByAdSku.cs b/SiNan.Model/Core/GOI/GOIByAdSku.cs new file mode 100644 index 0000000..2e4ba74 --- /dev/null +++ b/SiNan.Model/Core/GOI/GOIByAdSku.cs @@ -0,0 +1,7 @@ +namespace SiNan.Model.Core +{ + public class GOIByAdSku : GOIBySku + { + public int BusinessType { get; set; } + } +} diff --git a/SiNan.Model/Dto/Request/Aggregation/AdSkuAggregationRequest.cs b/SiNan.Model/Dto/Request/Aggregation/AdSkuAggregationRequest.cs new file mode 100644 index 0000000..277e6b4 --- /dev/null +++ b/SiNan.Model/Dto/Request/Aggregation/AdSkuAggregationRequest.cs @@ -0,0 +1,16 @@ +namespace SiNan.Model.Dto +{ + public class AdSkuAggregationRequest + { + /// + /// ShopId可空 + /// + public long? ShopId { get; set; } + + public DateTime AggregationStartDate { get; set; } + + public DateTime AggregationEndDate { get; set; } + + public string SkuId { get; set; } + } +}