| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace App\Module\Promotion\Services;
- use App\Module\Promotion\Logics\ReferralLogic;
- use App\Module\Promotion\Logics\TalentLogic;
- use App\Module\Promotion\Models\PromotionUserTalent;
- use Illuminate\Support\Facades\Log;
- /**
- * 达人等级服务类
- *
- * 对外提供达人等级相关的服务,包括获取达人等级、获取达人权益、
- * 获取达人等级配置等功能。该类对外提供服务,内部调用逻辑层实现。
- */
- class TalentService
- {
- /**
- * 获取用户的达人等级信息
- *
- * @param int $userId 用户ID
- * @return array|null
- */
- public static function getUserTalentInfo(int $userId): ?array
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- $talent = $talentLogic->getUserTalent($userId);
- if (!$talent) {
- return null;
- }
- $config = $talentLogic->getTalentConfig($talent->talent_level);
- return [
- 'user_id' => $talent->user_id,
- 'talent_level' => $talent->talent_level,
- 'talent_name' => $config ? $config->name : '',
- 'direct_count' => $talent->direct_count,
- 'promotion_count' => $talent->promotion_count,
- 'icon' => $config ? $config->icon : '',
- 'icon_url' => $config ? $config->icon_url : '',
- 'benefits' => $config ? (is_array($config->benefits) ? $config->benefits : json_decode($config->benefits, true)) : [],
- 'profit_rate' => $config ? $config->profit_rate : 0.0,
- 'created_at' => $talent->created_at,
- 'updated_at' => $talent->updated_at
- ];
- } catch (\Exception $e) {
- Log::error("获取用户达人等级信息失败: " . $e->getMessage());
- return null;
- }
- }
- /**
- * 获取所有达人等级配置
- *
- * @return array
- */
- public static function getAllTalentConfigs(): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- $configs = $talentLogic->getAllTalentConfigs();
- $result = [];
- foreach ($configs as $config) {
- $result[] = [
- 'level' => $config->level,
- 'name' => $config->name,
- 'direct_count_required' => $config->direct_count_required,
- 'promotion_count_required' => $config->promotion_count_required,
- 'profit_rate' => $config->profit_rate,
- 'benefits' => is_array($config->benefits) ? $config->benefits : json_decode($config->benefits, true),
- 'icon' => $config->icon,
- 'icon_url' => $config->icon_url
- ];
- }
- return $result;
- } catch (\Exception $e) {
- Log::error("获取所有达人等级配置失败: " . $e->getMessage());
- return [];
- }
- }
- /**
- * 获取达人等级权益
- *
- * @param int $level 等级
- * @return array
- */
- public static function getTalentBenefits(int $level): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- return $talentLogic->getTalentBenefits($level);
- } catch (\Exception $e) {
- Log::error("获取达人等级权益失败: " . $e->getMessage());
- return [];
- }
- }
- /**
- * 检查并更新用户的达人等级
- *
- * @param int $userId 用户ID
- * @return bool
- */
- public static function checkAndUpdateUserTalentLevel(int $userId): bool
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- return $talentLogic->checkAndUpdateTalentLevel($userId);
- } catch (\Exception $e) {
- Log::error("检查并更新用户达人等级失败: " . $e->getMessage());
- return false;
- }
- }
- /**
- * 获取达人等级排行榜
- *
- * @param int $limit 限制数量
- * @return array
- */
- public static function getTalentRanking(int $limit = 10): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- $talents = PromotionUserTalent::where('talent_level', '>', 0)
- ->orderBy('talent_level', 'desc')
- ->orderBy('promotion_count', 'desc')
- ->orderBy('direct_count', 'desc')
- ->limit($limit)
- ->with('user')
- ->get();
- $result = [];
- foreach ($talents as $talent) {
- if ($talent->user) {
- $config = $talentLogic->getTalentConfig($talent->talent_level);
- $result[] = [
- 'user_id' => $talent->user_id,
- 'username' => $talent->user->username ?? '',
- 'nickname' => $talent->user->nickname ?? '',
- 'avatar' => $talent->user->avatar ?? '',
- 'talent_level' => $talent->talent_level,
- 'talent_name' => $config ? $config->name : '',
- 'direct_count' => $talent->direct_count,
- 'promotion_count' => $talent->promotion_count,
- 'icon' => $config ? $config->icon : '',
- 'icon_url' => $config ? $config->icon_url : ''
- ];
- }
- }
- return $result;
- } catch (\Exception $e) {
- Log::error("获取达人等级排行榜失败: " . $e->getMessage());
- return [];
- }
- }
- /**
- * 获取用户的达人等级进度
- *
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserTalentProgress(int $userId): array
- {
- try {
- $referralLogic = new ReferralLogic();
- $talentLogic = new TalentLogic($referralLogic);
- $talent = $talentLogic->getUserTalent($userId);
- if (!$talent) {
- return [
- 'current_level' => 0,
- 'next_level' => 1,
- 'direct_count' => 0,
- 'direct_count_required' => 5,
- 'direct_count_progress' => 0,
- 'promotion_count' => 0,
- 'promotion_count_required' => 10,
- 'promotion_count_progress' => 0
- ];
- }
- $configs = $talentLogic->getAllTalentConfigs();
- // 找到下一个等级
- $nextLevel = null;
- $nextLevelConfig = null;
- foreach ($configs as $config) {
- if ($config->level > $talent->talent_level) {
- $nextLevel = $config->level;
- $nextLevelConfig = $config;
- break;
- }
- }
- if (!$nextLevel) {
- // 已经是最高等级
- $currentConfig = $talentLogic->getTalentConfig($talent->talent_level);
- return [
- 'current_level' => $talent->talent_level,
- 'next_level' => null,
- 'direct_count' => $talent->direct_count,
- 'direct_count_required' => $currentConfig ? $currentConfig->direct_count_required : 0,
- 'direct_count_progress' => 100,
- 'promotion_count' => $talent->promotion_count,
- 'promotion_count_required' => $currentConfig ? $currentConfig->promotion_count_required : 0,
- 'promotion_count_progress' => 100
- ];
- }
- // 计算进度
- $directCountProgress = $nextLevelConfig->direct_count_required > 0
- ? min(100, ($talent->direct_count / $nextLevelConfig->direct_count_required) * 100)
- : 0;
- $promotionCountProgress = $nextLevelConfig->promotion_count_required > 0
- ? min(100, ($talent->promotion_count / $nextLevelConfig->promotion_count_required) * 100)
- : 0;
- return [
- 'current_level' => $talent->talent_level,
- 'next_level' => $nextLevel,
- 'direct_count' => $talent->direct_count,
- 'direct_count_required' => $nextLevelConfig->direct_count_required,
- 'direct_count_progress' => $directCountProgress,
- 'promotion_count' => $talent->promotion_count,
- 'promotion_count_required' => $nextLevelConfig->promotion_count_required,
- 'promotion_count_progress' => $promotionCountProgress
- ];
- } catch (\Exception $e) {
- Log::error("获取用户达人等级进度失败: " . $e->getMessage());
- return [
- 'current_level' => 0,
- 'next_level' => 1,
- 'direct_count' => 0,
- 'direct_count_required' => 5,
- 'direct_count_progress' => 0,
- 'promotion_count' => 0,
- 'promotion_count_required' => 10,
- 'promotion_count_progress' => 0
- ];
- }
- }
- }
|