fix: ROI计算公式改为(销售额-退款金额)/推广费用,应用于公司概览、店铺概览及导出功能

This commit is contained in:
小蟑螂
2026-04-17 02:35:46 +08:00
parent 581c0b25d6
commit 55c579d304
3 changed files with 74 additions and 31 deletions
@@ -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<PromotionFeePO> shopPromotionFees = promotionFeesByShop.getOrDefault(shop.getId(), new ArrayList<>());
List<BrushOrderPO> 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<PromotionFeePO> shopPromotionFees = promotionFeesByShop.getOrDefault(shop.getId(), new ArrayList<>());
List<BrushOrderPO> 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<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders) {
BigDecimal totalProfit = calculateTotalProfit(orders);
List<BrushOrderPO> 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);
}
/**
@@ -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)
@@ -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<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders) {
BigDecimal totalProfit = calculateTotalProfit(orders);
List<BrushOrderPO> 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) {