TestFeeTransferCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. {--transfer-type=fee_transfer : 转移类型}';
  23. /**
  24. * 命令描述
  25. */
  26. protected $description = '测试手续费转移功能';
  27. /**
  28. * 执行命令
  29. */
  30. public function handle()
  31. {
  32. $appId = (int) $this->option('app-id');
  33. $targetUserId = (int) $this->option('target-user-id');
  34. $amount = (float) $this->option('amount');
  35. $orderId = (int) $this->option('order-id');
  36. $transferType = (string) $this->option('transfer-type');
  37. $this->info("开始测试手续费转移功能");
  38. $this->info("应用ID: {$appId}");
  39. $this->info("目标用户ID: {$targetUserId}");
  40. $this->info("转移金额: {$amount}");
  41. $this->info("订单ID: {$orderId}");
  42. $this->info("转移类型: {$transferType}");
  43. $this->line('');
  44. // 检查应用是否存在
  45. $app = TransferApp::find($appId);
  46. if (!$app) {
  47. $this->error("应用ID {$appId} 不存在");
  48. return 1;
  49. }
  50. $this->info("应用信息:");
  51. $this->info(" - 名称: {$app->title}");
  52. $this->info(" - 标识: {$app->keyname}");
  53. $this->info(" - 手续费账户UID: {$app->getFeeAccountUid()}");
  54. $this->info(" - 资金类型ID: {$app->fund_id}");
  55. $this->line('');
  56. // 检查目标用户是否存在
  57. $targetUser = User::find($targetUserId);
  58. if (!$targetUser) {
  59. $this->error("目标用户ID {$targetUserId} 不存在");
  60. return 1;
  61. }
  62. $this->info("目标用户信息:");
  63. $this->info(" - 用户名: {$targetUser->username}");
  64. $this->info(" - 邮箱: {$targetUser->email}");
  65. $this->line('');
  66. // 检查账户余额
  67. $feeAccountUid = $app->getFeeAccountUid();
  68. $feeAccount = FundModel::getAccount($feeAccountUid, $app->fund_id);
  69. $targetAccount = FundModel::getAccount($targetUserId, $app->fund_id);
  70. $this->info("转移前余额:");
  71. $this->info(" - 手续费账户(UID {$feeAccountUid}): " . ($feeAccount ? $feeAccount->balance : '账户不存在'));
  72. $this->info(" - 目标用户账户: " . ($targetAccount ? $targetAccount->balance : '账户不存在'));
  73. $this->line('');
  74. // 执行转移
  75. $this->info("执行手续费转移...");
  76. try {
  77. DB::beginTransaction();
  78. $result = FeeService::transfer($appId, $targetUserId, $amount, $orderId, $transferType);
  79. if ($result === true) {
  80. DB::commit();
  81. $this->info("✅ 手续费转移成功!");
  82. // 重新查询余额
  83. $feeAccount = FundModel::getAccount($feeAccountUid, $app->fund_id);
  84. $targetAccount = FundModel::getAccount($targetUserId, $app->fund_id);
  85. $this->line('');
  86. $this->info("转移后余额:");
  87. $this->info(" - 手续费账户(UID {$feeAccountUid}): " . ($feeAccount ? $feeAccount->balance : '账户不存在'));
  88. $this->info(" - 目标用户账户: " . ($targetAccount ? $targetAccount->balance : '账户不存在'));
  89. } else {
  90. DB::rollBack();
  91. $this->error("❌ 手续费转移失败: {$result}");
  92. return 1;
  93. }
  94. } catch (\Exception $e) {
  95. DB::rollBack();
  96. $this->error("❌ 转移过程中发生异常: " . $e->getMessage());
  97. $this->error("异常堆栈: " . $e->getTraceAsString());
  98. return 1;
  99. }
  100. $this->line('');
  101. $this->info("测试完成!");
  102. return 0;
  103. }
  104. }