From 55c579d3044c9fc0cc169d0f7946e881bfea5cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Fri, 17 Apr 2026 02:35:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ROI=E8=AE=A1=E7=AE=97=E5=85=AC=E5=BC=8F?= =?UTF-8?q?=E6=94=B9=E4=B8=BA(=E9=94=80=E5=94=AE=E9=A2=9D-=E9=80=80?= =?UTF-8?q?=E6=AC=BE=E9=87=91=E9=A2=9D)/=E6=8E=A8=E5=B9=BF=E8=B4=B9?= =?UTF-8?q?=E7=94=A8=EF=BC=8C=E5=BA=94=E7=94=A8=E4=BA=8E=E5=85=AC=E5=8F=B8?= =?UTF-8?q?=E6=A6=82=E8=A7=88=E3=80=81=E5=BA=97=E9=93=BA=E6=A6=82=E8=A7=88?= =?UTF-8?q?=E5=8F=8A=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/CompanyOverviewServiceImpl.java | 60 ++++++++++++++----- .../service/impl/ExportServiceImpl.java | 14 +++-- .../ShopOverviewStatisticsServiceImpl.java | 31 ++++++---- 3 files changed, 74 insertions(+), 31 deletions(-) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java index 0fdadf59..1e2ed311 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java @@ -205,7 +205,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { overviewVO.setShopComparisons(shopComparisons); // 10. 汇总ROI和总税后盈亏 - BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders); + BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId); overviewVO.setTotalROI(totalROI); overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax); @@ -361,9 +361,18 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopPromotionFees = promotionFeesByShop.getOrDefault(shop.getId(), new ArrayList<>()); List shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>()); + BigDecimal shopSales = shopOrders.stream() + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal shopRefund = shopOrders.stream() + .filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId())) + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal shopPromotionFee = shopPromotionFees.stream() + .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee); BigDecimal profit = calculateShopProfit(shopOrders, refundStatusId); - BigDecimal totalCost = calculateShopTotalCost(shopPromotionFees, shopBrushOrders); - BigDecimal roi = calculateShopROI(profit, totalCost); BigDecimal netProfit = calculateShopNetProfit(profit); allROIs.add(roi); @@ -383,9 +392,18 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopPromotionFees = promotionFeesByShop.getOrDefault(shop.getId(), new ArrayList<>()); List shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>()); + BigDecimal shopSales = shopOrders.stream() + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal shopRefund = shopOrders.stream() + .filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId())) + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal shopPromotionFee = shopPromotionFees.stream() + .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee); BigDecimal profit = calculateShopProfit(shopOrders, refundStatusId); - BigDecimal totalCost = calculateShopTotalCost(shopPromotionFees, shopBrushOrders); - BigDecimal roi = calculateShopROI(profit, totalCost); BigDecimal netProfit = calculateShopNetProfit(profit); int quadrant; @@ -507,8 +525,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { // 税后盈亏(扣除销售额4%税额) BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); - BigDecimal totalCostForROI = totalPromotionFee.add(brushFee); - BigDecimal roi = calculateShopROI(profitBeforeTax, totalCostForROI); + BigDecimal roi = calculateSalesROI(salesAmount.subtract(refundAmount), totalPromotionFee); BigDecimal feeRatio = BigDecimal.ZERO; if (salesAmount.compareTo(BigDecimal.ZERO) > 0) { @@ -634,6 +651,16 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { return profit.divide(totalCost, 4, RoundingMode.HALF_UP); } + /** + * 计算ROI(新公式):(销售额 - 退款金额)/ 推广费用 + */ + private BigDecimal calculateSalesROI(BigDecimal netSales, BigDecimal promotionFee) { + if (promotionFee == null || promotionFee.compareTo(BigDecimal.ZERO) <= 0) { + return BigDecimal.ZERO; + } + return netSales.divide(promotionFee, 4, RoundingMode.HALF_UP); + } + /** * 计算店铺净利(税后) */ @@ -643,19 +670,22 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } /** - * 计算汇总ROI + * 计算汇总ROI(新公式):(销售额 - 退款金额)/ 推广费用 */ private BigDecimal calculateTotalROI(List orders, List promotionFees, - List brushOrders) { - BigDecimal totalProfit = calculateTotalProfit(orders); + List brushOrders, Long refundStatusId) { + BigDecimal totalSales = orders.stream() + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal totalRefund = orders.stream() + .filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId())) + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal netSales = totalSales.subtract(totalRefund); BigDecimal totalPromotionFee = promotionFees.stream() .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) .reduce(BigDecimal.ZERO, BigDecimal::add); - BigDecimal totalBrushFee = brushOrders.stream() - .map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - BigDecimal totalCost = totalPromotionFee.add(totalBrushFee); - return calculateShopROI(totalProfit, totalCost); + return calculateSalesROI(netSales, totalPromotionFee); } /** diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java index c0d9ba4a..1b730610 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java @@ -97,6 +97,7 @@ public class ExportServiceImpl implements ExportService { BigDecimal totalPromotionFee = BigDecimal.ZERO; BigDecimal totalBrushFee = BigDecimal.ZERO; BigDecimal totalProfitBeforeTax = BigDecimal.ZERO; + BigDecimal totalRefundAmount = BigDecimal.ZERO; BigDecimal taxRate = dto.getTaxRate() != null ? dto.getTaxRate() : DEFAULT_TAX_RATE; @@ -107,6 +108,7 @@ public class ExportServiceImpl implements ExportService { totalSales = totalSales.add((BigDecimal) data.get("salesAmount")); totalOrders = totalOrders.add(new BigDecimal((Integer) data.get("orderCount"))); totalPromotionFee = totalPromotionFee.add((BigDecimal) data.get("totalPromotionFee")); + totalRefundAmount = totalRefundAmount.add((BigDecimal) data.get("refundAmount")); totalBrushFee = totalBrushFee.add((BigDecimal) data.get("brushFee")); totalProfitBeforeTax = totalProfitBeforeTax.add((BigDecimal) data.get("totalProfitBeforeTax")); } @@ -115,8 +117,8 @@ public class ExportServiceImpl implements ExportService { BigDecimal totalTax = totalProfitBeforeTax.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); BigDecimal totalProfitAfterTax = totalProfitBeforeTax.subtract(totalTax); BigDecimal totalCost = totalPromotionFee.add(totalBrushFee); - BigDecimal totalRoi = totalCost.compareTo(BigDecimal.ZERO) > 0 - ? totalProfitBeforeTax.divide(totalCost, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal totalRoi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 + ? totalSales.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; BigDecimal avgFeeRatio = totalSales.compareTo(BigDecimal.ZERO) > 0 ? totalPromotionFee.divide(totalSales, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; @@ -475,8 +477,8 @@ public class ExportServiceImpl implements ExportService { BigDecimal promotionFee = BigDecimal.ZERO; BigDecimal brushFee = BigDecimal.ZERO; - BigDecimal roi = totalCost.compareTo(BigDecimal.ZERO) > 0 - ? profitBeforeTax.divide(totalCost, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal roi = promotionFee.compareTo(BigDecimal.ZERO) > 0 + ? salesAmount.subtract(refundAmount).divide(promotionFee, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; BigDecimal feeRatio = salesAmount.compareTo(BigDecimal.ZERO) > 0 ? promotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; @@ -877,8 +879,8 @@ public class ExportServiceImpl implements ExportService { BigDecimal profitAfterTax = profitBeforeTax.subtract(tax); BigDecimal feeRatio = salesAmount.compareTo(BigDecimal.ZERO) > 0 ? promotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; - BigDecimal roi = promotionFee.add(brushFee).compareTo(BigDecimal.ZERO) > 0 - ? profitBeforeTax.divide(promotionFee.add(brushFee), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal roi = promotionFee.compareTo(BigDecimal.ZERO) > 0 + ? salesAmount.subtract(refundAmount).divide(promotionFee, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; BigDecimal salesAchievementRate = targetAmount.compareTo(BigDecimal.ZERO) > 0 ? salesAmount.divide(targetAmount, 4, RoundingMode.HALF_UP) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java index 0527fd0f..3fa48700 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java @@ -206,7 +206,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics calculateTargetData(overviewVO, companyId, queryDTO.getShopId(), startDate, endDate, totalSalesAmount, totalOrderCount); // 8. 汇总ROI和总税后盈亏 - BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders); + BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId); overviewVO.setTotalROI(totalROI); overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax); @@ -300,16 +300,28 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics * 计算汇总ROI */ private BigDecimal calculateTotalROI(List orders, List promotionFees, - List brushOrders) { - BigDecimal totalProfit = calculateTotalProfit(orders); + List brushOrders, Long refundStatusId) { + BigDecimal totalSales = orders.stream() + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal totalRefund = orders.stream() + .filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId())) + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal totalPromotionFee = promotionFees.stream() .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) .reduce(BigDecimal.ZERO, BigDecimal::add); - BigDecimal totalBrushFee = brushOrders.stream() - .map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - BigDecimal totalCost = totalPromotionFee.add(totalBrushFee); - return calculateROI(totalProfit, totalCost); + return calculateSalesROI(totalSales.subtract(totalRefund), totalPromotionFee); + } + + /** + * 计算ROI(新公式):(销售额 - 退款金额)/ 推广费用 + */ + private BigDecimal calculateSalesROI(BigDecimal netSales, BigDecimal promotionFee) { + if (promotionFee == null || promotionFee.compareTo(BigDecimal.ZERO) <= 0) { + return BigDecimal.ZERO; + } + return netSales.divide(promotionFee, 4, RoundingMode.HALF_UP); } /** @@ -416,8 +428,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics // 税后盈亏(扣除销售额4%税额) BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); - BigDecimal totalCostForROI = productPromotionFee.add(brushFee); - BigDecimal roi = calculateROI(profitBeforeTax, totalCostForROI); + BigDecimal roi = calculateSalesROI(salesAmount.subtract(refundAmount), productPromotionFee); BigDecimal feeRatio = BigDecimal.ZERO; if (salesAmount.compareTo(BigDecimal.ZERO) > 0) {