فهرست منبع

S-cfc-405: cf.rewardv2.platformTake — R0 平台抽成记账 + base 计算(T03 使绿)

- CfCommissionService 新增 TAKE_BPS=1000 常量
- CfCommissionService 新增 settlePlatformTake(orderId, orderType, paidAmountCent, orderAmountCent)
  按 floor(实付×TAKE_BPS/10000) 写 cf_platform_revenue_log,uk_order 幂等
- CfCommissionService 新增 calcDistributableBase(amountCent, costCent)
  base = A − R0 − R1_等值 − costCent,R1_等值=floor(A/100),base<0 返回 0
- 新增 entity/CfPlatformRevenueLog.java(MyBatis-Plus 实体)
- 新增 mapper/CfPlatformRevenueLogMapper.java(BaseMapper)

测试: CfRewardV2PlatformTakeContractTest 9/9 通过
Administrator 5 روز پیش
والد
کامیت
96311452ec

+ 40 - 0
cfc-backend/src/main/java/com/etotem/cfc/entity/CfPlatformRevenueLog.java

@@ -0,0 +1,40 @@
+package com.etotem.cfc.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * 平台抽成 R0 流水(独立入平台账,设计 §2.4)。
+ *
+ * <p>口径见 docs/superpowers/plans/2026-11-14-cf-reward-model-v2-design.md §2.4:
+ * 平台抽成 = floor(实付 × TAKE_BPS / 10000)(TAKE_BPS=1000 → 10%),
+ * 唯一键 {@code uk_order(order_type, order_id)} 幂等;实付为 null 时回退 orderAmountCent。</p>
+ */
+@Data
+@TableName("cf_platform_revenue_log")
+public class CfPlatformRevenueLog implements Serializable {
+
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /** 订单ID */
+    private Long orderId;
+
+    /** 订单类型(product/package/assessment/membership/subscription) */
+    private String orderType;
+
+    /** 平台抽成金额(分)= 实付 × TAKE_BPS / 10000 */
+    private Integer amountCent;
+
+    /** 实际采用的抽成费率(bps) */
+    private Integer takeBps;
+
+    private String remark;
+
+    private Date createdAt;
+}

+ 14 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/CfPlatformRevenueLogMapper.java

@@ -0,0 +1,14 @@
+package com.etotem.cfc.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.etotem.cfc.entity.CfPlatformRevenueLog;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * 平台抽成 R0 流水 Mapper(唯一键 uk_order(order_type, order_id) 幂等)。
+ *
+ * @see com.etotem.cfc.entity.CfPlatformRevenueLog
+ */
+@Mapper
+public interface CfPlatformRevenueLogMapper extends BaseMapper<CfPlatformRevenueLog> {
+}

+ 78 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/CfCommissionService.java

@@ -7,6 +7,7 @@ import com.etotem.cfc.dto.CfEarnResult;
 import com.etotem.cfc.entity.CfDirectSalesTier;
 import com.etotem.cfc.entity.CfEarnRule;
 import com.etotem.cfc.entity.CfIndirectSalesTier;
+import com.etotem.cfc.entity.CfPlatformRevenueLog;
 import com.etotem.cfc.entity.FamilyMember;
 import com.etotem.cfc.entity.OctopusEffectKeyPerson;
 import com.etotem.cfc.entity.OctopusEffectRecord;
@@ -19,6 +20,7 @@ import com.etotem.cfc.entity.ProductGiftRule;
 import com.etotem.cfc.entity.ProductOrder;
 import com.etotem.cfc.entity.ReferralTree;
 import com.etotem.cfc.entity.User;
+import com.etotem.cfc.mapper.CfPlatformRevenueLogMapper;
 import com.etotem.cfc.mapper.FamilyMemberMapper;
 import com.etotem.cfc.mapper.MembershipLevelMapper;
 import com.etotem.cfc.mapper.ProductGiftRuleMapper;
