From 09d26e357f95ffc40f224ab7282d48dc7e788e36 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Thu, 28 Jul 2022 02:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B7=E5=8D=95=E8=8A=B1=E8=B4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Statistics/StatisticsBusiness.cs | 141 +++++++++++------- .../Order/XingXiangSearchOrderRequest.cs | 5 +- .../Response/Order/XingXinagSearchResponse.cs | 5 + 3 files changed, 97 insertions(+), 54 deletions(-) diff --git a/BBWY.Server.Business/Statistics/StatisticsBusiness.cs b/BBWY.Server.Business/Statistics/StatisticsBusiness.cs index 07734057..de39b076 100644 --- a/BBWY.Server.Business/Statistics/StatisticsBusiness.cs +++ b/BBWY.Server.Business/Statistics/StatisticsBusiness.cs @@ -1,8 +1,10 @@ -using BBWY.Common.Models; + +using BBWY.Common.Models; using BBWY.Server.Model; using BBWY.Server.Model.Db; using BBWY.Server.Model.Db.Mds; using BBWY.Server.Model.Dto; +using System; using System.Collections.Generic; using System.Linq; using Yitter.IdGenerator; @@ -53,6 +55,23 @@ namespace BBWY.Server.Business return response; } + private void XingXiangCumulative(IList detailList, string spuId, bool isSD, decimal profit, decimal sdProductAmount, decimal sdCost) + { + var xxRespose = detailList.FirstOrDefault(xx => xx.Spu == spuId); + if (xxRespose == null) + { + xxRespose = new XingXiangItemResponse() { Spu = spuId, Profit = 0M }; + detailList.Add(xxRespose); + } + xxRespose.Profit += profit; + if (isSD) + { + xxRespose.SDOrderCount++; + xxRespose.SDOrderAmount += sdProductAmount; + xxRespose.SDOrderCost += sdCost; + } + } + public XingXinagSearchResponse XingXiangStatistics(XingXiangSearchOrderRequest xingXiangSearchOrderRequest) { var mdsShop = freeSqlMultiDBManager.MDSfsql.Select().Where(s => s.ShopId == xingXiangSearchOrderRequest.ShopId.ToString()).ToOne(); @@ -66,8 +85,6 @@ namespace BBWY.Server.Business .Where((o, oc) => o.ShopId == xingXiangSearchOrderRequest.ShopId) .Where((o, oc) => o.StartTime >= beginTime && o.StartTime <= endTime) .Where((o, oc) => !invalidOrderStateList.Contains(o.OrderState.Value)) - .WhereIf(xingXiangSearchOrderRequest.SpuList != null && xingXiangSearchOrderRequest.SpuList.Count() > 0, - (o, oc) => fsql.Select().As("osku").Where(osku => xingXiangSearchOrderRequest.SpuList.Contains(osku.ProductId) && osku.OrderId == o.Id).Any()) .OrderByDescending((o, oc) => o.StartTime) .ToList((o, oc) => new Order() { @@ -82,7 +99,8 @@ namespace BBWY.Server.Business DeliveryExpressFreight = oc.DeliveryExpressFreight, PreferentialAmount = oc.PreferentialAmount, OrderSellerPrice = o.OrderSellerPrice, - SellerPreferentialAmount = o.SellerPreferentialAmount + SellerPreferentialAmount = o.SellerPreferentialAmount, + Profit = oc.Profit }); if (orderList.Count() == 0) @@ -95,54 +113,76 @@ namespace BBWY.Server.Business var orderCostDetailList = fsql.Select().Where(ocd => orderIdList.Contains(ocd.OrderId) && ocd.IsEnabled == true).ToList(); detailList = new List(); - var totalSDOrderCost = 0M; foreach (var order in orderList) { - var skuCount = orderSkuList.Count(osku => osku.OrderId == order.Id); - var currentOrderSkuGroupsSelect = orderSkuList.Where(osku => osku.OrderId == order.Id); - if (xingXiangSearchOrderRequest.SpuList != null && xingXiangSearchOrderRequest.SpuList.Count() > 0) - currentOrderSkuGroupsSelect = currentOrderSkuGroupsSelect.Where(osku => xingXiangSearchOrderRequest.SpuList.Contains(osku.ProductId)); - var currentOrderSkuGroups = currentOrderSkuGroupsSelect.GroupBy(osku => osku.ProductId); - foreach (var group in currentOrderSkuGroups) + var currentOrderSkuList = orderSkuList.Where(osku => osku.OrderId == order.Id).ToList(); + + if (order.StorageType == Enums.StorageType.SD) + { + XingXiangCumulative(detailList, currentOrderSkuList[0].ProductId, true, order.Profit ?? 0M, order.OrderSellerPrice, Math.Abs(order.Profit ?? 0M)); + continue; + } + + var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id); + var spuGroups = currentOrderSkuList.GroupBy(osku => osku.ProductId); + + var tempOrderProdcutAmount = currentOrderSkuList.Sum(osku => osku.ItemTotal * osku.Price) ?? 0M; + var tempOrderCost = currentOrderCostDetailList.Count() > 0 ? currentOrderCostDetailList.Sum(ocd => ocd.TotalCost) : 0M; + var tempOrderProfit = tempOrderProdcutAmount - tempOrderCost; + + foreach (var spuGroup in spuGroups) { - var spuId = group.Key; - - var profit = 0M; - var sdCost = 0M; - var prodcutAmount = group.Sum(osku => osku.Price * osku.ItemTotal) ?? 0; //货款 - var skuSellerPreferentialAmount = order.SellerPreferentialAmount / skuCount * group.Count(); //该SPU分摊的商家优惠金额 - prodcutAmount -= skuSellerPreferentialAmount; - var commissionAmount = prodcutAmount * platformCommissionRatio; //该SPU的平台扣点金额 - var freightPriceByUser = order.FreightPrice == 0 ? 0 : order.FreightPrice / skuCount * group.Count(); //该SPU分摊的用户承担运费 - var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id && ocd.ProductId == spuId).ToList(); - var purchaseAmount = currentOrderCostDetailList.Count() > 0 ? currentOrderCostDetailList.Sum(ocd => ocd.TotalCost) : 0; - var deliveryFreight = currentOrderCostDetailList.Count() > 0 ? currentOrderCostDetailList.Sum(ocd => ocd.DeliveryExpressFreight) : 0; - - if (order.StorageType != Enums.StorageType.SD) - { - profit = prodcutAmount + freightPriceByUser - purchaseAmount - deliveryFreight - commissionAmount; - } - else - { - var sdCommissionAmount = order.SDCommissionAmount.Value / skuCount * group.Count(); - profit = 0; - sdCost = sdCommissionAmount + commissionAmount + order.DeliveryExpressFreight ?? 0M; - totalSDOrderCost += sdCost; - } - var xxRespose = detailList.FirstOrDefault(xx => xx.Spu == spuId); - if (xxRespose == null) - { - xxRespose = new XingXiangItemResponse() { Spu = spuId, Profit = 0M }; - detailList.Add(xxRespose); - } - xxRespose.Profit += profit; - if (order.StorageType == Enums.StorageType.SD) - { - xxRespose.SDOrderCount++; - xxRespose.SDOrderAmount += prodcutAmount; - xxRespose.SDOrderCost += sdCost; - } + var currentSpuCostDetailList = currentOrderCostDetailList.Where(ocd => ocd.ProductId == spuGroup.Key); + + var tempSpuProductAmount = spuGroup.Sum(osku => osku.Price * osku.ItemTotal) ?? 0M; + var tempSpuCost = currentSpuCostDetailList.Count() > 0 ? currentSpuCostDetailList.Sum(ocd => ocd.TotalCost) : 0M; + var tempSpuProfit = tempSpuProductAmount - tempSpuCost; + var spuProfitPercent = tempOrderProfit == 0M ? 0M : tempSpuProfit / tempOrderProfit; + + var realSpuProfit = (order.Profit ?? 0M) * spuProfitPercent; + XingXiangCumulative(detailList, spuGroup.Key, false, realSpuProfit, 0, 0); } + + //foreach (var group in spuGroups) + //{ + // var spuId = group.Key; + + // var profit = 0M; + // var sdCost = 0M; + // var prodcutAmount = group.Sum(osku => osku.Price * osku.ItemTotal) ?? 0; //货款 + // var skuSellerPreferentialAmount = order.SellerPreferentialAmount / skuCount * group.Count(); //该SPU分摊的商家优惠金额 + // prodcutAmount -= skuSellerPreferentialAmount; + // var commissionAmount = prodcutAmount * platformCommissionRatio; //该SPU的平台扣点金额 + // var freightPriceByUser = order.FreightPrice == 0 ? 0 : order.FreightPrice / skuCount * group.Count(); //该SPU分摊的用户承担运费 + // var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id && ocd.ProductId == spuId).ToList(); + // var purchaseAmount = currentOrderCostDetailList.Count() > 0 ? currentOrderCostDetailList.Sum(ocd => ocd.TotalCost) : 0; + // var deliveryFreight = currentOrderCostDetailList.Count() > 0 ? currentOrderCostDetailList.Sum(ocd => ocd.DeliveryExpressFreight) : 0; + + // if (order.StorageType != Enums.StorageType.SD) + // { + // profit = prodcutAmount + freightPriceByUser - purchaseAmount - deliveryFreight - commissionAmount; + // } + // else + // { + // var sdCommissionAmount = order.SDCommissionAmount.Value / skuCount * group.Count(); + // profit = 0; + // sdCost = sdCommissionAmount + commissionAmount + order.DeliveryExpressFreight ?? 0M; + // totalSDOrderCost += sdCost; + // } + // var xxRespose = detailList.FirstOrDefault(xx => xx.Spu == spuId); + // if (xxRespose == null) + // { + // xxRespose = new XingXiangItemResponse() { Spu = spuId, Profit = 0M }; + // detailList.Add(xxRespose); + // } + // xxRespose.Profit += profit; + // if (order.StorageType == Enums.StorageType.SD) + // { + // xxRespose.SDOrderCount++; + // xxRespose.SDOrderAmount += prodcutAmount; + // xxRespose.SDOrderCost += sdCost; + // } + //} } var sdOrderList = orderList.Where(o => o.StorageType == Enums.StorageType.SD); @@ -150,9 +190,10 @@ namespace BBWY.Server.Business return new XingXinagSearchResponse() { ItemList = detailList, - TotalSDOrderAmount = sdOrderList.Count() > 0 ? sdOrderList.Sum(o => o.OrderSellerPrice) : 0M, + TotalSDOrderAmount = detailList.Sum(xx => xx.SDOrderAmount), TotalSDOrderCount = sdOrderList.Count(), - TotalSDOrderCost = totalSDOrderCost + TotalSDOrderCost = detailList.Sum(xx => xx.SDOrderCost), + TotalProfit = detailList.Sum(xx => xx.Profit) }; } diff --git a/BBWY.Server.Model/Dto/Request/Order/XingXiangSearchOrderRequest.cs b/BBWY.Server.Model/Dto/Request/Order/XingXiangSearchOrderRequest.cs index d7518647..b9aa0cca 100644 --- a/BBWY.Server.Model/Dto/Request/Order/XingXiangSearchOrderRequest.cs +++ b/BBWY.Server.Model/Dto/Request/Order/XingXiangSearchOrderRequest.cs @@ -6,10 +6,7 @@ namespace BBWY.Server.Model.Dto { public class XingXiangSearchOrderRequest { - /// - /// 不传默认全店查询 - /// - public IList SpuList { get; set; } + //public IList SpuList { get; set; } public DateTime SearchDate { get; set; } diff --git a/BBWY.Server.Model/Dto/Response/Order/XingXinagSearchResponse.cs b/BBWY.Server.Model/Dto/Response/Order/XingXinagSearchResponse.cs index a4359793..1531e0ec 100644 --- a/BBWY.Server.Model/Dto/Response/Order/XingXinagSearchResponse.cs +++ b/BBWY.Server.Model/Dto/Response/Order/XingXinagSearchResponse.cs @@ -23,6 +23,11 @@ namespace BBWY.Server.Model.Dto /// Spu刷单明细 /// public IList ItemList { get; set; } + + /// + /// 总利润 + /// + public decimal TotalProfit { get; set; } } public class XingXiangItemResponse