From cb4f91bee571adc758747c51b2026b935f51d64f Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sat, 13 Apr 2024 03:17:50 +0800 Subject: [PATCH] fix exam get begin and end --- app/Models/Exam.php | 8 ++++---- app/Repositories/ExamRepository.php | 23 ++++------------------- include/constants.php | 2 +- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/app/Models/Exam.php b/app/Models/Exam.php index 07fb30d4..c58a0a42 100644 --- a/app/Models/Exam.php +++ b/app/Models/Exam.php @@ -164,14 +164,14 @@ class Exam extends NexusModel return implode("
", $arr); } - public function begin(): Attribute + protected function beginForUser(): Attribute { return new Attribute( get: fn ($value) => $value ? Carbon::parse($value) : Carbon::now() ); } - public function end(): Attribute + protected function endForUser(): Attribute { return new Attribute( get: function ($value, $attributes) { @@ -180,8 +180,8 @@ class Exam extends NexusModel } if (!empty($attributes['duration'])) { /** @var Carbon $begin */ - $begin = $this->begin; - return $begin->addDays($attributes['duration']); + $begin = $this->begin_for_user; + return $begin->clone()->addDays($attributes['duration']); } throw new \RuntimeException("No specific end or duration"); } diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index de2b5bdb..517a1eff 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -349,27 +349,12 @@ class ExamRepository extends BaseRepository 'exam_id' => $exam->id, ]; if (empty($begin)) { - if (!empty($exam->begin)) { - $begin = $exam->begin; - $logPrefix .= ", begin from exam->begin: $begin"; - } else { - $begin = now(); - $logPrefix .= ", begin from now: $begin"; - } + $begin = $exam->begin_for_user; } else { $begin = Carbon::parse($begin); } if (empty($end)) { - if (!empty($exam->end)) { - $end = $exam->end; - $logPrefix .= ", end from exam->end: $end"; - } elseif ($exam->duration > 0) { - $duration = $exam->duration; - $end = $begin->clone()->addDays($duration)->toDateTimeString(); - $logPrefix .= ", end from begin + duration($duration): $end"; - } else { - throw new \RuntimeException("No specific end or duration"); - } + $end = $exam->end_for_user; } else { $end = Carbon::parse($end); } @@ -1018,8 +1003,8 @@ class ExamRepository extends BaseRepository $size = 1000; $minId = 0; $result = 0; - $begin = $exam->begin; - $end = $exam->end; + $begin = $exam->begin_for_user; + $end = $exam->end_for_user; while (true) { $logPrefix = sprintf('[%s], exam: %s, size: %s', __FUNCTION__, $exam->id , $size); $users = (clone $baseQuery)->where("$userTable.id", ">", $minId)->limit($size)->get(); diff --git a/include/constants.php b/include/constants.php index be847c06..6dddd22c 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@