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.*;
import com.ruoyi.test.domain.DO.ExamPaperDO1;
import com.ruoyi.test.seriver.*;
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/examPaper")
public class StudentExamPaperController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private IClassExamPaperService iClassExamPaperService;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @ApiOperation("试卷查询")
    @GetMapping("examPaperByTypeAndSubject")
    public R<IPage<ExamPaperDO1>> examPaperByTypeAndSubject(int pagenum,int pagesize,String type,String subject){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        Long userid = iUserService.selectByzhid(id);
        StudentClass studentClass = iStudentClassService.selectById(userid);
        if(studentClass==null){
            return R.fail("你还没有班级或你不是学生");
        }
        String grade = studentClass.getGrade();
        String Class1 = studentClass.getClass1();
        List<ClassExamPaper> list = iClassExamPaperService.selectByGradeAndClass(grade,Class1);
        List<ExamPaperDO1> list1 = new ArrayList<>();
        for(ClassExamPaper classExamPaper : list ){
            ExamPaper examPaper = iExamPaperService.selectById(classExamPaper.getId());
            if(type!=null&&!examPaper.getType().equals(type)){
                continue;
            }
            if(subject!=null&&!examPaper.getSubject().equals(subject)){
                continue;
            }
            ExamPaperDO1 examPaperDO1 = new ExamPaperDO1();
            examPaperDO1.setName(examPaper.getName());
            examPaperDO1.setTime(examPaper.getTime());
            examPaperDO1.setSubject(examPaper.getSubject());
            examPaperDO1.setStart_time(null);
            examPaperDO1.setEnd_time(null);
            Long testid = examPaper.getId();
            List<Questionbank> list2 = iExamCreateService.selectListByPid(testid);
            examPaperDO1.setTotalquestion(list2.size());
            Double total=0.0;
            for(Questionbank questionbank : list2){
                total+=questionbank.getScore();
            }
            examPaperDO1.setTotalscore(total);
            list1.add(examPaperDO1);
        }
        Page<ExamPaperDO1> page = new Page<>(pagenum,pagesize);
        IPage<ExamPaperDO1> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }


//    @ApiOperation("试卷查询（试卷类型查询）")
//    @GetMapping("examPaperByType")
//    public R<IPage<ExamPaperDO1>> examPaperByType(int pagenum,String type){
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        Long id = sysUser.getUserId();
//        Long userid = iUserService.selectByzhid(id);
//        StudentClass studentClass = iStudentClassService.selectById(userid);
//        String grade = studentClass.getGrade();
//        String Class1 = studentClass.getClass1();
//        List<ClassExamPaper> list = iClassExamPaperService.selectByGradeAndClass(grade,Class1);
//        List<ExamPaperDO1> list1 = new ArrayList<>();
//        for(ClassExamPaper classExamPaper : list ){
//            ExamPaper examPaper = iExamPaperService.selectById(classExamPaper.getId());
//            if(!examPaper.getType().equals(type)){
//                continue;
//            }
//            ExamPaperDO1 examPaperDO1 = new ExamPaperDO1();
//            examPaperDO1.setName(examPaper.getName());
//            examPaperDO1.setTime(examPaper.getTime());
//            examPaperDO1.setSubject(examPaper.getSubject());
//            examPaperDO1.setStart_time(null);
//            examPaperDO1.setEnd_time(null);
//            Long testid = examPaper.getId();
//            List<Questionbank> list2 = iExamCreateService.selectListByPid(testid);
//            examPaperDO1.setTotalquestion(list2.size());
//            Double total=0.0;
//            for(Questionbank questionbank : list2){
//                total+=questionbank.getScore();
//            }
//            examPaperDO1.setTotalscore(total);
//            list1.add(examPaperDO1);
//        }
//        Page<ExamPaperDO1> page = new Page<>(pagenum,10);
//        IPage<ExamPaperDO1> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
//        iPage.setRecords(list1);
//        return R.ok(iPage);
//    }
//
//    @ApiOperation("试卷查询（学科查询）")
//    @GetMapping("examPaperBySubject")
//    public R<IPage<ExamPaperDO1>> examPaperBySubject(int pagenum,String subject){
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        Long id = sysUser.getUserId();
//        Long userid = iUserService.selectByzhid(id);
//        StudentClass studentClass = iStudentClassService.selectById(userid);
//        String grade = studentClass.getGrade();
//        String Class1 = studentClass.getClass1();
//        List<ClassExamPaper> list = iClassExamPaperService.selectByGradeAndClass(grade,Class1);
//        List<ExamPaperDO1> list1 = new ArrayList<>();
//        for(ClassExamPaper classExamPaper : list ){
//            ExamPaper examPaper = iExamPaperService.selectById(classExamPaper.getId());
//            if(!examPaper.getSubject().equals(subject)){
//                continue;
//            }
//            ExamPaperDO1 examPaperDO1 = new ExamPaperDO1();
//            examPaperDO1.setName(examPaper.getName());
//            examPaperDO1.setTime(examPaper.getTime());
//            examPaperDO1.setSubject(examPaper.getSubject());
//            examPaperDO1.setStart_time(null);
//            examPaperDO1.setEnd_time(null);
//            Long testid = examPaper.getId();
//            List<Questionbank> list2 = iExamCreateService.selectListByPid(testid);
//            examPaperDO1.setTotalquestion(list2.size());
//            Double total=0.0;
//            for(Questionbank questionbank : list2){
//                total+=questionbank.getScore();
//            }
//            examPaperDO1.setTotalscore(total);
//            list1.add(examPaperDO1);
//        }
//        Page<ExamPaperDO1> page = new Page<>(pagenum,10);
//        IPage<ExamPaperDO1> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
//        iPage.setRecords(list1);
//        return R.ok(iPage);
//    }



}
