using FreeSql; using SBF.Common.Log; using SBF.Common.Models; using SBF.Model.Db; using SBF.Model.Dto; using SiNan.Business; using Yitter.IdGenerator; namespace SBF.Business { public class AutoTaskBusiness : BaseBusiness, IDenpendency { private VenderBusiness venderBusiness; private TaskSchedulerManager taskSchedulerManager; public AutoTaskBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, VenderBusiness venderBusiness, TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator) { this.venderBusiness = venderBusiness; this.taskSchedulerManager = taskSchedulerManager; } public void StartTrusteeshipTaskStatistics(TrusteeshipTaskMonitorRequest request) { var shopList = venderBusiness.GetShopList(request.ShopId); foreach (var shop in shopList) { Task.Factory.StartNew(() => TrusteeshipTaskStatistics(long.Parse(shop.ShopId)), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.TaskMonitorScheduler); } } private void TrusteeshipTaskStatistics(long shopId) { var inTrusteeshipTaskList = fsql.Select().Where(s => s.ShopId == shopId && s.IsEnd == false && s.IsEnabled == true) .ToList(); if (inTrusteeshipTaskList.Count() == 0) return; var mindate = inTrusteeshipTaskList.Min(s => s.StartTrusteeshipDate); var maxdate = DateTime.Now.Date.AddDays(-1); if (maxdate < mindate) return; var skuIdList = inTrusteeshipTaskList.Select(s => s.SkuId).Distinct().ToList(); #region 推广花费 var adskudailyAggregationList = fsql.Select() .Where(x => x.ShopId == shopId && x.Date >= mindate && x.Date <= maxdate && skuIdList.Contains(x.SkuId)) .ToList(x => new { x.Date, x.SkuId, x.CampaignId, x.BusinessType, x.Cost }); #endregion #region SKU商品营业额 var actualAmountList = fsql.Select() .Where(x => x.ShopId == shopId && x.Date >= mindate && x.Date <= maxdate && skuIdList.Contains(x.SkuId)) .ToList(x => new { x.Date, x.SkuId, //x.Cost, x.ActualAmount }); #endregion IList> updateList = new List>(); foreach (var task in inTrusteeshipTaskList) { task.CostInTrusteeship = adskudailyAggregationList.Where(x => x.SkuId == task.SkuId && x.CampaignId == task.CampaignId).Sum(x => x.Cost); task.ActualAmountInTrusteeship = actualAmountList.Where(x => x.SkuId == task.SkuId).Sum(x => x.ActualAmount); var update = fsql.Update(task.Id).Set(t => t.CostInTrusteeship, task.CostInTrusteeship) .Set(t => t.ActualAmountInTrusteeship, task.ActualAmountInTrusteeship); updateList.Add(update); } fsql.Transaction(() => { foreach (var update in updateList) update.ExecuteAffrows(); }); } } }