| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <?php
- namespace App\Module\UrsPromotion\Logics;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Models\UrsProfit;
- use App\Module\UrsPromotion\Models\UrsTalentConfig;
- use App\Module\UrsPromotion\Enums\UrsProfitType;
- use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * URS收益分成逻辑类
- *
- * 处理URS推广系统的收益分成逻辑,支持三代推广关系
- */
- class UrsProfitLogic
- {
- /**
- * 计算并分发URS推广收益(按人头奖励)
- *
- * @param int $userId 新注册用户ID
- * @param string $sourceType 收益来源类型
- * @param int $sourceId 收益来源ID
- * @return array 分成记录
- */
- public function distributePromotionReward(
- int $userId,
- string $sourceType,
- int $sourceId
- ): array {
- $profits = [];
- try {
- // 获取用户的推荐关系链(三代)
- $referralChain = $this->getUserReferralChain($userId);
- if (empty($referralChain)) {
- Log::info("用户 {$userId} 无推荐关系,无需分成");
- return $profits;
- }
- // 获取达人等级配置
- $talentConfigs = $this->getTalentConfigs();
- // 为每一级推荐人发放奖励
- foreach ($referralChain as $level => $referrerId) {
- $profit = $this->calculatePromotionReward(
- $referrerId,
- $userId,
- $sourceType,
- $sourceId,
- $level,
- $talentConfigs
- );
- if ($profit) {
- $profits[] = $profit;
- }
- }
- Log::info("用户 {$userId} 推广收益分发完成", [
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'profits_count' => count($profits)
- ]);
- } catch (\Exception $e) {
- Log::error("URS推广收益分发失败", [
- 'user_id' => $userId,
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- }
- return $profits;
- }
- /**
- * 计算并分发URS种植收益(按比例分成)
- *
- * @param int $userId 产生收益的用户ID
- * @param string $sourceType 收益来源类型
- * @param int $sourceId 收益来源ID
- * @param string $originalAmount 原始收益金额
- * @return array 分成记录
- */
- public function distributePlantingReward(
- int $userId,
- string $sourceType,
- int $sourceId,
- string $originalAmount
- ): array {
- $profits = [];
-
- try {
- // 获取用户的推荐关系链(三代)
- $referralChain = $this->getUserReferralChain($userId);
-
- if (empty($referralChain)) {
- Log::info("用户 {$userId} 无推荐关系,无需分成");
- return $profits;
- }
-
- // 获取达人等级配置
- $talentConfigs = $this->getTalentConfigs();
-
- // 为每一级推荐人计算分成
- foreach ($referralChain as $level => $referrerId) {
- $profit = $this->calculatePlantingReward(
- $referrerId,
- $userId,
- $sourceType,
- $sourceId,
- $level,
- $originalAmount,
- $talentConfigs
- );
- if ($profit) {
- $profits[] = $profit;
- }
- }
- Log::info("用户 {$userId} 种植收益分成完成", [
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'original_amount' => $originalAmount,
- 'profits_count' => count($profits)
- ]);
- } catch (\Exception $e) {
- Log::error("URS种植收益分成失败", [
- 'user_id' => $userId,
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'error' => $e->getMessage()
- ]);
- }
-
- return $profits;
- }
-
- /**
- * 获取用户的推荐关系链(三代)
- *
- * @param int $userId 用户ID
- * @return array [level => referrer_id] 1:直推 2:间推 3:三推
- */
- private function getUserReferralChain(int $userId): array
- {
- $chain = [];
- $currentUserId = $userId;
-
- // 最多查找三代
- for ($level = 1; $level <= UrsPromotionRelationLevel::getMaxLevel(); $level++) {
- $referral = UrsUserReferral::where('user_id', $currentUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->first();
-
- if (!$referral) {
- break;
- }
-
- $chain[$level] = $referral->referrer_id;
- $currentUserId = $referral->referrer_id;
- }
-
- return $chain;
- }
-
- /**
- * 获取达人等级配置
- *
- * @return array [level => config]
- */
- private function getTalentConfigs(): array
- {
- static $configs = null;
-
- if ($configs === null) {
- $configs = UrsTalentConfig::where('status', UrsTalentConfig::STATUS_ENABLED)
- ->get()
- ->keyBy('level')
- ->toArray();
- }
-
- return $configs;
- }
-
- /**
- * 计算推广收益奖励(按人头)
- *
- * @param int $referrerId 推荐人ID
- * @param int $memberId 新注册用户ID
- * @param string $sourceType 收益来源类型
- * @param int $sourceId 收益来源ID
- * @param int $relationLevel 推荐层级
- * @param array $talentConfigs 达人等级配置
- * @return UrsProfit|null
- */
- private function calculatePromotionReward(
- int $referrerId,
- int $memberId,
- string $sourceType,
- int $sourceId,
- int $relationLevel,
- array $talentConfigs
- ): ?UrsProfit {
- // 获取推荐人的达人等级
- $talent = UrsUserTalent::where('user_id', $referrerId)->first();
- $talentLevel = $talent ? $talent->talent_level : 0;
- // 获取对应等级的配置
- $config = $talentConfigs[$talentLevel] ?? null;
- if (!$config) {
- Log::warning("推荐人 {$referrerId} 达人等级 {$talentLevel} 配置不存在");
- return null;
- }
- // 获取奖励组ID
- $rewardGroupId = $this->getPromotionRewardGroupId($config, $relationLevel);
- if (!$rewardGroupId) {
- Log::debug("推荐人 {$referrerId} 等级 {$talentLevel} 层级 {$relationLevel} 无奖励组配置");
- return null;
- }
- // TODO: 这里需要根据奖励组ID获取具体的奖励金额
- // 暂时使用固定金额作为示例
- $rewardAmount = $this->getRewardAmountByGroupId($rewardGroupId);
- // 创建收益记录
- $profit = UrsProfit::create([
- 'user_id' => $referrerId,
- 'promotion_member_id' => $memberId,
- 'source_id' => $sourceId,
- 'source_type' => $sourceType,
- 'profit_type' => UrsProfitType::PROMOTION_REWARD->value,
- 'relation_level' => $relationLevel,
- 'original_amount' => '0', // 推广收益无原始金额概念
- 'profit_amount' => $rewardAmount,
- 'profit_rate' => 0, // 推广收益无比例概念
- 'reward_group_id' => $rewardGroupId,
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- Log::info("URS推广收益记录创建", [
- 'profit_id' => $profit->id,
- 'referrer_id' => $referrerId,
- 'member_id' => $memberId,
- 'relation_level' => $relationLevel,
- 'talent_level' => $talentLevel,
- 'reward_group_id' => $rewardGroupId,
- 'reward_amount' => $rewardAmount
- ]);
- return $profit;
- }
- /**
- * 计算种植收益分成(按比例)
- *
- * @param int $referrerId 推荐人ID
- * @param int $memberId 团队成员ID
- * @param string $sourceType 收益来源类型
- * @param int $sourceId 收益来源ID
- * @param int $relationLevel 推荐层级
- * @param string $originalAmount 原始收益金额
- * @param array $talentConfigs 达人等级配置
- * @return UrsProfit|null
- */
- private function calculatePlantingReward(
- int $referrerId,
- int $memberId,
- string $sourceType,
- int $sourceId,
- int $relationLevel,
- string $originalAmount,
- array $talentConfigs
- ): ?UrsProfit {
- // 获取推荐人的达人等级
- $talent = UrsUserTalent::where('user_id', $referrerId)->first();
- $talentLevel = $talent ? $talent->talent_level : 0;
- // 获取对应等级的配置
- $config = $talentConfigs[$talentLevel] ?? null;
- if (!$config) {
- Log::warning("推荐人 {$referrerId} 达人等级 {$talentLevel} 配置不存在");
- return null;
- }
- // 获取分成比例
- $profitRate = $this->getPlantingRewardRate($config, $relationLevel);
- if ($profitRate <= 0) {
- Log::debug("推荐人 {$referrerId} 等级 {$talentLevel} 层级 {$relationLevel} 分成比例为0");
- return null;
- }
- // 计算分成金额
- $profitAmount = bcmul($originalAmount, (string)$profitRate, 10);
- // 创建收益记录
- $profit = UrsProfit::create([
- 'user_id' => $referrerId,
- 'promotion_member_id' => $memberId,
- 'source_id' => $sourceId,
- 'source_type' => $sourceType,
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => $relationLevel,
- 'original_amount' => $originalAmount,
- 'profit_amount' => $profitAmount,
- 'profit_rate' => $profitRate,
- 'reward_group_id' => null, // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- Log::info("URS种植收益记录创建", [
- 'profit_id' => $profit->id,
- 'referrer_id' => $referrerId,
- 'member_id' => $memberId,
- 'relation_level' => $relationLevel,
- 'talent_level' => $talentLevel,
- 'profit_rate' => $profitRate,
- 'profit_amount' => $profitAmount
- ]);
- return $profit;
- }
-
- /**
- * 获取推广收益奖励组ID
- *
- * @param array $config 达人等级配置
- * @param int $relationLevel 推荐层级
- * @return int|null
- */
- private function getPromotionRewardGroupId(array $config, int $relationLevel): ?int
- {
- // 使用新的独立字段结构
- switch ($relationLevel) {
- case 1: // 直推
- return $config['promotion_direct_group'] ?? null;
- case 2: // 间推
- return $config['promotion_indirect_group'] ?? null;
- case 3: // 三推
- return $config['promotion_third_group'] ?? null;
- default:
- return null;
- }
- }
- /**
- * 获取种植收益分成比例
- *
- * @param array $config 达人等级配置
- * @param int $relationLevel 推荐层级
- * @return float
- */
- private function getPlantingRewardRate(array $config, int $relationLevel): float
- {
- // 使用新的独立字段结构
- switch ($relationLevel) {
- case 1: // 直推
- return (float)($config['planting_direct_rate'] ?? 0);
- case 2: // 间推
- return (float)($config['planting_indirect_rate'] ?? 0);
- case 3: // 三推
- return (float)($config['planting_third_rate'] ?? 0);
- default:
- return 0;
- }
- }
- /**
- * 根据奖励组ID获取奖励金额
- *
- * TODO: 这里需要集成奖励组系统,暂时使用固定金额
- *
- * @param int $rewardGroupId 奖励组ID
- * @return string
- */
- private function getRewardAmountByGroupId(int $rewardGroupId): string
- {
- // 暂时使用固定金额映射,实际应该从奖励组系统获取
- $rewardAmounts = [
- 1001 => '50.0000000000', // 初级达人直推奖励
- 1002 => '30.0000000000', // 初级达人间推奖励
- 1003 => '10.0000000000', // 初级达人三推奖励
- 1004 => '80.0000000000', // 中级达人直推奖励
- 1005 => '50.0000000000', // 中级达人间推奖励
- 1006 => '20.0000000000', // 中级达人三推奖励
- 1007 => '120.0000000000', // 高级达人直推奖励
- 1008 => '80.0000000000', // 高级达人间推奖励
- 1009 => '40.0000000000', // 高级达人三推奖励
- 1010 => '200.0000000000', // 资深达人直推奖励
- 1011 => '120.0000000000', // 资深达人间推奖励
- 1012 => '60.0000000000', // 资深达人三推奖励
- 1013 => '300.0000000000', // 顶级达人直推奖励
- 1014 => '200.0000000000', // 顶级达人间推奖励
- 1015 => '100.0000000000', // 顶级达人三推奖励
- ];
- return $rewardAmounts[$rewardGroupId] ?? '0.0000000000';
- }
-
- /**
- * 获取用户的收益统计
- *
- * @param int $userId 用户ID
- * @param UrsProfitType|null $profitType 收益类型
- * @param string|null $startDate 开始日期
- * @param string|null $endDate 结束日期
- * @return array
- */
- public function getUserProfitStats(
- int $userId,
- ?UrsProfitType $profitType = null,
- ?string $startDate = null,
- ?string $endDate = null
- ): array {
- $query = UrsProfit::where('user_id', $userId)
- ->where('status', UrsProfit::STATUS_NORMAL);
-
- if ($profitType) {
- $query->where('profit_type', $profitType->value);
- }
-
- if ($startDate) {
- $query->where('created_at', '>=', $startDate);
- }
-
- if ($endDate) {
- $query->where('created_at', '<=', $endDate);
- }
-
- $profits = $query->get();
-
- $stats = [
- 'total_amount' => '0',
- 'total_count' => 0,
- 'by_type' => [],
- 'by_level' => [],
- ];
-
- foreach ($profits as $profit) {
- $stats['total_amount'] = bcadd($stats['total_amount'], $profit->profit_amount, 10);
- $stats['total_count']++;
-
- // 按收益类型统计
- $type = $profit->profit_type;
- if (!isset($stats['by_type'][$type])) {
- $stats['by_type'][$type] = ['amount' => '0', 'count' => 0];
- }
- $stats['by_type'][$type]['amount'] = bcadd($stats['by_type'][$type]['amount'], $profit->profit_amount, 10);
- $stats['by_type'][$type]['count']++;
-
- // 按推荐层级统计
- $level = $profit->relation_level;
- if (!isset($stats['by_level'][$level])) {
- $stats['by_level'][$level] = ['amount' => '0', 'count' => 0];
- }
- $stats['by_level'][$level]['amount'] = bcadd($stats['by_level'][$level]['amount'], $profit->profit_amount, 10);
- $stats['by_level'][$level]['count']++;
- }
-
- return $stats;
- }
- }
|