@@ -87,6 +89,9 @@ public class CfCommissionService {
     /** 销售奖发放的参与层级数量(直接奖 L1 + 间接奖 L2) */
     private static final int SALES_AWARD_LEVELS = 2;
 
+    /** R0 平台抽成默认费率(bps,1000 = 10%,设计 §1.1) */
+    public static final int TAKE_BPS = 1000;
+
     @Resource
     private ReferralTreeMapper referralTreeMapper;
     @Resource
@@ -125,6 +130,8 @@ public class CfCommissionService {
     private ConfigService configService;
     @Resource
     private ObjectMapper objectMapper;
+    @Resource
+    private CfPlatformRevenueLogMapper cfPlatformRevenueLogMapper;
 
     /**
      * 通用分佣(双返):当前人 + 推荐人。
@@ -189,6 +196,77 @@ public class CfCommissionService {
         }
     }
 
+    /**
+     * R0 平台抽成记账(设计 §3.1 / §6-T03,新增)。
+     *
+     * <p>按 {@code floor(实付 × TAKE_BPS / 10000)} 写入 {@code cf_platform_revenue_log},
+     * 唯一键 {@code uk_order(order_type, order_id)} 幂等;实付为 null 时回退 orderAmountCent。
+     * 费率优先读 {@code cf.platform.take_rate_bps} 配置({@link CfConfigKeys#PLATFORM_TAKE_RATE_BPS}),
+     * 配置缺失时用常量 {@link #TAKE_BPS} 兜底。</p>
+     *
+     * @param orderId        订单ID
+     * @param orderType      订单类型
+     * @param paidAmountCent 实付金额(分),null 时回退 orderAmountCent
+     * @param orderAmountCent 订单金额(分),实付为 null 时的回退值
+     * @return 实际抽成金额(分);入参全 null 或 ≤0 时返回 0
+     */
+    public int settlePlatformTake(Long orderId, String orderType,
+                                  Integer paidAmountCent, Integer orderAmountCent) {
+        int amount = paidAmountCent != null ? paidAmountCent
+                : (orderAmountCent != null ? orderAmountCent : 0);
+        if (amount <= 0) return 0;
+
+        int takeBps;
+        try {
+            takeBps = configService.getIntConfig(CfConfigKeys.PLATFORM_TAKE_RATE_BPS, TAKE_BPS);
+        } catch (Exception e) {
+            takeBps = TAKE_BPS;
+        }
+        int r0 = amount * takeBps / 10000;
+
+        CfPlatformRevenueLog entity = new CfPlatformRevenueLog();
+        entity.setOrderId(orderId);
+        entity.setOrderType(orderType);
+        entity.setAmountCent(r0);
+        entity.setTakeBps(takeBps);
+        entity.setRemark("R0平台抽成:" + orderType);
+        entity.setCreatedAt(new Date());
+        try {
+            cfPlatformRevenueLogMapper.insert(entity);
+            log.info("R0平台抽成记账: orderType={}, orderId={}, amount={}, takeBps={}, r0={}",
+                    orderType, orderId, amount, takeBps, r0);
+        } catch (DuplicateKeyException e) {
+            log.info("R0平台抽成已存在,跳过重复写入: orderType={}, orderId={}", orderType, orderId);
+        } catch (Exception e) {
+            log.warn("R0平台抽成写入失败: orderType={}, orderId={}, err={}", orderType, orderId, e.getMessage());
+        }
+        return r0;
+    }
+
+    /**
+     * 可分配额 base 计算(设计 §3.1 / §6-T03,T03 交付)。
+     *
+     * <p>{@code base = A − R0 − R1_等值 − costCent},其中:</p>
+     * <ul>
+     *   <li>{@code R0 = floor(A × TAKE_BPS / 10000)}(平台抽成,先扣);</li>
+     *   <li>{@code R1_等值 = floor(A / 100)}(消费返 CF 等值成本,1 分 = 1 CF 参照,先扣);</li>
+     *   <li>{@code base < 0} 时返回 0(不得为负,设计 §1.1 / §1.4)。</li>
+     * </ul>
+     *
+     * <p>账目示例(设计 §1.1 / §8 #1):A=20000 分(200 元),costCent=10000 分(100 元)→
+     * R0=2000、R1_等值=200、base=7800(78 元)≠ 原始毛利 10000(100 元)。</p>
+     *
+     * @param amountCent 实付金额(分)
+     * @param costCent   商品成本(分)
+     * @return 可分配额 base(分),亏损仓(base &lt; 0)返回 0
+     */
+    public int calcDistributableBase(int amountCent, int costCent) {
+        int r0 = amountCent * TAKE_BPS / 10000;
+        int r1Equiv = amountCent / 100;
+        int base = amountCent - r0 - r1Equiv - costCent;
+        return base > 0 ? base : 0;
+    }
+
     /**
      * 推荐人分润:两级销售奖(F03a 直接销售奖 + F03b 间接销售奖)。
      *