步步为盈
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.

120 lines
6.4 KiB

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<PlatformSDKBusiness> 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<SkuDailySalesDetail>().WhereIf(shopId != null, s => s.ShopId == shopId)
.Where(s => s.Date >= startDate && s.Date <= endDate)
.ExecuteAffrows();
List<SkuDailySalesDetail> insertSkuDailySalesDetailList = new List<SkuDailySalesDetail>();
var oskuGroups = fsql.Select<OrderSku, Order>()
.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<OrderSku, Order>()
.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<ProductSku>(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();
}
}
}
}