| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- namespace App\Module\UrsPromotion\Logics;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Models\UrsTalentConfig;
- use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
- use Illuminate\Support\Facades\Log;
- /**
- * URS达人等级逻辑类
- *
- * 处理URS推广系统的达人等级计算和升级逻辑
- */
- class UrsTalentLogic
- {
- /**
- * 更新用户的团队统计和达人等级
- *
- * @param int $userId 用户ID
- * @return UrsUserTalent|null
- */
- public function updateUserTalent(int $userId): ?UrsUserTalent
- {
- try {
- // 计算团队统计数据
- $teamStats = $this->calculateTeamStats($userId);
-
- // 获取或创建用户达人记录
- $talent = UrsUserTalent::firstOrCreate(
- ['user_id' => $userId],
- [
- 'talent_level' => 0,
- 'direct_count' => 0,
- 'indirect_count' => 0,
- 'third_count' => 0,
- 'promotion_count' => 0,
- ]
- );
-
- // 更新团队统计(包含20代总人数)
- $talent->updateTeamStats(
- $teamStats['direct_count'],
- $teamStats['indirect_count'],
- $teamStats['third_count'],
- $teamStats['promotion_count'] // 传递20代总人数
- );
-
- // 计算新的达人等级
- $newLevel = $this->calculateTalentLevel(
- $teamStats['direct_count'],
- $teamStats['promotion_count']
- );
-
- // 检查是否需要升级
- if ($newLevel > $talent->talent_level) {
- $oldLevel = $talent->talent_level;
- $talent->upgradeTalentLevel($newLevel);
-
- Log::info("用户达人等级升级", [
- 'user_id' => $userId,
- 'old_level' => $oldLevel,
- 'new_level' => $newLevel,
- 'direct_count' => $teamStats['direct_count'],
- 'promotion_count' => $teamStats['promotion_count']
- ]);
- }
-
- $talent->save();
-
- return $talent;
-
- } catch (\Exception $e) {
- Log::error("更新用户达人等级失败", [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
-
- /**
- * 计算用户的团队统计数据(支持20代统计)
- *
- * @param int $userId 用户ID
- * @return array
- */
- private function calculateTeamStats(int $userId): array
- {
- $stats = [
- 'direct_count' => 0,
- 'indirect_count' => 0,
- 'third_count' => 0,
- 'promotion_count' => 0,
- ];
- // 获取直推用户
- $directUsers = UrsUserReferral::where('referrer_id', $userId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->pluck('user_id')
- ->toArray();
- $stats['direct_count'] = count($directUsers);
- if (empty($directUsers)) {
- return $stats;
- }
- // 获取间推用户(直推用户的直推)
- $indirectUsers = UrsUserReferral::whereIn('referrer_id', $directUsers)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->pluck('user_id')
- ->toArray();
- $stats['indirect_count'] = count($indirectUsers);
- if (empty($indirectUsers)) {
- $stats['promotion_count'] = $stats['direct_count'];
- return $stats;
- }
- // 获取三推用户(间推用户的直推)
- $thirdUsers = UrsUserReferral::whereIn('referrer_id', $indirectUsers)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->pluck('user_id')
- ->toArray();
- $stats['third_count'] = count($thirdUsers);
- // 计算20代总人数(使用递归方式统计所有层级)
- $allTeamUsers = $this->getAllTeamMembers($userId, UrsPromotionRelationLevel::getTeamStatsDepth());
- $stats['promotion_count'] = count($allTeamUsers);
- return $stats;
- }
- /**
- * 递归获取所有团队成员(支持多层级)
- *
- * @param int $userId 用户ID
- * @param int $maxLevels 最大层级数
- * @return array 所有团队成员的用户ID数组
- */
- private function getAllTeamMembers(int $userId, ?int $maxLevels = null): array
- {
- // 如果没有指定层级数,使用配置的团队统计深度
- if ($maxLevels === null) {
- $maxLevels = UrsPromotionRelationLevel::getTeamStatsDepth();
- }
- $allMembers = [];
- $currentLevelUsers = [$userId];
- for ($level = 1; $level <= $maxLevels; $level++) {
- $nextLevelUsers = [];
- foreach ($currentLevelUsers as $currentUserId) {
- $directReferrals = UrsUserReferral::where('referrer_id', $currentUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->pluck('user_id')
- ->toArray();
- $nextLevelUsers = array_merge($nextLevelUsers, $directReferrals);
- }
- if (empty($nextLevelUsers)) {
- break; // 没有下级了
- }
- $allMembers = array_merge($allMembers, $nextLevelUsers);
- $currentLevelUsers = $nextLevelUsers;
- }
- return array_unique($allMembers);
- }
- /**
- * 根据团队数据计算达人等级
- *
- * @param int $directCount 直推人数
- * @param int $promotionCount 团队总人数
- * @return int
- */
- private function calculateTalentLevel(int $directCount, int $promotionCount): int
- {
- // 获取所有启用的达人等级配置,按等级倒序
- $configs = UrsTalentConfig::where('status', UrsTalentConfig::STATUS_ENABLED)
- ->orderBy('level', 'desc')
- ->get();
-
- foreach ($configs as $config) {
- if ($config->meetsRequirements($directCount, $promotionCount)) {
- return $config->level;
- }
- }
-
- return 0; // 默认为非达人
- }
-
- /**
- * 批量更新推荐人的达人等级
- *
- * @param array $userIds 用户ID数组
- * @return array 更新结果
- */
- public function batchUpdateTalents(array $userIds): array
- {
- $results = [
- 'success' => 0,
- 'failed' => 0,
- 'upgraded' => [],
- ];
-
- foreach ($userIds as $userId) {
- $talent = $this->updateUserTalent($userId);
-
- if ($talent) {
- $results['success']++;
-
- // 记录升级的用户
- if ($talent->wasChanged('talent_level')) {
- $results['upgraded'][] = [
- 'user_id' => $userId,
- 'old_level' => $talent->getOriginal('talent_level'),
- 'new_level' => $talent->talent_level,
- ];
- }
- } else {
- $results['failed']++;
- }
- }
-
- return $results;
- }
-
- /**
- * 获取用户的推荐关系树(三代)
- *
- * @param int $userId 用户ID
- * @return array
- */
- public function getUserReferralTree(int $userId): array
- {
- $tree = [
- 'user_id' => $userId,
- 'direct' => [],
- 'indirect' => [],
- 'third' => [],
- ];
-
- // 获取直推用户
- $directUsers = UrsUserReferral::where('referrer_id', $userId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->with('user')
- ->get();
-
- foreach ($directUsers as $referral) {
- $tree['direct'][] = [
- 'user_id' => $referral->user_id,
- 'referral_time' => $referral->referral_time,
- 'user' => $referral->user,
- ];
- }
-
- // 获取间推用户
- if (!empty($tree['direct'])) {
- $directUserIds = array_column($tree['direct'], 'user_id');
- $indirectUsers = UrsUserReferral::whereIn('referrer_id', $directUserIds)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->with('user')
- ->get();
-
- foreach ($indirectUsers as $referral) {
- $tree['indirect'][] = [
- 'user_id' => $referral->user_id,
- 'referrer_id' => $referral->referrer_id,
- 'referral_time' => $referral->referral_time,
- 'user' => $referral->user,
- ];
- }
- }
-
- // 获取三推用户
- if (!empty($tree['indirect'])) {
- $indirectUserIds = array_column($tree['indirect'], 'user_id');
- $thirdUsers = UrsUserReferral::whereIn('referrer_id', $indirectUserIds)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->with('user')
- ->get();
-
- foreach ($thirdUsers as $referral) {
- $tree['third'][] = [
- 'user_id' => $referral->user_id,
- 'referrer_id' => $referral->referrer_id,
- 'referral_time' => $referral->referral_time,
- 'user' => $referral->user,
- ];
- }
- }
-
- return $tree;
- }
-
- /**
- * 获取达人等级配置列表
- *
- * @return array
- */
- public function getTalentConfigs(): array
- {
- return UrsTalentConfig::where('status', UrsTalentConfig::STATUS_ENABLED)
- ->orderBy('sort_order')
- ->get()
- ->toArray();
- }
-
- /**
- * 检查用户是否可以升级到指定等级
- *
- * @param int $userId 用户ID
- * @param int $targetLevel 目标等级
- * @return bool
- */
- public function canUpgradeToLevel(int $userId, int $targetLevel): bool
- {
- $talent = UrsUserTalent::where('user_id', $userId)->first();
- if (!$talent) {
- return false;
- }
-
- $config = UrsTalentConfig::where('level', $targetLevel)
- ->where('status', UrsTalentConfig::STATUS_ENABLED)
- ->first();
-
- if (!$config) {
- return false;
- }
-
- return $config->meetsRequirements($talent->direct_count, $talent->promotion_count);
- }
- }
|