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.
104 lines
3.3 KiB
104 lines
3.3 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
class CourseAppointmentTotal extends SoftDeletesModel
|
|
{
|
|
protected $table = 'course_appointment_totals';
|
|
|
|
public function user()
|
|
{
|
|
return $this->hasOne(User::class, 'id', 'user_id');
|
|
}
|
|
|
|
public function course()
|
|
{
|
|
return $this->hasOne(Course::class, 'id', 'course_id');
|
|
}
|
|
|
|
/**
|
|
* 添加预约次数,通过课程
|
|
*/
|
|
public static function addByCourse($courseId)
|
|
{
|
|
// 判断课程是否已生成预约次数
|
|
// $has = self::where('course_id', $courseId)->first();
|
|
// if ($has) return true;
|
|
// 没有正常则生成预约次数
|
|
$course = Course::find($courseId);
|
|
$courseType = CourseType::find($course->type);
|
|
$start_date = $course->start_date;
|
|
if (empty($start_date)) return false;
|
|
// 获取一年后的日期
|
|
$oneYearLaterTimestamp = strtotime($start_date) + (365 * 24 * 60 * 60);
|
|
// 获取所有合格学员
|
|
if ($course->is_fee == 1) {
|
|
$courseSigns = CourseSign::where('fee_status', 1)->where('course_id', $courseId)->get();
|
|
} else {
|
|
$courseSigns = CourseSign::where('status', 1)->where('course_id', $courseId)->get();
|
|
}
|
|
foreach ($courseSigns as $courseSign) {
|
|
$where = [
|
|
'user_id' => $courseSign->user_id,
|
|
'course_id' => $courseSign->course_id,
|
|
'course_sign_id' => $courseSign->id
|
|
];
|
|
$data = [
|
|
'user_id' => $courseSign->user_id,
|
|
'course_id' => $courseSign->course_id,
|
|
'course_sign_id' => $courseSign->id,
|
|
'total' => $courseType->year_total,
|
|
'start_time' => $start_date,
|
|
'end_time' => date('Y-m-d H:i:s', $oneYearLaterTimestamp)
|
|
];
|
|
self::firstOrCreate($where, $data);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 添加预约次数
|
|
*/
|
|
public static function add($userId, $course_sign_id)
|
|
{
|
|
$has = self::where('user_id', $userId)->where('course_sign_id', $course_sign_id)->first();
|
|
if ($has) {
|
|
return true;
|
|
}
|
|
// 加预约次数
|
|
$courseSign = CourseSign::find($course_sign_id);
|
|
$course = Course::find($courseSign->course_id);
|
|
$courseType = CourseType::whereHas('courses', function ($query) use ($courseSign) {
|
|
$query->where('id', $courseSign->course_id);
|
|
})->first();
|
|
$start_date = $course->start_date;
|
|
if (empty($start_date)) $start_date = date('Y-m-d H:i:s');
|
|
// 获取一年后的日期
|
|
$oneYearLaterTimestamp = strtotime($start_date) + (365 * 24 * 60 * 60);
|
|
self::create([
|
|
'user_id' => $userId,
|
|
'course_id' => $courseSign->course_id,
|
|
'course_sign_id' => $course_sign_id,
|
|
'total' => $courseType->year_total,
|
|
'start_time' => $start_date,
|
|
'end_time' => date('Y-m-d H:i:s', $oneYearLaterTimestamp)
|
|
]);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 减少预约次数
|
|
*/
|
|
public static function reduce($userId, $course_sign_id)
|
|
{
|
|
$has = self::where('user_id', $userId)->where('course_sign_id', $course_sign_id)->first();
|
|
if (empty($has)) {
|
|
return true;
|
|
}
|
|
$has->delete();
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|