| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use Illuminate\Console\Command;
- use App\Module\UrsPromotion\Services\UrsTransferFeeService;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\Farm\Models\FarmUser;
- use App\Module\Transfer\Events\FeeCalculatingEvent;
- use App\Module\Transfer\Models\TransferApp;
- use Illuminate\Support\Facades\Event;
- /**
- * URS转出手续费功能测试命令
- */
- class TestUrsTransferFeeCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'urs:test-transfer-fee {--user-id=1 : 测试用户ID} {--clear-cache : 清除缓存}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'URS转出手续费功能测试';
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('=== URS转出手续费功能测试 ===');
- $userId = (int)$this->option('user-id');
- $clearCache = $this->option('clear-cache');
- if ($clearCache) {
- $this->info('清除缓存...');
- UrsTransferFeeService::clearAllFeeRateCache();
- $this->info('缓存已清除');
- }
- // 测试1: 获取用户手续费率
- $this->testUserFeeRate($userId);
- // 测试2: 测试不同等级的手续费率
- $this->testDifferentLevelFeeRates();
- // 测试3: 测试事件监听器
- $this->testFeeCalculatingEvent($userId);
- // 测试4: 获取所有配置
- $this->testGetAllConfigs();
- // 测试5: 获取用户手续费优惠信息
- $this->testUserFeeInfo($userId);
- $this->info('=== 测试完成 ===');
- }
- /**
- * 测试用户手续费率
- */
- private function testUserFeeRate(int $userId): void
- {
- $this->info("\n--- 测试用户手续费率 ---");
- $this->info("测试用户ID: {$userId}");
- // 获取用户农场信息
- $farmUser = FarmUser::where('user_id', $userId)->first();
- if ($farmUser) {
- $this->info("用户房屋等级: {$farmUser->house_level}");
- } else {
- $this->warn("用户农场信息不存在");
- }
- // 获取用户URS信息
- $ursUserId = UrsUserMappingService::getUrsUserId($userId);
- if ($ursUserId) {
- $this->info("URS用户ID: {$ursUserId}");
-
- $talentDto = UrsTalentService::getTalentInfo($ursUserId);
- if ($talentDto) {
- $this->info("达人等级: {$talentDto->talentLevel} ({$talentDto->talentName})");
- } else {
- $this->warn("达人等级信息不存在");
- }
- } else {
- $this->warn("用户未进入URS系统");
- }
- // 获取手续费率
- $feeRate = UrsTransferFeeService::getBestFeeRateForUser($userId);
- $this->info("最优手续费率: " . number_format($feeRate * 100, 2) . '%');
- }
- /**
- * 测试不同等级的手续费率
- */
- private function testDifferentLevelFeeRates(): void
- {
- $this->info("\n--- 测试不同等级的手续费率 ---");
- $testCases = [
- ['house' => 1, 'talent' => 0, 'desc' => '新手用户(房屋1级,无达人等级)'],
- ['house' => 7, 'talent' => 0, 'desc' => '房屋7级用户'],
- ['house' => 10, 'talent' => 0, 'desc' => '房屋10级用户'],
- ['house' => 12, 'talent' => 0, 'desc' => '房屋12级用户'],
- ['house' => 1, 'talent' => 1, 'desc' => '初级达人'],
- ['house' => 1, 'talent' => 3, 'desc' => '高级达人'],
- ['house' => 1, 'talent' => 5, 'desc' => '顶级达人'],
- ['house' => 10, 'talent' => 3, 'desc' => '房屋10级+高级达人'],
- ['house' => 12, 'talent' => 5, 'desc' => '房屋12级+顶级达人'],
- ];
- foreach ($testCases as $case) {
- $feeRate = UrsTransferFeeService::calculateBestFeeRate($case['house'], $case['talent']);
- $this->info(sprintf(
- "%s: %s",
- $case['desc'],
- number_format($feeRate * 100, 2) . '%'
- ));
- }
- }
- /**
- * 测试手续费计算事件
- */
- private function testFeeCalculatingEvent(int $userId): void
- {
- $this->info("\n--- 测试手续费计算事件 ---");
- // 创建一个模拟的Transfer应用
- $app = new TransferApp();
- $app->id = 1;
- $app->name = '测试应用';
- // 创建手续费计算事件
- $event = new FeeCalculatingEvent(
- $app,
- '1000.0000',
- 'out',
- 0.05, // 5%的默认费率
- '50.0000',
- '950.0000',
- ['user_id' => $userId]
- );
- $this->info("事件创建前:");
- $this->info("- 原始金额: {$event->amount}");
- $this->info("- 原始费率: " . number_format($event->feeRate * 100, 2) . '%');
- $this->info("- 原始手续费: {$event->feeAmount}");
- // 触发事件
- Event::dispatch($event);
- $this->info("事件处理后:");
- $this->info("- 最终费率: " . number_format($event->feeRate * 100, 2) . '%');
- $this->info("- 最终手续费: {$event->feeAmount}");
- $this->info("- 实际到账: {$event->actualAmount}");
- $this->info("- 是否被修改: " . ($event->isModified ? '是' : '否'));
- if ($event->isModified) {
- $this->info("- 修改原因: {$event->modificationReason}");
- $this->info("- 修改者: {$event->modifiedBy}");
- }
- }
- /**
- * 测试获取所有配置
- */
- private function testGetAllConfigs(): void
- {
- $this->info("\n--- 测试获取所有配置 ---");
- $configs = UrsTransferFeeService::getAllConfigs();
- $this->info("配置总数: " . count($configs));
- foreach ($configs as $config) {
- $this->info(sprintf(
- "ID:%d - %s - %s - 优先级:%d",
- $config->id,
- $config->getMatchConditionDescription(),
- $config->getFeeRatePercentage(),
- $config->priority
- ));
- }
- }
- /**
- * 测试用户手续费优惠信息
- */
- private function testUserFeeInfo(int $userId): void
- {
- $this->info("\n--- 测试用户手续费优惠信息 ---");
- $feeInfo = UrsTransferFeeService::getUserFeeInfo($userId);
- $this->info("用户手续费信息:");
- $this->info("- 当前费率: {$feeInfo['fee_rate_percentage']}");
- $this->info("- 默认费率: {$feeInfo['default_rate_percentage']}");
- $this->info("- 优惠金额: " . number_format($feeInfo['discount_amount'] * 100, 2) . '%');
- $this->info("- 优惠幅度: {$feeInfo['discount_percentage']}");
- $this->info("- 是否有优惠: " . ($feeInfo['has_discount'] ? '是' : '否'));
- }
- }
|