diff --git a/app/Caches/IndexFeaturedCourseList.php b/app/Caches/IndexFeaturedCourseList.php new file mode 100644 index 00000000..3f84919f --- /dev/null +++ b/app/Caches/IndexFeaturedCourseList.php @@ -0,0 +1,118 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_featured_course_list'; + } + + public function getContent($id = null) + { + $categoryLimit = 5; + + $courseLimit = 8; + + $categories = $this->findCategories($categoryLimit); + + if ($categories->count() == 0) { + return []; + } + + $result = []; + + foreach ($categories as $category) { + + $item = []; + + $item['category'] = [ + 'id' => $category->id, + 'name' => $category->name, + ]; + + $item['courses'] = []; + + $courses = $this->findCategoryCourses($category->id, $courseLimit); + + if ($courses->count() == 0) { + continue; + } + + $categoryCourses = []; + + foreach ($courses as $course) { + $categoryCourses[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + ]; + } + + $item['courses'] = $categoryCourses; + + $result[] = $item; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|CategoryModel[] + */ + protected function findCategories($limit = 5) + { + return CategoryModel::query() + ->where('type = :type:', ['type' => CategoryModel::TYPE_COURSE]) + ->andWhere('level = 1 AND published = 1') + ->orderBy('priority ASC') + ->limit($limit) + ->execute(); + } + + /** + * @param int $categoryId + * @param int $limit + * @return ResultsetInterface|Resultset|CourseModel[] + */ + protected function findCategoryCourses($categoryId, $limit = 8) + { + $categoryService = new CategoryService(); + + $categoryIds = $categoryService->getChildCategoryIds($categoryId); + + return CourseModel::query() + ->inWhere('category_id', $categoryIds) + ->andWhere('published = 1') + ->andWhere('featured = 1') + ->orderBy('id DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/IndexSimpleFeaturedCourseList.php b/app/Caches/IndexSimpleFeaturedCourseList.php new file mode 100644 index 00000000..c7201cc7 --- /dev/null +++ b/app/Caches/IndexSimpleFeaturedCourseList.php @@ -0,0 +1,70 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_simple_featured_course_list'; + } + + public function getContent($id = null) + { + $limit = 8; + + $courses = $this->findCourses($limit); + + if ($courses->count() == 0) { + return []; + } + + $result = []; + + foreach ($courses as $course) { + $result[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|CourseModel[] + */ + protected function findCourses($limit = 8) + { + return CourseModel::query() + ->where('published = 1') + ->andWhere('featured = 1') + ->orderBy('id DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Http/Admin/Services/Course.php b/app/Http/Admin/Services/Course.php index 10eaf27e..94525b17 100644 --- a/app/Http/Admin/Services/Course.php +++ b/app/Http/Admin/Services/Course.php @@ -172,6 +172,10 @@ class Course extends Service } } + if (isset($post['featured'])) { + $data['featured'] = $validator->checkFeatureStatus($post['featured']); + } + if (isset($post['published'])) { $data['published'] = $validator->checkPublishStatus($post['published']); if ($post['published'] == 1) { diff --git a/app/Http/Admin/Views/course/list.volt b/app/Http/Admin/Views/course/list.volt index b4ed45a7..4a2a84ce 100644 --- a/app/Http/Admin/Views/course/list.volt +++ b/app/Http/Admin/Views/course/list.volt @@ -59,6 +59,7 @@
市场:{{ '¥%0.2f'|format(item.market_price) }}
会员:{{ '¥%0.2f'|format(item.vip_price) }}
+