TestUrsTransferFeeCommand.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\UrsPromotion\Services\UrsTransferFeeService;
  5. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  6. use App\Module\UrsPromotion\Services\UrsTalentService;
  7. use App\Module\Farm\Models\FarmUser;
  8. use App\Module\Transfer\Events\FeeCalculatingEvent;
  9. use App\Module\Transfer\Models\TransferApp;
  10. use Illuminate\Support\Facades\Event;
  11. /**
  12. * URS转出手续费功能测试命令
  13. */
  14. class TestUrsTransferFeeCommand extends Command
  15. {
  16. /**
  17. * 命令签名
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'urs:test-transfer-fee {--user-id=1 : 测试用户ID} {--clear-cache : 清除缓存}';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = 'URS转出手续费功能测试';
  28. /**
  29. * 执行命令
  30. */
  31. public function handle()
  32. {
  33. $this->info('=== URS转出手续费功能测试 ===');
  34. $userId = (int)$this->option('user-id');
  35. $clearCache = $this->option('clear-cache');
  36. if ($clearCache) {
  37. $this->info('清除缓存...');
  38. UrsTransferFeeService::clearAllFeeRateCache();
  39. $this->info('缓存已清除');
  40. }
  41. // 测试1: 获取用户手续费率
  42. $this->testUserFeeRate($userId);
  43. // 测试2: 测试不同等级的手续费率
  44. $this->testDifferentLevelFeeRates();
  45. // 测试3: 测试事件监听器
  46. $this->testFeeCalculatingEvent($userId);
  47. // 测试4: 获取所有配置
  48. $this->testGetAllConfigs();
  49. // 测试5: 获取用户手续费优惠信息
  50. $this->testUserFeeInfo($userId);
  51. $this->info('=== 测试完成 ===');
  52. }
  53. /**
  54. * 测试用户手续费率
  55. */
  56. private function testUserFeeRate(int $userId): void
  57. {
  58. $this->info("\n--- 测试用户手续费率 ---");
  59. $this->info("测试用户ID: {$userId}");
  60. // 获取用户农场信息
  61. $farmUser = FarmUser::where('user_id', $userId)->first();
  62. if ($farmUser) {
  63. $this->info("用户房屋等级: {$farmUser->house_level}");
  64. } else {
  65. $this->warn("用户农场信息不存在");
  66. }
  67. // 获取用户URS信息
  68. $ursUserId = UrsUserMappingService::getUrsUserId($userId);
  69. if ($ursUserId) {
  70. $this->info("URS用户ID: {$ursUserId}");
  71. $talentDto = UrsTalentService::getTalentInfo($ursUserId);
  72. if ($talentDto) {
  73. $this->info("达人等级: {$talentDto->talentLevel} ({$talentDto->talentName})");
  74. } else {
  75. $this->warn("达人等级信息不存在");
  76. }
  77. } else {
  78. $this->warn("用户未进入URS系统");
  79. }
  80. // 获取手续费率
  81. $feeRate = UrsTransferFeeService::getBestFeeRateForUser($userId);
  82. $this->info("最优手续费率: " . number_format($feeRate * 100, 2) . '%');
  83. }
  84. /**
  85. * 测试不同等级的手续费率
  86. */
  87. private function testDifferentLevelFeeRates(): void
  88. {
  89. $this->info("\n--- 测试不同等级的手续费率 ---");
  90. $testCases = [
  91. ['house' => 1, 'talent' => 0, 'desc' => '新手用户(房屋1级,无达人等级)'],
  92. ['house' => 7, 'talent' => 0, 'desc' => '房屋7级用户'],
  93. ['house' => 10, 'talent' => 0, 'desc' => '房屋10级用户'],
  94. ['house' => 12, 'talent' => 0, 'desc' => '房屋12级用户'],
  95. ['house' => 1, 'talent' => 1, 'desc' => '初级达人'],
  96. ['house' => 1, 'talent' => 3, 'desc' => '高级达人'],
  97. ['house' => 1, 'talent' => 5, 'desc' => '顶级达人'],
  98. ['house' => 10, 'talent' => 3, 'desc' => '房屋10级+高级达人'],
  99. ['house' => 12, 'talent' => 5, 'desc' => '房屋12级+顶级达人'],
  100. ];
  101. foreach ($testCases as $case) {
  102. $feeRate = UrsTransferFeeService::calculateBestFeeRate($case['house'], $case['talent']);
  103. $this->info(sprintf(
  104. "%s: %s",
  105. $case['desc'],
  106. number_format($feeRate * 100, 2) . '%'
  107. ));
  108. }
  109. }
  110. /**
  111. * 测试手续费计算事件
  112. */
  113. private function testFeeCalculatingEvent(int $userId): void
  114. {
  115. $this->info("\n--- 测试手续费计算事件 ---");
  116. // 创建一个模拟的Transfer应用
  117. $app = new TransferApp();
  118. $app->id = 1;
  119. $app->name = '测试应用';
  120. // 创建手续费计算事件
  121. $event = new FeeCalculatingEvent(
  122. $app,
  123. '1000.0000',
  124. 'out',
  125. 0.05, // 5%的默认费率
  126. '50.0000',
  127. '950.0000',
  128. ['user_id' => $userId]
  129. );
  130. $this->info("事件创建前:");
  131. $this->info("- 原始金额: {$event->amount}");
  132. $this->info("- 原始费率: " . number_format($event->feeRate * 100, 2) . '%');
  133. $this->info("- 原始手续费: {$event->feeAmount}");
  134. // 触发事件
  135. Event::dispatch($event);
  136. $this->info("事件处理后:");
  137. $this->info("- 最终费率: " . number_format($event->feeRate * 100, 2) . '%');
  138. $this->info("- 最终手续费: {$event->feeAmount}");
  139. $this->info("- 实际到账: {$event->actualAmount}");
  140. $this->info("- 是否被修改: " . ($event->isModified ? '是' : '否'));
  141. if ($event->isModified) {
  142. $this->info("- 修改原因: {$event->modificationReason}");
  143. $this->info("- 修改者: {$event->modifiedBy}");
  144. }
  145. }
  146. /**
  147. * 测试获取所有配置
  148. */
  149. private function testGetAllConfigs(): void
  150. {
  151. $this->info("\n--- 测试获取所有配置 ---");
  152. $configs = UrsTransferFeeService::getAllConfigs();
  153. $this->info("配置总数: " . count($configs));
  154. foreach ($configs as $config) {
  155. $this->info(sprintf(
  156. "ID:%d - %s - %s - 优先级:%d",
  157. $config->id,
  158. $config->getMatchConditionDescription(),
  159. $config->getFeeRatePercentage(),
  160. $config->priority
  161. ));
  162. }
  163. }
  164. /**
  165. * 测试用户手续费优惠信息
  166. */
  167. private function testUserFeeInfo(int $userId): void
  168. {
  169. $this->info("\n--- 测试用户手续费优惠信息 ---");
  170. $feeInfo = UrsTransferFeeService::getUserFeeInfo($userId);
  171. $this->info("用户手续费信息:");
  172. $this->info("- 当前费率: {$feeInfo['fee_rate_percentage']}");
  173. $this->info("- 默认费率: {$feeInfo['default_rate_percentage']}");
  174. $this->info("- 优惠金额: " . number_format($feeInfo['discount_amount'] * 100, 2) . '%');
  175. $this->info("- 优惠幅度: {$feeInfo['discount_percentage']}");
  176. $this->info("- 是否有优惠: " . ($feeInfo['has_discount'] ? '是' : '否'));
  177. }
  178. }