| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Module\Transfer\Commands;
- use App\Module\Transfer\Services\FeeService;
- use App\Module\Transfer\Models\TransferApp;
- use App\Module\User\Models\User;
- use App\Module\Fund\Models\FundModel;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * 测试手续费转移功能的命令
- */
- class TestFeeTransferCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'transfer:test-fee-transfer
- {--app-id=1 : 应用ID}
- {--target-user-id=2 : 目标用户ID}
- {--amount=10.0 : 转移金额}
- {--order-id=12345 : 订单ID}';
- /**
- * 命令描述
- */
- protected $description = '测试手续费转移功能';
- /**
- * 执行命令
- */
- public function handle()
- {
- $appId = (int) $this->option('app-id');
- $targetUserId = (int) $this->option('target-user-id');
- $amount = (float) $this->option('amount');
- $orderId = (int) $this->option('order-id');
- $this->info("开始测试手续费转移功能");
- $this->info("应用ID: {$appId}");
- $this->info("目标用户ID: {$targetUserId}");
- $this->info("转移金额: {$amount}");
- $this->info("订单ID: {$orderId}");
- $this->line('');
- // 检查应用是否存在
- $app = TransferApp::find($appId);
- if (!$app) {
- $this->error("应用ID {$appId} 不存在");
- return 1;
- }
- $this->info("应用信息:");
- $this->info(" - 名称: {$app->title}");
- $this->info(" - 标识: {$app->keyname}");
- $this->info(" - 手续费账户UID: {$app->getFeeAccountUid()}");
- $this->info(" - 资金类型ID: {$app->fund_id}");
- $this->line('');
- // 检查目标用户是否存在
- $targetUser = User::find($targetUserId);
- if (!$targetUser) {
- $this->error("目标用户ID {$targetUserId} 不存在");
- return 1;
- }
- $this->info("目标用户信息:");
- $this->info(" - 用户名: {$targetUser->username}");
- $this->info(" - 邮箱: {$targetUser->email}");
- $this->line('');
- // 检查账户余额
- $feeAccountUid = $app->getFeeAccountUid();
- $feeAccount = FundModel::getAccount($feeAccountUid, $app->fund_id);
- $targetAccount = FundModel::getAccount($targetUserId, $app->fund_id);
- $this->info("转移前余额:");
- $this->info(" - 手续费账户(UID {$feeAccountUid}): " . ($feeAccount ? $feeAccount->balance : '账户不存在'));
- $this->info(" - 目标用户账户: " . ($targetAccount ? $targetAccount->balance : '账户不存在'));
- $this->line('');
- // 执行转移
- $this->info("执行手续费转移...");
-
- try {
- DB::beginTransaction();
-
- $result = FeeService::transfer($appId, $targetUserId, $amount, $orderId);
-
- if ($result === true) {
- DB::commit();
- $this->info("✅ 手续费转移成功!");
-
- // 重新查询余额
- $feeAccount = FundModel::getAccount($feeAccountUid, $app->fund_id);
- $targetAccount = FundModel::getAccount($targetUserId, $app->fund_id);
-
- $this->line('');
- $this->info("转移后余额:");
- $this->info(" - 手续费账户(UID {$feeAccountUid}): " . ($feeAccount ? $feeAccount->balance : '账户不存在'));
- $this->info(" - 目标用户账户: " . ($targetAccount ? $targetAccount->balance : '账户不存在'));
-
- } else {
- DB::rollBack();
- $this->error("❌ 手续费转移失败: {$result}");
- return 1;
- }
-
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("❌ 转移过程中发生异常: " . $e->getMessage());
- $this->error("异常堆栈: " . $e->getTraceAsString());
- return 1;
- }
- $this->line('');
- $this->info("测试完成!");
- return 0;
- }
- }
|