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.

118 lines
3.9 KiB

5 months ago
<?php
namespace App\Models;
use Illuminate\Support\Facades\Cache;
class Calendar extends SoftDeletesModel
{
2 weeks ago
protected $appends = ['is_publish_text', 'type_text', 'is_count_days_text', 'is_count_people_text'];
1 month ago
public function getIsPublishTextAttribute()
{
return $this->attributes['is_publish'] == 1 ? '是' : '否';
}
public function getTypeTextAttribute()
{
1 week ago
$array = [1 => '课程', 3 => '自定义事件', 4 => '资讯'];
1 month ago
return $array[$this->attributes['type']] ?? '';
}
2 weeks ago
public function getIsCountDaysTextAttribute()
{
return ($this->attributes['is_count_days'] ?? 1) == 1 ? '是' : '否';
}
public function getIsCountPeopleTextAttribute()
{
return ($this->attributes['is_count_people'] ?? 1) == 1 ? '是' : '否';
}
5 months ago
public function course()
{
return $this->hasOne(Course::class, 'id', 'course_id');
}
5 months ago
public function courseContent()
{
5 months ago
return $this->hasMany(CourseContent::class, 'id', 'course_content_id');
5 months ago
}
2 weeks ago
public function historyCourses()
{
2 weeks ago
return $this->hasMany(HistoryCourse::class, 'calendar_id', 'id');
2 weeks ago
}
2 weeks ago
public function courseType()
{
return $this->belongsTo(CourseType::class, 'course_type_id', 'id');
}
1 week 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);
});
}
});
}
/**
* 获取开课场次数量
* @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');
}
5 months ago
}