level_id],用于 B2 校验 * @param int $qty 订单中报单商品数量,积分按数量倍乘(B3) */ public function reward(int $orderUid, string $orderId, int $orderDbId = 0, array $preUpgradeLevels = [], int $qty = 1): void { $qty = max(1, $qty); try { // 幂等检查:若该订单已有积分奖励记录则跳过,防止重复发放 $exists = Db::name('points_release_log') ->where('order_id', $orderId) ->whereIn('type', ['reward_direct', 'reward_umbrella']) ->count(); if ($exists > 0) { Log::info("[PointsReward] 订单 {$orderId} 已有积分奖励记录,跳过重复发放"); return; } $buyer = $this->userDao->get($orderUid); if (!$buyer || !$buyer['spread_uid']) { return; } // 取买家自身的直推奖励积分作为级差下限,确保第一级父节点只拿差额 $buyerLevelId = array_key_exists($orderUid, $preUpgradeLevels) ? $preUpgradeLevels[$orderUid] : (int)($buyer['agent_level'] ?? 0); $buyerDirectReward = $this->agentLevelServices->getDirectRewardPoints($buyerLevelId); $this->propagateReward((int)$buyer['spread_uid'], $orderUid, $orderId, $buyerDirectReward, 0, $orderDbId, $preUpgradeLevels, $qty); } catch (\Throwable $e) { Log::error("[PointsReward] 积分奖励失败 orderUid={$orderUid} orderId={$orderId}: " . $e->getMessage()); } } /** * 向上递归发放直推积分奖励(标准逐级级差) * * 算法:沿推荐链向上遍历,每个分销等级大于0的祖先获得 * max(0, 自身 direct_reward_points - 链中已见最高 direct_reward_points) * qty * 非分销员(grade=0)跳过但继续向上传递,$lowerDirectReward 不变。 * * @param int $uid 当前被奖励用户 * @param int $fromUid 触发方(下级)用户 ID * @param string $orderId 来源订单号 * @param int $lowerDirectReward 链中已被下级拿走的最高 direct_reward_points(级差下限) * @param int $depth 递归深度(防止无限递归) * @param int $orderDbId 订单表主键 id * @param array $preUpgradeLevels 升级前各用户的 agent_level 快照 [uid => level_id](B2) * @param int $qty 报单商品数量,积分按数量倍乘(B3) */ private function propagateReward( int $uid, int $fromUid, string $orderId, int $lowerDirectReward, int $depth = 0, int $orderDbId = 0, array $preUpgradeLevels = [], int $qty = 1 ): void { if ($depth >= 10 || $uid <= 0) { return; } $user = $this->userDao->get($uid); if (!$user) { return; } // fsgx B2:使用升级前的 agent_level 判断资格,避免触发升级的那笔订单就给新等级积分 $agentLevelId = array_key_exists($uid, $preUpgradeLevels) ? $preUpgradeLevels[$uid] : (int)($user['agent_level'] ?? 0); $grade = $this->agentLevelServices->getGradeByLevelId($agentLevelId); if ($grade === 0) { // 非分销员:跳过奖励,但继续向上传递,$lowerDirectReward 保持不变 if ($user['spread_uid']) { $this->propagateReward((int)$user['spread_uid'], $uid, $orderId, $lowerDirectReward, $depth + 1, $orderDbId, $preUpgradeLevels, $qty); } return; } $directReward = $this->agentLevelServices->getDirectRewardPoints($agentLevelId); // 级差:本节点只拿超出下级已取走部分的差额,再乘以数量 // 间接上级(depth>0,即非直推父节点)受伞下奖励开关控制(hjf_umbrella_reward_enable,默认关闭) if ($depth > 0) { $umbrellaRewardEnable = (int)sys_config('hjf_umbrella_reward_enable', 0); $actual = $umbrellaRewardEnable ? max(0, $directReward - $lowerDirectReward) * $qty : 0; } else { $actual = max(0, $directReward - $lowerDirectReward) * $qty; } if ($actual > 0) { $this->grantFrozenPoints( $uid, $actual, $orderId, 'reward_direct', '直推奖励(级差)' . " x{$qty} - 来源订单 {$orderId}", $orderDbId ); } // 向上传递时,下限取当前节点与已有下限中的较大值,确保上级只拿增量 $nextLower = max($directReward, $lowerDirectReward); if ($user['spread_uid']) { $this->propagateReward( (int)$user['spread_uid'], $uid, $orderId, $nextLower, $depth + 1, $orderDbId, $preUpgradeLevels, $qty ); } } /** * 写入待释放积分(frozen_points)并记录明细 */ private function grantFrozenPoints( int $uid, int $points, string $orderId, string $type, string $mark, int $orderDbId = 0 ): void { Db::transaction(function () use ($uid, $points, $orderId, $type, $mark, $orderDbId) { $this->userDao->bcInc($uid, 'frozen_points', (string)$points, 'uid'); $this->logDao->save([ 'uid' => $uid, 'points' => $points, 'pm' => 1, 'type' => $type, 'title' => ($type === 'reward_direct') ? '直推奖励' : '伞下奖励', 'mark' => $mark, 'status' => 'frozen', 'order_id' => $orderId, ]); // PRD §3.3:营销后台积分日志读 eb_user_bill,双写待释放积分明细(不增加可消费 integral) $integralBalance = (int)($this->userDao->value(['uid' => $uid], 'integral') ?: 0); $billType = ($type === 'reward_direct') ? 'hjf_reward_direct_integral' : 'hjf_reward_umbrella_integral'; $this->userBillServices->income($billType, $uid, $points, $integralBalance, $orderDbId, 0, $mark); }); } }