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.

117 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Models;
class CourseType extends SoftDeletesModel
{
const START_DATE = '2000-01-01';
protected $appends = ['is_count_genban_text'];
/**
* 是否统计跟班学员数文字
*/
public function getIsCountGenbanTextAttribute()
{
return ($this->attributes['is_count_genban'] ?? 1) == 1 ? '是' : '否';
}
public function courses()
{
return $this->hasMany(Course::class, 'type', 'id');
}
/**
* 获取"其他"统计项is_chart=0 的课程类型合并统计)
* @param string $startDate 开始日期
* @param string $endDate 结束日期
* @return CourseType
*/
public static function getOtherStatistics($startDate, $endDate)
{
// 获取所有 is_chart=0 的课程类型
$otherCourseTypes = self::where('is_chart', 0)
->where('is_history', 0)
->get();
$otherCourseTypeIds = $otherCourseTypes->pluck('id')->toArray();
// 如果没有"其他"课程类型,返回空统计
if (empty($otherCourseTypeIds)) {
$otherCourseType = new self();
$otherCourseType->id = 0;
$otherCourseType->name = '其他';
$otherCourseType->history_course_periods_total = 0;
$otherCourseType->now_course_periods_total = 0;
$otherCourseType->history_course_signs_total = 0;
$otherCourseType->now_course_signs_total = 0;
$otherCourseType->course_periods_total = 0;
$otherCourseType->course_signs_total = 0;
return $otherCourseType;
}
// 历史课程数据(所有 is_chart=0 的课程类型)
$otherHistoryCourse = HistoryCourse::whereIn('type', $otherCourseTypeIds)
->where(function ($query) use ($startDate, $endDate) {
$query->whereBetween('start_time', [$startDate, $endDate])
->orWhereBetween('end_time', [$startDate, $endDate]);
})->get();
// 实际课程数据(所有 is_chart=0 的课程类型下的课程)
$otherCourses = Course::whereIn('type', $otherCourseTypeIds)->where('is_chart', 1)
->where(function ($query) use ($startDate, $endDate) {
$query->whereBetween('start_date', [$startDate, $endDate])
->orWhereBetween('end_date', [$startDate, $endDate]);
})->get();
$otherHistoryCoursePeriodsTotal = $otherHistoryCourse->count();
$otherNowCoursePeriodsTotal = $otherCourses->count();
$otherHistoryCourseSignsTotal = $otherHistoryCourse->sum('course_type_signs_pass_unique');
$otherNowCourseSignsTotal = CourseSign::courseSignsTotalByUnique($startDate, $endDate, 1, $otherCourses->pluck('id'), false, false);
// 创建"其他"统计项
$otherCourseType = new self();
$otherCourseType->id = 0;
$otherCourseType->name = '其他';
$otherCourseType->history_course_periods_total = $otherHistoryCoursePeriodsTotal;
$otherCourseType->now_course_periods_total = $otherNowCoursePeriodsTotal;
$otherCourseType->history_course_signs_total = $otherHistoryCourseSignsTotal;
$otherCourseType->now_course_signs_total = $otherNowCourseSignsTotal;
$otherCourseType->course_periods_total = $otherCourseType->now_course_periods_total + $otherCourseType->history_course_periods_total;
$otherCourseType->course_signs_total = $otherCourseType->history_course_signs_total + $otherCourseType->now_course_signs_total;
return $otherCourseType;
}
/**
* 获取"其他"课程类型的课程ID列表用于计算总去重人数
* @param string $startDate 开始日期
* @param string $endDate 结束日期
* @return \Illuminate\Support\Collection
*/
public static function getOtherCourseIds($startDate, $endDate)
{
// 获取所有 is_chart=0 的课程类型ID
$otherCourseTypeIds = self::where('is_chart', 0)
->where('is_history', 0)
->pluck('id')->toArray();
if (empty($otherCourseTypeIds)) {
return collect([]);
}
// 获取"其他"课程类型的课程数据
$otherCoursesAll = Course::whereIn('type', $otherCourseTypeIds)
->where('is_chart', 1)
->where(function ($query) use ($startDate, $endDate) {
$query->whereBetween('start_date', [$startDate, $endDate])
->orWhereBetween('end_date', [$startDate, $endDate]);
})->get();
return $otherCoursesAll->pluck('id');
}
}