using BBWY.Common.Models; using BBWY.Server.Model.Db; using System; using System.Collections.Generic; using System.Linq; using Yitter.IdGenerator; namespace BBWY.Server.Business { public class OrderSkuSaleDetailSyncBusiness : BaseBusiness, IDenpendency { public OrderSkuSaleDetailSyncBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, IEnumerable platformSDKBusinessList) : base(fsql, nLogManager, idGenerator, platformSDKBusinessList) { } public void SyncOrderSkuSaleDetail(long shopId, DateTime startDate, DateTime endDate) { StatisticsOrderSkuSaleDetail(shopId, startDate, endDate); } public void SyncAllShopOrderSkuSaleDetail(DateTime startDate, DateTime endDate) { StatisticsOrderSkuSaleDetail(null, startDate, endDate); } private void StatisticsOrderSkuSaleDetail(long? shopId, DateTime startDate, DateTime endDate) { var endTime = endDate.Date.AddDays(1).AddSeconds(-1); fsql.Delete().WhereIf(shopId != null, s => s.ShopId == shopId) .Where(s => s.Date >= startDate && s.Date <= endDate) .ExecuteAffrows(); List insertSkuDailySalesDetailList = new List(); var oskuGroups = fsql.Select() .InnerJoin((osku, o) => osku.OrderId == o.Id) .WhereIf(shopId != null, (osku, o) => o.ShopId == shopId) .Where((osku, o) => o.StartTime >= startDate && o.StartTime <= endTime) .Where((osku, o) => o.StorageType != Model.Enums.StorageType.SD) //.Where((osku, o) => o.OrderState != Model.Enums.OrderState.已取消) .GroupBy((osku, o) => new { o.Platform, o.ShopId, osku.ProductId, osku.SkuId, osku.Price, o.StartTime.Value.Date }) .ToList(g => new SkuDailySalesDetail { Platform = g.Key.Platform, ShopId = g.Key.ShopId, Spu = g.Key.ProductId, Sku = g.Key.SkuId, Date = g.Key.Date, Price = g.Key.Price, Amount = g.Sum(g.Value.Item1.Price * g.Value.Item1.ItemTotal), ItemTotal = (int)g.Sum(g.Value.Item1.ItemTotal) }); var cancelOskuGroups = fsql.Select() .InnerJoin((osku, o) => osku.OrderId == o.Id) .WhereIf(shopId != null, (osku, o) => o.ShopId == shopId) .Where((osku, o) => o.ModifyTime >= startDate && o.ModifyTime <= endTime) .Where((osku, o) => o.StorageType != Model.Enums.StorageType.SD) .Where((osku, o) => o.OrderState == Model.Enums.OrderState.已取消) .GroupBy((osku, o) => new { o.Platform, o.ShopId, osku.ProductId, osku.SkuId, osku.Price, o.ModifyTime.Value.Date }) .ToList(g => new SkuDailySalesDetail { Platform = g.Key.Platform, ShopId = g.Key.ShopId, Spu = g.Key.ProductId, Sku = g.Key.SkuId, Date = g.Key.Date, Price = g.Key.Price, Amount = 0, ItemTotal = 0, CancelItemTotal = (int)g.Sum(g.Value.Item1.ItemTotal) }); insertSkuDailySalesDetailList.AddRange(oskuGroups); foreach (var canceloSkuGroup in cancelOskuGroups) { var skuDaily = insertSkuDailySalesDetailList.FirstOrDefault(s => s.ShopId == canceloSkuGroup.ShopId && s.Date == canceloSkuGroup.Date && s.Sku == canceloSkuGroup.Sku); if (skuDaily != null) skuDaily.CancelItemTotal = canceloSkuGroup.CancelItemTotal; else insertSkuDailySalesDetailList.Add(canceloSkuGroup); } if (insertSkuDailySalesDetailList.Count() > 0) { var noSpuSkuIds = insertSkuDailySalesDetailList.Where(s => string.IsNullOrEmpty(s.Spu)).Select(s => s.Sku).Distinct().ToList(); var skuList = fsql.Select(noSpuSkuIds).ToList(ps => new { ps.ProductId, ps.Id }); foreach (var skuDaily in insertSkuDailySalesDetailList) { if (string.IsNullOrEmpty(skuDaily.Spu)) skuDaily.Spu = skuList.FirstOrDefault(ps => ps.Id == skuDaily.Sku)?.ProductId; skuDaily.Id = idGenerator.NewLong(); skuDaily.IsGift = skuDaily.Price == 0M; skuDaily.CreateTime = DateTime.Now; } fsql.Insert(insertSkuDailySalesDetailList).ExecuteAffrows(); } } } }