using FreeSql; using SiNan.Common.Log; using SiNan.Common.Models; using SiNan.Model.Db; using SiNan.Model.Dto; using Yitter.IdGenerator; namespace SiNan.Business { public class AggregationBusiness : BaseBusiness, IDenpendency { private VenderBusiness venderBusiness; private GOIBusiness goiBusiness; private TaskSchedulerManager taskSchedulerManager; public AggregationBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, VenderBusiness venderBusiness, GOIBusiness goiBusiness, TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator) { this.venderBusiness = venderBusiness; this.goiBusiness = goiBusiness; this.taskSchedulerManager = taskSchedulerManager; } #region SPU聚合任务 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); } } private void SpuAggregation(long shopId, string spu, 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) .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) .OrderBy(ps => ps.CreateTime) .ToList(ps => new { ps.ProductId, ps.Id }); var dbAggregationJDPopularizeSpuList = fsql.Select() .InnerJoin((aspu, p) => aspu.Id == p.Id) .Where((aspu, p) => p.ShopId == shopId && p.State == 8) .WhereIf(!string.IsNullOrEmpty(spu), (aspu, p) => p.Id == spu) .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 insertAggregationSpuDailyList = new List(); List insertAggregationSkuDailyList = new List(); List insertAggregationSpuList = new List(); List insertAggregationSkuList = new List(); IDictionary> updateAggregationSpuDictionary = new Dictionary>(); IDictionary> updateAggregationSkuDictionary = new Dictionary>(); var aggregationDate_ProductLevelList = goiBusiness.StatisticsProductLevelGOI(currentGroupSkuIdList, aggregationStartDate, aggregationEndDate); var aggregationDate_PopularizeLevelList = goiBusiness.StatisticsPopularizeLevelGOI(currentGroupSkuIdList, aggregationStartDate, aggregationEndDate); if ((DateTime.Now.Date - aggregationStartDate).TotalDays <= 31) { } #region 处理sku #endregion } } #endregion } }