| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?php
- namespace App\Module\UrsPromotion\Logics;
- use App\Module\UrsPromotion\Enums\UrsProfitType;
- use App\Module\UrsPromotion\Models\UrsProfit;
- use App\Module\UrsPromotion\Services\UrsReferralService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Models\UrsTalentConfig;
- use App\Module\Game\Services\RewardService;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- /**
- * URS推荐奖励补发逻辑层
- *
- * 处理用户首次进入农场时的推荐奖励补发核心逻辑
- */
- class UrsBackfillRewardLogic
- {
- /**
- * 为用户补发推荐奖励
- *
- * @param int $ursUserId URS用户ID
- * @param int $farmUserId 农场用户ID
- * @return array 补发结果
- */
- public function backfillPromotionReward(int $ursUserId, int $farmUserId): array
- {
- // 1. 检查用户是否需要补发奖励
- if (!$this->needsBackfillReward($ursUserId)) {
- return [
- 'success' => false,
- 'message' => '用户无下级或不需要补发奖励',
- 'backfilled_count' => 0,
- 'total_subordinates' => 0,
- 'talent_level' => 0,
- 'reward_group_id' => 0
- ];
- }
- // 2. 获取用户下级统计
- $subordinateStats = $this->getSubordinateStats($ursUserId);
- $totalSubordinates = $subordinateStats['total_count'];
- if ($totalSubordinates <= 0) {
- return [
- 'success' => false,
- 'message' => '用户无下级,无需补发奖励',
- 'backfilled_count' => 0,
- 'total_subordinates' => 0,
- 'talent_level' => 0,
- 'reward_group_id' => 0
- ];
- }
- // 3. 获取用户当前达人等级
- $talentLevel = $this->getUserTalentLevel($ursUserId);
- // 4. 获取对应等级的推荐奖励组配置
- $rewardGroups = $this->getPromotionRewardGroups($talentLevel);
- if (empty($rewardGroups)) {
- return [
- 'success' => false,
- 'message' => "达人等级 {$talentLevel} 无对应的奖励组配置",
- 'backfilled_count' => 0,
- 'total_subordinates' => $totalSubordinates,
- 'talent_level' => $talentLevel,
- 'reward_details' => []
- ];
- }
- // 5. 按推荐层级分别发放奖励
- try {
- DB::beginTransaction();
- $rewardDetails = [];
- $totalBackfilledCount = 0;
- // 直推奖励
- if ($subordinateStats['direct_count'] > 0 && $rewardGroups['direct'] > 0) {
- $directResult = RewardService::grantReward(
- $farmUserId,
- $rewardGroups['direct'],
- REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL,
- $ursUserId,
- $subordinateStats['direct_count']
- );
- if (!$directResult->success) {
- DB::rollBack();
- return [
- 'success' => false,
- 'message' => "直推奖励发放失败: {$directResult->errorMessage}",
- 'backfilled_count' => 0,
- 'total_subordinates' => $totalSubordinates,
- 'talent_level' => $talentLevel,
- 'reward_details' => []
- ];
- }
- $rewardDetails['direct'] = [
- 'type' => '直推奖励',
- 'count' => $subordinateStats['direct_count'],
- 'reward_group_id' => $rewardGroups['direct'],
- 'items_count' => count($directResult->items)
- ];
- $totalBackfilledCount += count($directResult->items);
- // 创建收益记录
- $profit = UrsProfit::create([
- 'urs_user_id' => $ursUserId,
- 'urs_promotion_member_id' => 0,
- 'promotion_member_farm_user_id' => 0,
- 'farm_user_id' => $farmUserId,
- 'source_id' => $directResult->sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => 1,
- 'original_amount' => (string)0,
- 'profit_amount' => (string)0, // 记录实际发放的物品数量
- 'profit_rate' => 1,
- 'reward_group_id' => $rewardGroups['direct'], // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- }else{
- // 创建收益记录-跳过
- $profit = UrsProfit::create([
- 'urs_user_id' => $ursUserId,
- 'urs_promotion_member_id' => 0,
- 'promotion_member_farm_user_id' => 0,
- 'farm_user_id' => $farmUserId,
- 'source_id' => 0,
- 'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => 1,
- 'original_amount' => (string)0,
- 'profit_amount' => (string)0, // 记录实际发放的物品数量
- 'profit_rate' => 1,
- 'reward_group_id' => $rewardGroups['direct'], // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_SKIPPED,
- ]);
- }
- // 间推奖励
- if ($subordinateStats['indirect_count'] > 0 && $rewardGroups['indirect'] > 0) {
- $indirectResult = RewardService::grantReward(
- $farmUserId,
- $rewardGroups['indirect'],
- REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL,
- $ursUserId,
- $subordinateStats['indirect_count']
- );
- if (!$indirectResult->success) {
- DB::rollBack();
- return [
- 'success' => false,
- 'message' => "间推奖励发放失败: {$indirectResult->errorMessage}",
- 'backfilled_count' => $totalBackfilledCount,
- 'total_subordinates' => $totalSubordinates,
- 'talent_level' => $talentLevel,
- 'reward_details' => $rewardDetails
- ];
- }
- $rewardDetails['indirect'] = [
- 'type' => '间推奖励',
- 'count' => $subordinateStats['indirect_count'],
- 'reward_group_id' => $rewardGroups['indirect'],
- 'items_count' => count($indirectResult->items)
- ];
- $totalBackfilledCount += count($indirectResult->items);
- // 创建收益记录
- $profit = UrsProfit::create([
- 'urs_user_id' => $ursUserId,
- 'urs_promotion_member_id' => 0,
- 'promotion_member_farm_user_id' => 0,
- 'farm_user_id' => $farmUserId,
- 'source_id' => $directResult->sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => 2,
- 'original_amount' => (string)0,
- 'profit_amount' => (string)0, // 记录实际发放的物品数量
- 'profit_rate' => 1,
- 'reward_group_id' => $rewardGroups['direct'], // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- }else{
- // 创建收益记录-跳过
- $profit = UrsProfit::create([
- 'urs_user_id' => $ursUserId,
- 'urs_promotion_member_id' => 0,
- 'promotion_member_farm_user_id' => 0,
- 'farm_user_id' => $farmUserId,
- 'source_id' => 0,
- 'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => 2,
- 'original_amount' => (string)0,
- 'profit_amount' => (string)0, // 记录实际发放的物品数量
- 'profit_rate' => 1,
- 'reward_group_id' => $rewardGroups['direct'], // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_SKIPPED,
- ]);
- }
- // 三推奖励
- if ($subordinateStats['third_count'] > 0 && $rewardGroups['third'] > 0) {
- $thirdResult = RewardService::grantReward(
- $farmUserId,
- $rewardGroups['third'],
- REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL,
- $ursUserId,
- $subordinateStats['third_count']
- );
- if (!$thirdResult->success) {
- DB::rollBack();
- return [
- 'success' => false,
- 'message' => "三推奖励发放失败: {$thirdResult->errorMessage}",
- 'backfilled_count' => $totalBackfilledCount,
- 'total_subordinates' => $totalSubordinates,
- 'talent_level' => $talentLevel,
- 'reward_details' => $rewardDetails
- ];
- }
- $rewardDetails['third'] = [
- 'type' => '三推奖励',
- 'count' => $subordinateStats['third_count'],
- 'reward_group_id' => $rewardGroups['third'],
- 'items_count' => count($thirdResult->items)
- ];
- $totalBackfilledCount += count($thirdResult->items);
- // 创建收益记录
- $profit = UrsProfit::create([
- 'urs_user_id' => $ursUserId,
- 'urs_promotion_member_id' => 0,
- 'promotion_member_farm_user_id' => 0,
- 'farm_user_id' => $farmUserId,
- 'source_id' => $directResult->sourceId,
- 'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => 2,
- 'original_amount' => (string)0,
- 'profit_amount' => (string)0, // 记录实际发放的物品数量
- 'profit_rate' => 1,
- 'reward_group_id' => $rewardGroups['direct'], // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_NORMAL,
- ]);
- }else{
- // 创建收益记录-跳过
- $profit = UrsProfit::create([
- 'urs_user_id' => $ursUserId,
- 'urs_promotion_member_id' => 0,
- 'promotion_member_farm_user_id' => 0,
- 'farm_user_id' => $farmUserId,
- 'source_id' => 0,
- 'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
- 'profit_type' => UrsProfitType::PLANTING_REWARD->value,
- 'relation_level' => 3,
- 'original_amount' => (string)0,
- 'profit_amount' => (string)0, // 记录实际发放的物品数量
- 'profit_rate' => 1,
- 'reward_group_id' => $rewardGroups['direct'], // 种植收益不使用奖励组
- 'talent_level' => $talentLevel,
- 'status' => UrsProfit::STATUS_SKIPPED,
- ]);
- }
- DB::commit();
- Log::info('URS推荐奖励补发成功', [
- 'urs_user_id' => $ursUserId,
- 'farm_user_id' => $farmUserId,
- 'talent_level' => $talentLevel,
- 'subordinate_stats' => $subordinateStats,
- 'reward_details' => $rewardDetails,
- 'total_backfilled_count' => $totalBackfilledCount
- ]);
- return [
- 'success' => true,
- 'message' => '推荐奖励补发成功',
- 'backfilled_count' => $totalBackfilledCount,
- 'total_subordinates' => $totalSubordinates,
- 'talent_level' => $talentLevel,
- 'reward_details' => $rewardDetails
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- throw $e;
- }
- }
- /**
- * 检查用户是否需要补发推荐奖励
- *
- * @param int $ursUserId URS用户ID
- * @return bool
- */
- public function needsBackfillReward(int $ursUserId): bool
- {
- // 检查用户是否有下级
- $subordinateStats = $this->getSubordinateStats($ursUserId);
- return $subordinateStats['total_count'] > 0;
- }
- /**
- * 获取用户的下级统计信息
- *
- * @param int $ursUserId URS用户ID
- * @return array
- */
- public function getSubordinateStats(int $ursUserId): array
- {
- // 获取直推下级
- $directReferrals = UrsReferralService::getDirectReferrals($ursUserId);
- $directCount = count($directReferrals);
- // 获取间推下级(直推的直推)
- $indirectCount = 0;
- $indirectReferrals = [];
- foreach ($directReferrals as $directUserId) {
- $secondLevelReferrals = UrsReferralService::getDirectReferrals($directUserId);
- $indirectReferrals = array_merge($indirectReferrals, $secondLevelReferrals);
- $indirectCount += count($secondLevelReferrals);
- }
- // 获取三推下级(间推的直推)
- $thirdCount = 0;
- foreach ($indirectReferrals as $indirectUserId) {
- $thirdLevelReferrals = UrsReferralService::getDirectReferrals($indirectUserId);
- $thirdCount += count($thirdLevelReferrals);
- }
- $totalCount = $directCount + $indirectCount + $thirdCount;
- return [
- 'direct_count' => $directCount,
- 'indirect_count' => $indirectCount,
- 'third_count' => $thirdCount,
- 'total_count' => $totalCount
- ];
- }
- /**
- * 获取用户当前达人等级
- *
- * @param int $ursUserId URS用户ID
- * @return int 达人等级,最低为0(青铜级)
- */
- private function getUserTalentLevel(int $ursUserId): int
- {
- $talentDto = UrsTalentService::getTalentInfo($ursUserId);
- // 如果用户没有达人等级记录,返回最低级(青铜级)
- if (!$talentDto) {
- return 0;
- }
- return $talentDto->talentLevel;
- }
- /**
- * 获取指定达人等级的推荐奖励组配置
- *
- * @param int $talentLevel 达人等级
- * @return array 奖励组配置数组,包含direct、indirect、third三个层级的奖励组ID
- */
- private function getPromotionRewardGroups(int $talentLevel): array
- {
- $config = UrsTalentConfig::where('level', $talentLevel)
- ->where('status', UrsTalentConfig::STATUS_ENABLED)
- ->first();
- if (!$config) {
- Log::warning('达人等级配置不存在', [
- 'talent_level' => $talentLevel
- ]);
- return [];
- }
- return [
- 'direct' => $config->promotion_direct_group ?? 0,
- 'indirect' => $config->promotion_indirect_group ?? 0,
- 'third' => $config->promotion_third_group ?? 0,
- ];
- }
- }
|