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.CorrectionDO;
import com.ruoyi.test.domain.DO.QuestionbankDO1;
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/examRecord")
public class StudentExamRecordController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IClassExamPaperService iClassExamPaperService;

    @Autowired
    private IClass1Service iClass1Service;

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @Autowired
    private IQuestionbankService iQuestionbankService;

    @ApiOperation("试卷批改情况")
    @GetMapping("correction")
    public R<IPage<CorrectionDO>> correction(int pagenum,int pagesize){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        List<Answer> list = iAnswerService.selectBySender(sender);
        List<CorrectionDO> list1 = new ArrayList<>();
        for(Answer answer : list){
            CorrectionDO correctionDO = new CorrectionDO();
            correctionDO.setFinishtime(answer.getFinishtime());
            correctionDO.setTime(answer.getTime());
            Long testid = answer.getTestid();
            ExamPaper examPaper = iExamPaperService.selectById(testid);
            correctionDO.setSubject(examPaper.getSubject());
            correctionDO.setType(examPaper.getType());
            correctionDO.setName(examPaper.getName());
            if(iMarkedtestService.selectmarkedtestBySenderAndTestid(sender,testid)!=null){
                Markedtest markedtest = iMarkedtestService.selectmarkedtestBySenderAndTestid(sender,testid);
                correctionDO.setTotalscore(markedtest.getTotalscore());
                correctionDO.setScore(markedtest.getScore());
                correctionDO.setTotalquestions(markedtest.getTotalquestions());
                correctionDO.setTquestions(markedtest.getTquestions());
                correctionDO.setState(0);
            }else{
                List<Questionbank> list2 = iExamCreateService.selectListByPid(testid);
                correctionDO.setTotalquestions(list2.size());
                Double total = 0.0;
                for(Questionbank questionbank : list2){
                    total +=questionbank.getScore();
                }
                correctionDO.setTotalscore(total);
                correctionDO.setScore(null);
                correctionDO.setTquestions(null);
                correctionDO.setState(1);
            }
            list1.add(correctionDO);
        }
        Page<CorrectionDO> page = new Page<>(pagenum,pagesize);
        IPage<CorrectionDO> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

    @ApiOperation("试卷批改情况")
    @GetMapping("correctionBySubject")
    public R<IPage<CorrectionDO>> correctionBySubject(int pagenum,String subject){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        String sender = iUserService.user(iUserService.selectByzhid(id)).getTname();
        List<Answer> list = iAnswerService.selectBySender(sender);
        List<CorrectionDO> list1 = new ArrayList<>();
        for(Answer answer : list){
            Long testid = answer.getTestid();
            ExamPaper examPaper = iExamPaperService.selectById(testid);
            if(!examPaper.getSubject().equals(subject)){
                continue;
            }
            CorrectionDO correctionDO = new CorrectionDO();
            correctionDO.setFinishtime(answer.getFinishtime());
            correctionDO.setTime(answer.getTime());
            correctionDO.setSubject(examPaper.getSubject());
            correctionDO.setType(examPaper.getType());
            correctionDO.setName(examPaper.getName());
            if(iMarkedtestService.selectmarkedtestBySenderAndTestid(sender,testid)!=null){
                Markedtest markedtest = iMarkedtestService.selectmarkedtestBySenderAndTestid(sender,testid);
                correctionDO.setTotalscore(markedtest.getTotalscore());
                correctionDO.setScore(markedtest.getScore());
                correctionDO.setTotalquestions(markedtest.getTotalquestions());
                correctionDO.setTquestions(markedtest.getTquestions());
                correctionDO.setState(0);
            }else{
                List<Questionbank> list2 = iExamCreateService.selectListByPid(testid);
                correctionDO.setTotalquestions(list2.size());
                Double total = 0.0;
                for(Questionbank questionbank : list2){
                    total +=questionbank.getScore();
                }
                correctionDO.setTotalscore(total);
                correctionDO.setScore(null);
                correctionDO.setTquestions(null);
                correctionDO.setState(1);
            }
            list1.add(correctionDO);
        }
        Page<CorrectionDO> page = new Page<>(pagenum,10);
        IPage<CorrectionDO> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

}
