refactor(ecom-dmb): 店铺概览商品/品类对比表改为对象结构含合计行

This commit is contained in:
小蟑螂
2026-04-23 21:30:48 +08:00
parent c1f718ed7e
commit 85cce1ff7a
6 changed files with 237 additions and 26 deletions
@@ -136,16 +136,40 @@ public class ShopOverviewStatisticsVO {
private TrendData orderCountTrend;
/**
* 多商品数据横向对比
* 多商品数据横向对比(含合计行)
*/
@Schema(description = "多商品数据横向对比")
private List<ProductComparison> productComparisons;
private ProductOverview productOverview;
/**
* 品类多维度数据对比
* 品类多维度数据对比(含合计行)
*/
@Schema(description = "品类多维度数据对比")
private List<CategoryComparison> categoryComparisons;
private CategoryOverview categoryOverview;
/**
* 商品概览(含合计行和商品列表)
*/
@Data
@Schema(description = "商品概览")
public static class ProductOverview {
@Schema(description = "合计行")
private ProductComparison total;
@Schema(description = "各商品对比列表")
private List<ProductComparison> products;
}
/**
* 品类概览(含合计行和品类列表)
*/
@Data
@Schema(description = "品类概览")
public static class CategoryOverview {
@Schema(description = "合计行")
private CategoryComparison total;
@Schema(description = "各品类对比列表")
private List<CategoryComparison> categories;
}
/**
* 商品对比数据
@@ -203,9 +203,9 @@ public class ExportServiceImpl implements ExportService {
}
if (("product".equals(exportType) || "all".equals(exportType))
&& vo.getProductComparisons() != null && !vo.getProductComparisons().isEmpty()) {
&& vo.getProductOverview() != null && vo.getProductOverview().getProducts() != null && !vo.getProductOverview().getProducts().isEmpty()) {
Sheet productSheet = workbook.createSheet("商品明细");
createProductDetailSheetFromVO(productSheet, dateRangeLabel, vo.getProductComparisons(), taxRate);
createProductDetailSheetFromVO(productSheet, dateRangeLabel, vo.getProductOverview().getProducts(), taxRate);
}
if (("trend".equals(exportType) || "all".equals(exportType))
@@ -241,14 +241,14 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
overviewVO.setOrderCountTrend(orderCountTrend);
// 10. 多商品数据横向对比
List<ShopOverviewStatisticsVO.ProductComparison> productComparisons = calculateProductComparisons(
ShopOverviewStatisticsVO.ProductOverview productOverview = calculateProductComparisons(
orders, promotionFees, brushOrders, refundStatusId);
overviewVO.setProductComparisons(productComparisons);
overviewVO.setProductOverview(productOverview);
// 11. 品类多维度数据对比
List<ShopOverviewStatisticsVO.CategoryComparison> categoryComparisons = calculateCategoryComparisons(
ShopOverviewStatisticsVO.CategoryOverview categoryOverview = calculateCategoryComparisons(
orders, promotionFees, brushOrders, refundStatusId);
overviewVO.setCategoryComparisons(categoryComparisons);
overviewVO.setCategoryOverview(categoryOverview);
return overviewVO;
}
@@ -366,7 +366,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
/**
* 计算多商品数据横向对比
*/
private List<ShopOverviewStatisticsVO.ProductComparison> calculateProductComparisons(
private ShopOverviewStatisticsVO.ProductOverview calculateProductComparisons(
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders, Long refundStatusId) {
List<ShopOverviewStatisticsVO.ProductComparison> comparisons = new ArrayList<>();
@@ -492,7 +492,73 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
// 按销售额降序排序
comparisons.sort(Comparator.comparing(ShopOverviewStatisticsVO.ProductComparison::getSalesAmount).reversed());
return comparisons;
// 计算合计行
ShopOverviewStatisticsVO.ProductComparison total = computeProductTotal(comparisons);
// 封装为 ProductOverview
ShopOverviewStatisticsVO.ProductOverview overview = new ShopOverviewStatisticsVO.ProductOverview();
overview.setTotal(total);
overview.setProducts(comparisons);
return overview;
}
/**
* 计算商品对比的合计行
*/
private ShopOverviewStatisticsVO.ProductComparison computeProductTotal(List<ShopOverviewStatisticsVO.ProductComparison> products) {
BigDecimal totalSales = BigDecimal.ZERO;
int totalOrders = 0;
BigDecimal totalProductCost = BigDecimal.ZERO;
BigDecimal totalShippingCost = BigDecimal.ZERO;
BigDecimal totalOtherCost = BigDecimal.ZERO;
BigDecimal totalRefund = BigDecimal.ZERO;
BigDecimal totalPromoFee = BigDecimal.ZERO;
BigDecimal totalBrushFee = BigDecimal.ZERO;
for (ShopOverviewStatisticsVO.ProductComparison p : products) {
totalSales = totalSales.add(p.getSalesAmount() != null ? p.getSalesAmount() : BigDecimal.ZERO);
totalOrders += p.getOrderCount() != null ? p.getOrderCount() : 0;
totalProductCost = totalProductCost.add(p.getProductCost() != null ? p.getProductCost() : BigDecimal.ZERO);
totalShippingCost = totalShippingCost.add(p.getShippingCost() != null ? p.getShippingCost() : BigDecimal.ZERO);
totalOtherCost = totalOtherCost.add(p.getOtherCost() != null ? p.getOtherCost() : BigDecimal.ZERO);
totalRefund = totalRefund.add(p.getRefundAmount() != null ? p.getRefundAmount() : BigDecimal.ZERO);
totalPromoFee = totalPromoFee.add(p.getTotalPromotionFee() != null ? p.getTotalPromotionFee() : BigDecimal.ZERO);
totalBrushFee = totalBrushFee.add(p.getBrushFee() != null ? p.getBrushFee() : BigDecimal.ZERO);
}
BigDecimal avgOrderValue = totalOrders > 0
? totalSales.divide(new BigDecimal(totalOrders), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04"));
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal feeRatio = totalSales.compareTo(BigDecimal.ZERO) > 0
? totalPromoFee.add(totalBrushFee).divide(totalSales, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
ShopOverviewStatisticsVO.ProductComparison total = new ShopOverviewStatisticsVO.ProductComparison();
total.setProductId(0L);
total.setProductName("合计");
total.setSalesAmount(totalSales);
total.setOrderCount(totalOrders);
total.setAverageOrderValue(avgOrderValue);
total.setProductCost(totalProductCost);
total.setShippingCost(totalShippingCost);
total.setOtherCost(totalOtherCost);
total.setRefundAmount(totalRefund);
total.setTotalPromotionFee(totalPromoFee);
total.setBrushFee(totalBrushFee);
total.setFeeRatio(feeRatio);
total.setTotalProfitBeforeTax(profitBeforeTax);
total.setSalesTaxAmount(salesTax);
total.setRoi(roi);
total.setProfitAfterTax(profitAfterTax);
return total;
}
/**
@@ -619,7 +685,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
/**
* 计算品类多维度数据对比
*/
private List<ShopOverviewStatisticsVO.CategoryComparison> calculateCategoryComparisons(
private ShopOverviewStatisticsVO.CategoryOverview calculateCategoryComparisons(
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders, Long refundStatusId) {
@@ -752,7 +818,73 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
}
results.sort(Comparator.comparing(ShopOverviewStatisticsVO.CategoryComparison::getSalesAmount).reversed());
return results;
// 计算合计行
ShopOverviewStatisticsVO.CategoryComparison total = computeCategoryTotalForShop(results);
// 封装为 CategoryOverview
ShopOverviewStatisticsVO.CategoryOverview overview = new ShopOverviewStatisticsVO.CategoryOverview();
overview.setTotal(total);
overview.setCategories(results);
return overview;
}
/**
* 计算店铺视角品类对比的合计行
*/
private ShopOverviewStatisticsVO.CategoryComparison computeCategoryTotalForShop(List<ShopOverviewStatisticsVO.CategoryComparison> categories) {
BigDecimal totalSales = BigDecimal.ZERO;
int totalOrders = 0;
BigDecimal totalProductCost = BigDecimal.ZERO;
BigDecimal totalShippingCost = BigDecimal.ZERO;
BigDecimal totalOtherCost = BigDecimal.ZERO;
BigDecimal totalRefund = BigDecimal.ZERO;
BigDecimal totalPromoFee = BigDecimal.ZERO;
BigDecimal totalBrushFee = BigDecimal.ZERO;
for (ShopOverviewStatisticsVO.CategoryComparison c : categories) {
totalSales = totalSales.add(c.getSalesAmount() != null ? c.getSalesAmount() : BigDecimal.ZERO);
totalOrders += c.getOrderCount() != null ? c.getOrderCount() : 0;
totalProductCost = totalProductCost.add(c.getProductCost() != null ? c.getProductCost() : BigDecimal.ZERO);
totalShippingCost = totalShippingCost.add(c.getShippingCost() != null ? c.getShippingCost() : BigDecimal.ZERO);
totalOtherCost = totalOtherCost.add(c.getOtherCost() != null ? c.getOtherCost() : BigDecimal.ZERO);
totalRefund = totalRefund.add(c.getRefundAmount() != null ? c.getRefundAmount() : BigDecimal.ZERO);
totalPromoFee = totalPromoFee.add(c.getTotalPromotionFee() != null ? c.getTotalPromotionFee() : BigDecimal.ZERO);
totalBrushFee = totalBrushFee.add(c.getBrushFee() != null ? c.getBrushFee() : BigDecimal.ZERO);
}
BigDecimal avgOrderValue = totalOrders > 0
? totalSales.divide(new BigDecimal(totalOrders), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04"));
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal feeRatio = totalSales.compareTo(BigDecimal.ZERO) > 0
? totalPromoFee.add(totalBrushFee).divide(totalSales, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
ShopOverviewStatisticsVO.CategoryComparison total = new ShopOverviewStatisticsVO.CategoryComparison();
total.setCategoryId(0L);
total.setCategoryName("合计");
total.setSalesAmount(totalSales);
total.setOrderCount(totalOrders);
total.setAverageOrderValue(avgOrderValue);
total.setProductCost(totalProductCost);
total.setShippingCost(totalShippingCost);
total.setOtherCost(totalOtherCost);
total.setRefundAmount(totalRefund);
total.setTotalPromotionFee(totalPromoFee);
total.setBrushFee(totalBrushFee);
total.setFeeRatio(feeRatio);
total.setTotalProfitBeforeTax(profitBeforeTax);
total.setSalesTaxAmount(salesTax);
total.setRoi(roi);
total.setProfitAfterTax(profitAfterTax);
return total;
}
}