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

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.Correct;
import com.ruoyi.test.domain.DO.QuestionAnswerDO;
import com.ruoyi.test.domain.DO.ScoreListDO;
import com.ruoyi.test.domain.Vo.ExamPaperVo;
import com.ruoyi.test.domain.Vo.MessageVo;
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;
import java.util.stream.Collectors;

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

    @Autowired
    private IAnswerSheetDOService iAnswerSheetDOService;

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IQuestionanswerService iQuestionanswerService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @Autowired
    private IMessageService iMessageService;

    @Autowired
    private IClassExamPaperService iClassExamPaperService;

    @ApiOperation("发布成绩")
    @PutMapping("fabu")
    public R<Integer> fabu(Long testid,String grade,String Class1) {
        return R.ok(iMarkedtestService.fabu(testid,grade,Class1));
    }

    @ApiOperation("试卷列表查询")
    @GetMapping("getExamPaperList")
    public R<IPage<ExamPaperVo>> getExamPaperList(int pageNum,int pageSize) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long teacherId = sysUser.getUserId();
        List<ExamPaperVo> list = iExamPaperService.getExamPaperList(teacherId);
        IPage<ExamPaperVo> page = new Page<>(pageNum, pageSize);

        // 将 List 设置为 records
        page.setRecords(list);

        return R.ok(page);
    }

    @ApiOperation("学生成绩列表")
    @GetMapping("selectScoreList")
    public R<List<ScoreListDO>> selectScoreList(Long testid,String grade,String Class1){
        return R.ok(iMarkedtestService.selectScoreList(testid,grade,Class1));
    }

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

    @ApiOperation("检查是否批改")
    @GetMapping("selectMarkedtestList")
    public R<List<Correct>> selectMarkedtestList() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<ExamPaper> examPapers = iExamPaperService.selectByGlId(sysUser.getUserId());
        List<ClassExamPaper> list = new ArrayList<>();
        for(ExamPaper examPaper : examPapers){
            List<ClassExamPaper> studentClass = iClassExamPaperService.selectById(examPaper.getId());
            list.addAll(studentClass);
        }
        List<Long> list1 = iStudentClassService.getStudentClassByClassExamPaperList(list)
                .stream()
                .map(StudentClass::getStudentid)
                .collect(Collectors.toList());
        return R.ok(iMarkedtestService.selectByStudentList(sysUser.getUserId(),list1));
    }

    @ApiOperation("添加提醒消息")
    @PostMapping("addMessage")
    public R<Integer> addMessage(@RequestBody MessageVo messageVo) {
        return R.ok(iMessageService.addMessage(messageVo));
    }

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

    @ApiOperation("获取某一张的分数")
    @GetMapping("getScoreByTestId")
    public R<Markedtest> getScoreByTestId(Long studentId,Long testId) {
        return R.ok(iMarkedtestService.selectmarkedtestByTestid(studentId,testId));
    }


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

    @ApiOperation("学生成绩列表查询")
    @GetMapping("selectmarkedtest")
    public R<IPage<Markedtest>> selectmarkedtest(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long teacherid = sysUser.getUserId();
        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 teacherid = sysUser.getUserId();
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<StudentClass> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            list1.addAll(iStudentClassService.selectByGradeAndClass(teacherManageClass.getGrade(),teacherManageClass.getClass1()));
        }
        return R.ok(iMarkedtestService.selectmarkedtestBySubject(pagenum,list1,subject));
    }
}
