| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace App\Module\UrsPromotion\Logics;
- use App\Module\UrsPromotion\Models\UrsTransferFeeConfig;
- use App\Module\UrsPromotion\Dtos\UrsTransferFeeConfigDto;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\Farm\Models\FarmUser;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Cache;
- /**
- * URS转出手续费逻辑层
- *
- * 处理URS推广模块的转出手续费计算逻辑
- */
- class UrsTransferFeeLogic
- {
- /**
- * 缓存前缀
- */
- private const CACHE_PREFIX = 'urs_transfer_fee:';
- /**
- * 缓存时间(秒)
- */
- private const CACHE_TTL = 3600; // 1小时
- /**
- * 根据用户ID获取最优手续费率
- *
- * @param int $userId 农场用户ID
- * @return float 手续费率
- */
- public function getBestFeeRateForUser(int $userId): float
- {
- try {
- // 尝试从缓存获取
- $cacheKey = self::CACHE_PREFIX . "user_fee_rate:{$userId}";
- $cachedRate = Cache::get($cacheKey);
- if ($cachedRate !== null) {
- return (float)$cachedRate;
- }
- // 获取用户的房屋等级
- $houseLevel = $this->getUserHouseLevel($userId);
-
- // 获取用户的达人等级
- $talentLevel = $this->getUserTalentLevel($userId);
- // 获取最优手续费率
- $feeRate = $this->calculateBestFeeRate($houseLevel, $talentLevel);
- // 缓存结果
- Cache::put($cacheKey, $feeRate, self::CACHE_TTL);
- Log::info('URS转出手续费率计算完成', [
- 'user_id' => $userId,
- 'house_level' => $houseLevel,
- 'talent_level' => $talentLevel,
- 'fee_rate' => $feeRate
- ]);
- return $feeRate;
- } catch (\Exception $e) {
- Log::error('URS转出手续费率计算失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- // 返回默认费率
- return $this->getDefaultFeeRate();
- }
- }
- /**
- * 根据房屋等级和达人等级计算最优手续费率
- *
- * @param int $houseLevel 房屋等级
- * @param int $talentLevel 达人等级
- * @return float 手续费率
- */
- public function calculateBestFeeRate(int $houseLevel, int $talentLevel): float
- {
- // 获取所有匹配的配置,按优先级排序
- $configs = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
- ->where(function ($query) use ($houseLevel) {
- $query->where('house_level', 0)
- ->orWhere('house_level', $houseLevel);
- })
- ->where(function ($query) use ($talentLevel) {
- $query->where('talent_level', 0)
- ->orWhere('talent_level', $talentLevel);
- })
- ->orderBy('priority', 'desc')
- ->get();
- // 找到最匹配的配置
- $bestConfig = null;
- $bestScore = -1;
- foreach ($configs as $config) {
- $score = $this->calculateMatchScore($config, $houseLevel, $talentLevel);
- if ($score > $bestScore) {
- $bestScore = $score;
- $bestConfig = $config;
- }
- }
- if ($bestConfig) {
- Log::info('找到最优手续费配置', [
- 'config_id' => $bestConfig->id,
- 'house_level' => $houseLevel,
- 'talent_level' => $talentLevel,
- 'fee_rate' => $bestConfig->fee_rate,
- 'description' => $bestConfig->description
- ]);
- return (float)$bestConfig->fee_rate;
- }
- // 如果没有找到匹配的配置,返回默认费率
- return $this->getDefaultFeeRate();
- }
- /**
- * 计算配置的匹配分数
- *
- * @param UrsTransferFeeConfig $config 配置
- * @param int $houseLevel 房屋等级
- * @param int $talentLevel 达人等级
- * @return int 匹配分数
- */
- private function calculateMatchScore(UrsTransferFeeConfig $config, int $houseLevel, int $talentLevel): int
- {
- $score = 0;
- // 精确匹配房屋等级得分更高
- if ($config->house_level === $houseLevel) {
- $score += 100;
- } elseif ($config->house_level === 0) {
- $score += 10;
- } else {
- return 0; // 不匹配
- }
- // 精确匹配达人等级得分更高
- if ($config->talent_level === $talentLevel) {
- $score += 100;
- } elseif ($config->talent_level === 0) {
- $score += 10;
- } else {
- return 0; // 不匹配
- }
- // 加上优先级分数
- $score += $config->priority;
- return $score;
- }
- /**
- * 获取用户的房屋等级
- *
- * @param int $userId 农场用户ID
- * @return int 房屋等级
- */
- private function getUserHouseLevel(int $userId): int
- {
- $farmUser = FarmUser::where('user_id', $userId)->first();
- return $farmUser ? $farmUser->house_level : 1;
- }
- /**
- * 获取用户的达人等级
- *
- * @param int $userId 农场用户ID
- * @return int 达人等级
- */
- private function getUserTalentLevel(int $userId): int
- {
- try {
- // 通过映射关系获取URS用户ID
- $ursUserId = UrsUserMappingService::getUrsUserId($userId);
- if (!$ursUserId) {
- return 0; // 用户未进入URS系统
- }
- // 获取达人等级信息
- $talentDto = UrsTalentService::getTalentInfo($ursUserId);
- return $talentDto ? $talentDto->talentLevel : 0;
- } catch (\Exception $e) {
- Log::warning('获取用户达人等级失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return 0;
- }
- }
- /**
- * 获取默认手续费率
- *
- * @return float 默认手续费率
- */
- private function getDefaultFeeRate(): float
- {
- // 获取默认配置(房屋等级0,达人等级0)
- $defaultConfig = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
- ->where('house_level', 0)
- ->where('talent_level', 0)
- ->orderBy('priority', 'desc')
- ->first();
- return $defaultConfig ? (float)$defaultConfig->fee_rate : 0.05; // 默认5%
- }
- /**
- * 获取所有手续费配置
- *
- * @return array
- */
- public function getAllConfigs(): array
- {
- $configs = UrsTransferFeeConfig::orderBy('priority', 'desc')
- ->orderBy('house_level')
- ->orderBy('talent_level')
- ->get();
- return $configs->map(function ($config) {
- return UrsTransferFeeConfigDto::fromModel($config);
- })->all();
- }
- /**
- * 清除用户手续费率缓存
- *
- * @param int $userId 农场用户ID
- * @return void
- */
- public function clearUserFeeRateCache(int $userId): void
- {
- $cacheKey = self::CACHE_PREFIX . "user_fee_rate:{$userId}";
- Cache::forget($cacheKey);
- }
- /**
- * 清除所有手续费率缓存
- *
- * @return void
- */
- public function clearAllFeeRateCache(): void
- {
- // 这里可以实现更精确的缓存清理逻辑
- // 暂时使用简单的方式
- Cache::flush();
- }
- }
|