| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?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
- * @param string $transferType 转账类型:'in'转入,'out'转出
- * @return float 手续费率
- */
- public function getBestFeeRateForUser(int $userId, string $transferType = 'out'): float
- {
- return \UCore\Helper\Cache::cacheCall([
- __CLASS__, __FUNCTION__, $userId, $transferType
- ], function ($userId, $transferType) {
- // 获取用户的房屋等级
- $houseLevel = $this->getUserHouseLevel($userId);
- // 获取用户的达人等级
- $talentLevel = $this->getUserTalentLevel($userId);
- // 获取最优手续费率
- $feeRate = $this->calculateBestFeeRate($houseLevel, $talentLevel, $transferType);
- Log::info('URS转账手续费率计算完成', [
- 'user_id' => $userId,
- 'transfer_type' => $transferType,
- 'house_level' => $houseLevel,
- 'talent_level' => $talentLevel,
- 'fee_rate' => $feeRate
- ]);
- return $feeRate;
- }, [ $userId, $transferType ], 10);
- }
- /**
- * 根据房屋等级和达人等级计算最优手续费率
- *
- * @param int $houseLevel 房屋等级
- * @param int $talentLevel 达人等级
- * @param string $transferType 转账类型:'in'转入,'out'转出
- * @return float 手续费率
- */
- public function calculateBestFeeRate(int $houseLevel, int $talentLevel, string $transferType = 'out'): float
- {
- // 获取所有匹配的配置,按优先级排序
- $configs = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
- ->where('transfer_type', $transferType)
- ->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($transferType);
- }
- /**
- * 计算配置的匹配分数
- *
- * @param UrsTransferFeeConfig $config 配置
- * @param int $houseLevel 房屋等级
- * @param int $talentLevel 达人等级
- * @return int 匹配分数
- */
- private function calculateMatchScore(UrsTransferFeeConfig $config, int $houseLevel, int $talentLevel): int
- {
- $config->priority;
- return $config->priority;
- }
- /**
- * 获取用户的房屋等级
- *
- * @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($userId);
- return $talentDto ? $talentDto->talentLevel : 0;
- } catch (\Exception $e) {
- Log::warning('获取用户达人等级失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage()
- ]);
- return 0;
- }
- }
- /**
- * 获取默认手续费率
- *
- * @param string $transferType 转账类型
- * @return float 默认手续费率
- */
- private function getDefaultFeeRate(string $transferType = 'out'): float
- {
- // 获取默认配置(房屋等级0,达人等级0)
- $defaultConfig = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
- ->where('transfer_type', $transferType)
- ->where('house_level', 0)
- ->where('talent_level', 0)
- ->orderBy('priority', 'desc')
- ->first();
- if ($defaultConfig) {
- return (float)$defaultConfig->fee_rate;
- }
- // 如果没有配置,返回硬编码默认值
- return $transferType === 'in' ? 0.0 : 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
- * @param string|null $transferType 转账类型,null表示清除所有类型
- * @return void
- */
- public function clearUserFeeRateCache(int $userId, ?string $transferType = null): void
- {
- if ($transferType) {
- $cacheKey = self::CACHE_PREFIX . "user_fee_rate:{$userId}:{$transferType}";
- Cache::forget($cacheKey);
- } else {
- // 清除所有转账类型的缓存
- $cacheKeyIn = self::CACHE_PREFIX . "user_fee_rate:{$userId}:in";
- $cacheKeyOut = self::CACHE_PREFIX . "user_fee_rate:{$userId}:out";
- Cache::forget($cacheKeyIn);
- Cache::forget($cacheKeyOut);
- }
- }
- /**
- * 清除所有手续费率缓存
- *
- * @return void
- */
- public function clearAllFeeRateCache(): void
- {
- // 这里可以实现更精确的缓存清理逻辑
- // 暂时使用简单的方式
- Cache::flush();
- }
- }
|