|
|
@@ -3,11 +3,14 @@ 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.CfTeamPoolShare;
|
|
|
import com.etotem.cfc.entity.FamilyMember;
|
|
|
import com.etotem.cfc.entity.OctopusEffectKeyPerson;
|
|
|
import com.etotem.cfc.entity.OctopusEffectRecord;
|
|
|
@@ -21,6 +24,8 @@ 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.CfTeamPoolShareMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
import com.etotem.cfc.mapper.MembershipLevelMapper;
|
|
|
import com.etotem.cfc.mapper.ProductGiftRuleMapper;
|
|
|
@@ -42,6 +47,8 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
@@ -83,6 +90,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 +142,12 @@ public class CfCommissionService {
|
|
|
private ObjectMapper objectMapper;
|
|
|
@Resource
|
|
|
private CfPlatformRevenueLogMapper cfPlatformRevenueLogMapper;
|
|
|
+ @Resource
|
|
|
+ private CfTeamPoolOrderMapper cfTeamPoolOrderMapper;
|
|
|
+ @Resource
|
|
|
+ private CfTeamPoolShareMapper cfTeamPoolShareMapper;
|
|
|
+ @Resource
|
|
|
+ private TeamShareConfigService teamShareConfigService;
|
|
|
|
|
|
/**
|
|
|
* 通用分佣(双返):当前人 + 推荐人。
|
|
|
@@ -158,6 +174,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 +201,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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -243,6 +263,19 @@ public class CfCommissionService {
|
|
|
return r0;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 零参结算入口(契约 cf.rewardv2.e2eExample:v2 结算四入口方法)。
|
|
|
+ *
|
|
|
+ * <p>无请求/订单上下文的探活形态:不产生 R0 记账动作,仅记录日志并返回 0;
|
|
|
+ * 有订单上下文时使用带参重载 {@link #settlePlatformTake(Long, String, Integer, Integer)}。</p>
|
|
|
+ *
|
|
|
+ * @return 0(无上下文不记账)
|
|
|
+ */
|
|
|
+ public int settlePlatformTake() {
|
|
|
+ log.info("[cf.rewardv2.e2eExample] settlePlatformTake() 零参入口无订单上下文,跳过 R0 记账(有上下文请走带参重载)。");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 可分配额 base 计算(设计 §3.1 / §6-T03,T03 交付)。
|
|
|
*
|
|
|
@@ -267,6 +300,391 @@ 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 三层兜底第一层)→ 层级树瓜分 {@link #distributeTeamPool(Long, String, Long, Long, Integer, Long)}
|
|
|
+ * (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());
|
|
|
+ // 4. 层级树瓜分(T05 cf.teamshare.weightedShare:档位权重瓜分 + 尾差守恒 + 同家庭跳过)
|
|
|
+ try {
|
|
|
+ distributeTeamPool(orderId, orderType, buyerUserId, buyerFamilyId, paidAmountCent, productId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("团队池瓜分失败(池已建,可后续补跑): orderType={}, orderId={}, err={}",
|
|
|
+ orderType, orderId, e.getMessage());
|
|
|
+ }
|
|
|
+ } 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.rewardv2.e2eExample:v2 结算四入口方法)。
|
|
|
+ *
|
|
|
+ * <p>无请求/订单上下文的探活形态:不产生建池动作,仅记录日志;
|
|
|
+ * 有订单上下文时使用带参重载
|
|
|
+ * {@link #settleTeamPool(Long, String, Long, Long, Integer, Long)}。</p>
|
|
|
+ */
|
|
|
+ public void settleTeamPool() {
|
|
|
+ log.info("[cf.rewardv2.e2eExample] settleTeamPool() 零参入口无订单上下文,跳过建池(有上下文请走带参重载)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 零参结算入口(契约 cf.rewardv2.e2eExample:v2 结算四入口方法)。
|
|
|
+ *
|
|
|
+ * <p>层级树瓜分(设计 §5:档位权重瓜分 + 尾差守恒 + 同家庭跳过)由能力键
|
|
|
+ * {@code cf.teamshare.weightedShare}(T05)承接带参实现;本零参入口为无上下文的
|
|
|
+ * 契约探活形态,仅记录日志。</p>
|
|
|
+ */
|
|
|
+ public void distributeTeamPool() {
|
|
|
+ log.info("[cf.rewardv2.e2eExample] distributeTeamPool() 零参入口无订单上下文,跳过瓜分"
|
|
|
+ + "(层级树瓜分由 T05 cf.teamshare.weightedShare 承接带参实现)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 层级树瓜分(分润 v2 设计 §5,能力键 {@code cf.teamshare.weightedShare},T05)。
|
|
|
+ *
|
|
|
+ * <p>由 {@link #settleTeamPool(Long, String, Long, Long, Integer, Long)} 建池后调用,内部完成:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li>按 {@code uk_order(order_type, order_id)} 幂等取池(池不存在 / 已瓜分 / 无人认领直接短路);</li>
|
|
|
+ * <li>取层级树成员(买家上溯,同家庭跳过,≤ TEAM_DEPTH=8,设计 §1.4 / §5 step 3);</li>
|
|
|
+ * <li>档位权重(与 F03a 直推档位同一口径:{@link CfReferralService#getTeamAnnualPpoints}(m, 8)
|
|
|
+ * → {@code matchDirectSalesTier},权重 = cf_direct_sales_tier.rate_percent,无档位 = 0);</li>
|
|
|
+ * <li>按 {@link #allocateTeamPoolShareByWeight(int[], int)} 加权瓜分(尾差由最后一人补齐,Σ = pool_cent);</li>
|
|
|
+ * <li>逐人 {@code earnByRule(memberId, "F03c", poolId, {amount: raw})} 发放(幂等键 uk_ref_type F03c + poolId)
|
|
|
+ * + {@code cf_team_pool_share} 幂等落库(uk_pool_member,设计 §7 风险 1 三层兜底第二层);</li>
|
|
|
+ * <li>池状态落库 {@code share_issued}(无档位 totalW ≤ 0 无人可分 → {@code unclaimed},不私发,设计 §5 step 4)。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p>参数 {@code paidCent} 为契约签名入参(§3.1 T05),池金额以 {@code cf_team_pool_order.pool_cent}
|
|
|
+ * 落库值为准(建池已完成);依赖注入缺省(裸实例/部分 mock)时静默跳过,不影响既有结算链路。</p>
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public void distributeTeamPool(Long orderId, String orderType, Long buyerUserId, Long buyerFamilyId,
|
|
|
+ Integer paidCent, Long productId) {
|
|
|
+ try {
|
|
|
+ if (orderId == null || orderType == null || buyerUserId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 依赖注入缺省(既有单测裸实例/部分 mock)时静默跳过,不影响既有结算链路
|
|
|
+ if (cfTeamPoolOrderMapper == null || cfTeamPoolShareMapper == null || referralTreeMapper == null
|
|
|
+ || userMapper == null || familyMemberMapper == null || cfReferralService == null
|
|
|
+ || cfBalanceService == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 幂等取池(uk_order(order_type, order_id):建池先行,池不存在直接返回)
|
|
|
+ CfTeamPoolOrder pool = cfTeamPoolOrderMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<CfTeamPoolOrder>()
|
|
|
+ .eq(CfTeamPoolOrder::getOrderId, orderId)
|
|
|
+ .eq(CfTeamPoolOrder::getOrderType, orderType)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (pool == null || pool.getId() == null) {
|
|
|
+ log.info("团队池不存在,跳过瓜分: orderType={}, orderId={}", orderType, orderId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 已瓜分 / 无人认领:幂等短路(设计 §7 风险 1)
|
|
|
+ if ("share_issued".equals(pool.getStatus()) || "unclaimed".equals(pool.getStatus())) {
|
|
|
+ log.info("团队池已处理(status={}),跳过重复瓜分: orderType={}, orderId={}",
|
|
|
+ pool.getStatus(), orderType, orderId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int poolCent = pool.getPoolCent() != null ? pool.getPoolCent() : 0;
|
|
|
+ if (poolCent <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 取层级树成员(买家上溯,同家庭跳过,≤ max_depth)
|
|
|
+ List<TeamPoolCandidate> candidates = new ArrayList<>();
|
|
|
+ List<ReferralTree> referrals = referralTreeMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<ReferralTree>()
|
|
|
+ .eq(ReferralTree::getMemberId, buyerUserId)
|
|
|
+ .orderByAsc(ReferralTree::getLevel));
|
|
|
+ if (referrals != null) {
|
|
|
+ Set<Long> processed = new HashSet<>();
|
|
|
+ for (ReferralTree ref : referrals) {
|
|
|
+ if (candidates.size() >= TEAM_DEPTH) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Long parentId = ref.getParentId();
|
|
|
+ if (parentId == null || processed.contains(parentId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ processed.add(parentId);
|
|
|
+ User parent = userMapper.selectById(parentId);
|
|
|
+ if (parent == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 同家庭跳过(设计 §1.4 / §5 step 3:团队池瓜分成员跳过同家庭)
|
|
|
+ if (buyerFamilyId != null && buyerFamilyId.equals(parent.getFamilyId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 档位权重(与 F03a 直推档位同一口径:团队 8 层滚动一年 P 点 → rate_percent)
|
|
|
+ int teamPp = cfReferralService.getTeamAnnualPpoints(parentId, TEAM_DEPTH);
|
|
|
+ CfDirectSalesTier tier = cfReferralService.matchDirectSalesTier(teamPp);
|
|
|
+ int weight = (tier == null || tier.getRatePercent() == null) ? 0 : tier.getRatePercent();
|
|
|
+ int layer = ref.getLevel() != null ? ref.getLevel() : candidates.size() + 1;
|
|
|
+ candidates.add(new TeamPoolCandidate(parentId, resolveMemberId(parentId), layer, weight));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (candidates.isEmpty()) {
|
|
|
+ // 无推荐链 → 无人可分 → 池归商家(unclaimed,不私发)
|
|
|
+ markPoolResult(pool, "unclaimed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 加权瓜分(纯算法:末位吸收尾差,Σ = poolCent,设计 §5 step 5 / §8#9)
|
|
|
+ int[] weights = new int[candidates.size()];
|
|
|
+ for (int i = 0; i < candidates.size(); i++) {
|
|
|
+ weights[i] = candidates.get(i).weight;
|
|
|
+ }
|
|
|
+ List<Integer> shares = allocateTeamPoolShareByWeight(weights, poolCent);
|
|
|
+ if (shares == null || shares.isEmpty()) {
|
|
|
+ // 无档位(totalW ≤ 0)→ 无人可分 → unclaimed(不私发,设计 §5 step 4)
|
|
|
+ markPoolResult(pool, "unclaimed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 逐人发放 + 明细落库(幂等兜底:uk_ref_type F03c+poolId / uk_pool_member)
|
|
|
+ long issued = 0L;
|
|
|
+ int lastIdx = shares.size() - 1;
|
|
|
+ for (int i = 0; i < shares.size(); i++) {
|
|
|
+ TeamPoolCandidate cand = candidates.get(i);
|
|
|
+ // 末位吸收尾差(按实际已发放额,与 §5 step 5 一致;非末位用纯算法值)
|
|
|
+ int raw = (i == lastIdx) ? (int) (poolCent - issued) : shares.get(i);
|
|
|
+ if (raw <= 0 || cand.memberId == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Map<String, Object> ctx = new HashMap<>();
|
|
|
+ ctx.put("amount", raw);
|
|
|
+ cfBalanceService.earnByRule(cand.memberId, RULE_TEAM_POOL, pool.getId(), ctx);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("团队池 F03c 发放失败: memberId={}, poolId={}, raw={}, err={}",
|
|
|
+ cand.memberId, pool.getId(), raw, e.getMessage());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ CfTeamPoolShare share = new CfTeamPoolShare();
|
|
|
+ share.setPoolId(pool.getId());
|
|
|
+ share.setMemberId(cand.memberId);
|
|
|
+ share.setUserId(cand.userId);
|
|
|
+ share.setLayer(cand.layer);
|
|
|
+ share.setRatePercent(cand.weight);
|
|
|
+ share.setWeight(cand.weight);
|
|
|
+ share.setShareCent(raw);
|
|
|
+ share.setStatus("issued");
|
|
|
+ share.setCreatedAt(new Date());
|
|
|
+ cfTeamPoolShareMapper.insert(share);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ log.info("团队池份额已存在,幂等跳过: poolId={}, memberId={}", pool.getId(), cand.memberId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("团队池份额落库失败: poolId={}, memberId={}, err={}",
|
|
|
+ pool.getId(), cand.memberId, e.getMessage());
|
|
|
+ }
|
|
|
+ issued += raw;
|
|
|
+ }
|
|
|
+ markPoolResult(pool, "share_issued");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("团队池瓜分失败: orderType={}, orderId={}, err={}", orderType, orderId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 纯加权瓜分(设计 §5 step 5 算法抽离,能力键 {@code cf.teamshare.weightedShare} 数学锚点 §8#1/#9)。
|
|
|
+ *
|
|
|
+ * <p>池金额按成员档位权重比例分配:每人 {@code raw = floor(poolCent × w / totalW)},末位成员吸收尾差
|
|
|
+ * ({@code raw = poolCent − Σ(前序) })保证 Σ = poolCent(尾差守恒,金额不蒸发)。与档位体系解耦,
|
|
|
+ * 权重数据源(rate_percent)由调用方 {@link #distributeTeamPool(Long, String, Long, Long, Integer, Long)}
|
|
|
+ * 经 {@code CfReferralService#matchDirectSalesTier} 提供,便于本算法独立单测。</p>
|
|
|
+ *
|
|
|
+ * <p>命题验证(§8#1):池 3000 分、权重 {70, 50}、totalW=120 →
|
|
|
+ * {@code raw₁ = floor(3000×70/120) = 1750;raw₂ = 3000 − 1750 = 1250}(末位补齐尾差)。</p>
|
|
|
+ *
|
|
|
+ * @param weights 各成员档位权重(= cf_direct_sales_tier.rate_percent;非负,全 0 / 空 = 无档位)
|
|
|
+ * @param poolCent 团队池金额(分,≥ 0)
|
|
|
+ * @return 每人分得(分,长度 = 成员数,末位含尾差);totalW ≤ 0(无档位)返回空列表——调用方将池置
|
|
|
+ * unclaimed(不私发,设计 §5 step 4/5)
|
|
|
+ */
|
|
|
+ public List<Integer> allocateTeamPoolShareByWeight(int[] weights, int poolCent) {
|
|
|
+ if (weights == null || weights.length == 0) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ long totalW = 0L;
|
|
|
+ for (int w : weights) {
|
|
|
+ totalW += w;
|
|
|
+ }
|
|
|
+ if (totalW <= 0) {
|
|
|
+ // 无档位(totalW ≤ 0):无人可分,调用方将池置 unclaimed(不私发)
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<Integer> shares = new ArrayList<>(weights.length);
|
|
|
+ long issued = 0L;
|
|
|
+ for (int i = 0; i < weights.length; i++) {
|
|
|
+ int raw;
|
|
|
+ if (i == weights.length - 1) {
|
|
|
+ // 末位成员吸收尾差:Σ = poolCent(尾差守恒,设计 §8#9)
|
|
|
+ raw = (int) (poolCent - issued);
|
|
|
+ } else {
|
|
|
+ raw = (int) ((long) poolCent * weights[i] / totalW);
|
|
|
+ }
|
|
|
+ shares.add(raw);
|
|
|
+ issued += raw;
|
|
|
+ }
|
|
|
+ return shares;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 池结算状态落库(share_issued=已瓜分 / unclaimed=无成员可瓜分,设计 §2.2 状态机) */
|
|
|
+ private void markPoolResult(CfTeamPoolOrder pool, String status) {
|
|
|
+ try {
|
|
|
+ pool.setStatus(status);
|
|
|
+ pool.setSettledAt(new Date());
|
|
|
+ cfTeamPoolOrderMapper.updateById(pool);
|
|
|
+ log.info("团队池状态落库: poolId={}, status={}", pool.getId(), status);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("团队池状态落库失败: poolId={}, status={}, err={}", pool.getId(), status, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 团队池瓜分候选成员(推荐链层级 + 档位权重 + 成员档案),供 §5 步骤 4/5 使用 */
|
|
|
+ private static class TeamPoolCandidate {
|
|
|
+ final Long userId;
|
|
|
+ final Long memberId;
|
|
|
+ final int layer;
|
|
|
+ final int weight;
|
|
|
+
|
|
|
+ TeamPoolCandidate(Long userId, Long memberId, int layer, int weight) {
|
|
|
+ this.userId = userId;
|
|
|
+ this.memberId = memberId;
|
|
|
+ this.layer = layer;
|
|
|
+ this.weight = weight;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 命中作用域标注(供 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 间接销售奖)。
|
|
|
*
|