package com.yinlihupo.enlish.service.service.grade;

import com.yinlihupo.enlish.service.domain.dataobject.GradeDO;
import com.yinlihupo.enlish.service.domain.mapper.GradeClassDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.GradeDOMapper;
import com.yinlihupo.enlish.service.service.GradeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GradeServiceImpl implements GradeService {

    @Resource
    private GradeDOMapper gradeDOMapper;
    @Resource
    private GradeClassDOMapper gradeClassDOMapper;

    @Override
    public GradeDO findByClassId(Integer classId) {
        return gradeDOMapper.selectById(classId);
    }

    @Override
    public List<GradeDO> findGradeList(Integer page, Integer PageSize) {
        return gradeDOMapper.selectList((page - 1) * PageSize, PageSize);
    }

    @Override
    public Integer findGradeCount() {
        return gradeDOMapper.selectCount();
    }

    @Override
    public void insert(String name) {
        gradeDOMapper.insert(name);
    }

    @Override
    public void delete(Integer id) {

        Integer classIds = gradeClassDOMapper.selectByGradeId(id);
        if (classIds == null || classIds == 0) {
            gradeDOMapper.deleteById(id);
        } else {
            throw new RuntimeException("请先删除与该年级关联的班级");
        }
    }
}
