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.

638 lines
44 KiB

2 years ago
using FreeSql;
using SiNan.Common.Log;
using SiNan.Common.Models;
2 years ago
using SiNan.Model.Core;
2 years ago
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;
}
2 years ago
#region SPU/SKU聚合任务
2 years ago
public void StartSpuAggregationTask()
{
StartSpuAggregationTaskByCondition(new SpuAggregationRequest()
{
AggregationDate = 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(long.Parse(shop.ShopId), request.Spu, request.AggregationDate), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationSpuGOIScheduler);
}
}
2 years ago
private void SpuAggregation(long shopId, string querySpu, DateTime aggregationDate)
2 years ago
{
2 years ago
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<Product>().Where(p => p.ShopId == shopId && p.State == 8)
.WhereIf(!string.IsNullOrEmpty(querySpu), p => p.Id == querySpu)
2 years ago
.OrderBy(p => p.CreateTime)
.ToList(p => p.Id);
2 years ago
var skuList = fsql.Select<ProductSku>().Where(ps => ps.ShopId == shopId && ps.State == 1)
.WhereIf(!string.IsNullOrEmpty(querySpu), ps => ps.ProductId == querySpu)
2 years ago
.OrderBy(ps => ps.CreateTime)
.ToList(ps => new
{
ps.ProductId,
ps.Id
});
var dbAggregationJDPopularizeSpuList = fsql.Select<AggregationJDPopularizeSpu, Product>()
.InnerJoin((aspu, p) => aspu.Id == p.Id)
.Where((aspu, p) => p.ShopId == shopId && p.State == 8)
2 years ago
.WhereIf(!string.IsNullOrEmpty(querySpu), (aspu, p) => p.Id == querySpu)
.ToList();
var dbAggregationJDPopularizeSkuList = fsql.Select<AggregationJDPopularizeSku, ProductSku>()
.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)
2 years ago
.ToList();
var i = 0;
var spuGroups = from s in spuIdList
let num = i++
group s by num / 10 into g
select g.ToArray(); //10个spu为一组
foreach (var spuGroup in spuGroups)
{
var currentGroupSkuList = skuList.Where(s => spuGroup.Contains(s.ProductId)).ToList();
var currentGroupSkuIdList = currentGroupSkuList.Select(ps => ps.Id).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>();
2 years ago
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);
IList<GOIBySku> recent7d_ProductLevelList = null;
IList<GOIBySku> recent7d_PopularizeLevelList = null;
IList<GOIBySku> recent30d_ProductLevelList = null;
IList<GOIBySku> 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<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();
#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),
};
2 years ago
2 years ago
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),
};
2 years ago
2 years ago
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),
};
2 years ago
2 years ago
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);
2 years ago
2 years ago
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<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(() =>
2 years ago
{
2 years ago
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()
{
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<JDPopularizeCampaign>().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<AggregationJDPopularizeCampaign>(campaignIdList).ToList();
2 years ago
2 years ago
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 ((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);
2 years ago
}
2 years ago
#endregion
2 years ago
2 years ago
#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);
2 years ago
2 years ago
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);
}
2 years ago
#endregion
}
2 years ago
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()
{
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);
}
2 years ago
}
2 years ago
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<JDPopularizeAdGroup>().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<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 ((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<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创意维度聚合任务
#endregion
#region SKU智能投放维度聚合任务
2 years ago
#endregion
}
}