| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Module\Promotion\Services;
- use App\Module\Promotion\Logics\ReferralLogic;
- use App\Module\Promotion\Logics\RelationCacheLogic;
- use App\Module\Promotion\Logics\TalentLogic;
- use App\Module\Promotion\Models\PromotionUserReferral;
- use Illuminate\Support\Facades\Log;
- /**
- * 推荐关系服务类
- *
- * 对外提供推荐关系相关的服务,包括获取推荐关系、设置推荐关系、
- * 查询团队成员等功能。该类对外提供服务,内部调用逻辑层实现。
- */
- class ReferralService
- {
- /**
- * 获取用户的直接推荐人
- *
- * @param int $userId 用户ID
- * @return array|null 推荐人信息
- */
- public static function getUserReferrer(int $userId): ?array
- {
- try {
- $referralLogic = new ReferralLogic();
- $referrerId = $referralLogic->getDirectReferrerId($userId);
- if (!$referrerId) {
- return null;
- }
- // 获取推荐人信息
- $user = app('db')->table('users')->where('id', $referrerId)->first();
- if (!$user) {
- return null;
- }
- return [
- 'user_id' => $user->id,
- 'username' => $user->username ?? '',
- 'nickname' => $user->nickname ?? '',
- 'avatar' => $user->avatar ?? '',
- 'created_at' => $user->created_at
- ];
- } catch (\Exception $e) {
- Log::error("获取用户推荐人失败: " . $e->getMessage());
- return null;
- }
- }
- /**
- * 获取用户的直接推荐列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @return array
- */
- public static function getUserDirectReferrals(int $userId, int $page = 1, int $pageSize = 20): array
- {
- try {
- $query = PromotionUserReferral::where('referrer_id', $userId)
- ->with('user');
- $total = $query->count();
- $referrals = $query->orderBy('created_at', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- $result = [];
- foreach ($referrals as $referral) {
- if ($referral->user) {
- $result[] = [
- 'user_id' => $referral->user->id,
- 'username' => $referral->user->username ?? '',
- 'nickname' => $referral->user->nickname ?? '',
- 'avatar' => $referral->user->avatar ?? '',
- 'created_at' => $referral->created_at
- ];
- }
- }
- return [
- 'total' => $total,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => ceil($total / $pageSize),
- 'referrals' => $result
- ];
- } catch (\Exception $e) {
- Log::error("获取用户直接推荐列表失败: " . $e->getMessage());
- return [
- 'total' => 0,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => 0,
- 'referrals' => []
- ];
- }
- }
- /**
- * 获取用户的团队成员列表
- *
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $pageSize 每页数量
- * @param int $maxLevel 最大层级
- * @return array
- */
- public static function getUserPromotionMembers(int $userId, int $page = 1, int $pageSize = 20, int $maxLevel = 20): array
- {
- try {
- $referralLogic = new ReferralLogic();
- return $referralLogic->getAllPromotionMembers($userId, $maxLevel, $page, $pageSize);
- } catch (\Exception $e) {
- Log::error("获取用户团队成员列表失败: " . $e->getMessage());
- return [
- 'total' => 0,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'total_pages' => 0,
- 'members' => []
- ];
- }
- }
- /**
- * 设置用户的推荐人
- *
- * @param int $userId 用户ID
- * @param int $referrerId 推荐人ID
- * @param string $reason 设置原因
- * @param int $operatorId 操作人ID
- * @return array 包含设置结果和消息
- */
- public static function setUserReferrer(int $userId, int $referrerId, string $reason, int $operatorId): array
- {
- try {
- // 验证用户和推荐人是否存在
- $user = app('db')->table('users')->where('id', $userId)->first();
- $referrer = app('db')->table('users')->where('id', $referrerId)->first();
- if (!$user) {
- return [
- 'success' => false,
- 'message' => '用户不存在'
- ];
- }
- if (!$referrer) {
- return [
- 'success' => false,
- 'message' => '推荐人不存在'
- ];
- }
- // 检查是否自己推荐自己
- if ($userId == $referrerId) {
- return [
- 'success' => false,
- 'message' => '不能设置自己为推荐人'
- ];
- }
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- // 检查是否形成循环推荐
- if ($referralLogic->checkCircularReferral($userId, $referrerId)) {
- return [
- 'success' => false,
- 'message' => '设置此推荐人会形成循环推荐关系'
- ];
- }
- // 设置推荐关系
- $result = $referralLogic->updateReferralRelation($userId, $referrerId, $reason, $operatorId);
- if (!$result) {
- return [
- 'success' => false,
- 'message' => '设置推荐人失败'
- ];
- }
- // 更新团队统计和达人等级
- $talentLogic->updatePromotionCounts($referrerId);
- $talentLogic->checkAndUpdateTalentLevel($referrerId);
- return [
- 'success' => true,
- 'message' => '设置推荐人成功'
- ];
- } catch (\Exception $e) {
- Log::error("设置用户推荐人失败: " . $e->getMessage());
- return [
- 'success' => false,
- 'message' => '设置推荐人时发生错误: ' . $e->getMessage()
- ];
- }
- }
- /**
- * 获取用户的团队统计数据
- *
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserPromotionStats(int $userId): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- $talent = $talentLogic->getUserTalent($userId);
- if (!$talent) {
- return [
- 'direct_count' => 0,
- 'promotion_count' => 0,
- 'talent_level' => 0
- ];
- }
- return [
- 'direct_count' => $talent->direct_count,
- 'promotion_count' => $talent->promotion_count,
- 'talent_level' => $talent->talent_level
- ];
- } catch (\Exception $e) {
- Log::error("获取用户团队统计数据失败: " . $e->getMessage());
- return [
- 'direct_count' => 0,
- 'promotion_count' => 0,
- 'talent_level' => 0
- ];
- }
- }
- /**
- * 重建用户的关系缓存
- *
- * @param int $userId 用户ID
- * @return bool
- */
- public static function rebuildUserRelationCache(int $userId): bool
- {
- try {
- $relationCacheLogic = new RelationCacheLogic();
- return $relationCacheLogic->generateUserRelationCache($userId);
- } catch (\Exception $e) {
- Log::error("重建用户关系缓存失败: " . $e->getMessage());
- return false;
- }
- }
- }
|