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.

185 lines
4.8 KiB

<?php
namespace App\Models;
use EasyWeChat\Factory;
use Illuminate\Filesystem\Filesystem;
class Course extends SoftDeletesModel
{
protected $appends = ['date_status', 'publicize', 'sign_date_status', 'qrcode', 'teacher_detail'];
protected $casts = ['publicize_ids' => 'json'];
public function getQrcodeAttribute($value)
{
return $this->getCourseQrcode($this->attributes['id']);
}
public function getTeacherDetailAttribute($value)
{
$teacherIds = explode(',', $this->teacher_id);
return Teacher::whereIn('id', $teacherIds)->get();
}
public function getPublicizeAttribute($value)
{
if (empty($this->publicize_ids)) return [];
return Upload::whereIn('id', $this->publicize_ids)->get();
}
public function getDateStatusAttribute()
{
$text = '课程未开始';
if ($this->course_status == 10) {
$text = '课程进行中';
}
if ($this->course_status == 40) {
$text = '课程已结束';
}
return $text;
}
public function getSignDateStatusAttribute()
{
$text = '未开始';
if ($this->sign_status == 10) {
$text = '进行中';
}
if ($this->sign_status == 40) {
$text = '已结束';
}
return $text;
}
public function typeDetail()
{
return $this->hasOne(CourseType::class, 'id', 'type');
}
public function courseForms()
{
return $this->hasMany(CourseForm::class, 'course_id', 'id');
}
public function teacher()
{
return $this->hasOne(Teacher::class, 'id', 'teacher_id');
}
public function courseSettings()
{
return $this->hasMany(CourseSetting::class, 'course_id', 'id');
}
public function coursePeriods()
{
return $this->hasMany(CoursePeriod::class, 'course_id', 'id');
}
public function courseSigns()
{
return $this->hasMany(CourseSign::class, 'course_id', 'id');
}
public function image()
{
return $this->hasOne(Upload::class, 'id', 'image_id');
}
public function qunImage()
{
return $this->hasOne(Upload::class, 'id', 'qun_image_id');
}
public function courseContents()
{
return $this->hasMany(CourseContent::class, 'course_id', 'id');
}
/**
* 更新课程报名状态
*/
public static function updateSignStatus($courseId)
{
// 进行中-未开始-已结束
$now = date('Y-m-d');
$course = Course::find($courseId);
// 默认未开始
$sign_status = 30;
if ($course->sign_start_date && $now < $course->sign_start_date) {
// 未开始
$sign_status = 20;
}
if ($course->sign_start_date && $now >= $course->sign_start_date) {
// 进行中
$sign_status = 10;
}
if ($course->sign_end_date && $now > $course->sign_end_date) {
// 已结束
$sign_status = 40;
}
$course->sign_status = $sign_status;
$course->save();
return $course;
}
/**
* 更新课程状态
*/
public static function updateStatus($courseId)
{
// 进行中-未开始-已结束
$now = date('Y-m-d');
$course = Course::find($courseId);
// 默认待定
$course_status = 30;
if ($course->start_date && $now < $course->start_date) {
// 未开始
$course_status = 20;
}
if ($course->start_date && $now >= $course->start_date) {
// 进行中
$course_status = 10;
}
if ($course->end_date && $now > $course->end_date) {
// 已结束
$course_status = 40;
}
$course->course_status = $course_status;
$course->save();
return $course;
}
/**
* 获取课程详情小程序码
*/
public function getCourseQrcode($courseId)
{
$course = Course::find($courseId);
$path = config('filesystems.disks.public.root') . '/course_qrcode/' . $course->id . '.png';
$url = config('filesystems.disks.public.url') . '/course_qrcode/' . $course->id . '.png';
$fileSys = new Filesystem();
if ($fileSys->exists($path)) {
return $url;
}
$config = [
'app_id' => \config('app.applet_appid'),
'secret' => \config('app.applet_secret')
];
$app = Factory::miniProgram($config);
$tmp = $app->app_code->get('packages/course/detail?' . 'id=' . $courseId, [
'env_version' => "release" // 正式版
// 'env_version' => "trial" // 体验版
]);
$dir = dirname($path);
$fileSys->ensureDirectoryExists($dir, 0755, true);
$fileSys->put($path, $tmp);
return $url;
}
}