diff --git a/app/Http/Controllers/Admin/CompanyController.php b/app/Http/Controllers/Admin/CompanyController.php index 1685aac..72aff74 100644 --- a/app/Http/Controllers/Admin/CompanyController.php +++ b/app/Http/Controllers/Admin/CompanyController.php @@ -137,43 +137,44 @@ class CompanyController extends BaseController })->with('courseSigns.course'); } ])->whereHas('users', function ($query) use ($all) { - if (isset($all['course_type_id']) && !empty($all['course_type_id'])) { - $query->whereHas('courses', function ($q) use ($all) { - $q->where('type', $all['course_type_id']); - }); - } - if (isset($all['course_name']) && !empty($all['course_name'])) { - $query->whereHas('courses', function ($q) use ($all) { - $q->where('name', 'like', '%' . $all['course_name'] . '%'); - }); - } - // 课程起止时间筛选 - if ( - (isset($all['course_start_date']) && !empty($all['course_start_date'])) || - (isset($all['course_end_date']) && !empty($all['course_end_date'])) - ) { - $query->whereHas('courses', function ($q) use ($all) { - $course_start_date = $all['course_start_date'] ?? null; - $course_end_date = $all['course_end_date'] ?? null; - if ($course_start_date && $course_end_date) { - // 课程开始时间或结束时间在指定时间范围内 - $q->where(function ($subQuery) use ($course_start_date, $course_end_date) { - $subQuery->whereBetween('start_date', [$course_start_date, $course_end_date]) - ->orWhereBetween('end_date', [$course_start_date, $course_end_date]); - }); - } elseif ($course_start_date) { - // 只指定开始日期,筛选课程结束时间 >= 开始日期 - $q->where('end_date', '>=', $course_start_date); - } elseif ($course_end_date) { - // 只指定结束日期,筛选课程开始时间 <= 结束日期 - $q->where('start_date', '<=', $course_end_date); - } - }); - } - // 课程是否参与统计:0否 1是(按公司下用户关联的课程的 is_chart 筛选) - if (isset($all['is_chart']) && $all['is_chart'] !== '') { - $query->whereHas('courses', function ($q) use ($all) { - $q->where('is_chart', $all['is_chart']); + // 课程筛选:与 courses-home / getStudentList 口径一致,仅 status=1 的报名,且由同一条 courseSign 满足 type/name/日期/is_chart + $hasCourseFilter = (isset($all['course_type_id']) && $all['course_type_id'] !== '') || + (isset($all['course_name']) && $all['course_name'] !== '') || + (isset($all['course_start_date']) && $all['course_start_date'] !== '') || + (isset($all['course_end_date']) && $all['course_end_date'] !== '') || + (isset($all['is_chart']) && $all['is_chart'] !== ''); + + if ($hasCourseFilter) { + $query->whereHas('courseSigns', function ($q) use ($all) { + $q->where('status', 1); + $q->whereHas('course', function ($c) use ($all) { + if (isset($all['course_type_id']) && $all['course_type_id'] !== '') { + $c->where('type', $all['course_type_id']); + } + if (isset($all['course_name']) && $all['course_name'] !== '') { + $c->where('name', 'like', '%' . $all['course_name'] . '%'); + } + if ( + (isset($all['course_start_date']) && $all['course_start_date'] !== '') || + (isset($all['course_end_date']) && $all['course_end_date'] !== '') + ) { + $course_start_date = $all['course_start_date'] ?? null; + $course_end_date = $all['course_end_date'] ?? null; + if ($course_start_date && $course_end_date) { + $c->where(function ($sub) use ($course_start_date, $course_end_date) { + $sub->whereBetween('start_date', [$course_start_date, $course_end_date]) + ->orWhereBetween('end_date', [$course_start_date, $course_end_date]); + }); + } elseif ($course_start_date) { + $c->where('end_date', '>=', $course_start_date); + } elseif ($course_end_date) { + $c->where('start_date', '<=', $course_end_date); + } + } + if (isset($all['is_chart']) && $all['is_chart'] !== '') { + $c->where('is_chart', $all['is_chart']); + } + }); }); } if (isset($all['user_name']) && !empty($all['user_name'])) {