| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Module\Fund\Commands;
- use App\Module\Fund\Models\FundLogModel;
- use App\Module\Fund\Services\LogService;
- use Illuminate\Console\Command;
- class CheckUserLogsCommand extends \UCore\Command\Command
- {
- /**
- * 命令名称和参数
- *
- * @var string
- */
- protected $signature = 'fund:check-logs {user_id : 用户ID}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '检查指定用户的资金日志完整性';
- /**
- * 执行命令
- */
- public function handleRun()
- {
- $userId = $this->argument('user_id');
- $this->info("开始检查用户 {$userId} 的资金日志...");
- try {
- // 获取用户所有日志
- $logs = FundLogModel::where('user_id', $userId)
- ->orderBy('id', 'asc')
- ->get();
- if ($logs->isEmpty()) {
- $this->warn("该用户没有资金日志记录");
- return 0;
- }
- $this->info("共发现 {$logs->count()} 条日志记录");
- $this->newLine();
- $bar = $this->output->createProgressBar($logs->count());
- $bar->start();
- $prevHash = '';
- $errors = [];
- foreach ($logs as $log) {
- // 验证哈希值
- $currentHash = $log->generateHash();
- if ($currentHash !== $log->hash) {
- $errors[] = [
- 'id' => $log->id,
- 'type' => 'hash',
- 'message' => "日志ID {$log->id} 的哈希值不匹配"
- ];
- }
- // 验证链式哈希
- if ($log->prev_hash !== $prevHash) {
- $errors[] = [
- 'id' => $log->id,
- 'type' => 'chain',
- 'message' => "日志ID {$log->id} 的前序哈希不匹配"
- ];
- }
- $prevHash = $log->hash;
- $bar->advance();
- }
- $bar->finish();
- $this->newLine(2);
- if (empty($errors)) {
- $this->info("✓ 验证通过!所有日志记录完整性检查通过。");
- return 0;
- }
- $this->error("× 发现以下问题:");
- foreach ($errors as $error) {
- $this->line("- {$error['message']}");
- }
- return 1;
- } catch (\Exception $e) {
- $this->error("检查过程中发生错误:" . $e->getMessage());
- return 1;
- }
- }
- }
|