lion 7 months ago
commit 302d160c1c

@ -68,8 +68,12 @@ class AutoSchoolmate extends Command
} }
// 只更新还不是校友的学员,避免重复处理 // 只更新还不是校友的学员,避免重复处理
// 注意is_schoolmate != 1 不会匹配 NULL 值,需要显式包含 NULL
$updated = User::whereIn('id', $courseSigns->pluck('user_id')) $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) //->where('is_black',0)
->update(['is_schoolmate' => 1]); ->update(['is_schoolmate' => 1]);

@ -57,42 +57,62 @@ 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);
// 更新用户编号 // 生成学号,如果已存在则顺位+1继续尝试
$user->no = $no; $baseNo = $course->student_prefix . str_pad($i, 3, '0', STR_PAD_LEFT);
$user->save(); $no = $baseNo;
$this->info('学号: ' . $no); $attempt = 0;
$maxAttempts = 1000; // 防止无限循环
// 检查学号是否已存在,如果存在则顺位+1继续尝试
while (User::where('no', $no)->where('id', '!=', $user->id)->exists() && $attempt < $maxAttempts) {
$i++; $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 . ' 个学号');
} }
/** /**

@ -72,14 +72,19 @@ class CompanyController extends BaseController
$directions = explode(',', $direction); $directions = explode(',', $direction);
foreach ($directions as $dir) { foreach ($directions as $dir) {
$dir = trim($dir); $dir = trim($dir);
if (!empty($dir)) { // 去除空数据空字符串、null、只包含空白字符的字符串、类似"[]"的数组字符串
if (!empty($dir) && $dir !== '' && $dir !== null && $dir !== '[]') {
$allDirections[] = $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); sort($directions);

Loading…
Cancel
Save