whereNotNull('start_date') ->where('start_date', '<=', $today) ->get(); if ($courses->isEmpty()) { return $this->info('没有需要处理的课程'); } $this->info("找到 {$courses->count()} 个需要处理的课程"); $totalUpdated = 0; $totalSkipped = 0; $processedUserIds = []; // 记录已处理的用户ID,避免重复处理 foreach ($courses as $course) { // 获取报名通过的学员,并排除已经是校友的用户 $courseSigns = CourseSign::where('course_id', $course->id) ->where('status', 1) ->whereHas('user', function ($query) { // 排除已经是校友的用户(is_schoolmate = 1) $query->whereRaw('COALESCE(is_schoolmate, 0) != 1'); })->get(); if ($courseSigns->isEmpty()) { continue; } // 获取需要更新的用户ID,并排除已经在本脚本中处理过的用户 $userIds = $courseSigns->pluck('user_id') ->unique() ->filter(function ($userId) use (&$processedUserIds) { // 如果已经处理过,跳过 if (in_array($userId, $processedUserIds)) { return false; } $processedUserIds[] = $userId; return true; }) ->values() ->toArray(); if (empty($userIds)) { continue; } // 批量更新:只更新还不是校友的学员;使用课程开课时间作为 schoolmate_time $updated = User::batchUpdateToSchoolmate($userIds, $course->start_date); $totalUpdated += $updated; $skipped = count($userIds) - $updated; $totalSkipped += $skipped; if ($updated > 0) { $this->info("课程【{$course->name}】: 更新 {$updated} 位学员,跳过 {$skipped} 位已处理学员"); } } return $this->info("更新完成,共处理 {$totalUpdated} 位学员,跳过 {$totalSkipped} 位已处理学员"); } }