package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.ClassExamPaper;
import com.ruoyi.test.domain.StudentClass;
import com.ruoyi.test.domain.TeacherManageClass;
import com.ruoyi.test.mapper.StudentClassMapper;
import com.ruoyi.test.seriver.IStudentClassService;
import org.springframework.beans.factory.annotation.Autowired;
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 StudentClassServiceImpl extends ServiceImpl<StudentClassMapper, StudentClass> implements IStudentClassService {

    @Autowired
    private StudentClassMapper studentClassMapper;

    @Override
    public Integer totalStudent(List<TeacherManageClass> list) {
        Integer total=0;
        for(TeacherManageClass teacherManageClass : list){
            LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StudentClass::getGrade,teacherManageClass.getGrade())
                    .eq(StudentClass::getClass1,teacherManageClass.getClass1());
            total += studentClassMapper.selectList(queryWrapper).size();
        }
        return total;
    }

    @Override
    public List<StudentClass> selectByGradeAndClass(String grade, String Class) {
        LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentClass::getGrade,grade)
                .eq(StudentClass::getClass1,Class);
        return studentClassMapper.selectList(queryWrapper);
    }

    @Override
    public int deleteStudentClass(Long id) {
        return studentClassMapper.deleteStudentClass(id);
    }

    @Override
    public StudentClass selectById(Long id) {
        LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentClass::getStudentid,id);
        return studentClassMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Long> selectByTeacherIdList(List<TeacherManageClass> teacherManageClasses) {
        // 用于存储查询结果的学生ID列表
        List<Long> studentIds = teacherManageClasses.stream()
                .flatMap(teacherManageClass -> {
                    // 对每个TeacherManageClass查询符合的学生ID
                    LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(StudentClass::getClass1, teacherManageClass.getClass1())
                            .eq(StudentClass::getGrade, teacherManageClass.getGrade());

                    // 执行查询，获取学生ID列表
                    List<Long> ids = studentClassMapper.selectList(queryWrapper)
                            .stream()
                            .map(StudentClass::getStudentid)  // 提取学生ID
                            .collect(Collectors.toList());
                    return ids.stream();
                })
                .collect(Collectors.toList()); // 最终返回所有符合条件的学生ID列表

        return studentIds;
    }

    @Override
    public List<StudentClass> getStudentClassByTeacherIdList(List<TeacherManageClass> teacherManageClasses) {
        // 用于存储查询结果的学生ID列表
        List<StudentClass> studentIds = teacherManageClasses.stream()
                .flatMap(teacherManageClass -> {
                    // 对每个TeacherManageClass查询符合的学生ID
                    LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(StudentClass::getClass1, teacherManageClass.getClass1())
                            .eq(StudentClass::getGrade, teacherManageClass.getGrade());

                    // 执行查询，获取学生ID列表
                    List<StudentClass> ids = studentClassMapper.selectList(queryWrapper);
                    return ids.stream();
                })
                .collect(Collectors.toList()); // 最终返回所有符合条件的学生ID列表

        return studentIds;
    }

    @Override
    public List<StudentClass> getStudentClassByClassExamPaperList(List<ClassExamPaper> list) {
        List<StudentClass> studentIds = new ArrayList<>();
        for(ClassExamPaper classExam : list){
            LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StudentClass::getGrade,classExam.getGrade())
                    .eq(StudentClass::getClass1,classExam.getClass1());
            studentIds.addAll(studentClassMapper.selectList(queryWrapper));
        }
        return studentIds;
    }


}
