TestUserLogClearCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Services\UserLogService;
  4. use App\Module\Game\Models\UserLogClearRecord;
  5. use App\Module\Game\Models\UserLog;
  6. use Illuminate\Console\Command;
  7. /**
  8. * 测试用户日志清理逻辑命令
  9. */
  10. class TestUserLogClearCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'game:test-user-log-clear {user_id=10001 : 测试用户ID}';
  18. /**
  19. * 命令描述
  20. *
  21. * @var string
  22. */
  23. protected $description = '测试用户日志清理逻辑';
  24. /**
  25. * 执行命令
  26. *
  27. * @return int
  28. */
  29. public function handle(): int
  30. {
  31. $testUserId = (int) $this->argument('user_id');
  32. $this->info("=== 用户日志清理逻辑测试 ===");
  33. $this->newLine();
  34. // 1. 查看用户当前的日志数量
  35. $this->info("1. 查看用户 {$testUserId} 当前可见的日志数量:");
  36. $logs = UserLogService::getUserLogs($testUserId, 1, 10);
  37. $this->line(" 可见日志总数: {$logs->total()}");
  38. $this->line(" 当前页日志数: {$logs->count()}");
  39. $this->newLine();
  40. // 2. 查看用户的清理记录
  41. $this->info("2. 查看用户的清理记录:");
  42. $clearRecord = UserLogClearRecord::getUserLastClearTime($testUserId);
  43. if ($clearRecord) {
  44. $this->line(" 最后清理时间: {$clearRecord->toDateTimeString()}");
  45. } else {
  46. $this->line(" 无清理记录");
  47. }
  48. $this->newLine();
  49. // 3. 执行清理操作
  50. $this->info("3. 执行清理操作:");
  51. $success = UserLogService::clearUserLogs($testUserId);
  52. if ($success) {
  53. $this->line(" 清理成功");
  54. } else {
  55. $this->line(" 清理失败");
  56. }
  57. $this->newLine();
  58. // 4. 查看清理后的日志数量
  59. $this->info("4. 查看清理后的日志数量:");
  60. $logsAfterClear = UserLogService::getUserLogs($testUserId, 1, 10);
  61. $this->line(" 可见日志总数: {$logsAfterClear->total()}");
  62. $this->line(" 当前页日志数: {$logsAfterClear->count()}");
  63. $this->newLine();
  64. // 5. 查看新的清理记录
  65. $this->info("5. 查看新的清理记录:");
  66. $newClearRecord = UserLogClearRecord::getUserLastClearTime($testUserId);
  67. if ($newClearRecord) {
  68. $this->line(" 最后清理时间: {$newClearRecord->toDateTimeString()}");
  69. } else {
  70. $this->line(" 无清理记录");
  71. }
  72. $this->newLine();
  73. // 6. 验证数据库中的实际日志数量没有减少
  74. $this->info("6. 验证数据库中的实际日志数量:");
  75. $totalLogsInDb = UserLog::byUser($testUserId)->count();
  76. $this->line(" 数据库中实际日志总数: {$totalLogsInDb}");
  77. $this->line(" 说明: 实际日志没有被删除,只是逻辑上被隐藏");
  78. $this->newLine();
  79. $this->info("=== 测试完成 ===");
  80. return 0;
  81. }
  82. }