master
cody 3 months ago
parent d14673c5e1
commit ed9022ddb4

@ -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++;
}

Loading…
Cancel
Save