feat(ecom): 推广费表增加商品维度,移除冗余字段和平台外键

- ec_promotion_fee 新增 product_id 字段(可空,关联 ec_product)
- 移除冗余字段:fee_name / base_amount / fee_rate / click_count / impression_count / conversion_count
- 移除 ec_promotion_fee.platform_id 外键(店铺已关联平台,此处冗余)
- 后端 PO/DTO/VO 层字段更新,DTO 新增 productId 过滤条件
- ShopOverviewStatisticsServiceImpl 推广费计算改为有 product_id 直接归属,无则按销售额比例分配
- 前端类型定义和推广费表单同步更新
This commit is contained in:
小蟑螂
2026-05-06 02:13:46 +08:00
parent 655a1025b3
commit 98a4c4293f
14 changed files with 181 additions and 287 deletions
@@ -34,9 +34,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -399,12 +401,29 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
.filter(brush -> brush.getProductId() != null)
.collect(Collectors.groupingBy(BrushOrderPO::getProductId));
// 推广费无法按商品分组(推广费只有店铺级别),所以每个商品使用相同的推广费比例
BigDecimal totalPromotionFee = promotionFees.stream()
// 按商品ID分组推广费:有 product_id 的为直接归属,无 product_id 的进入比例池
Map<Long, BigDecimal> directPromotionByProduct = promotionFees.stream()
.filter(fee -> fee.getProductId() != null)
.collect(Collectors.groupingBy(
PromotionFeePO::getProductId,
Collectors.reducing(
BigDecimal.ZERO,
fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO,
BigDecimal::add)));
// 无商品归属的推广费(店铺级),按销售比例分配
BigDecimal ratioPool = promotionFees.stream()
.filter(fee -> fee.getProductId() == null)
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalSalesAmount = orders.stream()
.map(stat::receivedForSales)
// 计算各商品销售额(仅用于比例分配)
Map<Long, BigDecimal> salesByProduct = ordersByProduct.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().stream()
.map(stat::receivedForSales)
.reduce(BigDecimal.ZERO, BigDecimal::add)));
BigDecimal totalSalesAmount = salesByProduct.values().stream()
.reduce(BigDecimal.ZERO, BigDecimal::add);
for (Map.Entry<Long, List<OrderPO>> entry : ordersByProduct.entrySet()) {
@@ -437,11 +456,11 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
.map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 推广费按销售额比例分配
BigDecimal productPromotionFee = BigDecimal.ZERO;
if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
// 推广费:有商品级直接归属的用直接归属,其余用比例分配
BigDecimal productPromotionFee = directPromotionByProduct.getOrDefault(productId, BigDecimal.ZERO);
if (productPromotionFee.compareTo(BigDecimal.ZERO) == 0 && ratioPool.compareTo(BigDecimal.ZERO) > 0 && totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal ratio = salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP);
productPromotionFee = totalPromotionFee.multiply(ratio);
productPromotionFee = ratioPool.multiply(ratio);
}
// 计算商品成本
@@ -705,12 +724,29 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
ordersByCategory.computeIfAbsent(categoryId, k -> new ArrayList<>()).add(order);
}
// 推广费总额(按销售额比例分配)
BigDecimal totalPromotionFee = promotionFees.stream()
// 推广费:按商品ID聚合直接归属 + 无商品归属的进入比例池
Map<Long, BigDecimal> directPromotionByProduct = promotionFees.stream()
.filter(fee -> fee.getProductId() != null)
.collect(Collectors.groupingBy(
PromotionFeePO::getProductId,
Collectors.reducing(
BigDecimal.ZERO,
fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO,
BigDecimal::add)));
BigDecimal ratioPool = promotionFees.stream()
.filter(fee -> fee.getProductId() == null)
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalSalesAmount = orders.stream()
.map(stat::receivedForSales)
// 品类总销售额(用于比例分配)
Map<Long, BigDecimal> salesByCategory = new HashMap<>();
for (Map.Entry<Long, List<OrderPO>> e : ordersByCategory.entrySet()) {
BigDecimal catSales = e.getValue().stream()
.map(stat::receivedForSales)
.reduce(BigDecimal.ZERO, BigDecimal::add);
salesByCategory.put(e.getKey(), catSales);
}
BigDecimal totalSalesAmount = salesByCategory.values().stream()
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalBrushFeeAll = brushOrders.stream()
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
@@ -732,11 +768,20 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
.map(stat::receivedForRefund)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 推广费按销售额比例
BigDecimal catPromotionFee = BigDecimal.ZERO;
if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
catPromotionFee = totalPromotionFee.multiply(
salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP));
// 推广费:先汇总品类内各商品的直接归属推广费,再按品类销售额比例分配比例池
Set<Long> catProductIds = new HashSet<>();
for (OrderPO order : catOrders) {
Long pid = specToProduct.get(order.getProductSpecId());
if (pid != null) catProductIds.add(pid);
}
BigDecimal directCatPromotion = catProductIds.stream()
.map(pid -> directPromotionByProduct.getOrDefault(pid, BigDecimal.ZERO))
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 分配比例池
BigDecimal catPromotionFee = directCatPromotion;
if (ratioPool.compareTo(BigDecimal.ZERO) > 0 && totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal ratio = salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP);
catPromotionFee = catPromotionFee.add(ratioPool.multiply(ratio));
}
BigDecimal catBrushFee = BigDecimal.ZERO;
@@ -1,7 +1,6 @@
package top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@@ -29,19 +28,18 @@ public class CreatePromotionFeeDTO {
@NotNull(message = "推广费类型ID不能为空")
private Long feeTypeId;
/**
* 推广费名称
*/
@Schema(description = "推广费名称", example = "直通车推广", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "推广费名称不能为空")
private String feeName;
/**
* 平台ID
*/
@Schema(description = "平台ID", example = "1")
private Long platformId;
/**
* 商品ID(可选,关联到具体商品)
*/
@Schema(description = "商品ID(可选)", example = "1")
private Long productId;
/**
* 推广活动名称
*/
@@ -55,36 +53,6 @@ public class CreatePromotionFeeDTO {
@NotNull(message = "推广费金额不能为空")
private BigDecimal feeAmount;
/**
* 基础金额(用于按比例计算)
*/
@Schema(description = "基础金额", example = "10000.00")
private BigDecimal baseAmount;
/**
* 费率(用于按比例计算)
*/
@Schema(description = "费率", example = "0.05")
private BigDecimal feeRate;
/**
* 点击次数
*/
@Schema(description = "点击次数", example = "1000")
private Integer clickCount;
/**
* 展示次数
*/
@Schema(description = "展示次数", example = "10000")
private Integer impressionCount;
/**
* 转化次数
*/
@Schema(description = "转化次数", example = "50")
private Integer conversionCount;
/**
* 推广费发生日期
*/
@@ -30,6 +30,12 @@ public class PromotionFeeFilterDTO {
@Schema(description = "平台ID", example = "1")
private Long platformId;
/**
* 商品ID(可选)
*/
@Schema(description = "商品ID(可选)", example = "1")
private Long productId;
/**
* 推广费发生日期开始
*/
@@ -1,7 +1,6 @@
package top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@@ -36,19 +35,18 @@ public class UpdatePromotionFeeDTO {
@NotNull(message = "推广费类型ID不能为空")
private Long feeTypeId;
/**
* 推广费名称
*/
@Schema(description = "推广费名称", example = "直通车推广", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "推广费名称不能为空")
private String feeName;
/**
* 平台ID
*/
@Schema(description = "平台ID", example = "1")
private Long platformId;
/**
* 商品ID(可选,关联到具体商品)
*/
@Schema(description = "商品ID(可选)", example = "1")
private Long productId;
/**
* 推广活动名称
*/
@@ -62,36 +60,6 @@ public class UpdatePromotionFeeDTO {
@NotNull(message = "推广费金额不能为空")
private BigDecimal feeAmount;
/**
* 基础金额(用于按比例计算)
*/
@Schema(description = "基础金额", example = "10000.00")
private BigDecimal baseAmount;
/**
* 费率(用于按比例计算)
*/
@Schema(description = "费率", example = "0.05")
private BigDecimal feeRate;
/**
* 点击次数
*/
@Schema(description = "点击次数", example = "1000")
private Integer clickCount;
/**
* 展示次数
*/
@Schema(description = "展示次数", example = "10000")
private Integer impressionCount;
/**
* 转化次数
*/
@Schema(description = "转化次数", example = "50")
private Integer conversionCount;
/**
* 推广费发生日期
*/
@@ -47,10 +47,10 @@ public class PromotionFeePO {
private Long platformId;
/**
* 推广费名称
* 商品ID(可选)
*/
@TableField("fee_name")
private String feeName;
@TableField("product_id")
private Long productId;
/**
* 推广活动名称
@@ -64,36 +64,6 @@ public class PromotionFeePO {
@TableField("fee_amount")
private BigDecimal feeAmount;
/**
* 基础金额(用于按比例计算)
*/
@TableField("base_amount")
private BigDecimal baseAmount;
/**
* 费率(用于按比例计算)
*/
@TableField("fee_rate")
private BigDecimal feeRate;
/**
* 点击次数
*/
@TableField("click_count")
private Integer clickCount;
/**
* 展示次数
*/
@TableField("impression_count")
private Integer impressionCount;
/**
* 转化次数
*/
@TableField("conversion_count")
private Integer conversionCount;
/**
* 推广费发生日期
*/
@@ -63,16 +63,10 @@ public class PromotionFeeVO {
private String platformName;
/**
* 推广费名称
* 商品ID(可选)
*/
@Schema(description = "推广费名称", example = "直通车推广")
private String feeName;
/**
* 平台(淘宝、京东、拼多多等)
*/
@Schema(description = "平台", example = "淘宝")
private String platform;
@Schema(description = "商品ID(可选)", example = "1")
private Long productId;
/**
* 推广活动名称
@@ -86,36 +80,6 @@ public class PromotionFeeVO {
@Schema(description = "推广费金额", example = "1000.00")
private BigDecimal feeAmount;
/**
* 基础金额(用于按比例计算)
*/
@Schema(description = "基础金额", example = "10000.00")
private BigDecimal baseAmount;
/**
* 费率(用于按比例计算)
*/
@Schema(description = "费率", example = "0.05")
private BigDecimal feeRate;
/**
* 点击次数
*/
@Schema(description = "点击次数", example = "1000")
private Integer clickCount;
/**
* 展示次数
*/
@Schema(description = "展示次数", example = "10000")
private Integer impressionCount;
/**
* 转化次数
*/
@Schema(description = "转化次数", example = "50")
private Integer conversionCount;
/**
* 推广费发生日期
*/
@@ -130,6 +130,9 @@ public class PromotionFeeServiceImpl implements PromotionFeeService {
if (filter.getPlatformId() != null) {
wrapper.eq(PromotionFeePO::getPlatformId, filter.getPlatformId());
}
if (filter.getProductId() != null) {
wrapper.eq(PromotionFeePO::getProductId, filter.getProductId());
}
if (filter.getFeeDateStart() != null) {
wrapper.ge(PromotionFeePO::getFeeDate, filter.getFeeDateStart());
}