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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.DO.QuestionAnswerDO;
import com.ruoyi.test.domain.Markedtest;
import com.ruoyi.test.domain.StudentClass;
import com.ruoyi.test.domain.TeacherManageClass;
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.*;

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

@Api(tags = "教师端答卷管理")
@RestController
@RequestMapping("/teacher/answerSheet")
public class TeacherAnswerSheetController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IAnswerSheetDOService iAnswerSheetDOService;

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IQuestionanswerService iQuestionanswerService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @ApiOperation("查看学生的考试情况(最新)")
    @GetMapping("selectQuestionanswer")
    public R<QuestionAnswerDO> selectQuestionanswer(String sender, Long testid) {
        return R.ok(iQuestionanswerService.selectQuestionanswer(sender,testid));
    }

    @ApiOperation("批改客观题")
    @PostMapping("markObjectiveQuestions")
    public R<String> markObjectiveQuestions(@RequestBody QuestionAnswerDO questionAnswerDO) {
        if(iQuestionanswerService.markObjectiveQuestions(questionAnswerDO)!=0){
            return R.ok("批改完成！");
        }
        return R.fail("批改提交失败！");
    }


    @ApiOperation("试卷批改完毕提交")
    @PostMapping("addmarkedtest")
    public R<String> addmarkedtest(String sender,Long testid){
        if(iMarkedtestService.addmarkedtest(sender,testid)!=0){
            return R.ok("提交完成！");
        }
        return R.fail("提交失败！");
    }

    @ApiOperation("学生成绩列表查询")
    @GetMapping("selectmarkedtest")
    public R<IPage<Markedtest>> selectmarkedtest(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<StudentClass> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            list1.addAll(iStudentClassService.selectByGradeAndClass(teacherManageClass.getGrade(),teacherManageClass.getClass1()));
        }
        System.out.println(11111+" "+list1);
        return R.ok(iMarkedtestService.selectmarkedtest(pagenum,list1));
    }

    @ApiOperation("通过学科查询学生成绩列表")
    @GetMapping("selectmarkedtestBySubject")
    public R<IPage<Markedtest>> selectmarkedtestBySubject(int pagenum,String subject){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<StudentClass> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            list1.addAll(iStudentClassService.selectByGradeAndClass(teacherManageClass.getGrade(),teacherManageClass.getClass1()));
        }
        System.out.println(11111+" "+list1);
        return R.ok(iMarkedtestService.selectmarkedtestBySubject(pagenum,list1,subject));
    }
}
