package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.*;
import com.ruoyi.test.domain.DO.QuestionAnswerDO;
import com.ruoyi.test.domain.Vo.MarkedtestVo;
import com.ruoyi.test.domain.Vo.QuestionanswerVo;
import com.ruoyi.test.domain.Vo.QuestionbankVo;
import com.ruoyi.test.mapper.AnswerMapper;
import com.ruoyi.test.mapper.ExamPaperMapper;
import com.ruoyi.test.mapper.MarkedtestMapper;
import com.ruoyi.test.seriver.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

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

@Service
public class MarkedtestServiceImpl extends ServiceImpl<MarkedtestMapper, Markedtest> implements IMarkedtestService {

    @Autowired
    private MarkedtestMapper markedtestMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private IQuestionanswerService iQuestionanswerService;

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IUserService iUserService;

    @Override
    public int addmarkedtest(String sender,Long testid) {
        QuestionAnswerDO questionAnswerDO = iQuestionanswerService.selectQuestionanswer(sender,testid);

        int totalscore = 0;
        int score = 0;
        int tquestions = 0;
        int totalquestions = 0;

        List<QuestionanswerVo> list = questionAnswerDO.getList();

        for(QuestionanswerVo questionanswerVo : list){
            String tf = questionanswerVo.getTf();
            if(tf.equals("true")){
                score+=questionanswerVo.getScore();
                tquestions++;
            }
            totalscore+=questionanswerVo.getScore();
            totalquestions++;
        }

        Answer answer = iAnswerService.selectByTestId(sender, testid);

        Markedtest markedtest = new Markedtest();
        markedtest.setSender(sender);
        markedtest.setTestid(testid);
        markedtest.setScore(score);
        markedtest.setTotalscore(totalscore);
        markedtest.setFinishtime(answer.getFinishtime());
        markedtest.setTime(answer.getTime());
        markedtest.setTquestions(tquestions);
        markedtest.setTotalquestions(totalquestions);
        return markedtestMapper.addmarkedtest(markedtest);
    }

    @Override
    public IPage<Markedtest> selectmarkedtest(int pagenum) {
        Page<Markedtest> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<Markedtest> queryWrapper = new LambdaQueryWrapper<>();
        IPage iPage = markedtestMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public IPage<Markedtest> selectmarkedtestBySubject(int pagenum,String subject){
        Page<ExamPaper> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getSubject,subject);
        IPage<ExamPaper> iPage = examPaperMapper.selectPage(page,queryWrapper);
        List<ExamPaper> list = page.getRecords();
        List<Markedtest> list1 = new ArrayList<>();
        for(ExamPaper examPaper : list){
            Long id = examPaper.getId();
            LambdaQueryWrapper<Markedtest> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Markedtest::getTestid,id);
            List<Markedtest> l = markedtestMapper.selectList(queryWrapper1);
            list1.addAll(l);
        }
        IPage<Markedtest> pageVo = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageVo.setRecords(list1);
        return pageVo;
    }

    @Override
    public IPage<Markedtest> selectmarkedtest(int pagenum, List<StudentClass> list1) {
        List<Markedtest> list = new ArrayList<>();
        for(StudentClass studentClass : list1){
            String tname = iUserService.user(studentClass.getStudentid()).getTname();
            LambdaQueryWrapper<Markedtest> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Markedtest::getSender,tname);
            list.addAll(markedtestMapper.selectList(queryWrapper));
        }
        Page<Markedtest> page = new Page<>(pagenum,10);
        IPage<Markedtest> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list);
        return iPage;
    }

    @Override
    public IPage<Markedtest> selectmarkedtestBySubject(int pagenum, List<StudentClass> list, String subject) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getSubject,subject);
        List<ExamPaper> list1  = examPaperMapper.selectList(queryWrapper);
        List<Long> list2 = new ArrayList<>();
        for(ExamPaper examPaper : list1){
            list2.add(examPaper.getId());
        }
        List<Markedtest> list3 = new ArrayList<>();
        for(StudentClass studentClass : list){
            String tname = iUserService.user(studentClass.getStudentid()).getTname();
            LambdaQueryWrapper<Markedtest> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Markedtest::getSender,tname);
            List<Markedtest> list4 = markedtestMapper.selectList(queryWrapper1);
            for(Markedtest markedtest : list4){
                if(list2.contains(markedtest.getTestid())){
                    list3.add(markedtest);
                }
            }
        }
        Page<Markedtest> page = new Page<>(pagenum,10);
        IPage<Markedtest> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list3);
        return iPage;
    }

    @Override
    public Markedtest selectmarkedtestBySenderAndTestid(String sender, Long testid) {
        LambdaQueryWrapper<Markedtest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Markedtest::getSender,sender)
                .eq(Markedtest::getTestid,testid);
        return markedtestMapper.selectOne(queryWrapper);
    }

}