You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.6 KiB

9 months ago
<?php
namespace App\Models;
use Illuminate\Support\Facades\Cache;
class Calendar extends SoftDeletesModel
{
4 months ago
protected $appends = ['is_publish_text', 'type_text', 'is_count_days_text', 'is_count_people_text'];
5 months ago
public function getIsPublishTextAttribute()
{
return $this->attributes['is_publish'] == 1 ? '是' : '否';
}
public function getTypeTextAttribute()
{
4 months ago
$array = [1 => '课程', 3 => '自定义事件', 4 => '资讯'];
5 months ago
return $array[$this->attributes['type']] ?? '';
}
4 months ago
public function getIsCountDaysTextAttribute()
{
return ($this->attributes['is_count_days'] ?? 1) == 1 ? '是' : '否';
}
public function getIsCountPeopleTextAttribute()
{
return ($this->attributes['is_count_people'] ?? 1) == 1 ? '是' : '否';
}
9 months ago
public function course()
{
return $this->hasOne(Course::class, 'id', 'course_id');
}
9 months ago
public function courseContent()
{
9 months ago
return $this->hasMany(CourseContent::class, 'id', 'course_content_id');
9 months ago
}
4 months ago
public function historyCourses()
{
4 months ago
return $this->hasMany(HistoryCourse::class, 'calendar_id', 'id');
4 months ago
}
4 months ago
public function courseType()
{
return $this->belongsTo(CourseType::class, 'course_type_id', 'id');
}
4 months ago
/**
* 根据日期范围和课程体系获取日历查询构建器
* @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);
});
}
});
}
3 months ago
/**
* 获取开课统计(场次数量或天数总和)
* @param string|null $start_date 开始日期
* @param string|null $end_date 结束日期
* @param array|null $course_type_id 课程体系ID数组
* @param string $type 统计类型:'count' 统计次数,'sum' 统计天数总和
* @return int|float
*/
public static function getCourseStatistics($start_date = null, $end_date = null, $course_type_id = null, $type = 'count')
{
$query = self::getCalendarsByDateRange($start_date, $end_date, $course_type_id)
->where('is_count_days', 1);
if ($type === 'sum') {
return $query->sum('days');
}
return $query->count();
}
4 months ago
/**
* 获取开课场次数量
* @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)
{
3 months ago
return self::getCourseStatistics($start_date, $end_date, $course_type_id, 'count');
4 months ago
}
/**
* 获取开课天数总和
* @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)
{
3 months ago
return self::getCourseStatistics($start_date, $end_date, $course_type_id, 'sum');
4 months ago
}
9 months ago
}