TestFeeTransferCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Module\Transfer\Commands;
  3. use App\Module\Transfer\Services\FeeService;
  4. use App\Module\Transfer\Models\TransferApp;
  5. use App\Module\User\Models\User;
  6. use App\Module\Fund\Models\FundModel;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 测试手续费转移功能的命令
  11. */
  12. class TestFeeTransferCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. */
  17. protected $signature = 'transfer:test-fee-transfer
  18. {--app-id=1 : 应用ID}
  19. {--target-user-id=2 : 目标用户ID}
  20. {--amount=10.0 : 转移金额}
  21. {--order-id=12345 : 订单ID}';
  22. /**
  23. * 命令描述
  24. */
  25. protected $description = '测试手续费转移功能';
  26. /**
  27. * 执行命令
  28. */
  29. public function handle()
  30. {
  31. $appId = (int) $this->option('app-id');
  32. $targetUserId = (int) $this->option('target-user-id');
  33. $amount = (float) $this->option('amount');
  34. $orderId = (int) $this->option('order-id');
  35. $this->info("开始测试手续费转移功能");
  36. $this->info("应用ID: {$appId}");
  37. $this->info("目标用户ID: {$targetUserId}");
  38. $this->info("转移金额: {$amount}");
  39. $this->info("订单ID: {$orderId}");
  40. $this->line('');
  41. // 检查应用是否存在
  42. $app = TransferApp::find($appId);
  43. if (!$app) {
  44. $this->error("应用ID {$appId} 不存在");
  45. return 1;
  46. }
  47. $this->info("应用信息:");
  48. $this->info(" - 名称: {$app->title}");
  49. $this->info(" - 标识: {$app->keyname}");
  50. $this->info(" - 手续费账户UID: {$app->getFeeAccountUid()}");
  51. $this->info(" - 资金类型ID: {$app->fund_id}");
  52. $this->line('');
  53. // 检查目标用户是否存在
  54. $targetUser = User::find($targetUserId);
  55. if (!$targetUser) {
  56. $this->error("目标用户ID {$targetUserId} 不存在");
  57. return 1;
  58. }
  59. $this->info("目标用户信息:");
  60. $this->info(" - 用户名: {$targetUser->username}");
  61. $this->info(" - 邮箱: {$targetUser->email}");
  62. $this->line('');
  63. // 检查账户余额
  64. $feeAccountUid = $app->getFeeAccountUid();
  65. $feeAccount = FundModel::getAccount($feeAccountUid, $app->fund_id);
  66. $targetAccount = FundModel::getAccount($targetUserId, $app->fund_id);
  67. $this->info("转移前余额:");
  68. $this->info(" - 手续费账户(UID {$feeAccountUid}): " . ($feeAccount ? $feeAccount->balance : '账户不存在'));
  69. $this->info(" - 目标用户账户: " . ($targetAccount ? $targetAccount->balance : '账户不存在'));
  70. $this->line('');
  71. // 执行转移
  72. $this->info("执行手续费转移...");
  73. try {
  74. DB::beginTransaction();
  75. $result = FeeService::transfer($appId, $targetUserId, $amount, $orderId);
  76. if ($result === true) {
  77. DB::commit();
  78. $this->info("✅ 手续费转移成功!");
  79. // 重新查询余额
  80. $feeAccount = FundModel::getAccount($feeAccountUid, $app->fund_id);
  81. $targetAccount = FundModel::getAccount($targetUserId, $app->fund_id);
  82. $this->line('');
  83. $this->info("转移后余额:");
  84. $this->info(" - 手续费账户(UID {$feeAccountUid}): " . ($feeAccount ? $feeAccount->balance : '账户不存在'));
  85. $this->info(" - 目标用户账户: " . ($targetAccount ? $targetAccount->balance : '账户不存在'));
  86. } else {
  87. DB::rollBack();
  88. $this->error("❌ 手续费转移失败: {$result}");
  89. return 1;
  90. }
  91. } catch (\Exception $e) {
  92. DB::rollBack();
  93. $this->error("❌ 转移过程中发生异常: " . $e->getMessage());
  94. $this->error("异常堆栈: " . $e->getTraceAsString());
  95. return 1;
  96. }
  97. $this->line('');
  98. $this->info("测试完成!");
  99. return 0;
  100. }
  101. }