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.
253 lines
12 KiB
253 lines
12 KiB
using AutoMapper;
|
|
using FreeSql;
|
|
using Google.Protobuf.Collections;
|
|
using SiNan.Common.Extensions;
|
|
using SiNan.Common.Log;
|
|
using SiNan.Common.Models;
|
|
using SiNan.Model;
|
|
using SiNan.Model.Core;
|
|
using SiNan.Model.Db;
|
|
using SiNan.Model.Dto;
|
|
using Yitter.IdGenerator;
|
|
|
|
namespace SiNan.Business
|
|
{
|
|
public class GOIBusiness : BaseBusiness, IDenpendency
|
|
{
|
|
private FreeSqlMultiDBManager freeSqlMultiDBManager;
|
|
public GOIBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, FreeSqlMultiDBManager freeSqlMultiDBManager) : base(fsql, nLogManager, idGenerator)
|
|
{
|
|
this.freeSqlMultiDBManager = freeSqlMultiDBManager;
|
|
}
|
|
|
|
private IList<GOIBySku> StatisticsProductLevelGOI(IList<string> skuIdList, DateTime startDate, DateTime endDate)
|
|
{
|
|
var costs = fsql.Select<JDPopularizeAdSku>()
|
|
.Where(jas => skuIdList.Contains(jas.Sku) && jas.Date >= startDate && jas.Date <= endDate)
|
|
.GroupBy(jas => jas.Sku)
|
|
.ToList(g => new
|
|
{
|
|
Cost = g.Sum(g.Value.Cost),
|
|
Sku = g.Key
|
|
});
|
|
|
|
var profits = fsql.Select<OrderCostDetail, Order>()
|
|
.InnerJoin((ocd, o) => ocd.OrderId == o.Id)
|
|
.Where((ocd, o) => skuIdList.Contains(ocd.SkuId) &&
|
|
ocd.IsEnabled &&
|
|
ocd.CreateTime >= startDate &&
|
|
ocd.CreateTime <= endDate &&
|
|
o.OrderState != Enums.OrderState.已取消)
|
|
.GroupBy((ocd, o) => ocd.SkuId)
|
|
.ToList(g => new
|
|
{
|
|
Profit = g.Sum(g.Value.Item1.SkuGrossProfit),
|
|
Sku = g.Key
|
|
});
|
|
IList<GOIBySku> list = new List<GOIBySku>();
|
|
foreach (var c in costs)
|
|
{
|
|
var skugoi = list.FirstOrDefault(x => x.Sku == c.Sku);
|
|
if (skugoi == null)
|
|
{
|
|
skugoi = new GOIBySku() { Sku = c.Sku };
|
|
list.Add(skugoi);
|
|
}
|
|
skugoi.Cost = c.Cost;
|
|
}
|
|
|
|
foreach (var p in profits)
|
|
{
|
|
var skugoi = list.FirstOrDefault(x => x.Sku == p.Sku);
|
|
if (skugoi == null)
|
|
{
|
|
skugoi = new GOIBySku() { Sku = p.Sku };
|
|
list.Add(skugoi);
|
|
}
|
|
skugoi.Profit = p.Profit;
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private IList<GOIBySku> StatisticsPopularizeLevelGOI(IList<string> skuIdList, DateTime startDate, DateTime endDate)
|
|
{
|
|
IList<GOIBySku> list = new List<GOIBySku>();
|
|
|
|
var costs = fsql.Select<JDPopularizeAdSku>()
|
|
.Where(jas => skuIdList.Contains(jas.Sku) &&
|
|
jas.Date >= startDate && jas.Date <= endDate)
|
|
.GroupBy(jas => jas.Sku)
|
|
.ToList(g => new
|
|
{
|
|
Cost = g.Sum(g.Value.Cost),
|
|
Sku = g.Key
|
|
});
|
|
|
|
var profits = fsql.Select<JDOrderPopularizeRelation, OrderCostDetail, Order>()
|
|
.InnerJoin((jr, ocd, o) => jr.OrderId == ocd.OrderId)
|
|
.InnerJoin((jr, ocd, o) => jr.OrderId == o.Id)
|
|
.Where((jr, ocd, o) => skuIdList.Contains(jr.PopularizeSku) &&
|
|
jr.CookieTime >= startDate && jr.CookieTime <= endDate &&
|
|
ocd.IsEnabled == true &&
|
|
o.OrderState != Enums.OrderState.已取消)
|
|
.GroupBy((jr, ocd, o) => jr.PopularizeSku)
|
|
.ToList(g => new
|
|
{
|
|
Profit = g.Sum(g.Value.Item2.SkuGrossProfit),
|
|
Sku = g.Key
|
|
});
|
|
|
|
foreach (var c in costs)
|
|
{
|
|
var skugoi = list.FirstOrDefault(x => x.Sku == c.Sku);
|
|
if (skugoi == null)
|
|
{
|
|
skugoi = new GOIBySku() { Sku = c.Sku };
|
|
list.Add(skugoi);
|
|
}
|
|
skugoi.Cost = c.Cost;
|
|
}
|
|
|
|
foreach (var p in profits)
|
|
{
|
|
var skugoi = list.FirstOrDefault(x => x.Sku == p.Sku);
|
|
if (skugoi == null)
|
|
{
|
|
skugoi = new GOIBySku() { Sku = p.Sku };
|
|
list.Add(skugoi);
|
|
}
|
|
skugoi.Profit = p.Profit;
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public ListResponse<ProductGOIResponse> QueryProductGOI(QueryProductGOIRequest request)
|
|
{
|
|
if (request.ShopId == 0)
|
|
throw new BusinessException("缺少店铺Id");
|
|
|
|
ISelect<ProductSku>? skuChildSelect = string.IsNullOrEmpty(request.Sku) ?
|
|
null :
|
|
fsql.Select<ProductSku>().As("ps").Where(ps => ps.ShopId == request.ShopId && ps.Id == request.Sku);
|
|
var productList = fsql.Select<Product>().Where(p => p.ShopId == request.ShopId)
|
|
.WhereIf(!string.IsNullOrEmpty(request.Spu), p => p.Id == request.Spu)
|
|
.WhereIf(skuChildSelect != null, p => skuChildSelect.Where(ps => ps.ProductId == p.Id).Any())
|
|
.Page(request.PageIndex, request.PageSize)
|
|
.Count(out var productCount)
|
|
.ToList<ProductResponse>();
|
|
if (productList.Count == 0)
|
|
return new ListResponse<ProductGOIResponse>() { ItemList = new List<ProductGOIResponse>() };
|
|
|
|
var productIdList = productList.Select(p => p.Id).ToList();
|
|
var skuList = fsql.Select<ProductSku>().Where(ps => productIdList.Contains(ps.Id)).ToList<ProductSkuResponse>();
|
|
var skuIdList = skuList.Select(s => s.Id).ToList();
|
|
|
|
var startDate_yestoday = DateTime.Now.Date.AddDays(-1);
|
|
var endDate_yestoday = DateTime.Now.Date.AddSeconds(-1);
|
|
|
|
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);
|
|
|
|
#region 商品维度
|
|
|
|
//昨天
|
|
var yestodayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_yestoday, endDate_yestoday);
|
|
|
|
//近7天
|
|
var recent7dayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_Recent7day, endDate_Recent7day);
|
|
|
|
//近30天
|
|
var recent30dayProductLevelGOIList = StatisticsProductLevelGOI(skuIdList, startDate_Recent30day, endDate_Recent30day);
|
|
|
|
#endregion
|
|
|
|
#region 推广维度
|
|
//昨天
|
|
var yestodayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_yestoday, endDate_yestoday);
|
|
|
|
//近7天
|
|
var recent7dayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_Recent7day, endDate_Recent7day);
|
|
|
|
//近30天
|
|
var recent30dayPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, startDate_Recent30day, endDate_Recent30day);
|
|
#endregion
|
|
|
|
#region 累计花费
|
|
var accumulatCosts = fsql.Select<JDPopularizeAdSku>()
|
|
.Where(x => x.ShopId == request.ShopId &&
|
|
skuIdList.Contains(x.Sku) &&
|
|
x.Date >= request.StartDate &&
|
|
x.Date <= request.EndDate)
|
|
.GroupBy(x => x.Sku)
|
|
.ToList(g => new
|
|
{
|
|
Cost = g.Sum(g.Value.Cost),
|
|
Sku = g.Key
|
|
});
|
|
#endregion
|
|
|
|
List<ProductGOIResponse> productGOIList = new List<ProductGOIResponse>();
|
|
foreach (var product in productList)
|
|
{
|
|
var productGoi = product.Map<ProductGOIResponse>();
|
|
productGOIList.Add(productGoi);
|
|
|
|
productGoi.ProductSkuGOIList = skuList.Where(ps => ps.ProductId == product.Id).Map<List<ProductSkuGOIResponse>>();
|
|
foreach (var productSku in productGoi.ProductSkuGOIList)
|
|
{
|
|
productSku.ProductGOI_Yestoday = yestodayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.ProductGOI_Recent7Day = recent7dayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.ProductGOI_Recent30Day = recent30dayProductLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.PromotionGOI_Yestoday = yestodayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.PromotionGOI_Recent7Day = recent7dayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.PromotionGOI_Recent30Day = recent30dayPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.TotalCost = accumulatCosts.FirstOrDefault(x => x.Sku == productSku.Id)?.Cost ?? 0M;
|
|
}
|
|
|
|
productGoi.ProductGOI_Yestoday = new GOIResponse()
|
|
{
|
|
Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Yestoday?.Cost ?? 0M),
|
|
Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Yestoday?.Profit ?? 0M)
|
|
};
|
|
productGoi.ProductGOI_Recent7Day = new GOIResponse()
|
|
{
|
|
Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent7Day?.Cost ?? 0M),
|
|
Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent7Day?.Profit ?? 0M)
|
|
};
|
|
productGoi.ProductGOI_Recent30Day = new GOIResponse()
|
|
{
|
|
Cost = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent30Day?.Cost ?? 0M),
|
|
Profit = productGoi.ProductSkuGOIList.Sum(x => x.ProductGOI_Recent30Day?.Profit ?? 0M)
|
|
};
|
|
|
|
productGoi.PromotionGOI_Yestoday = new GOIResponse()
|
|
{
|
|
Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Yestoday?.Cost ?? 0M),
|
|
Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Yestoday?.Profit ?? 0M)
|
|
};
|
|
productGoi.PromotionGOI_Recent7Day = new GOIResponse()
|
|
{
|
|
Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent7Day?.Cost ?? 0M),
|
|
Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent7Day?.Profit ?? 0M)
|
|
};
|
|
productGoi.PromotionGOI_Recent30Day = new GOIResponse()
|
|
{
|
|
Cost = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent30Day?.Cost ?? 0M),
|
|
Profit = productGoi.ProductSkuGOIList.Sum(x => x.PromotionGOI_Recent30Day?.Profit ?? 0M)
|
|
};
|
|
productGoi.TotalCost = productGoi.ProductSkuGOIList.Sum(x => x.TotalCost);
|
|
}
|
|
|
|
|
|
return new ListResponse<ProductGOIResponse>()
|
|
{
|
|
Count = productCount,
|
|
ItemList = productGOIList
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|