master
cody 3 months ago
parent 389e4129ed
commit d14673c5e1

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