| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Module\Fund\Commands;
- use App\Module\AppGame\Events\LoginSuccessEvent;
- use App\Module\Fund\Services\AccountService;
- use App\Module\User\Models\User;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Event;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试登录时资金账户创建功能
- */
- class TestLoginAccountCreationCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'fund:test-login-account-creation {user_id : 用户ID}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试用户登录时资金账户自动创建功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $userId = $this->argument('user_id');
- $this->info("开始测试用户 {$userId} 的登录资金账户创建功能...");
- try {
- // 检查用户是否存在
- $user = User::find($userId);
- if (!$user) {
- $this->error("用户 {$userId} 不存在");
- return 1;
- }
- $this->info("用户信息: ID={$user->id}, 用户名={$user->username}");
- // 检查用户当前的资金账户
- $this->info("检查用户当前的资金账户...");
- $accounts = \App\Module\Fund\Models\FundModel::userAccount($userId);
- $this->info("当前资金账户数量: " . $accounts->count());
- foreach ($accounts as $account) {
- $this->line(" - 账户ID: {$account->id}, 资金类型: {$account->fund_id->value}, 余额: {$account->balance}");
- }
- // 模拟触发登录成功事件
- $this->info("模拟触发登录成功事件...");
- Event::dispatch(new LoginSuccessEvent($user, 'test-session-' . time()));
- // 再次检查用户的资金账户
- $this->info("重新检查用户的资金账户...");
- $accountsAfter = \App\Module\Fund\Models\FundModel::userAccount($userId);
- $this->info("登录后资金账户数量: " . $accountsAfter->count());
- foreach ($accountsAfter as $account) {
- $this->line(" - 账户ID: {$account->id}, 资金类型: {$account->fund_id->value}, 余额: {$account->balance}");
- }
- // 直接测试AccountService::check4user方法
- $this->info("直接测试 AccountService::check4user 方法...");
- AccountService::check4user($userId);
- // 最终检查
- $this->info("最终检查用户的资金账户...");
- $accountsFinal = \App\Module\Fund\Models\FundModel::userAccount($userId);
- $this->info("最终资金账户数量: " . $accountsFinal->count());
- foreach ($accountsFinal as $account) {
- $this->line(" - 账户ID: {$account->id}, 资金类型: {$account->fund_id->value}, 余额: {$account->balance}");
- }
- $this->info("测试完成!");
- return 0;
- } catch (\Exception $e) {
- $this->error("测试失败: " . $e->getMessage());
- $this->error("错误详情: " . $e->getTraceAsString());
- return 1;
- }
- }
- }
|