|
|
@@ -3,10 +3,12 @@ package com.etotem.cfc.service;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.etotem.cfc.config.ConfigService;
|
|
|
import com.etotem.cfc.constant.CfConfigKeys;
|
|
|
+import com.etotem.cfc.context.MiniAppContextHolder;
|
|
|
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.CfTeamPoolOrder;
|
|
|
import com.etotem.cfc.entity.FamilyMember;
|
|
|
import com.etotem.cfc.entity.OctopusEffectKeyPerson;
|
|
|
import com.etotem.cfc.entity.OctopusEffectRecord;
|
|
|
@@ -19,6 +21,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.CfTeamPoolOrderMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
import com.etotem.cfc.mapper.MembershipLevelMapper;
|
|
|
import com.etotem.cfc.mapper.ProductGiftRuleMapper;
|
|
|
@@ -81,6 +84,9 @@ public class CfCommissionService {
|
|
|
/** 间接销售奖规则编码(cf_earn_rule.code) */
|
|
|
public static final String RULE_INDIRECT_SALES = "F03b";
|
|
|
|
|
|
+ /** 团队分润池规则编码(cf_earn_rule.code,分润 v2 §3.3;T06 发放接入使用) */
|
|
|
+ public static final String RULE_TEAM_POOL = "F03c";
|
|
|
+
|
|
|
/** F03a 团队口径最大层级(§4.1.1:团队 8 层以内) */
|
|
|
private static final int TEAM_DEPTH = 8;
|
|
|
|
|
|
@@ -122,6 +128,10 @@ public class CfCommissionService {
|
|
|
@Resource
|
|
|
private PpointConsumptionLogMapper ppointConsumptionLogMapper;
|
|
|
@Resource
|
|
|
+ private CfTeamPoolOrderMapper cfTeamPoolOrderMapper;
|
|
|
+ @Resource
|
|
|
+ private TeamShareConfigService teamShareConfigService;
|
|
|
+ @Resource
|
|
|
private ConfigService configService;
|
|
|
@Resource
|
|
|
private ObjectMapper objectMapper;
|
|
|
@@ -151,6 +161,8 @@ public class CfCommissionService {
|
|
|
// 推荐人分润(两级销售奖)
|
|
|
distributeToReferrers(orderId, orderType, buyerUserId, buyerFamilyId,
|
|
|
orderAmountCent, paidAmountCent, productId);
|
|
|
+ // ⑤ 团队池建池(分润 v2:成本 × T%,从成本侧切出、不占 base;uk_order 幂等)
|
|
|
+ settleTeamPool(orderId, orderType, buyerUserId, buyerFamilyId, paidAmountCent, productId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -176,6 +188,168 @@ public class CfCommissionService {
|
|
|
earnBuyerCf(orderId, orderType, buyerUserId, paidAmountCent);
|
|
|
distributeToReferrers(orderId, orderType, buyerUserId, buyerFamilyId,
|
|
|
orderAmountCent, paidAmountCent, null);
|
|
|
+ // ⑤ 团队池建池(会员/订阅/测评等非商品订单:成本 = 实付 × non_goods_cost_rate)
|
|
|
+ settleTeamPool(orderId, orderType, buyerUserId, buyerFamilyId, paidAmountCent, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 可分配额 base 计算(分润 v2 §3.1 / §1.1,T03 交付、T04 建池结算复用)。
|
|
|
+ *
|
|
|
+ * <p>base = A − R0 − R1_等值 − costCent:
|
|
|
+ * <ul>
|
|
|
+ * <li>R0 = floor(A × 1000 / 10000)(平台抽成 10%,TAKE_BPS=1000);</li>
|
|
|
+ * <li>R1_等值 = floor(A / 100)(消费返 CF 1 元 = 100 CF,等值成本);</li>
|
|
|
+ * <li>base < 0 → 0(不得为负)。</li>
|
|
|
+ * </ul>
|
|
|
+ * 纯入参计算、不触任何注入服务——契约级单测以裸实例反射调用。</p>
|
|
|
+ */
|
|
|
+ public int calcDistributableBase(int amountCent, int costCent) {
|
|
|
+ if (amountCent <= 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int a = amountCent;
|
|
|
+ int cost = costCent;
|
|
|
+ // R0 = floor(A × TAKE_BPS / 10000),TAKE_BPS=1000(§1.1),long 防溢出
|
|
|
+ int r0 = (int) ((long) a * 1000 / 10000);
|
|
|
+ // R1_等值 = floor(A / 100)(1 元 = 100 CF)
|
|
|
+ int r1Equiv = a / 100;
|
|
|
+ long base = (long) a - r0 - r1Equiv - cost;
|
|
|
+ return base > 0 ? (int) base : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 团队池建池(分润 v2 §3.1 结算顺序 ⑤,能力键 cf.teamshare.createPool,T04)。
|
|
|
+ *
|
|
|
+ * <p>解析 T({@link TeamShareConfigService}:MINIAPP→SYSTEM→GLOBAL→兜底 3000bps)→ 算池金额
|
|
|
+ * (商品 = Σ floor(C × qty × T_bps / 10000);非商品 = floor(实付 × non_goods_cost_rate × T_bps / 10000))
|
|
|
+ * → 幂等建 {@code cf_team_pool_order}(唯一键 {@code uk_order(order_type, order_id)},重复结算命中
|
|
|
+ * 唯一键直接返回,设计 §7 风险 1 三层兜底第一层)。池内瓜分(distributeTeamPool)由 T05 能力键
|
|
|
+ * {@code cf.teamshare.weightedShare} 承接。</p>
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public void settleTeamPool(Long orderId, String orderType, Long buyerUserId, Long buyerFamilyId,
|
|
|
+ Integer paidAmountCent, Long productId) {
|
|
|
+ try {
|
|
|
+ if (orderId == null || orderType == null || buyerUserId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 依赖注入缺省(既有单测裸实例/部分 mock)时静默跳过,不影响既有结算链路
|
|
|
+ if (cfTeamPoolOrderMapper == null || teamShareConfigService == null || configService == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 团队池总开关(cf.team_pool.enabled,默认 true)
|
|
|
+ if (!configService.getBooleanConfig(CfConfigKeys.TEAM_POOL_ENABLED, true)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 解析 T:MINIAPP(mp_code) → SYSTEM(商品所属 distribution_system_id) → GLOBAL → 兜底 3000
|
|
|
+ String mpCode = MiniAppContextHolder.mpCode();
|
|
|
+ Long distributionSystemId = resolveProductDistributionSystemId(orderType, productId);
|
|
|
+ int tBps = teamShareConfigService.resolveTbps(mpCode, distributionSystemId);
|
|
|
+ if (tBps <= 0) {
|
|
|
+ log.info("团队池比例 T<=0,跳过建池: orderType={}, orderId={}", orderType, orderId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 2. 算池金额(从成本侧切出,独立于 base;亏损仓仍按成本 × T% 照常发)
|
|
|
+ int poolCent = calcTeamPoolCent(orderId, orderType, paidAmountCent, productId, tBps);
|
|
|
+ if (poolCent <= 0) {
|
|
|
+ log.info("团队池金额为 0,跳过建池: orderType={}, orderId={}", orderType, orderId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 3. 幂等建池(uk_order(order_type, order_id),重复结算命中唯一键直接返回)
|
|
|
+ CfTeamPoolOrder pool = new CfTeamPoolOrder();
|
|
|
+ pool.setOrderId(orderId);
|
|
|
+ pool.setOrderType(orderType);
|
|
|
+ pool.setProductId(productId);
|
|
|
+ pool.setBuyerUserId(buyerUserId);
|
|
|
+ pool.setPoolCent(poolCent);
|
|
|
+ pool.setTBps(tBps);
|
|
|
+ pool.setConfigScope(resolveScope(mpCode, distributionSystemId));
|
|
|
+ pool.setStatus("pooled");
|
|
|
+ pool.setCreatedAt(new Date());
|
|
|
+ pool.setRemark("成本×T%建池");
|
|
|
+ cfTeamPoolOrderMapper.insert(pool);
|
|
|
+ log.info("团队池建池成功: orderType={}, orderId={}, poolCent={}分, tBps={}", orderType, orderId, poolCent, tBps);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ // 幂等:重复结算命中 uk_order,直接返回(设计 §7 风险 1 三层兜底第一层)
|
|
|
+ log.info("团队池已存在,幂等跳过建池: orderType={}, orderId={}", orderType, orderId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("团队池建池失败: orderType={}, orderId={}, err={}", orderType, orderId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 命中作用域标注(供 cf_team_pool_order.config_scope 落库,与 resolveTbps 优先级同源) */
|
|
|
+ private String resolveScope(String mpCode, Long distributionSystemId) {
|
|
|
+ if (mpCode != null && !mpCode.trim().isEmpty()) {
|
|
|
+ return TeamShareConfigService.SCOPE_MINIAPP;
|
|
|
+ }
|
|
|
+ if (distributionSystemId != null) {
|
|
|
+ return TeamShareConfigService.SCOPE_SYSTEM;
|
|
|
+ }
|
|
|
+ return TeamShareConfigService.SCOPE_GLOBAL;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 商品所属销售体系ID(SYSTEM 作用域键;非商品/无商品返回 null) */
|
|
|
+ private Long resolveProductDistributionSystemId(String orderType, Long productId) {
|
|
|
+ if (!"product".equals(orderType) || productId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Product p = productMapper.selectById(productId);
|
|
|
+ return p != null ? p.getDistributionSystemId() : null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 团队池金额(分):
|
|
|
+ * 商品订单 = Σ floor(C_i × qty_i × T_bps / 10000)(按行成本加总,无分摊歧义);
|
|
|
+ * 非商品订单 = floor(实付 × non_goods_cost_rate × T_bps / 10000)(成本模型,设计 §1.3)。
|
|
|
+ */
|
|
|
+ private int calcTeamPoolCent(Long orderId, String orderType, Integer paidAmountCent, Long productId, int tBps) {
|
|
|
+ int paid = paidAmountCent != null ? paidAmountCent : 0;
|
|
|
+ if (!"product".equals(orderType)) {
|
|
|
+ int ncd = configService.getIntConfig(CfConfigKeys.TEAM_POOL_NON_GOODS_COST_RATE, 5000);
|
|
|
+ long nonGoodsCost = (long) paid * ncd / 10000;
|
|
|
+ long pool = nonGoodsCost * tBps / 10000;
|
|
|
+ return pool > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) pool;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ProductOrder order = productOrderMapper.selectById(orderId);
|
|
|
+ if (order == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ List<OrderItem> items = orderItemMapper.findByOrderId(orderId);
|
|
|
+ if (items != null && !items.isEmpty()) {
|
|
|
+ long total = 0L;
|
|
|
+ for (OrderItem it : items) {
|
|
|
+ int qty = it.getQuantity() != null ? it.getQuantity() : 0;
|
|
|
+ if (qty <= 0 || it.getProductId() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Product p = productMapper.selectById(it.getProductId());
|
|
|
+ if (p == null || p.getPurchasePrice() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ total += (long) p.getPurchasePrice() * qty * tBps / 10000;
|
|
|
+ }
|
|
|
+ return total > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) total;
|
|
|
+ }
|
|
|
+ // 单商品订单:实付结算传 productId;成本价未配(null)时按 0 处理→池 0 跳过
|
|
|
+ if (productId == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ Product p = productMapper.selectById(productId);
|
|
|
+ if (p == null || p.getPurchasePrice() == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int qty = order.getQuantity() != null ? order.getQuantity() : 1;
|
|
|
+ long single = (long) p.getPurchasePrice() * qty * tBps / 10000;
|
|
|
+ return single > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) single;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("计算团队池金额失败: orderId={}, err={}", orderId, e.getMessage());
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|