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.DO.PaperCreateDO;
import com.ruoyi.test.domain.ExamPaper;
import com.ruoyi.test.domain.Subject;
import com.ruoyi.test.domain.TeacherManageClass;
import com.ruoyi.test.domain.TeacherSubject;
import com.ruoyi.test.mapper.ExamPaperMapper;
import com.ruoyi.test.seriver.IExamPaperService;
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.List;

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

    @Autowired
    private ExamPaperMapper examPaperMapper;

    @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> 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;
//    }


}
