diff --git a/app/Console/Commands/AutoSchoolmate.php b/app/Console/Commands/AutoSchoolmate.php index a2cfb21..1fc7cbd 100755 --- a/app/Console/Commands/AutoSchoolmate.php +++ b/app/Console/Commands/AutoSchoolmate.php @@ -45,18 +45,38 @@ class AutoSchoolmate extends Command */ public function handle() { - // 获取今天上课的课程 + // 获取所有已开始且需要自动加入校友库的课程 $today = date('Y-m-d'); - $courses = Course::where('start_date', $today)->where('auto_schoolmate', 1)->get(); + $courses = Course::where('auto_schoolmate', 1) + ->where(function ($query) use ($today) { + // 方式1: start_date 已填写且 <= 今天 + $query->where(function ($q) use ($today) { + $q->whereNotNull('start_date') + ->where('start_date', '<=', $today); + }) + // 方式2: 或者课程状态为进行中(即使 start_date 未及时填写) + ->orWhere('course_status', 10); + }) + ->get(); + + $totalUpdated = 0; foreach ($courses as $course) { // 获取报名通过的学员 $courseSigns = CourseSign::where('course_id', $course->id)->where('status', 1)->get(); - // 用户设置成校友 - User::whereIn('id', $courseSigns->pluck('user_id')) + if ($courseSigns->isEmpty()) { + continue; + } + + // 只更新还不是校友的学员,避免重复处理 + $updated = User::whereIn('id', $courseSigns->pluck('user_id')) + ->where('is_schoolmate', '!=', 1) //->where('is_black',0) ->update(['is_schoolmate' => 1]); + + $totalUpdated += $updated; } - return $this->info('更新完成'); + + return $this->info("更新完成,共处理 {$totalUpdated} 位学员"); } } diff --git a/app/Console/Commands/SyncTeacherDirection.php b/app/Console/Commands/SyncTeacherDirection.php new file mode 100644 index 0000000..0dfb31b --- /dev/null +++ b/app/Console/Commands/SyncTeacherDirection.php @@ -0,0 +1,124 @@ +info('开始同步老师课程方向...'); + + // 获取所有有课程内容的老师 + $teachers = Teacher::whereHas('courseContents', function ($query) { + $query->whereNotNull('direction') + ->where('direction', '!=', ''); + })->get(); + + $total = $teachers->count(); + if ($total == 0) { + return $this->info('没有需要同步的老师'); + } + + $this->info("共找到 {$total} 个老师需要同步"); + $bar = $this->output->createProgressBar($total); + $bar->start(); + + $updatedCount = 0; + foreach ($teachers as $teacher) { + // 获取该老师所有课程内容的direction + $courseContents = CourseContent::where('teacher_id', $teacher->id) + ->whereNotNull('direction') + ->where('direction', '!=', '') + ->get(); + + if ($courseContents->isEmpty()) { + $bar->advance(); + continue; + } + + // 收集所有direction值 + $allDirections = []; + foreach ($courseContents as $content) { + if (!empty($content->direction)) { + // 将逗号分隔的字符串拆分成数组 + $directions = explode(',', $content->direction); + foreach ($directions as $direction) { + $direction = trim($direction); + if (!empty($direction)) { + $allDirections[] = $direction; + } + } + } + } + + if (empty($allDirections)) { + $bar->advance(); + continue; + } + + // 去重并合并现有direction + $existingDirections = []; + if (!empty($teacher->direction)) { + $existingDirections = array_map('trim', explode(',', $teacher->direction)); + $existingDirections = array_filter($existingDirections); + } + + // 合并并去重 + $mergedDirections = array_unique(array_merge($existingDirections, $allDirections)); + $mergedDirections = array_filter($mergedDirections); // 移除空值 + + // 排序并重新组合 + sort($mergedDirections); + $newDirection = implode(',', $mergedDirections); + + // 如果direction有变化,则更新 + if ($teacher->direction !== $newDirection) { + $teacher->direction = $newDirection; + $teacher->save(); + $updatedCount++; + } + + $bar->advance(); + } + + $bar->finish(); + $this->newLine(); + $this->info("同步完成,共更新 {$updatedCount} 个老师的课程方向"); + + return 0; + } +} + diff --git a/app/Console/Commands/UpdateCompany.php b/app/Console/Commands/UpdateCompany.php index f225b49..00be7b9 100755 --- a/app/Console/Commands/UpdateCompany.php +++ b/app/Console/Commands/UpdateCompany.php @@ -51,14 +51,15 @@ class UpdateCompany extends Command */ public function compnay($user_id = null) { - // 更新 + // 扫描待更新的用户:company_id = -1(待更新)或 company_id = null(兼容旧数据) $users = User::whereNotNull('company_name') ->where(function ($query) use ($user_id) { if ($user_id) { $query->where('id', $user_id); } - })->whereNull('company_id') - ->orderBy('id', 'desc') + })->where(function ($query) { + $query->where('company_id', -1)->orWhereNull('company_id'); + })->orderBy('id', 'desc') ->get(); $total = $users->count(); diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index f1b43ea..bf141d1 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -29,7 +29,11 @@ class Kernel extends ConsoleKernel // 更新学员编号 $schedule->command('update_user_no')->dailyAt('00:05'); // 更新课程校友资格 - $schedule->command('auto_schoolmate')->dailyAt('23:50'); + $schedule->command('auto_schoolmate')->everyThirtyMinutes(); + // 更新公司信息 + $schedule->command('update_company')->everyTenMinutes(); + // 同步老师课程方向 + $schedule->command('sync:teacher_direction')->hourly(); } /** diff --git a/app/Http/Controllers/Admin/TeacherController.php b/app/Http/Controllers/Admin/TeacherController.php index beb6333..6ca5aff 100755 --- a/app/Http/Controllers/Admin/TeacherController.php +++ b/app/Http/Controllers/Admin/TeacherController.php @@ -38,7 +38,7 @@ class TeacherController extends BaseController * @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"), * @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"), * @OA\Parameter(name="theme", in="query", @OA\Schema(type="string"), required=false, description="主题"), - * @OA\Parameter(name="direction", in="query", @OA\Schema(type="string"), required=false, description="方向"), + * @OA\Parameter(name="direction", in="query", @OA\Schema(type="string"), required=false, description="课程方向多个英文逗号分隔"), * @OA\Parameter(name="keyword", in="query", @OA\Schema(type="string"), required=true, description="搜索关键词"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), * @OA\Response( @@ -112,20 +112,28 @@ class TeacherController extends BaseController })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc'); // 应用与列表一致的筛选 if (isset($all['theme']) || isset($all['direction'])) { - $list = $list->whereHas('courseContents', function ($query) use ($all) { + $list = $list->where(function ($query) use ($all) { + // 筛选 courseContents 的 theme if (isset($all['theme'])) { - $query->where('theme', $all['theme']); + $query->whereHas('courseContents', function ($q) use ($all) { + $q->where('theme', $all['theme']); + }); } - if (isset($all['direction'])) { - $query->where('direction', $all['direction']); + // 只从 teachers 表的 direction 字段筛选 + if (isset($all['direction']) && !empty($all['direction'])) { + $directions = explode(',', $all['direction']); + $query->where(function ($q) use ($directions) { + foreach ($directions as $direction) { + $q->orWhereRaw('FIND_IN_SET(?, direction)', [trim($direction)]); + } + }); } }); } if (isset($all['keyword'])) { $list = $list->where(function ($query) use ($all) { - $query->whereHas('courseContents', function ($query) use ($all) { - $query->where('direction', 'like', '%' . $all['keyword'] . '%'); - })->orWhere('name', 'like', '%' . $all['keyword'] . '%'); + $query->where('name', 'like', '%' . $all['keyword'] . '%') + ->orWhere('direction', 'like', '%' . $all['keyword'] . '%'); }); } if (isset($all['is_export']) && !empty($all['is_export'])) { @@ -142,9 +150,10 @@ class TeacherController extends BaseController 'introduce' => $teacher->introduce ?? '', 'sex' => $teacher->sex ?? '', 'mobile' => $teacher->mobile ?? '', + 'teacher_direction' => $teacher->direction ?? '', 'course_name' => optional($content->course)->name ?? '', 'theme' => $content->theme ?? '', - 'direction' => $content->direction ?? '', + 'course_direction' => $content->direction ?? '', ]; } } else { @@ -154,9 +163,10 @@ class TeacherController extends BaseController 'introduce' => $teacher->introduce ?? '', 'sex' => $teacher->sex ?? '', 'mobile' => $teacher->mobile ?? '', + 'teacher_direction' => $teacher->direction ?? '', 'course_name' => '', 'theme' => '', - 'direction' => '', + 'course_direction' => '', ]; } } @@ -166,9 +176,10 @@ class TeacherController extends BaseController 'introduce' => '老师简介', 'sex' => '性别', 'mobile' => '联系方式', + 'teacher_direction' => '老师课程方向', 'course_name' => '课程名称', 'theme' => '课程主题', - 'direction' => '课程方向', + 'course_direction' => '课程内容方向', ]; $fileName = ($all['file_name'] ?? '老师课程_') . date('YmdHis') . '.xlsx'; return Excel::download(new CommonExport($rows, $exportFields), $fileName); @@ -298,6 +309,7 @@ class TeacherController extends BaseController 'remark' => $item['remark'] ?? '', 'introduce' => $item['introduce'] ?? '', 'mobile' => $item['mobile'] ?? '', + 'direction' => $item['direction'] ?? '', ]; $this->model->updateOrCreate($where, $data); } diff --git a/app/Http/Controllers/Mobile/UserController.php b/app/Http/Controllers/Mobile/UserController.php index 54f68c4..9250802 100755 --- a/app/Http/Controllers/Mobile/UserController.php +++ b/app/Http/Controllers/Mobile/UserController.php @@ -9,7 +9,6 @@ use App\Helpers\ResponseCode; use App\Helpers\StarterResponseCode; use App\Jobs\SendAppointCar; use App\Jobs\SendCourseCar; -use App\Jobs\UpdateCompanyJob; use App\Models\Appointment; use App\Models\Config; use App\Models\CourseContentCheck; @@ -186,8 +185,9 @@ class UserController extends CommonController $model->save(); // 如果有公司信息,就更新一下公司 if (isset($all['company_name']) && !empty($all['company_name']) && $model->company_name != $all['company_name']) { - // 调用Job异步更新公司信息 - dispatch(new UpdateCompanyJob($model->id)); + // 设置待更新标记,由定时任务处理 + $model->company_id = -1; + $model->save(); } // 判断下,如果用户新加入车牌号,并且有未开始或者进行中的预约,则直接预约车牌号 $appointmentModel = Appointment::where('user_id', $this->getUserId()) diff --git a/app/Jobs/UpdateCompanyJob.php b/app/Jobs/UpdateCompanyJob.php deleted file mode 100644 index 32efa41..0000000 --- a/app/Jobs/UpdateCompanyJob.php +++ /dev/null @@ -1,52 +0,0 @@ -userId = $userId; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - $user = User::find($this->userId); - - if (!$user) { - return; - } - - // 调用模型方法更新公司信息 - Company::updateCompanyFromUser($user); - } -} - diff --git a/app/Models/Company.php b/app/Models/Company.php index 745ecac..c45044b 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -173,8 +173,9 @@ class Company extends SoftDeletesModel return ['success' => false, 'message' => '用户或公司名称为空', 'company' => null]; } - // 如果已经有公司关联,跳过 - if ($user->company_id) { + // 如果已经有有效的公司关联(company_id > 0),跳过 + // 允许处理 company_id = -1(待更新)或 null(初始状态)的情况 + if ($user->company_id && $user->company_id > 0) { return ['success' => false, 'message' => '用户已有公司关联', 'company' => null]; }