From b842649fd09ef8d484b1bca4f374dae5dcac5f50 Mon Sep 17 00:00:00 2001 From: koogua Date: Sat, 28 Aug 2021 21:57:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=87=8D=E7=BD=AE=E6=BC=94?= =?UTF-8?q?=E7=A4=BA=E7=9B=B4=E6=92=AD=E8=AF=BE=E7=A8=8B=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Tasks/RenewDemoLiveCourseTask.php | 72 +++++++++++-------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/app/Console/Tasks/RenewDemoLiveCourseTask.php b/app/Console/Tasks/RenewDemoLiveCourseTask.php index 1313359e..e5bb204e 100644 --- a/app/Console/Tasks/RenewDemoLiveCourseTask.php +++ b/app/Console/Tasks/RenewDemoLiveCourseTask.php @@ -3,6 +3,8 @@ namespace App\Console\Tasks; use App\Caches\CourseChapterList as CourseChapterListCache; +use App\Models\Chapter as ChapterModel; +use App\Models\Course as CourseModel; use App\Repos\Chapter as ChapterRepo; use App\Repos\Course as CourseRepo; use App\Services\CourseStat as CourseStatService; @@ -15,51 +17,65 @@ class RenewDemoLiveCourseTask extends Task */ public function mainAction() { - $day = date('d'); - - if ($day != 25) return; + if (date('d') != 25) return; $courseRepo = new CourseRepo(); - $course = $courseRepo->findById(1393); + $courses = $this->findLiveCourses(); - $chapters = $courseRepo->findLessons($course->id); + if ($courses->count() == 0) return; - $chapterRepo = new ChapterRepo(); + foreach ($courses as $course) { - foreach ($chapters as $chapter) { + $lessons = $courseRepo->findLessons($course->id); - $live = $chapterRepo->findChapterLive($chapter->id); - - if ($live->start_time > time()) { - continue; + foreach ($lessons as $lesson) { + $this->handleLesson($lesson); } - $startTime = strtotime('+1 month', $live->start_time); - $endTime = strtotime('+1 month', $live->end_time); + $statService = new CourseStatService(); - $live->start_time = $startTime; - $live->end_time = $endTime; + $statService->updateLiveAttrs($course->id); - $live->update(); + $cache = new CourseChapterListCache(); - $attrs = $chapter->attrs; - - $attrs['start_time'] = $startTime; - $attrs['end_time'] = $endTime; - - $chapter->attrs = $attrs; - - $chapter->update(); + $cache->rebuild($course->id); } + } - $statService = new CourseStatService(); + protected function handleLesson(ChapterModel $chapter) + { + $chapterRepo = new ChapterRepo(); - $statService->updateLiveAttrs($course->id); + $live = $chapterRepo->findChapterLive($chapter->id); - $cache = new CourseChapterListCache(); + if ($live->start_time > time()) return; - $cache->rebuild($course->id); + $startTime = strtotime('+1 month', $live->start_time); + $endTime = strtotime('+1 month', $live->end_time); + + $live->start_time = $startTime; + $live->end_time = $endTime; + + $live->update(); + + $attrs = $chapter->attrs; + + $attrs['start_time'] = $startTime; + $attrs['end_time'] = $endTime; + + $chapter->attrs = $attrs; + + $chapter->update(); + } + + protected function findLiveCourses($limit = 8) + { + return CourseModel::query() + ->where('model = :model:', ['model' => CourseModel::MODEL_LIVE]) + ->orderBy('id DESC') + ->limit($limit) + ->execute(); } }