master
cody 3 months ago
parent 389e4129ed
commit d14673c5e1

@ -57,42 +57,39 @@ class UpdateUserNo extends Command
protected function updateUserNo() protected function updateUserNo()
{ {
$this->info('开始更新学号...'); $this->info('开始更新学号...');
// 已经开始的课程日期(所有历史数据处理) $today = date('Y-m-d');
// $dateList = Course::whereNotNull('start_date')
// ->where('start_date', '<=', date('Y-m-d')) // 获取所有已开始且有学号前缀的课程
// ->orderBy('start_date') $courses = Course::with([
// ->groupBy('start_date') 'courseSigns' => function ($query) {
// ->pluck('start_date') $query->where('status', 1);
// ->toArray(); }
// 当日数据处理(日常定时任务) ])->whereNotNull('start_date')
$dateList = [date('Y-m-d')]; ->where('start_date', '<=', $today)
foreach ($dateList as $date) { ->whereNotNull('student_prefix')
$courses = Course::with([ ->orderBy('start_date')
'courseSigns' => function ($query) { ->get();
$query->where('status', 1);
} $totalUpdated = 0;
])->where('start_date', $date) foreach ($courses as $course) {
->whereNotNull('student_prefix')
->orderBy('start_date')
->get();
$i = 1; $i = 1;
// 编号前缀 // 编号前缀
foreach ($courses as $course) { foreach ($course->courseSigns as $sign) {
foreach ($course->courseSigns as $sign) { $user = User::find($sign->user_id);
$user = User::find($sign->user_id); // 只要用户没有学号(包括 null 和空字符串),都需要更新
if ($user->no) { if ($user->no) {
continue; continue;
}
$no = $course->student_prefix . str_pad($i, 3, '0', STR_PAD_LEFT);
// 更新用户编号
$user->no = $no;
$user->save();
$this->info('学号: ' . $no);
$i++;
} }
$no = $course->student_prefix . str_pad($i, 3, '0', STR_PAD_LEFT);
// 更新用户编号
$user->no = $no;
$user->save();
$this->info('课程: ' . ($course->name ?? $course->id) . ', 学号: ' . $no . ', 用户: ' . ($user->name ?? $user->id));
$i++;
$totalUpdated++;
} }
} }
$this->info('学号更新完成'); $this->info('学号更新完成,共更新 ' . $totalUpdated . ' 个学号');
} }
/** /**

Loading…
Cancel
Save