CheckUserLogsCommand.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Module\Fund\Commands;
  3. use App\Module\Fund\Models\FundLogModel;
  4. use App\Module\Fund\Services\LogService;
  5. use Illuminate\Console\Command;
  6. class CheckUserLogsCommand extends \UCore\Command\Command
  7. {
  8. /**
  9. * 命令名称和参数
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'fund:check-logs {user_id : 用户ID}';
  14. /**
  15. * 命令描述
  16. *
  17. * @var string
  18. */
  19. protected $description = '检查指定用户的资金日志完整性';
  20. /**
  21. * 执行命令
  22. */
  23. public function handleRun()
  24. {
  25. $userId = $this->argument('user_id');
  26. $this->info("开始检查用户 {$userId} 的资金日志...");
  27. try {
  28. // 获取用户所有日志
  29. $logs = FundLogModel::where('user_id', $userId)
  30. ->orderBy('id', 'asc')
  31. ->get();
  32. if ($logs->isEmpty()) {
  33. $this->warn("该用户没有资金日志记录");
  34. return 0;
  35. }
  36. $this->info("共发现 {$logs->count()} 条日志记录");
  37. $this->newLine();
  38. $bar = $this->output->createProgressBar($logs->count());
  39. $bar->start();
  40. $prevHash = '';
  41. $errors = [];
  42. foreach ($logs as $log) {
  43. // 验证哈希值
  44. $currentHash = $log->generateHash();
  45. if ($currentHash !== $log->hash) {
  46. $errors[] = [
  47. 'id' => $log->id,
  48. 'type' => 'hash',
  49. 'message' => "日志ID {$log->id} 的哈希值不匹配"
  50. ];
  51. }
  52. // 验证链式哈希
  53. if ($log->prev_hash !== $prevHash) {
  54. $errors[] = [
  55. 'id' => $log->id,
  56. 'type' => 'chain',
  57. 'message' => "日志ID {$log->id} 的前序哈希不匹配"
  58. ];
  59. }
  60. $prevHash = $log->hash;
  61. $bar->advance();
  62. }
  63. $bar->finish();
  64. $this->newLine(2);
  65. if (empty($errors)) {
  66. $this->info("✓ 验证通过!所有日志记录完整性检查通过。");
  67. return 0;
  68. }
  69. $this->error("× 发现以下问题:");
  70. foreach ($errors as $error) {
  71. $this->line("- {$error['message']}");
  72. }
  73. return 1;
  74. } catch (\Exception $e) {
  75. $this->error("检查过程中发生错误:" . $e->getMessage());
  76. return 1;
  77. }
  78. }
  79. }