package stuTest.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import stuTest.mapper.StudentMapper;
import stuTest.pojo.Result;
import stuTest.pojo.Student;

@Component
public class StudentService {
    @Autowired
    StudentMapper studentMapper;

    public Result selectStudent(String name)
    {
        return Result.success(studentMapper.selectStudent(name));
    }

    public Result insertStudent(Student student)
    {
        if(student.getCode().isEmpty()||student.getName().isEmpty()||student.getMajor()==-1||student.getGrade()==-1||student.getSex()==-1){
            return Result.fail("invalid data");
        }
        int result = studentMapper.insertStudent(student);
        if (result == 1){
            return Result.success(student);
        }else{
            return Result.fail("insert failed");
        }
    }

    public Result deleteStudent(int id)
    {
        int result = studentMapper.deleteStudent(id);
        if (result == 1){
            return Result.success(id);
        }
        return Result.fail("delete failed");
    }

    public Result updateStudent(Student student)
    {
        if(student.getCode().isEmpty()||student.getName().isEmpty()||student.getMajor()==-1||student.getGrade()==-1||student.getSex()==-1){
            return Result.fail("invalid data");
        }
        int result = studentMapper.updateStudent(student);
        if (result == 1){
            return Result.success(student);
        }
        return Result.fail("update failed");
    }
}
