| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Services\UserLogService;
- use App\Module\Game\Models\UserLogClearRecord;
- use App\Module\Game\Models\UserLog;
- use Illuminate\Console\Command;
- /**
- * 测试用户日志清理逻辑命令
- */
- class TestUserLogClearCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'game:test-user-log-clear {user_id=10001 : 测试用户ID}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试用户日志清理逻辑';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $testUserId = (int) $this->argument('user_id');
- $this->info("=== 用户日志清理逻辑测试 ===");
- $this->newLine();
- // 1. 查看用户当前的日志数量
- $this->info("1. 查看用户 {$testUserId} 当前可见的日志数量:");
- $logs = UserLogService::getUserLogs($testUserId, 1, 10);
- $this->line(" 可见日志总数: {$logs->total()}");
- $this->line(" 当前页日志数: {$logs->count()}");
- $this->newLine();
- // 2. 查看用户的清理记录
- $this->info("2. 查看用户的清理记录:");
- $clearRecord = UserLogClearRecord::getUserLastClearTime($testUserId);
- if ($clearRecord) {
- $this->line(" 最后清理时间: {$clearRecord->toDateTimeString()}");
- } else {
- $this->line(" 无清理记录");
- }
- $this->newLine();
- // 3. 执行清理操作
- $this->info("3. 执行清理操作:");
- $success = UserLogService::clearUserLogs($testUserId);
- if ($success) {
- $this->line(" 清理成功");
- } else {
- $this->line(" 清理失败");
- }
- $this->newLine();
- // 4. 查看清理后的日志数量
- $this->info("4. 查看清理后的日志数量:");
- $logsAfterClear = UserLogService::getUserLogs($testUserId, 1, 10);
- $this->line(" 可见日志总数: {$logsAfterClear->total()}");
- $this->line(" 当前页日志数: {$logsAfterClear->count()}");
- $this->newLine();
- // 5. 查看新的清理记录
- $this->info("5. 查看新的清理记录:");
- $newClearRecord = UserLogClearRecord::getUserLastClearTime($testUserId);
- if ($newClearRecord) {
- $this->line(" 最后清理时间: {$newClearRecord->toDateTimeString()}");
- } else {
- $this->line(" 无清理记录");
- }
- $this->newLine();
- // 6. 验证数据库中的实际日志数量没有减少
- $this->info("6. 验证数据库中的实际日志数量:");
- $totalLogsInDb = UserLog::byUser($testUserId)->count();
- $this->line(" 数据库中实际日志总数: {$totalLogsInDb}");
- $this->line(" 说明: 实际日志没有被删除,只是逻辑上被隐藏");
- $this->newLine();
- $this->info("=== 测试完成 ===");
- return 0;
- }
- }
|