diff --git a/app/Console/Commands/AutoSchoolmate.php b/app/Console/Commands/AutoSchoolmate.php index 1fc7cbd..29ec123 100755 --- a/app/Console/Commands/AutoSchoolmate.php +++ b/app/Console/Commands/AutoSchoolmate.php @@ -68,8 +68,12 @@ class AutoSchoolmate extends Command } // 只更新还不是校友的学员,避免重复处理 + // 注意:is_schoolmate != 1 不会匹配 NULL 值,需要显式包含 NULL $updated = User::whereIn('id', $courseSigns->pluck('user_id')) - ->where('is_schoolmate', '!=', 1) + ->where(function ($query) { + $query->where('is_schoolmate', '!=', 1) + ->orWhereNull('is_schoolmate'); + }) //->where('is_black',0) ->update(['is_schoolmate' => 1]); diff --git a/app/Console/Commands/UpdateUserNo.php b/app/Console/Commands/UpdateUserNo.php index 4984525..10f24ad 100755 --- a/app/Console/Commands/UpdateUserNo.php +++ b/app/Console/Commands/UpdateUserNo.php @@ -57,42 +57,62 @@ 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); + foreach ($course->courseSigns as $sign) { + $user = User::find($sign->user_id); + // 只要用户没有学号(包括 null 和空字符串),都需要更新 + if ($user->no) { + continue; + } + + // 生成学号,如果已存在则顺位+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(); + + 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++; } } - $this->info('学号更新完成'); + $this->info('学号更新完成,共更新 ' . $totalUpdated . ' 个学号'); } /** diff --git a/app/Http/Controllers/Admin/CompanyController.php b/app/Http/Controllers/Admin/CompanyController.php index b8510e4..ce6e261 100644 --- a/app/Http/Controllers/Admin/CompanyController.php +++ b/app/Http/Controllers/Admin/CompanyController.php @@ -72,14 +72,19 @@ class CompanyController extends BaseController $directions = explode(',', $direction); foreach ($directions as $dir) { $dir = trim($dir); - if (!empty($dir)) { + // 去除空数据:空字符串、null、只包含空白字符的字符串、类似"[]"的数组字符串 + if (!empty($dir) && $dir !== '' && $dir !== null && $dir !== '[]') { $allDirections[] = $dir; } } } } - // 去重并重新索引 - $directions = array_values(array_unique($allDirections)); + // 去重并重新索引,再次过滤空数据 + $directions = array_values(array_unique(array_filter($allDirections, function ($item) { + $trimmed = trim($item); + // 过滤空字符串、null、只包含空白字符、类似"[]"的数组字符串 + return !empty($trimmed) && $trimmed !== '' && $trimmed !== '[]'; + }))); // 排序 sort($directions);