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.
843 lines
49 KiB
843 lines
49 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;
|
|
}
|
|
|
|
public 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 &&
|
|
o.StartTime >= startDate &&
|
|
o.StartTime <= endDate &&
|
|
o.OrderState != Enums.OrderState.已取消 &&
|
|
o.IsGift == false)
|
|
.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 skuId in skuIdList)
|
|
{
|
|
var cost = costs.FirstOrDefault(x => x.Sku == skuId)?.Cost ?? 0M;
|
|
var profit = profits.FirstOrDefault(x => x.Sku == skuId)?.Profit ?? 0M;
|
|
var skugoi = new GOIBySku() { Sku = skuId, Cost = cost, Profit = profit };
|
|
list.Add(skugoi);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public 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
|
|
.WhereIf(startDate != null, jas => jas.Date >= startDate)
|
|
.WhereIf(endDate != null, jas => 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) => o.OrderState != Enums.OrderState.已取消 && o.IsGift == false && ocd.IsEnabled == true)
|
|
.WhereIf(startDate != null, (jr, ocd, o) => jr.CookieTime >= startDate)
|
|
.WhereIf(endDate != null, (jr, ocd, o) => jr.CookieTime <= endDate)
|
|
.Where((jr, ocd, o) => skuIdList.Contains(jr.PopularizeSku))
|
|
.GroupBy((jr, ocd, o) => jr.PopularizeSku)
|
|
.ToList(g => new
|
|
{
|
|
Profit = g.Sum(g.Value.Item2.SkuGrossProfit),
|
|
Sku = g.Key
|
|
});
|
|
|
|
foreach (var skuId in skuIdList)
|
|
{
|
|
var cost = costs.FirstOrDefault(x => x.Sku == skuId)?.Cost ?? 0M;
|
|
var profit = profits.FirstOrDefault(x => x.Sku == skuId)?.Profit ?? 0M;
|
|
var skugoi = new GOIBySku() { Sku = skuId, Cost = cost, Profit = profit };
|
|
list.Add(skugoi);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private GOIByShop StatisticsProductLevelGOI(long shopId, DateTime? startDate, DateTime? endDate)
|
|
{
|
|
var cost = fsql.Select<JDPopularizeAdSku>()
|
|
.Where(jas => jas.ShopId == shopId) // &&jas.Date >= startDate && jas.Date <= endDate
|
|
.WhereIf(startDate != null, jas => jas.Date >= startDate)
|
|
.WhereIf(endDate != null, jas => jas.Date <= endDate)
|
|
.Sum(jas => jas.Cost);
|
|
|
|
var profit = fsql.Select<OrderCostDetail, Order>()
|
|
.InnerJoin((ocd, o) => ocd.OrderId == o.Id)
|
|
.Where((ocd, o) => o.ShopId == shopId &&
|
|
ocd.IsEnabled &&
|
|
o.StartTime >= startDate &&
|
|
o.StartTime <= endDate &&
|
|
o.OrderState != Enums.OrderState.已取消 &&
|
|
o.IsGift == false)
|
|
.ToAggregate((ocd, o) => ocd.Sum(ocd.Key.SkuGrossProfit));
|
|
|
|
return new GOIByShop()
|
|
{
|
|
ShopId = shopId,
|
|
Cost = cost,
|
|
Profit = profit,
|
|
};
|
|
}
|
|
|
|
public IList<GOIByLevel> CalculationCampaignLevelGOI(IList<long?> campaignIdList, DateTime startDate, DateTime endDate)
|
|
{
|
|
var costs = fsql.Select<JDPopularizeCampaign>().Where(x => campaignIdList.Contains(x.CampaignId.Value) &&
|
|
x.Date >= startDate &&
|
|
x.Date <= endDate)
|
|
.GroupBy(x => x.CampaignId)
|
|
.ToList(g => new
|
|
{
|
|
Cost = g.Sum(g.Value.Cost),
|
|
CampaignId = g.Key
|
|
});
|
|
|
|
var profits = fsql.Select<JDOrderPopularizeRelation, OrderCost, Order>().InnerJoin((jr, oc, o) => jr.OrderId == oc.OrderId)
|
|
.InnerJoin((jr, oc, o) => jr.OrderId == o.Id)
|
|
.Where((jr, oc, o) => campaignIdList.Contains(jr.CampaignId.Value) &&
|
|
jr.CookieTime >= startDate &&
|
|
jr.CookieTime <= endDate &&
|
|
o.OrderState != Enums.OrderState.已取消 &&
|
|
o.IsGift == false)
|
|
.GroupBy((jr, oc, o) => jr.CampaignId)
|
|
.ToList(g => new
|
|
{
|
|
CampaignId = g.Key,
|
|
Profit = g.Sum(g.Value.Item2.Profit)
|
|
});
|
|
IList<GOIByLevel> list = new List<GOIByLevel>();
|
|
foreach (var campaignId in campaignIdList)
|
|
{
|
|
var cost = costs.FirstOrDefault(x => x.CampaignId == campaignId)?.Cost ?? 0M;
|
|
var profit = profits.FirstOrDefault(x => x.CampaignId == campaignId)?.Profit ?? 0M;
|
|
var skugoi = new GOIByLevel() { LevelId = campaignId.Value, Cost = cost, Profit = profit };
|
|
list.Add(skugoi);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public IList<GOIByLevel> CalculationAdGroupLevelGOI(IList<long?> adGroupIdList, DateTime startDate, DateTime endDate)
|
|
{
|
|
|
|
|
|
var costs = fsql.Select<JDPopularizeAdGroup>().Where(x => adGroupIdList.Contains(x.AdGroupId.Value) &&
|
|
x.Date >= startDate &&
|
|
x.Date <= endDate)
|
|
.GroupBy(x => x.AdGroupId)
|
|
.ToList(g => new
|
|
{
|
|
Cost = g.Sum(g.Value.Cost),
|
|
AdGroupId = g.Key
|
|
});
|
|
|
|
var profits = fsql.Select<JDOrderPopularizeRelation, OrderCost, Order>().InnerJoin((jr, oc, o) => jr.OrderId == oc.OrderId)
|
|
.InnerJoin((jr, oc, o) => jr.OrderId == o.Id)
|
|
.Where((jr, oc, o) => adGroupIdList.Contains(jr.AdGroupId.Value) &&
|
|
jr.CookieTime >= startDate &&
|
|
jr.CookieTime <= endDate &&
|
|
o.OrderState != Enums.OrderState.已取消 &&
|
|
o.IsGift == false)
|
|
.GroupBy((jr, oc, o) => jr.AdGroupId)
|
|
.ToList(g => new
|
|
{
|
|
AdGroupId = g.Key,
|
|
Profit = g.Sum(g.Value.Item2.Profit)
|
|
});
|
|
|
|
IList<GOIByLevel> list = new List<GOIByLevel>();
|
|
foreach (var adGroupId in adGroupIdList)
|
|
{
|
|
var cost = costs.FirstOrDefault(x => x.AdGroupId == adGroupId)?.Cost ?? 0M;
|
|
var profit = profits.FirstOrDefault(x => x.AdGroupId == adGroupId)?.Profit ?? 0M;
|
|
var skugoi = new GOIByLevel() { LevelId = adGroupId.Value, Cost = cost, Profit = profit };
|
|
list.Add(skugoi);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public IList<GOIByAdSku> CalculationAdSkuLevelGOI(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 => new { jas.Sku, jas.BusinessType })
|
|
.ToList(g => new
|
|
{
|
|
Cost = g.Sum(g.Value.Cost),
|
|
Sku = g.Key.Sku,
|
|
BusinessType = g.Key.BusinessType
|
|
});
|
|
|
|
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) => new { jr.PopularizeSku, jr.BusinessType })
|
|
.ToList(g => new
|
|
{
|
|
Profit = g.Sum(g.Value.Item2.SkuGrossProfit),
|
|
Sku = g.Key.PopularizeSku,
|
|
BusinessType = g.Key.BusinessType
|
|
});
|
|
|
|
IList<GOIByAdSku> list = new List<GOIByAdSku>();
|
|
foreach (var skuId in skuIdList)
|
|
{
|
|
{
|
|
var cost = costs.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 2)?.Cost ?? 0M;
|
|
var profit = profits.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 2)?.Profit ?? 0M;
|
|
|
|
if (cost != 0 || profit != 0)
|
|
{
|
|
var adskuGoi = new GOIByAdSku() { Sku = skuId, Cost = cost, Profit = profit, BusinessType = 2 };
|
|
list.Add(adskuGoi);
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
var cost = costs.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 134217728)?.Cost ?? 0M;
|
|
var profit = profits.FirstOrDefault(x => x.Sku == skuId && x.BusinessType == 134217728)?.Profit ?? 0M;
|
|
if (cost != 0 || profit != 0)
|
|
{
|
|
var adskuGoi = new GOIByAdSku() { Sku = skuId, Cost = cost, Profit = profit, BusinessType = 134217728 };
|
|
list.Add(adskuGoi);
|
|
}
|
|
}
|
|
|
|
}
|
|
return list;
|
|
}
|
|
|
|
//public ListResponse<ProductGOIResponse> QueryProductGOI(QueryProductGOIRequest request)
|
|
//{
|
|
// if (request.ShopId == 0)
|
|
// throw new BusinessException("缺少店铺Id");
|
|
|
|
// if (request.PageSize > 5)
|
|
// request.PageSize = 5;
|
|
|
|
// var productList = fsql.Select<Product>().Where(p => p.ShopId == request.ShopId)
|
|
// .WhereIf(!string.IsNullOrEmpty(request.SpuTitle), p => p.Title.Contains(request.SpuTitle))
|
|
// .WhereIf(!string.IsNullOrEmpty(request.Spu), p => p.Id == request.Spu)
|
|
// .Where(p => fsql.Select<ProductSku, JDPopularizeAdSku>()
|
|
// .InnerJoin((ps, jas) => ps.Id == jas.Sku)
|
|
// .WhereIf(!string.IsNullOrEmpty(request.Sku), (ps, jas) => ps.Id == request.Sku)
|
|
// .Where((ps, jas) => jas.ShopId == request.ShopId && ps.ProductId == p.Id)
|
|
// .Any())
|
|
// .OrderByDescending(p => p.CreateTime)
|
|
// .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.ProductId)).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 historyPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, null, null);
|
|
// #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>>();
|
|
|
|
// productGoi.Logo = productGoi.ProductSkuGOIList.FirstOrDefault()?.Logo;
|
|
// 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);
|
|
|
|
// var historyPopularizeLevelGOI = historyPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
// productSku.TotalCost = historyPopularizeLevelGOI?.Cost ?? 0M;
|
|
// productSku.TotalDeficit = (historyPopularizeLevelGOI?.Profit ?? 0M) - (historyPopularizeLevelGOI?.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);
|
|
// productGoi.TotalDeficit = productGoi.ProductSkuGOIList.Sum(x => x.TotalDeficit);
|
|
|
|
// for (var i = 0; i < productGoi.ProductSkuGOIList.Count(); i++)
|
|
// {
|
|
// var productSku = productGoi.ProductSkuGOIList[i];
|
|
// if (productSku.TotalCost == 0)
|
|
// {
|
|
// productGoi.ProductSkuGOIList.RemoveAt(i);
|
|
// i--;
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// return new ListResponse<ProductGOIResponse>()
|
|
// {
|
|
// Count = productCount,
|
|
// ItemList = productGOIList
|
|
// };
|
|
//}
|
|
|
|
public ListResponse<ProductGOIResponse> QueryProductGOI(QueryProductGOIRequest request)
|
|
{
|
|
if (request.ShopId == 0)
|
|
throw new BusinessException("缺少店铺Id");
|
|
request.EndDate = request.EndDate.Date.AddDays(1).AddSeconds(-1);
|
|
|
|
if (request.PageSize > 10)
|
|
request.PageSize = 10;
|
|
|
|
var productList = fsql.Select<Product, AggregationJDPopularizeSpu>()
|
|
.InnerJoin((p, ap) => p.Id == ap.Id)
|
|
.Where((p, ap) => p.State == 8 && p.ShopId == request.ShopId)
|
|
.Where((p, ap) => ap.Recent7dCost > 0 || ap.Recent30dCost > 0)
|
|
.WhereIf(!string.IsNullOrEmpty(request.SpuTitle), (p, ap) => p.Title.Contains(request.SpuTitle))
|
|
.WhereIf(!string.IsNullOrEmpty(request.Spu), (p, ap) => p.Id == request.Spu)
|
|
.OrderByDescending((p, ap) => p.CreateTime)
|
|
.Page(request.PageIndex, request.PageSize)
|
|
.Count(out var productCount)
|
|
.ToList((p, ap) => new TempProduct()
|
|
{
|
|
Id = p.Id,
|
|
CreateTime = p.CreateTime,
|
|
Platform = p.Platform,
|
|
ProductItemNum = p.ProductItemNum,
|
|
ShopId = p.ShopId,
|
|
Stage = p.Stage,
|
|
State = p.State,
|
|
Title = p.Title,
|
|
YestodayCost = ap.YestodayCost,
|
|
YestodayProductLevelProfit = ap.YestodayProductLevelProfit,
|
|
YestodayProductLevelGOI = ap.YestodayProductLevelGOI,
|
|
YestodayPopularizeLevelProfit = ap.YestodayPopularizeLevelProfit,
|
|
YestodayPopularizeLevelGOI = ap.YestodayPopularizeLevelGOI,
|
|
Recent7dCost = ap.Recent7dCost,
|
|
Recent7dProductLevelProfit = ap.Recent7dProductLevelProfit,
|
|
Recent7dProductLevelGOI = ap.Recent7dProductLevelGOI,
|
|
Recent7dPopularizeLevelProfit = ap.Recent7dPopularizeLevelProfit,
|
|
Recent7dPopularizeLevelGOI = ap.Recent7dPopularizeLevelGOI,
|
|
Recent30dCost = ap.Recent30dCost,
|
|
Recent30dPopularizeLevelGOI = ap.Recent30dPopularizeLevelGOI,
|
|
Recent30dPopularizeLevelProfit = ap.Recent30dPopularizeLevelProfit,
|
|
Recent30dProductLevelGOI = ap.Recent30dProductLevelGOI,
|
|
Recent30dProductLevelProfit = ap.Recent30dProductLevelProfit,
|
|
Date = ap.Date,
|
|
UpdateTime = ap.UpdateTime,
|
|
MaxDeficitThreshold = p.MaxDeficitThreshold,
|
|
MainSkuId = p.MainSkuId
|
|
}).Map<List<ProductGOIResponse>>();
|
|
|
|
var productIdList = productList.Select(p => p.Id).ToList();
|
|
var skuList = fsql.Select<ProductSku>().Where(ps => productIdList.Contains(ps.ProductId) && ps.State == 1).ToList<ProductSkuResponse>();
|
|
var skuIdList = skuList.Select(s => s.Id).ToList();
|
|
|
|
var historyPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, request.StartDate, request.EndDate);
|
|
|
|
foreach (var product in productList)
|
|
{
|
|
var currentProductSkuIdList = skuList.Where(s => s.ProductId == product.Id).Select(s => s.Id).ToList();
|
|
|
|
ProductSku firstSku = null;
|
|
if (!string.IsNullOrEmpty(product.MainSkuId))
|
|
firstSku = skuList.FirstOrDefault(s => s.Id == product.MainSkuId);
|
|
if (firstSku == null)
|
|
firstSku = skuList.FirstOrDefault(s => currentProductSkuIdList.Contains(s.Id));
|
|
product.Logo = firstSku?.Logo;
|
|
product.MainSkuId = firstSku?.Id;
|
|
var currentProductHistoryPopularizeLevelGOIList = historyPopularizeLevelGOIList.Where(x => currentProductSkuIdList.Contains(x.Sku));
|
|
|
|
var totalCost = currentProductHistoryPopularizeLevelGOIList.Sum(x => x.Cost);
|
|
var totalProfit = currentProductHistoryPopularizeLevelGOIList.Sum(x => x.Profit);
|
|
|
|
product.TotalCost = totalCost;
|
|
product.TotalDeficit = totalProfit - totalCost;
|
|
}
|
|
|
|
return new ListResponse<ProductGOIResponse>() { ItemList = productList, Count = productCount };
|
|
}
|
|
|
|
public ListResponse<ProductSkuGOIResponse> QueryProductSkuGOI(QueryProductSkuGOIRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Spu))
|
|
throw new BusinessException("缺少spu");
|
|
request.EndDate = request.EndDate.Date.AddDays(1).AddSeconds(-1);
|
|
|
|
var productSkuList = fsql.Select<ProductSku, AggregationJDPopularizeSku>()
|
|
.InnerJoin((ps, aps) => ps.Id == aps.Id)
|
|
.Where((ps, aps) => ps.State == 1)
|
|
.Where((ps, aps) => ps.ProductId == request.Spu)
|
|
.Where((ps, aps) => aps.Recent7dCost > 0 || aps.Recent30dCost > 0)
|
|
.OrderByDescending((ps, aps) => ps.CreateTime)
|
|
.ToList((ps, aps) => new TempProductSku()
|
|
{
|
|
Id = ps.Id,
|
|
CreateTime = ps.CreateTime,
|
|
ShopId = ps.ShopId,
|
|
State = ps.State,
|
|
Title = ps.Title,
|
|
CategoryId = ps.CategoryId,
|
|
CategoryName = ps.CategoryName,
|
|
Logo = ps.Logo,
|
|
Platform = ps.Platform,
|
|
Price = ps.Price,
|
|
ProductId = ps.ProductId,
|
|
YestodayCost = aps.YestodayCost,
|
|
YestodayProductLevelProfit = aps.YestodayProductLevelProfit,
|
|
YestodayProductLevelGOI = aps.YestodayProductLevelGOI,
|
|
YestodayPopularizeLevelProfit = aps.YestodayPopularizeLevelProfit,
|
|
YestodayPopularizeLevelGOI = aps.YestodayPopularizeLevelGOI,
|
|
Recent7dCost = aps.Recent7dCost,
|
|
Recent7dProductLevelProfit = aps.Recent7dProductLevelProfit,
|
|
Recent7dProductLevelGOI = aps.Recent7dProductLevelGOI,
|
|
Recent7dPopularizeLevelProfit = aps.Recent7dPopularizeLevelProfit,
|
|
Recent7dPopularizeLevelGOI = aps.Recent7dPopularizeLevelGOI,
|
|
Recent30dCost = aps.Recent30dCost,
|
|
Recent30dPopularizeLevelGOI = aps.Recent30dPopularizeLevelGOI,
|
|
Recent30dPopularizeLevelProfit = aps.Recent30dPopularizeLevelProfit,
|
|
Recent30dProductLevelGOI = aps.Recent30dProductLevelGOI,
|
|
Recent30dProductLevelProfit = aps.Recent30dProductLevelProfit,
|
|
Date = aps.Date,
|
|
UpdateTime = aps.UpdateTime,
|
|
MaxDeficitThreshold = ps.MaxDeficitThreshold
|
|
}).Map<List<ProductSkuGOIResponse>>();
|
|
|
|
var skuIdList = productSkuList.Select(ps => ps.Id).ToList();
|
|
var historyPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, request.StartDate, request.EndDate);
|
|
foreach (var productSku in productSkuList)
|
|
{
|
|
var historyPopularizeLevelGOI = historyPopularizeLevelGOIList.FirstOrDefault(x => x.Sku == productSku.Id);
|
|
productSku.TotalCost = historyPopularizeLevelGOI?.Cost ?? 0M;
|
|
productSku.TotalDeficit = (historyPopularizeLevelGOI?.Profit ?? 0M) - (historyPopularizeLevelGOI?.Cost ?? 0M);
|
|
}
|
|
return new ListResponse<ProductSkuGOIResponse>() { ItemList = productSkuList, Count = productSkuList.Count() };
|
|
}
|
|
|
|
public ListResponse<Product360PopularizeAnalysisCampaginRepsonse> QueryProduct360PopularizeGOI(Product360PopularizeAnalysisRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Spu) && string.IsNullOrEmpty(request.Sku))
|
|
throw new BusinessException("不能同时缺少spu和sku条件");
|
|
|
|
List<string> skuIdList = null;
|
|
if (!string.IsNullOrEmpty(request.Sku))
|
|
{
|
|
request.Spu = string.Empty;
|
|
skuIdList = new List<string>() { request.Sku };
|
|
}
|
|
if (!string.IsNullOrEmpty(request.Spu))
|
|
{
|
|
skuIdList = fsql.Select<ProductSku>().Where(ps => ps.ProductId == request.Spu).ToList(ps => ps.Id);
|
|
}
|
|
|
|
|
|
List<Product360PopularizeAnalysisCampaginRepsonse> list = new List<Product360PopularizeAnalysisCampaginRepsonse>();
|
|
|
|
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);
|
|
|
|
var popularizeAdSkuSourceList = fsql.Select<JDPopularizeAdSku>()
|
|
.Where(x => x.ShopId == request.ShopId)
|
|
.Where(x => x.Date >= request.StartDate && x.Date <= request.EndDate)
|
|
.WhereIf(skuIdList.Count() == 1, x => x.Sku == skuIdList[0])
|
|
.WhereIf(skuIdList.Count() > 1, x => skuIdList.Contains(x.Sku))
|
|
.ToList();
|
|
|
|
var kuaicheCampaignSourceList = popularizeAdSkuSourceList.Where(x => x.BusinessType == 2).ToList();
|
|
var jstCampaignSourceList = popularizeAdSkuSourceList.Where(x => x.BusinessType == 134217728).ToList();
|
|
|
|
var allCampaignIdList = popularizeAdSkuSourceList.Select(x => x.CampaignId).Distinct().ToList();
|
|
|
|
var kuaicheCampaignIdList = kuaicheCampaignSourceList.Select(x => x.CampaignId).Distinct().ToList();
|
|
var jstCampaignIdList = jstCampaignSourceList.Select(x => x.CampaignId).Distinct().ToList();
|
|
|
|
|
|
#region 查询所有计划名称
|
|
var allCampaignNameList = fsql.Select<JDPopularizeCampaign>()
|
|
.Where(x => x.ShopId == request.ShopId && allCampaignIdList.Contains(x.CampaignId))
|
|
.GroupBy(x => new { x.CampaignId, x.CampaignName })
|
|
.ToList(g => new { g.Value.CampaignId, g.Value.CampaignName });
|
|
#endregion
|
|
|
|
#region 处理所有计划的GOI
|
|
var recent7DayCampaignGOIList = CalculationCampaignLevelGOI(allCampaignIdList, startDate_Recent7day, endDate_Recent7day);
|
|
var recent30DayCampaignGOIList = CalculationCampaignLevelGOI(allCampaignIdList, startDate_Recent30day, endDate_Recent30day);
|
|
#endregion
|
|
|
|
#region 处理快车
|
|
|
|
#region 处理快车GOI
|
|
foreach (var campaignId in kuaicheCampaignIdList)
|
|
{
|
|
var campaign = new Product360PopularizeAnalysisCampaginRepsonse()
|
|
{
|
|
CampaignId = campaignId.Value,
|
|
BusinessType = 2,
|
|
CampaignGOI_Recent7Day = recent7DayCampaignGOIList.FirstOrDefault(x => x.LevelId == campaignId),
|
|
CampaignGOI_Recent30Day = recent30DayCampaignGOIList.FirstOrDefault(x => x.LevelId == campaignId),
|
|
CampaignName = allCampaignNameList.FirstOrDefault(x => x.CampaignId == campaignId)?.CampaignName
|
|
};
|
|
list.Add(campaign);
|
|
}
|
|
#endregion
|
|
|
|
#region 处理单元GOI
|
|
var kuaicheAdGroupIdList = popularizeAdSkuSourceList.Where(x => x.BusinessType == 2).Select(x => x.AdGroupId).Distinct().ToList();
|
|
var recent7DayAdGroupGOIList = CalculationAdGroupLevelGOI(kuaicheAdGroupIdList, startDate_Recent7day, endDate_Recent7day);
|
|
var recent30DayAdGroupGOIList = CalculationAdGroupLevelGOI(kuaicheAdGroupIdList, startDate_Recent30day, endDate_Recent30day);
|
|
#endregion
|
|
|
|
#region 处理单元统计
|
|
var adGroupStatisticsList = kuaicheCampaignSourceList.GroupBy(x => x.AdGroupId);
|
|
var adGroupIdList = adGroupStatisticsList.Select(x => x.Key).ToList();
|
|
var allAdGroupList = new List<Product360PopularizeAnalysisAdGroupResponse>();
|
|
|
|
var allAdGroupNameList = fsql.Select<JDPopularizeAdGroup>()
|
|
.Where(x => x.ShopId == request.ShopId && adGroupIdList.Contains(x.AdGroupId))
|
|
.GroupBy(x => new { x.AdGroupId, x.AdGroupName })
|
|
.ToList(g => new { g.Value.AdGroupId, g.Value.AdGroupName });
|
|
|
|
foreach (var adGroupStatistics in adGroupStatisticsList)
|
|
{
|
|
var adGroupId = adGroupStatistics.Key;
|
|
|
|
var adGroup = new Product360PopularizeAnalysisAdGroupResponse()
|
|
{
|
|
AdGroupId = adGroupId.Value,
|
|
AdGroupName = allAdGroupNameList.FirstOrDefault(x => x.AdGroupId == adGroupId)?.AdGroupName,
|
|
BusinessType = 2,
|
|
CampaignId = adGroupStatistics.FirstOrDefault()?.CampaignId ?? 0,
|
|
Clicks = adGroupStatistics.Sum(x => x.Clicks),
|
|
Cost = adGroupStatistics.Sum(x => x.Cost),
|
|
Impressions = adGroupStatistics.Sum(x => x.Impressions),
|
|
OrderCnt = adGroupStatistics.Sum(x => x.TotalOrderCnt),
|
|
AdGroupGOI_Recent7Day = recent7DayAdGroupGOIList.FirstOrDefault(x => x.LevelId == adGroupId),
|
|
AdGroupGOI_Recent30Day = recent30DayAdGroupGOIList.FirstOrDefault(x => x.LevelId == adGroupId)
|
|
};
|
|
allAdGroupList.Add(adGroup);
|
|
var campagin = list.FirstOrDefault(x => x.CampaignId == adGroup.CampaignId);
|
|
if (campagin != null)
|
|
campagin.AdGroupList.Add(adGroup);
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region 处理京速推
|
|
|
|
#region 处理京速推GOI和统计
|
|
var jstCampaignStatisticsList = jstCampaignSourceList.GroupBy(x => x.CampaignId);
|
|
foreach (var jstCampaignStatistics in jstCampaignStatisticsList)
|
|
{
|
|
var campaignId = jstCampaignStatistics.Key;
|
|
var jstCampaign = new Product360PopularizeAnalysisCampaginRepsonse()
|
|
{
|
|
CampaignId = campaignId.Value,
|
|
CampaignName = allCampaignNameList.FirstOrDefault(x => x.CampaignId == campaignId)?.CampaignName,
|
|
BusinessType = 134217728,
|
|
CampaignGOI_Recent7Day = recent7DayCampaignGOIList.FirstOrDefault(x => x.LevelId == campaignId),
|
|
CampaignGOI_Recent30Day = recent30DayCampaignGOIList.FirstOrDefault(x => x.LevelId == campaignId),
|
|
Clicks = jstCampaignStatistics.Sum(x => x.Clicks),
|
|
Cost = jstCampaignStatistics.Sum(x => x.Cost),
|
|
Impressions = jstCampaignStatistics.Sum(x => x.Impressions),
|
|
OrderCnt = jstCampaignStatistics.Sum(x => x.TotalOrderCnt)
|
|
};
|
|
list.Add(jstCampaign);
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
return new ListResponse<Product360PopularizeAnalysisCampaginRepsonse>()
|
|
{
|
|
ItemList = list,
|
|
Count = list.Count
|
|
};
|
|
}
|
|
|
|
public List<JDXXHistogramResponse> QueryProduct360HistogramStatistics(JDXXHistogramRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Spu))
|
|
throw new BusinessException("缺少spu");
|
|
|
|
|
|
var skuIdList = fsql.Select<ProductSku>().Where(ps => ps.ProductId == request.Spu).ToList(ps => ps.Id);
|
|
|
|
var startDate = DateTime.Now.Date.AddDays(-14);
|
|
var endDate = DateTime.Now.Date.AddSeconds(-1);
|
|
var datas = freeSqlMultiDBManager.XXfsql.Select<Skus>().Where(s => skuIdList.Contains(s.Sku))
|
|
.Where(s => s.CreateTime >= startDate && s.CreateTime <= endDate)
|
|
.GroupBy(s => s.Sku)
|
|
.ToList(g => new JDXXHistogramResponse()
|
|
{
|
|
Sku = g.Key,
|
|
UV = g.Sum(g.Value.Uv),
|
|
Sales = g.Sum(g.Value.Sales),
|
|
Profit = g.Sum(g.Value.Profits),
|
|
TotalCost = g.Sum(g.Value.TotalCost)
|
|
});
|
|
|
|
var totalUV = datas.Sum(x => x.UV);
|
|
var totalSales = datas.Sum(x => x.Sales);
|
|
var totalProfit = datas.Sum(x => x.Profit);
|
|
var totalCost = datas.Sum(x => x.TotalCost);
|
|
|
|
foreach (var data in datas)
|
|
{
|
|
data.UVPercent = totalUV == 0 ? 0 : Math.Round((data.UV / totalUV) ?? 0, 2);
|
|
data.SalesPercent = totalSales == 0 ? 0 : Math.Round((data.Sales / totalSales) ?? 0, 2);
|
|
data.ProfitPercent = totalProfit == 0 ? 0 : Math.Round((data.Profit / totalProfit) ?? 0, 2);
|
|
data.TotalCostPercent = totalCost == 0 ? 0 : Math.Round((data.TotalCost / totalCost) ?? 0, 2);
|
|
}
|
|
|
|
return datas;
|
|
}
|
|
|
|
public Product360TopStatisticsResponse QueryProduct360TopStatistics(Product360TopStatisticsRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Sku) && string.IsNullOrEmpty(request.Spu))
|
|
throw new BusinessException("缺少必要参数");
|
|
|
|
if (!string.IsNullOrEmpty(request.Sku))
|
|
request.Spu = string.Empty;
|
|
|
|
Product p = null;
|
|
ProductSku productSku = null;
|
|
|
|
if (!string.IsNullOrEmpty(request.Spu))
|
|
p = fsql.Select<Product>(request.Spu).ToOne();
|
|
|
|
var psList = fsql.Select<ProductSku>().WhereIf(!string.IsNullOrEmpty(request.Spu), ps => ps.ProductId == request.Spu)
|
|
.WhereIf(!string.IsNullOrEmpty(request.Sku), ps => ps.Id == request.Sku)
|
|
.ToList();
|
|
if (!string.IsNullOrEmpty(request.Sku))
|
|
productSku = psList.FirstOrDefault(ps => ps.Id == request.Sku);
|
|
|
|
|
|
var skuIdList = psList.Select(ps => ps.Id).ToList();
|
|
var spu = psList.FirstOrDefault().ProductId;
|
|
|
|
var noOrderStateList = new List<Enums.OrderState?>()
|
|
{
|
|
Enums.OrderState.已取消,
|
|
Enums.OrderState.待付款
|
|
};
|
|
|
|
|
|
var saleQueryTime = DateTime.Now.Date.AddDays(-30);
|
|
|
|
var saleCount = fsql.Select<Order, OrderSku>()
|
|
.InnerJoin((o, osku) => o.Id == osku.OrderId)
|
|
.Where((o, osku) => !noOrderStateList.Contains(o.OrderState) &&
|
|
o.StartTime >= saleQueryTime)
|
|
.WhereIf(!string.IsNullOrEmpty(request.Spu), (o, osku) => osku.ProductId == request.Spu)
|
|
.WhereIf(!string.IsNullOrEmpty(request.Sku), (o, osku) => osku.SkuId == request.Sku)
|
|
.Sum((o, osku) => osku.ItemTotal);
|
|
|
|
var pingjia = freeSqlMultiDBManager.XXfsql.Select<Spuchange>().Where(x => x.SpusId == spu).OrderByDescending(x => x.CreateTime).ToOne(x => x.TotalCount);
|
|
|
|
|
|
var historyPopularizeLevelGOIList = StatisticsPopularizeLevelGOI(skuIdList, null, null);
|
|
var totalDeficit = historyPopularizeLevelGOIList.Sum(x => x.Profit) - historyPopularizeLevelGOIList.Sum(x => x.Cost);
|
|
|
|
return new Product360TopStatisticsResponse()
|
|
{
|
|
CreateTime = p?.CreateTime ?? productSku?.CreateTime,
|
|
EvaluateCount = pingjia,
|
|
MaxDeficit = p?.MaxDeficitThreshold ?? productSku?.MaxDeficitThreshold,
|
|
MonthSaleCount = saleCount,
|
|
TotalDeficit = totalDeficit
|
|
};
|
|
}
|
|
|
|
public List<PopularizeCostCurveStatisticsResponse> PopularizeCostCurveStatisticsBySpu(PopularizeCostCurveStatisticsBySpuRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Spu))
|
|
throw new BusinessException("缺少spu");
|
|
if (request.StartDate == null)
|
|
request.StartDate = DateTime.Now.Date.AddDays(-30);
|
|
if (request.EndDate == null)
|
|
request.EndDate = DateTime.Now.Date.AddDays(-1);
|
|
return fsql.Select<AggregationJDPopularizeSpuDaily>().Where(a => a.ShopId == request.ShopId &&
|
|
a.ProductId == request.Spu &&
|
|
a.Date >= request.StartDate &&
|
|
a.Date <= request.EndDate)
|
|
.OrderBy(a => a.Date)
|
|
.ToList(a => new PopularizeCostCurveStatisticsResponse()
|
|
{
|
|
Cost = a.Cost,
|
|
Date = a.Date.Value
|
|
});
|
|
}
|
|
|
|
public List<PopularizeCostCurveStatisticsResponse> PopularizeCostCurveStatisticsBySku(PopularizeCostCurveStatisticsBySkuRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Sku))
|
|
throw new BusinessException("缺少sku");
|
|
if (request.StartDate == null)
|
|
request.StartDate = DateTime.Now.Date.AddDays(-30);
|
|
if (request.EndDate == null)
|
|
request.EndDate = DateTime.Now.Date.AddDays(-1);
|
|
return fsql.Select<AggregationJDPopularizeSkuDaily>().Where(a => a.ShopId == request.ShopId &&
|
|
a.SkuId == request.Sku &&
|
|
a.Date >= request.StartDate &&
|
|
a.Date <= request.EndDate)
|
|
.OrderBy(a => a.Date)
|
|
.ToList(a => new PopularizeCostCurveStatisticsResponse()
|
|
{
|
|
Cost = a.Cost,
|
|
Date = a.Date.Value
|
|
});
|
|
}
|
|
|
|
public ListResponse<GOIBySpu> QueryPopularizeLevelGOIBySpuId(QueryPopularizeLevelGOIBySpuIdRequest request)
|
|
{
|
|
var list = new List<GOIBySpu>();
|
|
request.EndTime = request.EndTime.Date.AddDays(1).AddSeconds(-1);
|
|
var productSkuList = fsql.Select<ProductSku>().Where(ps => request.SpuIdList.Contains(ps.ProductId)).ToList();
|
|
var skuIdList = productSkuList.Select(ps => ps.Id).Distinct().ToList();
|
|
var goiList = StatisticsProductLevelGOI(skuIdList, request.StartTime, request.EndTime);
|
|
foreach (var spu in request.SpuIdList)
|
|
{
|
|
var currentSpuSkuIdList = productSkuList.Where(ps => ps.ProductId == spu).Select(ps => ps.Id).ToList();
|
|
var currentGoiList = goiList.Where(g => currentSpuSkuIdList.Contains(g.Sku));
|
|
var goi = new GOIBySpu()
|
|
{
|
|
Spu = spu,
|
|
Cost = currentGoiList.Sum(g => g.Cost),
|
|
Profit = currentGoiList.Sum(g => g.Profit),
|
|
};
|
|
list.Add(goi);
|
|
}
|
|
return new ListResponse<GOIBySpu>()
|
|
{
|
|
Count = list.Count,
|
|
ItemList = list
|
|
};
|
|
}
|
|
|
|
public GOIByShop QueryPopularizeLevelGOIByShopId(QueryPopularizeLevelGOIByShopIdRequest request)
|
|
{
|
|
request.EndTime = request.EndTime.Date.AddDays(1).AddSeconds(-1);
|
|
return StatisticsProductLevelGOI(request.ShopId, request.StartTime, request.EndTime);
|
|
}
|
|
}
|
|
}
|
|
|