| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- <?php
- namespace App\Module\UrsPromotion\Logics;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- 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\Models\UrsUserMapping;
- use App\Module\UrsPromotion\Enums\UrsProfitType;
- use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
- use App\Module\Game\Services\RewardService;
- use App\Module\GameItems\Services\ItemService;
- 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 int $originalAmount 原始收益数量(整数)
- * @param int $itemId 收获的物品ID
- * @return array 分成记录
- */
- public function distributePlantingReward(
- int $userId,
- string $sourceType,
- int $sourceId,
- int $originalAmount,
- int $itemId
- ): 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,
- $itemId // 传递物品ID
- );
- 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()
- ]);
- throw $e;
- }
- return $profits;
- }
- /**
- * 获取用户的推荐关系链(三代)
- *
- * @param int $userId 用户ID
- * @return array [level => user_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;
- }
- // 开启事务(奖励组系统要求在事务中执行)
- DB::beginTransaction();
- try {
- // 使用奖励组系统发放奖励
- $rewardResult = RewardService::grantReward(
- $referrerId,
- $rewardGroupId,
- REWARD_SOURCE_TYPE::ACHIEVEMENT,
- $sourceType,
- $sourceId
- );
- if (!$rewardResult->success) {
- DB::rollBack();
- Log::error("推广奖励发放失败", [
- 'referrer_id' => $referrerId,
- 'reward_group_id' => $rewardGroupId,
- 'error' => $rewardResult->errorMessage
- ]);
- return null;
- }
- // 计算奖励总金额(用于记录)
- $totalRewardAmount = $this->calculateTotalRewardAmount($rewardResult->items);
- // 获取产生收益的农场用户ID
- $memberMapping = UrsUserMapping::where('urs_user_id', $memberId)->first();
- $memberFarmUserId = $memberMapping ? $memberMapping->user_id : null;
- // 获取获得收益的农场用户ID
- $referrerMapping = UrsUserMapping::where('urs_user_id', $referrerId)->first();
- $referrerFarmUserId = $referrerMapping ? $referrerMapping->user_id : null;
- // 创建收益记录
- $profit = UrsProfit::create([
- 'urs_user_id' => $referrerId,
- 'urs_promotion_member_id' => $memberId,
- 'promotion_member_farm_user_id' => $memberFarmUserId,
- 'farm_user_id' => $referrerFarmUserId,
- 'source_id' => $sourceId,
- 'source_type' => $sourceType,
- 'profit_type' => UrsProfitType::PROMOTION_REWARD->value,
- 'relation_level' => $relationLevel,
- 'original_amount' => '0', // 推广收益无原始金额概念
- 'profit_amount' => $totalRewardAmount,
- 'profit_rate' => 0, // 推广收益无比例概念
- 'reward_group_id' => $rewardResult->groupId,
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error("推广奖励发放事务失败", [
- 'referrer_id' => $referrerId,
- 'reward_group_id' => $rewardGroupId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- 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' => $totalRewardAmount
- ]);
- return $profit;
- }
- /**
- * 计算种植收益分成(按比例发放物品)
- *
- * @param int $referrerId 推荐人ID
- * @param int $memberId 团队成员ID
- * @param string $sourceType 收益来源类型
- * @param int $sourceId 收益来源ID
- * @param int $relationLevel 推荐层级
- * @param int $originalAmount 原始收益数量(整数)
- * @param array $talentConfigs 达人等级配置
- * @param int $itemId 收获的物品ID
- * @return UrsProfit|null
- */
- private function calculatePlantingReward(
- int $referrerId,
- int $memberId,
- string $sourceType,
- int $sourceId,
- int $relationLevel,
- int $originalAmount,
- array $talentConfigs,
- int $itemId
- ): ?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;
- }
- // 计算应该奖励的物品数量(向下取整)
- $rewardQuantity = (int)floor($originalAmount * $profitRate);
- if ($rewardQuantity <= 0) {
- Log::debug("推荐人 {$referrerId} 计算出的奖励数量为0", [
- 'original_amount' => $originalAmount,
- 'profit_rate' => $profitRate,
- 'calculated_quantity' => $originalAmount * $profitRate
- ]);
- return null;
- }
- // 开启事务发放物品奖励
- try {
- // 使用物品模块服务发放物品
- $addResult = ItemService::addItem($referrerId, $itemId, $rewardQuantity, [
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'source' => 'urs_planting_reward',
- 'details' => [
- 'member_id' => $memberId,
- 'relation_level' => $relationLevel,
- 'talent_level' => $talentLevel,
- 'profit_rate' => $profitRate,
- 'original_amount' => $originalAmount
- ]
- ]);
- if (!$addResult['success']) {
- Log::error("种植收益物品发放失败", [
- 'referrer_id' => $referrerId,
- 'item_id' => $itemId,
- 'quantity' => $rewardQuantity,
- 'error' => $addResult['message'] ?? '未知错误'
- ]);
- return null;
- }
- // 获取产生收益的农场用户ID
- $memberMapping = UrsUserMapping::where('urs_user_id', $memberId)->first();
- $memberFarmUserId = $memberMapping ? $memberMapping->user_id : null;
- // 获取获得收益的农场用户ID
- $referrerMapping = UrsUserMapping::where('urs_user_id', $referrerId)->first();
- $referrerFarmUserId = $referrerMapping ? $referrerMapping->user_id : null;
- // 创建收益记录
- $profit = UrsProfit::create([
- 'urs_user_id' => $referrerId,
- 'urs_promotion_member_id' => $memberId,
- 'promotion_member_farm_user_id' => $memberFarmUserId,
- 'farm_user_id' => $referrerFarmUserId,
- 'source_id' => $sourceId,
- 'source_type' => $sourceType,
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => $relationLevel,
- 'original_amount' => (string)$originalAmount,
- 'profit_amount' => (string)$rewardQuantity, // 记录实际发放的物品数量
- 'profit_rate' => $profitRate,
- 'reward_group_id' => null, // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- } catch (\Exception $e) {
- Log::error("种植收益发放事务失败", [
- 'referrer_id' => $referrerId,
- 'item_id' => $itemId,
- 'quantity' => $rewardQuantity,
- 'error' => $e->getMessage()
- ]);
- throw $e;
- }
- Log::info("URS种植收益记录创建", [
- 'profit_id' => $profit->id,
- 'referrer_id' => $referrerId,
- 'member_id' => $memberId,
- 'relation_level' => $relationLevel,
- 'talent_level' => $talentLevel,
- 'profit_rate' => $profitRate,
- 'item_id' => $itemId,
- 'reward_quantity' => $rewardQuantity,
- 'original_amount' => $originalAmount
- ]);
- return $profit;
- }
- /**
- * 计算奖励总金额(用于记录)
- *
- * @param array $rewardItems 奖励项列表
- * @return string
- */
- private function calculateTotalRewardAmount(array $rewardItems): string
- {
- $totalAmount = '0';
- foreach ($rewardItems as $item) {
- // 计算货币类型的奖励金额
- if (in_array($item->rewardType, ['fund', 'currency', 'fund_config'])) {
- $totalAmount = bcadd($totalAmount, (string)$item->quantity, 10);
- }
- // 计算物品类型的奖励价值(按物品售价计算,如果没有售价则按数量计算)
- elseif ($item->rewardType === 'item') {
- // 获取物品售价作为价值参考
- $itemValue = $this->getItemValue($item->targetId);
- $itemTotalValue = bcmul((string)$itemValue, (string)$item->quantity, 10);
- $totalAmount = bcadd($totalAmount, $itemTotalValue, 10);
- }
- // 其他类型奖励按数量计算基础价值
- else {
- $totalAmount = bcadd($totalAmount, (string)$item->quantity, 10);
- }
- }
- return $totalAmount;
- }
- /**
- * 获取物品价值(用于奖励金额计算)
- *
- * @param int $itemId 物品ID
- * @return string
- */
- private function getItemValue(int $itemId): string
- {
- try {
- // 查询物品售价
- $item = \App\Module\GameItems\Models\Item::find($itemId);
- if ($item && $item->sell_price > 0) {
- return (string)$item->sell_price;
- }
- // 如果没有售价,返回默认价值(可以根据物品类型调整)
- return '1.0000000000';
- } catch (\Exception $e) {
- Log::warning("获取物品价值失败", [
- 'item_id' => $itemId,
- 'error' => $e->getMessage()
- ]);
- return '1.0000000000';
- }
- }
- /**
- * 获取推广收益奖励组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;
- }
- }
- /**
- * 获取用户的收益统计
- *
- * @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('urs_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;
- }
- }
|