You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

914 lines
62 KiB

using FreeSql;
using SiNan.Common.Log;
using SiNan.Common.Models;
using SiNan.Model;
using SiNan.Model.Core;
using SiNan.Model.Core.ROI;
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;
}
private IList<ActualAmountBySku> StatisticsActualAmountBySku(IList<string> skuList, DateTime startDate, DateTime endDate)
{
var list = fsql.Select<OrderSku, Order>().InnerJoin((osku, o) => osku.OrderId == o.Id)
.Where((osku, o) => o.OrderState != Enums.OrderState. &&
o.IsGift == false &&
o.StartTime >= startDate &&
o.StartTime <= endDate &&
osku.Price > 0 &&
skuList.Contains(osku.SkuId))
.GroupBy((osku, o) => new { osku.SkuId, osku.ProductId })
.ToList(g => new ActualAmountBySku
{
ActualAmount = g.Sum(g.Value.Item1.ActualAmount * g.Value.Item1.ItemTotal),
Sku = g.Key.SkuId,
Spu = g.Key.ProductId
});
return list;
}
private IList<PopularizeAmountBySku> StatisticsPopularizeAmountBySku(IList<string> skuList, DateTime startDate, DateTime endDate)
{
var list = fsql.Select<JDOrderPopularizeRelation, Order, OrderSku>()
.InnerJoin((jr, o, osku) => jr.OrderId == o.Id)
.InnerJoin((jr, o, osku) => o.Id == osku.OrderId)
.Where((jr, o, osku) => o.OrderState != Enums.OrderState. &&
o.IsGift == false &&
jr.CookieTime >= startDate &&
jr.CookieTime <= endDate &&
osku.Price > 0 &&
skuList.Contains(osku.SkuId))
.GroupBy((jr, o, osku) => new { osku.SkuId, jr.BusinessType, jr.CampaignId })
.ToList(g => new PopularizeAmountBySku
{
ActualAmount = g.Sum(g.Value.Item3.ActualAmount * g.Value.Item3.ItemTotal),
BusinessType = g.Value.Item1.BusinessType,
Sku = g.Key.SkuId,
CampaignId = g.Key.CampaignId
});
return list;
}
#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 spuQueryStartTime = DateTime.Now.Date.AddDays(-30);
//查询最近30天有销量的spu
var saleSpuList = fsql.Select<Order, OrderSku>()
.InnerJoin((o, osku) => osku.OrderId == o.Id)
.Where((o, osku) => o.ShopId == shopId && !string.IsNullOrEmpty(osku.ProductId))
.WhereIf(!string.IsNullOrEmpty(querySpu), (o, osku) => osku.ProductId == querySpu)
.WhereIf(string.IsNullOrEmpty(querySpu), (o, osku) => o.StartTime >= spuQueryStartTime)
.OrderByDescending((o, osku) => o.StartTime)
.Distinct()
.ToList((o, osku) => osku.ProductId);
//查询最近30天有推广的spu
var popularizeSpuList = fsql.Select<JDPopularizeAdSku, ProductSku>()
.InnerJoin((jas, ps) => jas.Sku == ps.Id)
.Where((jas, ps) => jas.ShopId == shopId && jas.Date >= spuQueryStartTime)
.WhereIf(!string.IsNullOrEmpty(querySpu), (jas, ps) => ps.ProductId == querySpu)
.OrderByDescending((jas, ps) => jas.Date)
.Distinct()
.ToList((jas, ps) => ps.ProductId);
var spuIdList = saleSpuList.Union(popularizeSpuList).Distinct().ToList();
var skuList = fsql.Select<ProductSku>()
.Where(ps => spuIdList.Contains(ps.ProductId) && ps.Price > 0 && ps.State == 1)
.ToList(ps => new { ps.ProductId, ps.Id });
var skuIdList = skuList.Select(ps => ps.Id).Distinct().ToList();
//var spuIdList = fsql.Select<Product>().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<ProductSku>().Where(ps => ps.ShopId == shopId && ps.State == 1 && ps.Price > 0)
// .WhereIf(!string.IsNullOrEmpty(querySpu), ps => ps.ProductId == querySpu)
// .OrderBy(ps => ps.CreateTime)
// .ToList(ps => new
// {
// ps.ProductId,
// ps.Id
// });
var dbAggregationJDPopularizeSpuList = fsql.Select<AggregationJDPopularizeSpu>()
.Where((aspu) => aspu.ShopId == shopId)
.WhereIf(string.IsNullOrEmpty(querySpu), aspu => spuIdList.Contains(aspu.Id))
.WhereIf(!string.IsNullOrEmpty(querySpu), (aspu) => aspu.Id == querySpu)
.ToList();
var dbAggregationJDPopularizeSkuList = fsql.Select<AggregationJDPopularizeSku>()
.Where((asku) => asku.ShopId == shopId && skuIdList.Contains(asku.Id))
.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).Distinct().ToList();
List<AggregationJDPopularizeSpuDaily> insertAggregationSpuDailyList = new List<AggregationJDPopularizeSpuDaily>();
List<AggregationJDPopularizeSkuDaily> insertAggregationSkuDailyList = new List<AggregationJDPopularizeSkuDaily>();
List<AggregationJDPopularizeSpu> insertAggregationSpuList = new List<AggregationJDPopularizeSpu>();
List<AggregationJDPopularizeSku> insertAggregationSkuList = new List<AggregationJDPopularizeSku>();
List<IUpdate<AggregationJDPopularizeSpu>> updateAggregationSpuList = new List<IUpdate<AggregationJDPopularizeSpu>>();
List<IUpdate<AggregationJDPopularizeSku>> updateAggregationSkuList = new List<IUpdate<AggregationJDPopularizeSku>>();
var aggregationDate_ProductLevelList = goiBusiness.StatisticsProductLevelGOI(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_PopularizeLevelList = goiBusiness.StatisticsPopularizeLevelGOI(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_SkuActualAmountList = StatisticsActualAmountBySku(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_PopularizeLevelROIList = goiBusiness.StatisticsPopluarizeLvelROI(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_DirectPopularizeLevelROIList = goiBusiness.StatisticsPopluarizeLvelROI(currentGroupSkuIdList, startDate_aggregationDate, endDate_aggregationDate, true);
IList<GOIBySku> recent7d_ProductLevelList = null;
IList<GOIBySku> recent7d_PopularizeLevelList = null;
IList<GOIBySku> recent30d_ProductLevelList = null;
IList<GOIBySku> recent30d_PopularizeLevelList = null;
IList<ROIBySku> recent7d_PopluarizeLevelROIList = 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);
recent7d_PopluarizeLevelROIList = goiBusiness.StatisticsPopluarizeLvelROI(currentGroupSkuIdList, startDate_Recent7day, endDate_Recent7day);
}
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<GOIBySku> currentSpu_Recent7d_ProductLevelList = recent7d_ProductLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList();
IList<GOIBySku> currentSpu_Recent7d_PopularizeLevelList = recent7d_PopularizeLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList();
IList<GOIBySku> currentSpu_Recent30d_ProductLevelList = recent30d_ProductLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList();
IList<GOIBySku> currentSpu_Recent30d_PopularizeLevelList = recent30d_PopularizeLevelList?.Where(x => currentSpuSkuIdList.Contains(x.Sku)).ToList();
//var currentSpu_AggregationDate_PopularizeLevelROIList = aggregationDate_PopularizeLevelROIList.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,
ActualAmount = aggregationDate_SkuActualAmountList.Where(x => x.Spu == spuId).Sum(x => x.ActualAmount)
};
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<AggregationJDPopularizeSpu>(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 skuroi_aggregationDate_PopularizeLevelROI = aggregationDate_PopularizeLevelROIList.FirstOrDefault(x => x.Sku == skuId);
var skuroi_aggregationDate_DirectPopularizeLevelROI = aggregationDate_DirectPopularizeLevelROIList.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 skuroi_Recent7d_PopularizeLevel = recent7d_PopluarizeLevelROIList?.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,
ActualAmount = aggregationDate_SkuActualAmountList.FirstOrDefault(x => x.Sku == skuId)?.ActualAmount ?? 0M,
PopularizeAmount = skuroi_aggregationDate_PopularizeLevelROI?.Amount ?? 0M,
PopularizeLevelROI = skuroi_aggregationDate_PopularizeLevelROI?.ROI ?? 0M,
DirectPopularizeAmount = skuroi_aggregationDate_DirectPopularizeLevelROI?.Amount ?? 0M,
DirectPopularizeLevelROI = skuroi_aggregationDate_DirectPopularizeLevelROI?.ROI ?? 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,
Recent7dPopularizeLevelROI = skuroi_Recent7d_PopularizeLevel?.ROI ?? 0M
});
}
else
{
var updateSkuAggregation = fsql.Update<AggregationJDPopularizeSku>(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<AggregationJDPopularizeSpuDaily>().Where(s => s.Date == aggregationDate && spuGroup.Contains(s.ProductId)).ExecuteAffrows();
fsql.Delete<AggregationJDPopularizeSkuDaily>().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<JDPopularizeCampaign>().Where(c => c.ShopId == shopId && c.Date == aggregationDate)
.WhereIf(campaignId != null && campaignId != 0, 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<AggregationJDPopularizeCampaign>(campaignIdList).ToList();
List<AggregationJDPopularizeCampaignDaily> insertAggregationCampaignDailyList = new List<AggregationJDPopularizeCampaignDaily>();
List<AggregationJDPopularizeCampaign> insertAggregationCampaignList = new List<AggregationJDPopularizeCampaign>();
List<IUpdate<AggregationJDPopularizeCampaign>> updateAggregationCampaignList = new List<IUpdate<AggregationJDPopularizeCampaign>>();
var aggregationDate_PopularizeLevelList = goiBusiness.CalculationCampaignLevelGOI(campaignIdList, startDate_aggregationDate, endDate_aggregationDate);
IList<GOIByLevel> recent7d_PopularizeLevelList = null;
IList<GOIByLevel> 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<AggregationJDPopularizeCampaign>(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<AggregationJDPopularizeCampaignDaily>().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.AggregationAdGroupGOIScheduler);
}
}
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<JDPopularizeAdGroup>().Where(c => c.ShopId == shopId && c.Date == aggregationDate)
.WhereIf(adGroupId != null && adGroupId != 0, 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<AggregationJDPopularizeAdGroup>(adGroupIdList).ToList();
List<AggregationJDPopularizeAdGroupDaily> insertAggregationAdGroupDailyList = new List<AggregationJDPopularizeAdGroupDaily>();
List<AggregationJDPopularizeAdGroup> insertAggregationAdGroupList = new List<AggregationJDPopularizeAdGroup>();
List<IUpdate<AggregationJDPopularizeAdGroup>> updateAggregationAdGroupList = new List<IUpdate<AggregationJDPopularizeAdGroup>>();
var aggregationDate_PopularizeLevelList = goiBusiness.CalculationAdGroupLevelGOI(adGroupIdList, startDate_aggregationDate, endDate_aggregationDate);
IList<GOIByLevel> recent7d_PopularizeLevelList = null;
IList<GOIByLevel> 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.AdGroupId);
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<AggregationJDPopularizeAdGroup>(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<AggregationJDPopularizeAdGroupDaily>().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维度聚合任务
public void StartAdSkuAggregationTask()
{
StartAdSkuAggregationTaskByCondition(new AdSkuAggregationRequest()
{
AggregationStartDate = DateTime.Now.Date.AddDays(-1),
AggregationEndDate = DateTime.Now.Date.AddDays(-1),
ShopId = null
});
}
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} 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<JDPopularizeAdSku>().Where(c => c.ShopId == shopId && c.Date == aggregationDate)
.WhereIf(!string.IsNullOrEmpty(skuId), 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<AggregationJDPopularizeAdSkuDaily> insertAggregationAdSkuDailyList = new List<AggregationJDPopularizeAdSkuDaily>();
var aggregationDate_PopularizeLevelList = goiBusiness.CalculationAdSkuLevelGOI(adSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_ProductLevelList = goiBusiness.CalculationAdSkuProductLevelGOI(adSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
//var aggregationDate_SkuPopularizeAmountList = StatisticsPopularizeAmountBySku(adSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_PopularizeLevelROIList = goiBusiness.StatisticsAdSkuLvelROI(adSkuIdList, startDate_aggregationDate, endDate_aggregationDate);
var aggregationDate_DirectPopularizeLevelROIList = goiBusiness.StatisticsAdSkuLvelROI(adSkuIdList, startDate_aggregationDate, endDate_aggregationDate, true);
var adSkuIndex = 0;
foreach (var adSku in adSkuList)
{
adSkuIndex++;
Console.WriteLine($"{DateTime.Now} {shopName} SKU聚合 {adSkuIndex}/{adSkuIdList.Count()}");
#region 处理SKU每日聚合
var adSkuGoi_AggregationDate_PopularizeLevel = aggregationDate_PopularizeLevelList.FirstOrDefault(x => x.Sku == adSku.Sku &&
x.BusinessType == adSku.BusinessType &&
x.CampaignId == adSku.CampaignId);
var adSkuGoi_AggregationDate_ProductLevel = aggregationDate_ProductLevelList.FirstOrDefault(x => x.Sku == adSku.Sku && x.BusinessType == adSku.BusinessType);
//var adSkuPoplarizeAmount = aggregationDate_SkuPopularizeAmountList.FirstOrDefault(x => x.Sku == adSku.Sku && x.BusinessType == adSku.BusinessType && x.CampaignId == adSku.CampaignId);
var adSkuPopularizeROI = aggregationDate_PopularizeLevelROIList.FirstOrDefault(x => x.Sku == adSku.Sku &&
x.BusinessType == adSku.BusinessType &&
x.CampaignId == adSku.CampaignId);
var adSkuDirectPopularizeROI = aggregationDate_DirectPopularizeLevelROIList.FirstOrDefault(x => x.Sku == adSku.Sku &&
x.BusinessType == adSku.BusinessType &&
x.CampaignId == adSku.CampaignId);
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,
ProductLevelProfit = adSkuGoi_AggregationDate_ProductLevel?.Profit ?? 0M,
ProductLevelGOI = adSkuGoi_AggregationDate_ProductLevel?.GOI ?? 0M,
AdGroupId = adSku.AdGroupId,
CampaignId = adSku.CampaignId,
BusinessType = adSku.BusinessType,
SkuId = adSku.Sku,
AdId = adSku.AdId,
Clicks = adSkuGoi_AggregationDate_PopularizeLevel?.Clicks ?? 0,
PopularizeAmount = adSkuPopularizeROI?.Amount ?? 0M,
PopularizeLevelROI = adSkuPopularizeROI?.ROI ?? 0M,
DirectPopularizeAmount = adSkuDirectPopularizeROI?.Amount ?? 0M,
DirectPopularizeLevelROI = adSkuDirectPopularizeROI?.ROI ?? 0M
};
insertAggregationAdSkuDailyList.Add(adSkuDailyAggregation);
#endregion
}
fsql.Transaction(() =>
{
fsql.Delete<AggregationJDPopularizeAdSkuDaily>().Where(s => s.Date == aggregationDate && adSkuIdList.Contains(s.SkuId)).ExecuteAffrows();
fsql.Insert(insertAggregationAdSkuDailyList).ExecuteAffrows();
});
}
#endregion
}
}