|
|
@@ -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 < 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 间接销售奖)。
|
|
|
*
|