package com.schoolinout.system.service.impl;

import com.schoolinout.common.utils.DateUtils;
import com.schoolinout.system.domain.Teacher;
import com.schoolinout.system.domain.vo.OptionVo;
import com.schoolinout.system.mapper.TeacherClassRelationMapper;
import com.schoolinout.system.mapper.TeacherMapper;
import com.schoolinout.system.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 教师管理Service业务层处理
 *
 * @author sakura
 * @date 2024-01-03
 */
@Service
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TeacherClassRelationMapper relationMapper;

    /**
     * 查询教师管理
     *
     * @param id 教师管理主键
     * @return 教师管理
     */
    @Override
    public Teacher selectTeacherById(Long id) {
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询教师管理列表
     *
     * @param teacher 教师管理
     * @return 教师管理
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher) {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher) {
        teacher.setCreateTime(DateUtils.getNowDate());
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher) {
        teacher.setUpdateTime(DateUtils.getNowDate());
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除教师管理
     *
     * @param ids 需要删除的教师管理主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteTeacherByIds(Long[] ids) {
        // 检查是否存在绑定关系，如果存在，则提示
        for (Long id : ids) {
            relationMapper.deleteAllRelationsForClass(null, null, id);
        }

        return teacherMapper.deleteTeacherByIds(ids);
    }

    /**
     * 删除教师管理信息
     *
     * @param id 教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(Long id) {
        return teacherMapper.deleteTeacherById(id);
    }

    @Override
    public List<OptionVo> selectTeacherOptionList(Long schoolId) {
        return teacherMapper.selectTeacherOption(schoolId);
    }
}
