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.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.Subject;
import com.ruoyi.test.domain.Vo.SubjectVo;
import com.ruoyi.test.mapper.SubjectMapper;
import com.ruoyi.test.seriver.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public IPage<Subject> selectAll(int pagenum) {
        Page<Subject> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        IPage<Subject> page1 = subjectMapper.selectPage(page,queryWrapper);
        return page1;
    }

    @Override
    public IPage<Subject> selectGrade(String Grade,int pagenum) {
        Page<Subject> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getGrade,Grade);
        IPage<Subject> page1 = subjectMapper.selectPage(page,queryWrapper);
        return page1;
    }

    @Override
    public int addSubject(SubjectVo subjectVo) {
        String grade = subjectVo.getGrade();
        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getGrade,grade);
        List<Subject> list = subjectMapper.selectList(queryWrapper);
        for(Subject subject : list){
            if(subject.getSubject().equals(subjectVo.getSubject())){
                return 2;
            }
        }
        return subjectMapper.addSubject(subjectVo);
    }

}
