# 课程章节信息
from django.shortcuts import render
from django.views import View

from apps.courses.models import Course, CourseResource
from apps.operation.models import UserCourse
from apps.utils.mixin_utils import LoginRequiredMixin


class CourseInfoView(LoginRequiredMixin, View):
    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        course.students += 1
        course.save()

        # 查询用户是否已经关联该课程
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_user_courses = UserCourse.objects.filter(user_id__in=user_ids)

        # 取出所有课程ID
        course_ids = [user_course.course.id for user_course in all_user_courses]

        # 获取学过该用户学过的其他所有课程
        relate_courses = Course.objects.filter(id__in=course_ids).order_by("-click_nums")[:5]

        all_resources = CourseResource.objects.filter(course=course)

        return render(request, 'course-video.html', {
            'courses': course,
            'course_resources': all_resources,
            'relate_courses': relate_courses
        })
