package test05.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test05.mapper.StudentMapper;
import test05.pojo.Result;
import test05.pojo.Student;
import test05.service.StudentService;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    private final StudentMapper studentMapper;

    @Autowired
    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public Result getStudent(Student student, int page, int pageSize) {
        try{
            int offset = Math.max(0, (page - 1)) * pageSize;
            List<Student> list = studentMapper.selectStudent(student, offset, pageSize);
            int total = studentMapper.selectStudentCount(student);
            return Result.success(list, total);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result addStudent(Student student) {

        //TODO:校验数据

        int count = studentMapper.insertStudent(student);
        return Result.success(count>0);
    }

    @Override
    public Result updateStudent(Student student) {
        int count = studentMapper.updateStudent(student);
        return Result.success(count>0);
    }

    @Override
    public Result deleteStudent(Student student) {
        int count = studentMapper.deleteStudent(student);
        return Result.success(count>0);
    }
}
