package com.ruoyi.web.controller.test.student;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.test.domain.ClassVedio;
import com.ruoyi.test.domain.StudentClass;
import com.ruoyi.test.domain.Video;
import com.ruoyi.test.seriver.IClassVedioService;
import com.ruoyi.test.seriver.IStudentClassService;
import com.ruoyi.test.seriver.IUserService;
import com.ruoyi.test.seriver.IVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "学生端视频课堂")
@RestController
@RequestMapping("/student/video")
public class StudentVideoController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private IClassVedioService iClassVedioService;

    @Autowired
    private IVideoService iVideoService;

    @ApiOperation("视频列表")
    @GetMapping("vedioList")
    public R<IPage<Video>> vedioList(int pagenum,int pagesize){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        Long userId = iUserService.user(iUserService.selectByzhid(id)).getUserId();
        StudentClass studentClass = iStudentClassService.selectById(userId);
        String grade = studentClass.getGrade();
        String Class1 = studentClass.getClass1();
        List<ClassVedio> list= iClassVedioService.selectClassVideo(grade,Class1);
        List<Video> list1 = new ArrayList<>();
        for(ClassVedio classVedio : list) {
            Long vedioid = classVedio.getVideoid();
            Video video = iVideoService.video(vedioid);
            list1.add(video);
        }
        Page<Video> page = new Page<>(pagenum,pagesize);
        IPage<Video> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

    @ApiOperation("视频列表")
    @GetMapping("vedioListBySubject")
    public R<IPage<Video>> vedioListBySubject(int pagenum,String subject,int pagesize){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        Long userId = iUserService.user(iUserService.selectByzhid(id)).getUserId();
        StudentClass studentClass = iStudentClassService.selectById(userId);
        String grade = studentClass.getGrade();
        String Class1 = studentClass.getClass1();
        List<ClassVedio> list= iClassVedioService.selectClassVideo(grade,Class1);
        List<Video> list1 = new ArrayList<>();
        for(ClassVedio classVedio : list) {
            Long vedioid = classVedio.getVideoid();
            Video video = iVideoService.video(vedioid);
            if(!video.getSubject().equals(subject)){
                continue;
            }
            list1.add(video);
        }
        Page<Video> page = new Page<>(pagenum,pagesize);
        IPage<Video> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

}
