refactor(ecom-dmb): 平台维度概览改为对象结构(含合计行)并优化订单排序配置

This commit is contained in:
小蟑螂
2026-04-23 20:45:23 +08:00
parent 41b8b3d22c
commit 60e3efab81
5 changed files with 288 additions and 108 deletions
@@ -79,11 +79,24 @@ public class CompanyOverviewVO {
@Schema(description = "多品类数据横向对比")
private List<CategoryComparison> categoryComparisons;
/**
* 平台维度概览(包含合计行和平台列表)
*/
@Data
@Schema(description = "平台维度概览")
public static class PlatformOverview {
@Schema(description = "合计行")
private PlatformSummary total;
@Schema(description = "各平台汇总列表")
private List<PlatformSummary> platforms;
}
/**
* 平台维度汇总
*/
@Schema(description = "平台维度汇总")
private List<PlatformSummary> platformSummary;
private PlatformOverview platformSummary;
/**
* 汇总ROI
@@ -225,9 +225,9 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
overviewVO.setCategoryComparisons(categoryComparisons);
// 11. 平台维度汇总
List<CompanyOverviewVO.PlatformSummary> platformSummary = calculatePlatformSummary(
CompanyOverviewVO.PlatformOverview platformOverview = calculatePlatformSummary(
shops, orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate);
overviewVO.setPlatformSummary(platformSummary);
overviewVO.setPlatformSummary(platformOverview);
// 12. 汇总ROI和总税后盈亏
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId);
@@ -906,7 +906,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
/**
* 计算平台维度汇总
*/
private List<CompanyOverviewVO.PlatformSummary> calculatePlatformSummary(
private CompanyOverviewVO.PlatformOverview calculatePlatformSummary(
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders, Long refundStatusId, Long companyId,
LocalDate startDate, LocalDate endDate) {
@@ -920,11 +920,16 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
Map<Long, PlatformPO> platformMap = allPlatforms.stream()
.collect(Collectors.toMap(PlatformPO::getId, p -> p));
// 按平台分组店铺
// 按平台分组店铺(有platformId的)
Map<Long, List<ShopPO>> shopsByPlatform = shops.stream()
.filter(shop -> shop.getPlatformId() != null)
.collect(Collectors.groupingBy(ShopPO::getPlatformId));
// 没有platformId的店铺,汇总为"其他平台"
List<ShopPO> shopsWithoutPlatform = shops.stream()
.filter(shop -> shop.getPlatformId() == null)
.collect(Collectors.toList());
// 生成目标月份列表
List<LocalDate> targetMonths = new ArrayList<>();
LocalDate currentMonth = startDate.withDayOfMonth(1);
@@ -1106,9 +1111,232 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
platformSummaries.add(platformSummary);
}
// 处理没有平台的店铺,汇总为"其他平台"
if (!shopsWithoutPlatform.isEmpty()) {
CompanyOverviewVO.PlatformSummary otherSummary = calculateOtherPlatformSummary(
shopsWithoutPlatform, ordersByShop, promotionFeesByShop, brushOrdersByShop,
targetsByShop, refundStatusId, taxRate);
if (otherSummary.getOrderCount() > 0) {
platformSummaries.add(otherSummary);
}
}
// 按销售额降序排序
platformSummaries.sort(Comparator.comparing(CompanyOverviewVO.PlatformSummary::getSalesAmount).reversed());
return platformSummaries;
// 计算合计行
CompanyOverviewVO.PlatformSummary total = computePlatformTotal(platformSummaries);
// 封装为 PlatformOverview
CompanyOverviewVO.PlatformOverview overview = new CompanyOverviewVO.PlatformOverview();
overview.setTotal(total);
overview.setPlatforms(platformSummaries);
return overview;
}
/**
* 计算平台汇总的合计行
*/
private CompanyOverviewVO.PlatformSummary computePlatformTotal(List<CompanyOverviewVO.PlatformSummary> platforms) {
BigDecimal totalSalesAmount = BigDecimal.ZERO;
int totalOrderCount = 0;
BigDecimal totalTargetAmount = BigDecimal.ZERO;
int totalTargetOrderCount = 0;
BigDecimal totalProductCost = BigDecimal.ZERO;
BigDecimal totalShippingCost = BigDecimal.ZERO;
BigDecimal totalOtherCost = BigDecimal.ZERO;
BigDecimal totalRefundAmount = BigDecimal.ZERO;
BigDecimal totalPromotionFee = BigDecimal.ZERO;
BigDecimal totalBrushFee = BigDecimal.ZERO;
for (CompanyOverviewVO.PlatformSummary p : platforms) {
totalSalesAmount = totalSalesAmount.add(p.getSalesAmount() != null ? p.getSalesAmount() : BigDecimal.ZERO);
totalOrderCount += p.getOrderCount() != null ? p.getOrderCount() : 0;
totalTargetAmount = totalTargetAmount.add(p.getTargetAmount() != null ? p.getTargetAmount() : BigDecimal.ZERO);
totalTargetOrderCount += p.getTargetOrderCount() != null ? p.getTargetOrderCount() : 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);
totalRefundAmount = totalRefundAmount.add(p.getRefundAmount() != null ? p.getRefundAmount() : BigDecimal.ZERO);
totalPromotionFee = totalPromotionFee.add(p.getTotalPromotionFee() != null ? p.getTotalPromotionFee() : BigDecimal.ZERO);
totalBrushFee = totalBrushFee.add(p.getBrushFee() != null ? p.getBrushFee() : BigDecimal.ZERO);
}
BigDecimal avgOrderValue = totalOrderCount > 0
? totalSalesAmount.divide(new BigDecimal(totalOrderCount), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
BigDecimal taxRate = new BigDecimal("0.04");
BigDecimal salesTax = totalSalesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal feeRatio = totalSalesAmount.compareTo(BigDecimal.ZERO) > 0
? totalPromotionFee.add(totalBrushFee).divide(totalSalesAmount, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal salesAchievementRate = totalTargetAmount.compareTo(BigDecimal.ZERO) > 0
? totalSalesAmount.divide(totalTargetAmount, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
: totalSalesAmount.compareTo(BigDecimal.ZERO) > 0 ? new BigDecimal("100") : BigDecimal.ZERO;
BigDecimal orderAchievementRate = totalTargetOrderCount > 0
? new BigDecimal(totalOrderCount).divide(new BigDecimal(totalTargetOrderCount), 4, RoundingMode.HALF_UP)
.multiply(new BigDecimal("100"))
: totalOrderCount > 0 ? new BigDecimal("100") : BigDecimal.ZERO;
CompanyOverviewVO.PlatformSummary total = new CompanyOverviewVO.PlatformSummary();
total.setPlatformId(0L);
total.setPlatformName("合计");
total.setSalesAmount(totalSalesAmount);
total.setOrderCount(totalOrderCount);
total.setAverageOrderValue(avgOrderValue);
total.setTargetAmount(totalTargetAmount);
total.setTargetOrderCount(totalTargetOrderCount);
total.setSalesAmountAchievementRate(salesAchievementRate);
total.setOrderCountAchievementRate(orderAchievementRate);
total.setProductCost(totalProductCost);
total.setShippingCost(totalShippingCost);
total.setOtherCost(totalOtherCost);
total.setRefundAmount(totalRefundAmount);
total.setTotalPromotionFee(totalPromotionFee);
total.setBrushFee(totalBrushFee);
total.setFeeRatio(feeRatio);
total.setTotalProfitBeforeTax(profitBeforeTax);
total.setSalesTaxAmount(salesTax);
total.setRoi(roi);
total.setProfitAfterTax(profitAfterTax);
return total;
}
/**
* 计算"其他平台"汇总(没有绑定平台的店铺)
*/
private CompanyOverviewVO.PlatformSummary calculateOtherPlatformSummary(
List<ShopPO> shops,
Map<Long, List<OrderPO>> ordersByShop,
Map<Long, List<PromotionFeePO>> promotionFeesByShop,
Map<Long, List<BrushOrderPO>> brushOrdersByShop,
Map<Long, List<ShopMonthlyTargetPO>> targetsByShop,
Long refundStatusId,
BigDecimal taxRate) {
BigDecimal totalSalesAmount = BigDecimal.ZERO;
int totalOrderCount = 0;
BigDecimal totalRefundAmount = BigDecimal.ZERO;
BigDecimal totalProductCost = BigDecimal.ZERO;
BigDecimal totalShippingCost = BigDecimal.ZERO;
BigDecimal totalOtherCost = BigDecimal.ZERO;
BigDecimal totalPromotionFee = BigDecimal.ZERO;
BigDecimal totalBrushFee = BigDecimal.ZERO;
BigDecimal totalTargetAmount = BigDecimal.ZERO;
int totalTargetOrderCount = 0;
for (ShopPO shop : shops) {
List<OrderPO> shopOrders = ordersByShop.getOrDefault(shop.getId(), new ArrayList<>());
List<PromotionFeePO> shopPromotionFees = promotionFeesByShop.getOrDefault(shop.getId(), new ArrayList<>());
List<BrushOrderPO> shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>());
List<ShopMonthlyTargetPO> shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>());
BigDecimal shopSales = shopOrders.stream()
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalSalesAmount = totalSalesAmount.add(shopSales);
totalOrderCount += shopOrders.size();
BigDecimal shopRefund = shopOrders.stream()
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalRefundAmount = totalRefundAmount.add(shopRefund);
BigDecimal shopProductCost = shopOrders.stream()
.map(o -> {
BigDecimal c = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO;
int qty = o.getQuantity() != null ? o.getQuantity() : 1;
return c.multiply(new BigDecimal(qty));
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalProductCost = totalProductCost.add(shopProductCost);
BigDecimal shopShippingCost = shopOrders.stream()
.map(o -> {
BigDecimal c = o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO;
int qty = o.getQuantity() != null ? o.getQuantity() : 1;
return c.multiply(new BigDecimal(qty));
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalShippingCost = totalShippingCost.add(shopShippingCost);
BigDecimal shopOtherCost = shopOrders.stream()
.map(o -> o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalOtherCost = totalOtherCost.add(shopOtherCost);
BigDecimal shopPromoFee = shopPromotionFees.stream()
.map(f -> f.getFeeAmount() != null ? f.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalPromotionFee = totalPromotionFee.add(shopPromoFee);
BigDecimal shopBrush = shopBrushOrders.stream()
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalBrushFee = totalBrushFee.add(shopBrush);
BigDecimal shopTargetAmount = shopTargets.stream()
.map(t -> t.getTargetAmount() != null ? t.getTargetAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
totalTargetAmount = totalTargetAmount.add(shopTargetAmount);
int shopTargetOrderCount = shopTargets.stream()
.mapToInt(t -> t.getTargetOrderCount() != null ? t.getTargetOrderCount() : 0)
.sum();
totalTargetOrderCount += shopTargetOrderCount;
}
BigDecimal averageOrderValue = totalOrderCount > 0
? totalSalesAmount.divide(new BigDecimal(totalOrderCount), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
BigDecimal salesTaxAmount = totalSalesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount);
BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal feeRatio = totalSalesAmount.compareTo(BigDecimal.ZERO) > 0
? totalPromotionFee.add(totalBrushFee).divide(totalSalesAmount, 4, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal salesAchievementRate = totalTargetAmount.compareTo(BigDecimal.ZERO) > 0
? totalSalesAmount.divide(totalTargetAmount, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))
: totalSalesAmount.compareTo(BigDecimal.ZERO) > 0 ? new BigDecimal("100") : BigDecimal.ZERO;
BigDecimal orderAchievementRate = totalTargetOrderCount > 0
? new BigDecimal(totalOrderCount).divide(new BigDecimal(totalTargetOrderCount), 4, RoundingMode.HALF_UP)
.multiply(new BigDecimal("100"))
: totalOrderCount > 0 ? new BigDecimal("100") : BigDecimal.ZERO;
CompanyOverviewVO.PlatformSummary summary = new CompanyOverviewVO.PlatformSummary();
summary.setPlatformId(0L); // 0表示"其他平台"
summary.setPlatformName("其他平台");
summary.setTargetOrderCount(totalTargetOrderCount);
summary.setOrderCountAchievementRate(orderAchievementRate);
summary.setTargetAmount(totalTargetAmount);
summary.setSalesAmountAchievementRate(salesAchievementRate);
summary.setSalesAmount(totalSalesAmount);
summary.setOrderCount(totalOrderCount);
summary.setAverageOrderValue(averageOrderValue);
summary.setProductCost(totalProductCost);
summary.setShippingCost(totalShippingCost);
summary.setOtherCost(totalOtherCost);
summary.setRefundAmount(totalRefundAmount);
summary.setTotalPromotionFee(totalPromotionFee);
summary.setBrushFee(totalBrushFee);
summary.setFeeRatio(feeRatio);
summary.setTotalProfitBeforeTax(profitBeforeTax);
summary.setSalesTaxAmount(salesTaxAmount);
summary.setRoi(roi);
summary.setProfitAfterTax(profitAfterTax);
return summary;
}
private String getPlatformNameById(Long platformId) {