UrsTransferFeeLogic.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Module\UrsPromotion\Logics;
  3. use App\Module\UrsPromotion\Models\UrsTransferFeeConfig;
  4. use App\Module\UrsPromotion\Dtos\UrsTransferFeeConfigDto;
  5. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  6. use App\Module\UrsPromotion\Services\UrsTalentService;
  7. use App\Module\Farm\Models\FarmUser;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Cache;
  10. /**
  11. * URS转出手续费逻辑层
  12. *
  13. * 处理URS推广模块的转出手续费计算逻辑
  14. */
  15. class UrsTransferFeeLogic
  16. {
  17. /**
  18. * 缓存前缀
  19. */
  20. private const CACHE_PREFIX = 'urs_transfer_fee:';
  21. /**
  22. * 缓存时间(秒)
  23. */
  24. private const CACHE_TTL = 3600; // 1小时
  25. /**
  26. * 根据用户ID获取最优手续费率
  27. *
  28. * @param int $userId 农场用户ID
  29. * @param string $transferType 转账类型:'in'转入,'out'转出
  30. * @return float 手续费率
  31. */
  32. public function getBestFeeRateForUser(int $userId, string $transferType = 'out'): float
  33. {
  34. return \UCore\Helper\Cache::cacheCall([
  35. __CLASS__, __FUNCTION__, $userId, $transferType
  36. ], function ($userId, $transferType) {
  37. // 获取用户的房屋等级
  38. $houseLevel = $this->getUserHouseLevel($userId);
  39. // 获取用户的达人等级
  40. $talentLevel = $this->getUserTalentLevel($userId);
  41. // 获取最优手续费率
  42. $feeRate = $this->calculateBestFeeRate($houseLevel, $talentLevel, $transferType);
  43. Log::info('URS转账手续费率计算完成', [
  44. 'user_id' => $userId,
  45. 'transfer_type' => $transferType,
  46. 'house_level' => $houseLevel,
  47. 'talent_level' => $talentLevel,
  48. 'fee_rate' => $feeRate
  49. ]);
  50. return $feeRate;
  51. }, [ $userId, $transferType ], 10);
  52. }
  53. /**
  54. * 根据房屋等级和达人等级计算最优手续费率
  55. *
  56. * @param int $houseLevel 房屋等级
  57. * @param int $talentLevel 达人等级
  58. * @param string $transferType 转账类型:'in'转入,'out'转出
  59. * @return float 手续费率
  60. */
  61. public function calculateBestFeeRate(int $houseLevel, int $talentLevel, string $transferType = 'out'): float
  62. {
  63. // 获取所有匹配的配置,按优先级排序
  64. $configs = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
  65. ->where('transfer_type', $transferType)
  66. ->where(function ($query) use ($houseLevel) {
  67. $query->where('house_level', 0)
  68. ->orWhere('house_level', '<=', $houseLevel);
  69. })
  70. ->where(function ($query) use ($talentLevel) {
  71. $query->where('talent_level', 0)
  72. ->orWhere('talent_level', '<=', $talentLevel);
  73. })
  74. ->orderBy('priority', 'desc')
  75. ->get();
  76. // 找到最匹配的配置
  77. $bestConfig = null;
  78. $bestScore = -1;
  79. foreach ($configs as $config) {
  80. $score = $this->calculateMatchScore($config, $houseLevel, $talentLevel);
  81. if ($score > $bestScore) {
  82. $bestScore = $score;
  83. $bestConfig = $config;
  84. }
  85. }
  86. if ($bestConfig) {
  87. Log::info('找到最优手续费配置', [
  88. 'config_id' => $bestConfig->id,
  89. 'house_level' => $houseLevel,
  90. 'talent_level' => $talentLevel,
  91. 'fee_rate' => $bestConfig->fee_rate,
  92. 'description' => $bestConfig->description
  93. ]);
  94. return (float)$bestConfig->fee_rate;
  95. }
  96. // 如果没有找到匹配的配置,返回默认费率
  97. return $this->getDefaultFeeRate($transferType);
  98. }
  99. /**
  100. * 计算配置的匹配分数
  101. *
  102. * @param UrsTransferFeeConfig $config 配置
  103. * @param int $houseLevel 房屋等级
  104. * @param int $talentLevel 达人等级
  105. * @return int 匹配分数
  106. */
  107. private function calculateMatchScore(UrsTransferFeeConfig $config, int $houseLevel, int $talentLevel): int
  108. {
  109. $config->priority;
  110. return $config->priority;
  111. }
  112. /**
  113. * 获取用户的房屋等级
  114. *
  115. * @param int $userId 农场用户ID
  116. * @return int 房屋等级
  117. */
  118. private function getUserHouseLevel(int $userId): int
  119. {
  120. $farmUser = FarmUser::where('user_id', $userId)->first();
  121. return $farmUser ? $farmUser->house_level : 1;
  122. }
  123. /**
  124. * 获取用户的达人等级
  125. *
  126. * @param int $userId 农场用户ID
  127. * @return int 达人等级
  128. */
  129. private function getUserTalentLevel(int $userId): int
  130. {
  131. try {
  132. // 通过映射关系获取URS用户ID
  133. $ursUserId = UrsUserMappingService::getUrsUserId($userId);
  134. if (!$ursUserId) {
  135. return 0; // 用户未进入URS系统
  136. }
  137. // 获取达人等级信息
  138. $talentDto = UrsTalentService::getTalentInfo($userId);
  139. return $talentDto ? $talentDto->talentLevel : 0;
  140. } catch (\Exception $e) {
  141. Log::warning('获取用户达人等级失败', [
  142. 'user_id' => $userId,
  143. 'error' => $e->getMessage()
  144. ]);
  145. return 0;
  146. }
  147. }
  148. /**
  149. * 获取默认手续费率
  150. *
  151. * @param string $transferType 转账类型
  152. * @return float 默认手续费率
  153. */
  154. private function getDefaultFeeRate(string $transferType = 'out'): float
  155. {
  156. // 获取默认配置(房屋等级0,达人等级0)
  157. $defaultConfig = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
  158. ->where('transfer_type', $transferType)
  159. ->where('house_level', 0)
  160. ->where('talent_level', 0)
  161. ->orderBy('priority', 'desc')
  162. ->first();
  163. if ($defaultConfig) {
  164. return (float)$defaultConfig->fee_rate;
  165. }
  166. // 如果没有配置,返回硬编码默认值
  167. return $transferType === 'in' ? 0.0 : 0.05; // 转入免费,转出5%
  168. }
  169. /**
  170. * 获取所有手续费配置
  171. *
  172. * @return array
  173. */
  174. public function getAllConfigs(): array
  175. {
  176. $configs = UrsTransferFeeConfig::orderBy('priority', 'desc')
  177. ->orderBy('house_level')
  178. ->orderBy('talent_level')
  179. ->get();
  180. return $configs->map(function ($config) {
  181. return UrsTransferFeeConfigDto::fromModel($config);
  182. })->all();
  183. }
  184. /**
  185. * 清除用户手续费率缓存
  186. *
  187. * @param int $userId 农场用户ID
  188. * @param string|null $transferType 转账类型,null表示清除所有类型
  189. * @return void
  190. */
  191. public function clearUserFeeRateCache(int $userId, ?string $transferType = null): void
  192. {
  193. if ($transferType) {
  194. $cacheKey = self::CACHE_PREFIX . "user_fee_rate:{$userId}:{$transferType}";
  195. Cache::forget($cacheKey);
  196. } else {
  197. // 清除所有转账类型的缓存
  198. $cacheKeyIn = self::CACHE_PREFIX . "user_fee_rate:{$userId}:in";
  199. $cacheKeyOut = self::CACHE_PREFIX . "user_fee_rate:{$userId}:out";
  200. Cache::forget($cacheKeyIn);
  201. Cache::forget($cacheKeyOut);
  202. }
  203. }
  204. /**
  205. * 清除所有手续费率缓存
  206. *
  207. * @return void
  208. */
  209. public function clearAllFeeRateCache(): void
  210. {
  211. // 这里可以实现更精确的缓存清理逻辑
  212. // 暂时使用简单的方式
  213. Cache::flush();
  214. }
  215. }