From ed9022ddb444fec233ae4f3bfe57c44c2aebdf24 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Sun, 4 Jan 2026 15:02:59 +0800 Subject: [PATCH] update --- app/Console/Commands/UpdateUserNo.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/UpdateUserNo.php b/app/Console/Commands/UpdateUserNo.php index 1a14836..10f24ad 100755 --- a/app/Console/Commands/UpdateUserNo.php +++ b/app/Console/Commands/UpdateUserNo.php @@ -80,11 +80,34 @@ class UpdateUserNo extends Command if ($user->no) { continue; } - $no = $course->student_prefix . str_pad($i, 3, '0', STR_PAD_LEFT); + + // 生成学号,如果已存在则顺位+1继续尝试 + $baseNo = $course->student_prefix . str_pad($i, 3, '0', STR_PAD_LEFT); + $no = $baseNo; + $attempt = 0; + $maxAttempts = 1000; // 防止无限循环 + + // 检查学号是否已存在,如果存在则顺位+1继续尝试 + while (User::where('no', $no)->where('id', '!=', $user->id)->exists() && $attempt < $maxAttempts) { + $i++; + $no = $course->student_prefix . str_pad($i, 3, '0', STR_PAD_LEFT); + $attempt++; + } + + if ($attempt >= $maxAttempts) { + $this->warn('课程: ' . ($course->name ?? $course->id) . ', 用户: ' . ($user->name ?? $user->id) . ' 无法生成唯一学号,跳过'); + continue; + } + // 更新用户编号 $user->no = $no; $user->save(); - $this->info('课程: ' . ($course->name ?? $course->id) . ', 学号: ' . $no . ', 用户: ' . ($user->name ?? $user->id)); + + if ($no != $baseNo) { + $this->info('课程: ' . ($course->name ?? $course->id) . ', 原学号: ' . $baseNo . ' 已存在,使用: ' . $no . ', 用户: ' . ($user->name ?? $user->id)); + } else { + $this->info('课程: ' . ($course->name ?? $course->id) . ', 学号: ' . $no . ', 用户: ' . ($user->name ?? $user->id)); + } $i++; $totalUpdated++; }