master
cody 3 months ago
parent 389e4129ed
commit d14673c5e1

@ -57,29 +57,26 @@ 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')
// ->groupBy('start_date')
// ->pluck('start_date')
// ->toArray();
// 当日数据处理(日常定时任务)
$dateList = [date('Y-m-d')];
foreach ($dateList as $date) {
$courses = Course::with([ $courses = Course::with([
'courseSigns' => function ($query) { 'courseSigns' => function ($query) {
$query->where('status', 1); $query->where('status', 1);
} }
])->where('start_date', $date) ])->whereNotNull('start_date')
->where('start_date', '<=', $today)
->whereNotNull('student_prefix') ->whereNotNull('student_prefix')
->orderBy('start_date') ->orderBy('start_date')
->get(); ->get();
$totalUpdated = 0;
foreach ($courses as $course) {
$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;
} }
@ -87,12 +84,12 @@ class UpdateUserNo extends Command
// 更新用户编号 // 更新用户编号
$user->no = $no; $user->no = $no;
$user->save(); $user->save();
$this->info('学号: ' . $no); $this->info('课程: ' . ($course->name ?? $course->id) . ', 学号: ' . $no . ', 用户: ' . ($user->name ?? $user->id));
$i++; $i++;
$totalUpdated++;
} }
} }
} $this->info('学号更新完成,共更新 ' . $totalUpdated . ' 个学号');
$this->info('学号更新完成');
} }
/** /**

Loading…
Cancel
Save