UrsTransferFeeLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * @return float 手续费率
  30. */
  31. public function getBestFeeRateForUser(int $userId): float
  32. {
  33. try {
  34. // 尝试从缓存获取
  35. $cacheKey = self::CACHE_PREFIX . "user_fee_rate:{$userId}";
  36. $cachedRate = Cache::get($cacheKey);
  37. if ($cachedRate !== null) {
  38. return (float)$cachedRate;
  39. }
  40. // 获取用户的房屋等级
  41. $houseLevel = $this->getUserHouseLevel($userId);
  42. // 获取用户的达人等级
  43. $talentLevel = $this->getUserTalentLevel($userId);
  44. // 获取最优手续费率
  45. $feeRate = $this->calculateBestFeeRate($houseLevel, $talentLevel);
  46. // 缓存结果
  47. Cache::put($cacheKey, $feeRate, self::CACHE_TTL);
  48. Log::info('URS转出手续费率计算完成', [
  49. 'user_id' => $userId,
  50. 'house_level' => $houseLevel,
  51. 'talent_level' => $talentLevel,
  52. 'fee_rate' => $feeRate
  53. ]);
  54. return $feeRate;
  55. } catch (\Exception $e) {
  56. Log::error('URS转出手续费率计算失败', [
  57. 'user_id' => $userId,
  58. 'error' => $e->getMessage(),
  59. 'trace' => $e->getTraceAsString()
  60. ]);
  61. // 返回默认费率
  62. return $this->getDefaultFeeRate();
  63. }
  64. }
  65. /**
  66. * 根据房屋等级和达人等级计算最优手续费率
  67. *
  68. * @param int $houseLevel 房屋等级
  69. * @param int $talentLevel 达人等级
  70. * @return float 手续费率
  71. */
  72. public function calculateBestFeeRate(int $houseLevel, int $talentLevel): float
  73. {
  74. // 获取所有匹配的配置,按优先级排序
  75. $configs = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
  76. ->where(function ($query) use ($houseLevel) {
  77. $query->where('house_level', 0)
  78. ->orWhere('house_level', $houseLevel);
  79. })
  80. ->where(function ($query) use ($talentLevel) {
  81. $query->where('talent_level', 0)
  82. ->orWhere('talent_level', $talentLevel);
  83. })
  84. ->orderBy('priority', 'desc')
  85. ->get();
  86. // 找到最匹配的配置
  87. $bestConfig = null;
  88. $bestScore = -1;
  89. foreach ($configs as $config) {
  90. $score = $this->calculateMatchScore($config, $houseLevel, $talentLevel);
  91. if ($score > $bestScore) {
  92. $bestScore = $score;
  93. $bestConfig = $config;
  94. }
  95. }
  96. if ($bestConfig) {
  97. Log::info('找到最优手续费配置', [
  98. 'config_id' => $bestConfig->id,
  99. 'house_level' => $houseLevel,
  100. 'talent_level' => $talentLevel,
  101. 'fee_rate' => $bestConfig->fee_rate,
  102. 'description' => $bestConfig->description
  103. ]);
  104. return (float)$bestConfig->fee_rate;
  105. }
  106. // 如果没有找到匹配的配置,返回默认费率
  107. return $this->getDefaultFeeRate();
  108. }
  109. /**
  110. * 计算配置的匹配分数
  111. *
  112. * @param UrsTransferFeeConfig $config 配置
  113. * @param int $houseLevel 房屋等级
  114. * @param int $talentLevel 达人等级
  115. * @return int 匹配分数
  116. */
  117. private function calculateMatchScore(UrsTransferFeeConfig $config, int $houseLevel, int $talentLevel): int
  118. {
  119. $score = 0;
  120. // 精确匹配房屋等级得分更高
  121. if ($config->house_level === $houseLevel) {
  122. $score += 100;
  123. } elseif ($config->house_level === 0) {
  124. $score += 10;
  125. } else {
  126. return 0; // 不匹配
  127. }
  128. // 精确匹配达人等级得分更高
  129. if ($config->talent_level === $talentLevel) {
  130. $score += 100;
  131. } elseif ($config->talent_level === 0) {
  132. $score += 10;
  133. } else {
  134. return 0; // 不匹配
  135. }
  136. // 加上优先级分数
  137. $score += $config->priority;
  138. return $score;
  139. }
  140. /**
  141. * 获取用户的房屋等级
  142. *
  143. * @param int $userId 农场用户ID
  144. * @return int 房屋等级
  145. */
  146. private function getUserHouseLevel(int $userId): int
  147. {
  148. $farmUser = FarmUser::where('user_id', $userId)->first();
  149. return $farmUser ? $farmUser->house_level : 1;
  150. }
  151. /**
  152. * 获取用户的达人等级
  153. *
  154. * @param int $userId 农场用户ID
  155. * @return int 达人等级
  156. */
  157. private function getUserTalentLevel(int $userId): int
  158. {
  159. try {
  160. // 通过映射关系获取URS用户ID
  161. $ursUserId = UrsUserMappingService::getUrsUserId($userId);
  162. if (!$ursUserId) {
  163. return 0; // 用户未进入URS系统
  164. }
  165. // 获取达人等级信息
  166. $talentDto = UrsTalentService::getTalentInfo($ursUserId);
  167. return $talentDto ? $talentDto->talentLevel : 0;
  168. } catch (\Exception $e) {
  169. Log::warning('获取用户达人等级失败', [
  170. 'user_id' => $userId,
  171. 'error' => $e->getMessage()
  172. ]);
  173. return 0;
  174. }
  175. }
  176. /**
  177. * 获取默认手续费率
  178. *
  179. * @return float 默认手续费率
  180. */
  181. private function getDefaultFeeRate(): float
  182. {
  183. // 获取默认配置(房屋等级0,达人等级0)
  184. $defaultConfig = UrsTransferFeeConfig::where('status', UrsTransferFeeConfig::STATUS_ENABLED)
  185. ->where('house_level', 0)
  186. ->where('talent_level', 0)
  187. ->orderBy('priority', 'desc')
  188. ->first();
  189. return $defaultConfig ? (float)$defaultConfig->fee_rate : 0.05; // 默认5%
  190. }
  191. /**
  192. * 获取所有手续费配置
  193. *
  194. * @return array
  195. */
  196. public function getAllConfigs(): array
  197. {
  198. $configs = UrsTransferFeeConfig::orderBy('priority', 'desc')
  199. ->orderBy('house_level')
  200. ->orderBy('talent_level')
  201. ->get();
  202. return $configs->map(function ($config) {
  203. return UrsTransferFeeConfigDto::fromModel($config);
  204. })->all();
  205. }
  206. /**
  207. * 清除用户手续费率缓存
  208. *
  209. * @param int $userId 农场用户ID
  210. * @return void
  211. */
  212. public function clearUserFeeRateCache(int $userId): void
  213. {
  214. $cacheKey = self::CACHE_PREFIX . "user_fee_rate:{$userId}";
  215. Cache::forget($cacheKey);
  216. }
  217. /**
  218. * 清除所有手续费率缓存
  219. *
  220. * @return void
  221. */
  222. public function clearAllFeeRateCache(): void
  223. {
  224. // 这里可以实现更精确的缓存清理逻辑
  225. // 暂时使用简单的方式
  226. Cache::flush();
  227. }
  228. }