|
|
@@ -3,11 +3,13 @@ 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.CfPlatformRevenueLog;
|
|
|
+import com.etotem.cfc.entity.CfTeamPoolOrder;
|
|
|
import com.etotem.cfc.entity.FamilyMember;
|
|
|
import com.etotem.cfc.entity.OctopusEffectKeyPerson;
|
|
|
import com.etotem.cfc.entity.OctopusEffectRecord;
|
|
|
@@ -21,6 +23,7 @@ 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.CfTeamPoolOrderMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
import com.etotem.cfc.mapper.MembershipLevelMapper;
|
|
|
import com.etotem.cfc.mapper.ProductGiftRuleMapper;
|
|
|
@@ -83,6 +86,9 @@ public class CfCommissionService {
|
|
|
/** 间接销售奖规则编码(cf_earn_rule.code) */
|
|
|
public static final String RULE_INDIRECT_SALES = "F03b";
|
|
|
|
|
|
+ /** 团队分润池规则编码(cf_earn_rule.code,分润 v2 §3.1:成本 × T% 池内瓜分) */
|
|
|
+ public static final String RULE_TEAM_POOL = "F03c";
|
|
|
+
|
|
|
/** F03a 团队口径最大层级(§4.1.1:团队 8 层以内) */
|
|
|
private static final int TEAM_DEPTH = 8;
|
|
|
|
|
|
@@ -132,6 +138,10 @@ public class CfCommissionService {
|
|
|
private ObjectMapper objectMapper;
|
|
|
@Resource
|
|
|
private CfPlatformRevenueLogMapper cfPlatformRevenueLogMapper;
|
|
|
+ @Resource
|
|
|
+ private CfTeamPoolOrderMapper cfTeamPoolOrderMapper;
|
|
|
+ @Resource
|
|
|
+ private TeamShareConfigService teamShareConfigService;
|
|
|
|
|
|
/**
|
|
|
* 通用分佣(双返):当前人 + 推荐人。
|
|
|
@@ -158,6 +168,8 @@ public class CfCommissionService {
|
|
|
// 推荐人分润(两级销售奖)
|
|
|
distributeToReferrers(orderId, orderType, buyerUserId, buyerFamilyId,
|
|
|
orderAmountCent, paidAmountCent, productId);
|
|
|
+ // ⑤ 团队池建池(分润 v2:成本 × T%,从成本侧切出、不占 base;uk_order 幂等)
|
|
|
+ settleTeamPool(orderId, orderType, buyerUserId, buyerFamilyId, paidAmountCent, productId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -183,6 +195,8 @@ 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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -267,6 +281,142 @@ public class CfCommissionService {
|
|
|
return base > 0 ? 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={}, scope={}",
|
|
|
+ orderType, orderId, poolCent, tBps, pool.getConfigScope());
|
|
|
+ } 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 推荐人分润:两级销售奖(F03a 直接销售奖 + F03b 间接销售奖)。
|
|
|
*
|