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.PaperCreateDO;
import com.ruoyi.test.domain.Vo.ExamPaperVo;
import com.ruoyi.test.mapper.ClassExamPaperMapper;
import com.ruoyi.test.mapper.ExamPaperMapper;
import com.ruoyi.test.mapper.MarkedtestMapper;
import com.ruoyi.test.mapper.StudentClassMapper;
import com.ruoyi.test.seriver.IExamPaperService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements IExamPaperService {

    @Autowired
    private ExamPaperMapper examPaperMapper;
    @Autowired
    private ClassExamPaperMapper classExamPaperMapper;
    @Autowired
    private StudentClassMapper studentClassMapper;
    @Autowired
    private MarkedtestMapper markedtestMapper;

    @Override
    public IPage<ExamPaper> selectAll(int pagenum,int pagesize) {
        Page<ExamPaper> page = new Page<>(pagenum,pagesize);
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ExamPaper::getSubject,ExamPaper::getId,ExamPaper::getName,ExamPaper::getGrade,ExamPaper::getCreatetime);
        IPage<ExamPaper> page1 = examPaperMapper.selectPage(page,queryWrapper);
        return page1;
    }

    @Override
    public IPage<ExamPaper> selectByGrate(int pagenum, String grate) {
        Page<ExamPaper> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ExamPaper::getSubject,ExamPaper::getId,ExamPaper::getName,ExamPaper::getGrade,ExamPaper::getCreatetime);
        queryWrapper.eq(ExamPaper::getGrade,grate);
        IPage<ExamPaper> page1 = examPaperMapper.selectPage(page,queryWrapper);
        return page1;
    }

    @Override
    public IPage<ExamPaper> selectBySubject(int pagenum, String subject) {
        Page<ExamPaper> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ExamPaper::getSubject,ExamPaper::getId,ExamPaper::getName,ExamPaper::getGrade,ExamPaper::getCreatetime);
        queryWrapper.eq(ExamPaper::getSubject,subject);
        IPage<ExamPaper> page1 = examPaperMapper.selectPage(page,queryWrapper);
        return page1;
    }

    @Override
    public IPage<ExamPaper> selectById(int pagenum,Long id) {
        Page<ExamPaper> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ExamPaper::getSubject,ExamPaper::getId,ExamPaper::getName,ExamPaper::getGrade,ExamPaper::getCreatetime);
        queryWrapper.eq(ExamPaper::getId,id);
        IPage<ExamPaper> page1 = examPaperMapper.selectPage(page,queryWrapper);
        return page1;
    }
    @Override
    public Long selectIdBySubjectAndGrade(String subject, String grade) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getGrade,grade);
        queryWrapper.eq(ExamPaper::getSubject,subject);
        return examPaperMapper.selectOne(queryWrapper).getId();
    }

    @Override
    public Long selectIdBySubjectAndGradeAndName(String subject, String grade, String name) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getGrade,grade);
        queryWrapper.eq(ExamPaper::getSubject,subject);
        queryWrapper.eq(ExamPaper::getName,name);
        return examPaperMapper.selectOne(queryWrapper).getId();
    }

    @Override
    public Long selectId(PaperCreateDO paperCreateDO) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getType,paperCreateDO.getType())
                .eq(ExamPaper::getName,paperCreateDO.getName())
                .eq(ExamPaper::getGrade,paperCreateDO.getGrade())
                .eq(ExamPaper::getSubject,paperCreateDO.getSubject());
        ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper);
        if(examPaper!=null){
            return examPaper.getId();
        }
        return -1L;
    }

    @Override
    public int addPaperExam(PaperCreateDO paperCreateDO) {

        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getSubject,paperCreateDO.getSubject())
                .eq(ExamPaper::getName,paperCreateDO.getName())
                .eq(ExamPaper::getType,paperCreateDO.getType())
                .eq(ExamPaper::getGrade,paperCreateDO.getGrade());
        List<ExamPaper> list = examPaperMapper.selectList(queryWrapper);
        if(list.isEmpty()){
            return examPaperMapper.addPaperExam(paperCreateDO);
        } else{
            return -1;
        }

    }

    @Override
    public ExamPaper selectById(Long id) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getId,id);
        return examPaperMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ExamPaper> selectBySubject(String subject) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getSubject,subject);
        return examPaperMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<ExamPaper> selectByGrateAndSubjectAndId(int pagenum, int pagesize, String grade, String subject, Long id) {
        Page<ExamPaper> page = new Page<>(pagenum,pagesize);
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        if(grade!=null){
            queryWrapper.eq(ExamPaper::getGrade,grade);
        }
        if(subject!=null){
            queryWrapper.eq(ExamPaper::getSubject,subject);
        }
        if(id!=null){
            queryWrapper.eq(ExamPaper::getId,id);
        }
        IPage<ExamPaper> iPage = examPaperMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public List<ExamPaper> selectByGlId(Long teacherId) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getGlId,teacherId);
        return examPaperMapper.selectList(queryWrapper);
    }

    @Override
    public List<ExamPaperVo> getExamPaperList(Long teacherId) {
        LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamPaper::getGlId,teacherId);
        List<ExamPaper> list = examPaperMapper.selectList(queryWrapper);
        List<ExamPaperVo> list1 = new ArrayList<>();
        for(ExamPaper examPaper : list){
            Long testid = examPaper.getId();
            LambdaQueryWrapper<ClassExamPaper> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ClassExamPaper::getId,testid);
            List<ClassExamPaper> studentClasses = classExamPaperMapper.selectList(queryWrapper1);
            for(ClassExamPaper classExamPaper : studentClasses){
                ExamPaperVo examPaperVo = new ExamPaperVo();
                BeanUtils.copyProperties(examPaper,examPaperVo);
                examPaperVo.setClass1(classExamPaper.getClass1());
                LambdaQueryWrapper<StudentClass> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(StudentClass::getGrade,classExamPaper.getGrade())
                        .eq(StudentClass::getClass1,classExamPaper.getClass1());
                List<Long> studentList = studentClassMapper.selectList(queryWrapper2)
                        .stream()
                        .map(StudentClass::getStudentid)
                        .collect(Collectors.toList());
                examPaperVo.setTotalCount((long) studentList.size());
                LambdaQueryWrapper<Markedtest> queryWrapper3 = new LambdaQueryWrapper<>();
                queryWrapper3.eq(Markedtest::getTestid,testid)
                        .in(Markedtest::getSenderId,studentList);
                examPaperVo.setCompletedCount(markedtestMapper.selectCount(queryWrapper3));
                list1.add(examPaperVo);
            }

        }
        return list1;
    }

//    @Override
//    public List<ExamPaper> totalExamPaper(List<TeacherManageClass> list, List<TeacherSubject> list2) {
//        List<ExamPaper> list1 = new ArrayList<>();
//        for(TeacherSubject teacherSubject : list2){
//            for(TeacherManageClass teacherManageClass : list){
//                LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(ExamPaper::getSubject,teacherSubject.getSubject())
//                        .eq(ExamPaper::getGrade,teacherManageClass.getGrade());
//                list1.addAll(examPaperMapper.selectList(queryWrapper));
//            }
//        }
//        return list1;
//    }


}
