package com.yinlihupo.enlish.service.service.classs;

import com.yinlihupo.enlish.service.domain.dataobject.ClassDO;
import com.yinlihupo.enlish.service.domain.dataobject.GradeClassDO;
import com.yinlihupo.enlish.service.domain.dataobject.GradeDO;
import com.yinlihupo.enlish.service.domain.mapper.ClassDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.GradeClassDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.GradeDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.StudentDOMapper;
import com.yinlihupo.enlish.service.service.ClassService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClassServiceImpl implements ClassService {

    @Resource
    private ClassDOMapper classDOMapper;
    @Resource
    private GradeClassDOMapper gradeClassDOMapper;
    @Resource
    private GradeDOMapper gradeDOMapper;
    @Resource
    private StudentDOMapper studentDOMapper;

    @Override
    public ClassDO findClassById(Integer id) {
        return classDOMapper.selectClassDOById(id);
    }

    @Override
    public List<ClassDO> findClassList(Integer page, Integer pageSize) {
        return classDOMapper.selectClassDOList((page - 1) * pageSize, pageSize);
    }

    @Override
    public int getClassCount() {
        return classDOMapper.selectClassDOCount();
    }

    @Override
    public Map<Integer, GradeDO> findClassIds2GradeMap(List<Integer> classIds) {

        List<GradeClassDO> gradeClassDOS = gradeClassDOMapper.selectByClassIds(classIds);
        List<GradeDO> gradeDOS = gradeDOMapper.selectByGradeIds(gradeClassDOS.stream().map(GradeClassDO::getGradeId).toList());
        HashMap<Integer, GradeDO> gradeId2GradeDO = gradeDOS.stream().collect(Collectors.toMap(
                GradeDO::getId,
                gradeDO -> gradeDO,
                (oldValue, newValue) -> newValue,  // mergeFunction：处理键冲突（比如重复键时保留新值）
                HashMap::new           // mapSupplier：指定返回HashMap类型
        ));

        return gradeClassDOS.stream().collect(Collectors.toMap(
                GradeClassDO::getClassId,  // key：GradeClassDO的classId
                // 修正后的valueMapper：lambda表达式处理映射逻辑
                gradeClassDO -> {
                    // 可选：处理空值，避免返回null（根据业务需求调整）
                    Integer gradeId = gradeClassDO.getGradeId();
                    GradeDO gradeDO = gradeId2GradeDO.get(gradeId);
                    if (gradeDO == null) {
                        // 业务上的空值处理，比如抛出异常、返回默认对象等
                        throw new IllegalArgumentException("未找到gradeId为" + gradeId + "的GradeDO");
                        // 或者：return new GradeDO(); // 返回默认对象
                    }
                    return gradeDO;
                },
                (oldValue, newValue) -> newValue, // 键冲突保留新值
                HashMap::new                     // 指定返回 HashMap
        ));
    }

    @Override
    public void addClass(String className, Integer gradeId) {
        ClassDO classDO = ClassDO.builder().title(className).build();
        classDOMapper.insert(classDO);
        Integer id = classDO.getId();

        gradeClassDOMapper.insert(id, gradeId);
    }

    @Override
    public void deleteClass(Integer classId) {
        int selectStudentCountByClassId = studentDOMapper.selectStudentCountByClassId(classId);
        if (selectStudentCountByClassId > 0) {
            throw new RuntimeException("该班级下有学生，请先删除该班级下的学生");
        }
        classDOMapper.delete(classId);
    }
}
