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.
 
 

278 lines
14 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
.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) => skuIdList.Contains(jr.PopularizeSku))
.WhereIf(startDate != null, (jr, ocd, o) => jr.CookieTime >= startDate)
.WhereIf(endDate != null, (jr, ocd, o) => jr.CookieTime <= endDate)
.Where((jr, ocd, o) => 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 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>>();
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);
}
return new ListResponse<ProductGOIResponse>()
{
Count = productCount,
ItemList = productGOIList
};
}
public ListResponse<Product360PopularizeAnalysisCampaginRepsonse> QueryProduct360PopularizeGOI(Product360PopularizeAnalysisRequest request)
{
if (request.SkuIdList == null || request.SkuIdList.Count() == 0)
throw new BusinessException("缺少sku");
List<Product360PopularizeAnalysisCampaginRepsonse> list = new List<Product360PopularizeAnalysisCampaginRepsonse>();
var sourcePopularizeAdSkuList = fsql.Select<JDPopularizeAdSku>()
.Where(x => x.ShopId == request.ShopId)
.Where(x => x.Date >= request.StartDate && x.Date <= request.EndDate)
.WhereIf(request.SkuIdList.Count() == 1, x => x.Sku == request.SkuIdList[0])
.WhereIf(request.SkuIdList.Count() > 1, x => request.SkuIdList.Contains(x.Sku))
.ToList();
var kuaicheCampaignList = sourcePopularizeAdSkuList.Where(x => x.BusinessType == 2).ToList();
var jstCampaignList = sourcePopularizeAdSkuList.Where(x => x.BusinessType == 134217728).ToList();
#region 处理快车
#endregion
#region 处理京速推
#endregion
return new ListResponse<Product360PopularizeAnalysisCampaginRepsonse>()
{
ItemList = list,
Count = list.Count
};
}
}
}