diff --git a/app/Http/Controllers/Admin/OtherController.php b/app/Http/Controllers/Admin/OtherController.php index 03e75e2..d173523 100755 --- a/app/Http/Controllers/Admin/OtherController.php +++ b/app/Http/Controllers/Admin/OtherController.php @@ -240,36 +240,9 @@ class OtherController extends CommonController // 审核通过人数去重 $list['course_signs_pass_unique'] = CourseSign::courseSignsTotalByUnique($start_date, $end_date, 1, $courses->pluck('id'), null); // 开课场次 - // 开课场次 - $calendar = Calendar::where(function ($query) use ($start_date, $end_date) { - $query->whereBetween('start_time', [$start_date, $end_date]) - ->orWhereBetween('end_time', [$start_date, $end_date]); - })->where(function ($query) use ($course_type_id) { - // 条件1:有 course_id 的数据,通过 course.type 匹配课程体系 - // 条件2:没有 course_id 的数据,直接用 course_type_id 字段匹配 - // 两个条件是或关系 - - if ($course_type_id) { - $course_type_id_array = is_array($course_type_id) ? $course_type_id : explode(',', $course_type_id); - - // 条件1:有 course_id 时,通过关联的 course.type 匹配 - $query->where(function ($q) use ($course_type_id_array) { - - $q->whereHas('course', function ($subQ) use ($course_type_id_array) { - $subQ->whereIn('type', $course_type_id_array); - }); - }); - - // 条件2:没有 course_id 时,直接用 course_type_id 字段匹配(或关系) - $query->orWhere(function ($q) use ($course_type_id_array) { - $q->whereIn('course_type_id', $course_type_id_array); - }); - } - })->get(); - - $list['course_total'] = (clone $calendar)->count(); + $list['course_total'] = Calendar::getCourseTotal($start_date, $end_date, $course_type_id); // 开课天数 - $list['course_day_total'] = (clone $calendar)->where('is_count_days', 1)->sum('days'); + $list['course_day_total'] = Calendar::getCourseDayTotal($start_date, $end_date, $course_type_id); $course_ids = $courses->pluck('id'); @@ -955,14 +928,8 @@ class OtherController extends CommonController case 'course_total': // 开课场次明细 - 与coursesHome算法一致 - $calendars = Calendar::whereBetween('date', [$start_date, $end_date]) - ->where(function ($query) use ($course_ids) { - $course_type_id = request('course_type_id'); - if ($course_type_id) { - $course_type_id = explode(',', $course_type_id); - $query->whereIn('course_type_id', $course_type_id); - } - })->with('course') + $calendars = Calendar::getCalendarsByDateRange($start_date, $end_date, $course_type_id) + ->with('course') ->get(); foreach ($calendars as $calendar) { @@ -988,14 +955,8 @@ class OtherController extends CommonController case 'course_day_total': // 开课天数明细 - 与coursesHome算法一致 - $calendars = Calendar::whereBetween('date', [$start_date, $end_date]) - ->where(function ($query) use ($course_ids) { - $course_type_id = request('course_type_id'); - if ($course_type_id) { - $course_type_id = explode(',', $course_type_id); - $query->whereIn('course_type_id', $course_type_id); - } - })->where('is_count_days', 1) + $calendars = Calendar::getCalendarsByDateRange($start_date, $end_date, $course_type_id) + ->where('is_count_days', 1) ->with('course') ->get(); diff --git a/app/Models/Calendar.php b/app/Models/Calendar.php index e9bd64f..b247246 100755 --- a/app/Models/Calendar.php +++ b/app/Models/Calendar.php @@ -18,7 +18,7 @@ class Calendar extends SoftDeletesModel public function getTypeTextAttribute() { - $array = [1=>'课程', 3=>'自定义事件', 4=>'资讯']; + $array = [1 => '课程', 3 => '自定义事件', 4 => '资讯']; return $array[$this->attributes['type']] ?? ''; } @@ -52,5 +52,66 @@ class Calendar extends SoftDeletesModel return $this->belongsTo(CourseType::class, 'course_type_id', 'id'); } + /** + * 根据日期范围和课程体系获取日历查询构建器 + * @param string|null $start_date 开始日期 + * @param string|null $end_date 结束日期 + * @param array|null $course_type_id 课程体系ID数组 + * @return \Illuminate\Database\Eloquent\Builder + */ + public static function getCalendarsByDateRange($start_date = null, $end_date = null, $course_type_id = null) + { + return self::where(function ($query) use ($start_date, $end_date) { + $query->whereBetween('start_time', [$start_date, $end_date]) + ->orWhereBetween('end_time', [$start_date, $end_date]); + })->where(function ($query) use ($course_type_id) { + // 条件1:有 course_id 的数据,通过 course.type 匹配课程体系 + // 条件2:没有 course_id 的数据,直接用 course_type_id 字段匹配 + // 两个条件是或关系 + + if ($course_type_id) { + $course_type_id_array = is_array($course_type_id) ? $course_type_id : explode(',', $course_type_id); + + // 条件1:有 course_id 时,通过关联的 course.type 匹配 + $query->where(function ($q) use ($course_type_id_array) { + $q->whereHas('course', function ($subQ) use ($course_type_id_array) { + $subQ->whereIn('type', $course_type_id_array); + }); + }); + + // 条件2:没有 course_id 时,直接用 course_type_id 字段匹配(或关系) + $query->orWhere(function ($q) use ($course_type_id_array) { + $q->whereIn('course_type_id', $course_type_id_array); + }); + } + }); + } + + /** + * 获取开课场次数量 + * @param string|null $start_date 开始日期 + * @param string|null $end_date 结束日期 + * @param array|null $course_type_id 课程体系ID数组 + * @return int + */ + public static function getCourseTotal($start_date = null, $end_date = null, $course_type_id = null) + { + return self::getCalendarsByDateRange($start_date, $end_date, $course_type_id)->count(); + } + + /** + * 获取开课天数总和 + * @param string|null $start_date 开始日期 + * @param string|null $end_date 结束日期 + * @param array|null $course_type_id 课程体系ID数组 + * @return int|float + */ + public static function getCourseDayTotal($start_date = null, $end_date = null, $course_type_id = null) + { + return self::getCalendarsByDateRange($start_date, $end_date, $course_type_id) + ->where('is_count_days', 1) + ->sum('days'); + } + }