referralLogic = $referralLogic; } /** * 获取用户的达人等级信息 * * @param int $userId 用户ID * @return PromotionUserTalent|null */ public function getUserTalent(int $userId): ?PromotionUserTalent { // 尝试从缓存获取 $cacheKey = "promotion:user:{$userId}:talent"; $cachedData = Redis::get($cacheKey); if ($cachedData !== null) { $talentData = json_decode($cachedData, true); $talent = new PromotionUserTalent(); $talent->fill($talentData); return $talent; } // 从数据库获取 $talent = PromotionUserTalent::where('user_id', $userId)->first(); // 如果不存在,创建一个默认的达人等级记录 if (!$talent) { $talent = $this->createDefaultTalent($userId); } // 缓存结果 if ($talent) { Redis::setex($cacheKey, 86400, json_encode($talent->toArray())); // 缓存1天 } return $talent; } /** * 创建默认的达人等级记录 * * @param int $userId 用户ID * @return PromotionUserTalent|null */ public function createDefaultTalent(int $userId): ?PromotionUserTalent { try { $talent = new PromotionUserTalent(); $talent->user_id = $userId; $talent->talent_level = TALENT_LEVEL::NONE; $talent->direct_count = 0; $talent->promotion_count = 0; $talent->save(); return $talent; } catch (\Exception $e) { Log::error("创建默认达人等级记录失败: " . $e->getMessage()); return null; } } /** * 更新用户的达人等级 * * @param int $userId 用户ID * @param int $talentLevel 达人等级 * @return bool */ public function updateTalentLevel(int $userId, int $talentLevel): bool { try { $talent = $this->getUserTalent($userId); if (!$talent) { return false; } // 如果等级没有变化,无需更新 if ($talent->talent_level == $talentLevel) { return true; } $talent->talent_level = $talentLevel; $result = $talent->save(); if ($result) { // 清除缓存 Redis::del("promotion:user:{$userId}:talent"); } return $result; } catch (\Exception $e) { Log::error("更新达人等级失败: " . $e->getMessage()); return false; } } /** * 更新用户的团队统计数据 * * @param int $userId 用户ID * @return bool */ public function updatePromotionCounts(int $userId): bool { try { $talent = $this->getUserTalent($userId); if (!$talent) { return false; } // 计算直推人数 $directCount = $this->referralLogic->countDirectReferrals($userId); // 计算团队总人数 $promotionCount = $this->referralLogic->countPromotionMembers($userId); // 更新数据 $talent->direct_count = $directCount; $talent->promotion_count = $promotionCount; $result = $talent->save(); if ($result) { // 清除缓存 Redis::del("promotion:user:{$userId}:talent"); } return $result; } catch (\Exception $e) { Log::error("更新团队统计数据失败: " . $e->getMessage()); return false; } } /** * 检查并更新用户的达人等级 * * @param int $userId 用户ID * @return bool */ public function checkAndUpdateTalentLevel(int $userId): bool { try { // 更新团队统计数据 $this->updatePromotionCounts($userId); // 获取用户的达人信息 $talent = $this->getUserTalent($userId); if (!$talent) { return false; } // 获取所有达人等级配置 $configs = $this->getAllTalentConfigs(); // 计算应该的达人等级 $newLevel = TALENT_LEVEL::NONE; foreach ($configs as $config) { if ($talent->direct_count >= $config->direct_count_required && $talent->promotion_count >= $config->promotion_count_required) { $newLevel = $config->level; } } // 如果等级有变化,更新达人等级 if ($talent->talent_level != $newLevel) { return $this->updateTalentLevel($userId, $newLevel); } return true; } catch (\Exception $e) { Log::error("检查并更新达人等级失败: " . $e->getMessage()); return false; } } /** * 获取达人等级配置 * * @param int $level 等级 * @return PromotionTalentConfig|null */ public function getTalentConfig(int $level): ?PromotionTalentConfig { // 尝试从缓存获取 $cacheKey = "promotion:talent_config:{$level}"; $cachedData = Redis::get($cacheKey); if ($cachedData !== null) { $configData = json_decode($cachedData, true); $config = new PromotionTalentConfig(); $config->fill($configData); return $config; } // 从数据库获取 $config = PromotionTalentConfig::where('level', $level)->first(); // 缓存结果 if ($config) { Redis::setex($cacheKey, 86400 * 7, json_encode($config->toArray())); // 缓存7天 } return $config; } /** * 获取所有达人等级配置 * * @return array */ public function getAllTalentConfigs(): array { // 尝试从缓存获取 $cacheKey = "promotion:talent_configs"; $cachedData = Redis::get($cacheKey); if ($cachedData !== null) { $configsData = json_decode($cachedData, true); $configs = []; foreach ($configsData as $configData) { $config = new PromotionTalentConfig(); $config->fill($configData); $configs[] = $config; } return $configs; } // 从数据库获取 $configs = PromotionTalentConfig::orderBy('level')->get()->toArray(); // 缓存结果 Redis::setex($cacheKey, 86400 * 7, json_encode($configs)); // 缓存7天 return $configs; } /** * 获取达人等级分成比例 * * @param int $level 等级 * @return float */ public function getTalentProfitRate(int $level): float { $config = $this->getTalentConfig($level); if (!$config) { return 0.0; } return $config->profit_rate; } /** * 获取达人等级权益 * * @param int $level 等级 * @return array */ public function getTalentBenefits(int $level): array { $config = $this->getTalentConfig($level); if (!$config || empty($config->benefits)) { return []; } return is_array($config->benefits) ? $config->benefits : json_decode($config->benefits, true); } /** * 批量更新用户的达人等级 * * @param array $userIds 用户ID数组 * @return int 成功更新的数量 */ public function batchUpdateTalentLevels(array $userIds): int { $successCount = 0; foreach ($userIds as $userId) { if ($this->checkAndUpdateTalentLevel($userId)) { $successCount++; } } return $successCount; } }