package test05.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import test05.mapper.TeacherMapper;
import test05.pojo.Result;
import test05.pojo.Teacher;
import test05.service.TeacherService;

@Service
public class TeacherServiceImpl implements TeacherService{
    private final TeacherMapper teacherMapper;

    @Autowired
    public TeacherServiceImpl(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }

    @Override
    public Result getTeacher(Teacher teacher, int page, int pageSize) {
        try{
            int offset = Math.max(0, (page - 1)) * pageSize;
            List<Teacher> list = teacherMapper.selectTeacher(teacher, offset, pageSize);
            int total = teacherMapper.selectTeacherCount(teacher);
            return Result.success(list, total);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result addTeacher(Teacher teacher) {
        // TODO：校验数据
        int count = teacherMapper.insertTeacher(teacher);
        return Result.success(count>0);
    }

    @Override
    public Result updateTeacher(Teacher teacher) {
        int count = teacherMapper.updateTeacher(teacher);
        return Result.success(count>0);
    }

    @Override
    public Result deleteTeacher(Teacher teacher) {
        int count = teacherMapper.deleteTeacher(teacher);
        return Result.success(count>0);
    }
    
}
