| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace App\Module\UrsPromotion\Services;
- use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * URS推荐关系服务
- *
- * 管理URS用户之间的推荐关系
- */
- class UrsReferralService
- {
- /**
- * 获取用户的推荐人
- *
- * @param int $ursUserId URS用户ID
- * @return int|null 推荐人URS用户ID,如果没有推荐人返回null
- */
- public static function getReferrer(int $ursUserId): ?int
- {
- $referral = UrsUserReferral::where('urs_user_id', $ursUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->first();
- return $referral ? $referral->urs_referrer_id : null;
- }
- /**
- * 获取用户的直推下级列表
- *
- * @param int $ursUserId URS用户ID
- * @return array 直推下级URS用户ID数组
- */
- public static function getDirectReferrals(int $ursUserId): array
- {
- return UrsUserReferral::where('urs_referrer_id', $ursUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->pluck('urs_user_id')
- ->toArray();
- }
- /**
- * 获取用户的推荐关系链(向上查找)
- *
- * @param int $ursUserId URS用户ID
- * @param int $maxLevels 最大层级数
- * @return array 推荐关系链,按层级排序 [level => urs_user_id]
- */
- public static function getReferralChain(int $ursUserId, int $maxLevels = 10): array
- {
- $chain = [];
- $currentUserId = $ursUserId;
- for ($level = 1; $level <= $maxLevels; $level++) {
- $referrerId = self::getReferrer($currentUserId);
- if (!$referrerId) {
- break; // 没有上级了
- }
- $chain[$level] = $referrerId;
- $currentUserId = $referrerId;
- }
- return $chain;
- }
- /**
- * 获取用户的团队成员(向下查找)
- *
- * @param int $ursUserId URS用户ID
- * @param int $maxLevels 最大层级数(默认20代,支持达人等级统计)
- * @return array 团队成员,按层级分组 [level => [urs_user_ids]]
- */
- public static function getTeamMembers(int $ursUserId, ?int $maxLevels = null): array
- {
- // 如果没有指定层级数,使用配置的团队统计深度
- if ($maxLevels === null) {
- $maxLevels = UrsPromotionRelationLevel::getTeamStatsDepth();
- }
- // 优先使用关系缓存表进行查询,性能更好
- return self::getTeamMembersFromCache($ursUserId, $maxLevels);
- }
- /**
- * 获取团队成员数量
- * @param int $ursUserId
- * @param int|null $maxLevels
- * @return array
- */
- public static function getTeamMNumber(int $ursUserId, ?int $maxLevels = null): array
- {
- // 如果没有指定层级数,使用配置的团队统计深度
- if ($maxLevels === null) {
- $maxLevels = UrsPromotionRelationLevel::getTeamStatsDepth();
- }
- // 优先使用关系缓存表进行查询,性能更好
- return self::getTeamMNumberFromCache($ursUserId, $maxLevels);
- }
- /**
- * 从关系缓存表获取团队成员(高性能版本)
- *
- * @param int $ursUserId URS用户ID
- * @param int $maxLevels 最大层级数
- * @return array 团队成员,按层级分组 [level => [urs_user_ids]]
- */
- private static function getTeamMembersFromCache(int $ursUserId, int $maxLevels): array
- {
- // 使用关系缓存表查询所有下级关系
- $relations = \App\Module\UrsPromotion\Models\UrsUserRelationCache::where('urs_related_user_id', $ursUserId)
- ->where('depth', '<=', $maxLevels)
- ->orderBy('depth')
- ->get(['urs_user_id', 'depth']);
- $team = [];
- foreach ($relations as $relation) {
- $level = $relation->depth;
- if (!isset($team[$level])) {
- $team[$level] = [];
- }
- $team[$level][] = $relation->urs_user_id;
- }
- return $team;
- }
- /**
- * 获取团队成员人数/层级
- * @param int $ursUserId
- * @param int $maxLevels
- * @return array
- */
- private static function getTeamMNumberFromCache(int $ursUserId, int $maxLevels): array
- {
- dump($maxLevels);
- // 使用关系缓存表查询所有下级关系
- $relations = \App\Module\UrsPromotion\Models\UrsUserRelationCache::where('urs_related_user_id', $ursUserId)
- ->where('depth', '<=', $maxLevels)
- ->orderBy('depth')
- ->groupBy('depth')
- ->get([DB::raw('count(*) as count'), 'depth']);
- dd($relations);
- return $team;
- }
- /**
- * 获取用户的团队成员(递归查询版本,作为备用方案)
- *
- * @param int $ursUserId URS用户ID
- * @param int $maxLevels 最大层级数
- * @return array 团队成员,按层级分组 [level => [urs_user_ids]]
- */
- private static function getTeamMembersRecursive(int $ursUserId, int $maxLevels): array
- {
- $team = [];
- $currentLevelUsers = [$ursUserId];
- for ($level = 1; $level <= $maxLevels; $level++) {
- $nextLevelUsers = [];
- foreach ($currentLevelUsers as $userId) {
- $directReferrals = self::getDirectReferrals($userId);
- $nextLevelUsers = array_merge($nextLevelUsers, $directReferrals);
- }
- if (empty($nextLevelUsers)) {
- break; // 没有下级了
- }
- $team[$level] = $nextLevelUsers;
- $currentLevelUsers = $nextLevelUsers;
- }
- return $team;
- }
- /**
- * 获取三级内活跃用户数量
- *
- * @param int $userId 农场用户ID
- * @return int 活跃用户数量(映射关系有效且用户活跃)
- */
- public static function getTeamANumber($userId): int
- {
- // 通过关系缓存表获取三代内所有的URS用户ID
- $ursUserIds = \App\Module\UrsPromotion\Models\UrsUserRelationCache::where('related_user_id', $userId)
- ->where('depth', '<=', 3) // 只统计前3级
- ->pluck('urs_user_id')
- ->toArray();
- if (empty($ursUserIds)) {
- return 0;
- }
- // 到映射表查询活跃的人数(is_active=1为活跃)
- $validCount = \App\Module\UrsPromotion\Models\UrsUserMapping::whereIn('urs_user_id', $ursUserIds)
- ->where('status', 1) // 映射关系必须有效
- ->where('is_active', 1) // 用户必须活跃
- ->count();
- return $validCount;
- }
- /**
- * 更新推荐人的团队统计
- *
- * @param int $ursReferrerId URS推荐人ID
- */
- private static function updateReferrerStats(int $ursReferrerId): void
- {
- $user_id = UrsUserMapping::getFarmUserIdByUrsUserId($ursReferrerId);
- // 获取团队成员统计(使用配置的团队统计深度)
- $teamMembers = self::getTeamMembers($ursReferrerId);
- $directCount = count($teamMembers[1] ?? []);
- $indirectCount = count($teamMembers[2] ?? []);
- $thirdCount = count($teamMembers[3] ?? []);
- // 计算20代总人数
- $totalCount = 0;
- foreach ($teamMembers as $members) {
- $totalCount += count($members);
- }
- // 获取或创建用户达人记录
- $talent = UrsUserTalent::firstOrCreate(
- ['user_id' => $user_id],
- [
- 'talent_level' => 0,
- 'direct_count' => 0,
- 'indirect_count' => 0,
- 'third_count' => 0,
- 'promotion_count' => 0,
- ]
- );
- // 使用模型方法更新团队统计,确保逻辑一致性
- $talent->updateTeamStats($directCount, $indirectCount, $thirdCount, $totalCount);
- $talent->save();
- }
- /**
- * 批量更新团队统计
- *
- * @param array $ursUserIds URS用户ID数组
- */
- public static function batchUpdateTeamStats(array $ursUserIds): void
- {
- foreach ($ursUserIds as $ursUserId) {
- self::updateReferrerStats($ursUserId);
- }
- }
- /**
- * 获取推荐关系统计
- *
- * @param int $ursUserId URS用户ID
- * @return array
- */
- public static function getReferralStats(int $ursUserId): array
- {
- $teamMembers = self::getTeamMembers($ursUserId,3);
- $referralChain = self::getReferralChain($ursUserId);
- dump($teamMembers);
- return [
- 'urs_user_id' => $ursUserId,
- 'referrer_id' => self::getReferrer($ursUserId),
- 'referrer_level' => count($referralChain),
- 'direct_count' => count($teamMembers[1] ?? []),
- 'indirect_count' => count($teamMembers[2] ?? []),
- 'third_count' => count($teamMembers[3] ?? []),
- 'total_team_count' => array_sum(array_map('count', $teamMembers)),
- 'team_members' => $teamMembers,
- 'referral_chain' => $referralChain,
- ];
- }
- /**
- * 验证推荐关系的有效性
- *
- * @param int $ursUserId URS用户ID
- * @return bool
- */
- public static function validateReferral(int $ursUserId): bool
- {
- $referral = UrsUserReferral::where('urs_user_id', $ursUserId)->first();
- return $referral && $referral->isValid();
- }
- /**
- * 禁用推荐关系
- *
- * @param int $ursUserId URS用户ID
- * @return bool
- */
- public static function disableReferral(int $ursUserId): bool
- {
- try {
- $referral = UrsUserReferral::where('urs_user_id', $ursUserId)->first();
- if (!$referral) {
- return false;
- }
- $referral->setInvalid();
- $referral->save();
- // 更新推荐人的团队统计
- self::updateReferrerStats($referral->urs_referrer_id);
- Log::info('URS推荐关系已禁用', [
- 'urs_user_id' => $ursUserId,
- 'referral_id' => $referral->id
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('URS推荐关系禁用失败', [
- 'urs_user_id' => $ursUserId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 批量更新推荐关系表中的农场用户ID
- *
- * 当用户进入农场后,批量更新推荐关系表中的农场用户ID字段
- *
- * @return array 更新结果
- */
- public static function batchUpdateFarmUserIds(): array
- {
- try {
- Log::info('开始批量更新推荐关系表中的农场用户ID');
- // 使用SQL批量更新,提高性能
- $updateSql = "
- UPDATE `kku_urs_promotion_user_referrals` r
- LEFT JOIN `kku_urs_promotion_user_mappings` um ON r.urs_user_id = um.urs_user_id AND um.status = 1
- LEFT JOIN `kku_urs_promotion_user_mappings` rm ON r.urs_referrer_id = rm.urs_user_id AND rm.status = 1
- SET
- r.user_id = COALESCE(um.user_id, 0),
- r.referrer_id = COALESCE(rm.user_id, 0)
- WHERE r.status = 1
- ";
- $affectedRows = DB::update($updateSql);
- Log::info('批量更新推荐关系表中的农场用户ID完成', [
- 'affected_rows' => $affectedRows
- ]);
- return [
- 'success' => true,
- 'affected_rows' => $affectedRows,
- 'message' => "成功更新 {$affectedRows} 条推荐关系记录"
- ];
- } catch (\Exception $e) {
- Log::error('批量更新推荐关系表中的农场用户ID失败', [
- 'error' => $e->getMessage()
- ]);
- return [
- 'success' => false,
- 'affected_rows' => 0,
- 'message' => '批量更新失败: ' . $e->getMessage()
- ];
- }
- }
- /**
- * 更新单个推荐关系的农场用户ID
- *
- * 当特定用户进入农场时,更新其相关的推荐关系记录
- *
- * @param int $ursUserId URS用户ID
- * @param int $farmUserId 农场用户ID
- * @return bool
- */
- public static function updateFarmUserIdForUser(int $ursUserId, int $farmUserId): bool
- {
- try {
- DB::beginTransaction();
- // 更新该用户作为被推荐人的记录
- UrsUserReferral::where('urs_user_id', $ursUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->update(['user_id' => $farmUserId]);
- // 更新该用户作为推荐人的记录
- UrsUserReferral::where('urs_referrer_id', $ursUserId)
- ->where('status', UrsUserReferral::STATUS_VALID)
- ->update(['referrer_id' => $farmUserId]);
- DB::commit();
- Log::info('更新用户推荐关系农场用户ID成功', [
- 'urs_user_id' => $ursUserId,
- 'farm_user_id' => $farmUserId
- ]);
- return true;
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('更新用户推荐关系农场用户ID失败', [
- 'urs_user_id' => $ursUserId,
- 'farm_user_id' => $farmUserId,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- }
|