package test05.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test05.mapper.CourseMapper;
import test05.mapper.ScoreMapper;
import test05.mapper.StudentMapper;
import test05.pojo.Course;
import test05.pojo.Result;
import test05.pojo.Score;
import test05.pojo.Student;
import test05.service.ScoreService;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    ScoreMapper scoreMapper;
    StudentMapper studentMapper;
    CourseMapper courseMapper;
    @Autowired
    public ScoreServiceImpl(ScoreMapper scoreMapper, StudentMapper studentMapper, CourseMapper courseMapper) {
        this.scoreMapper = scoreMapper;
        this.studentMapper = studentMapper;
        this.courseMapper = courseMapper;
    }

    @Override
    public Result addScore(Student student, Course course, Float score) {
        if (student == null || student.getId()==-1){
            student = studentMapper.selectStudent(student, 1,10).getFirst();
        }
        if (course == null || course.getId()==-1){
            course = courseMapper.selectCourse(course, 1,10).getFirst();
        }
        Score toCreate = new Score();
        toCreate.setScore(score);
        toCreate.setStudentId(student.getId());
        toCreate.setCourseId(course.getId());
        return Result.success(scoreMapper.insertScore(toCreate));
    }

    @Override
    public Result getScore(Student student, Course course) {
        if (student == null || student.getId()==-1){
            student = studentMapper.selectStudent(student, 1,10).getFirst();
            if (student == null){
                student = new Student();
            }
        }
        if (course == null || course.getId()==-1){
            course = courseMapper.selectCourse(course, 1,10).getFirst();
            if (course == null){
                course = new Course();
            }
        }
        Score score = new Score();
        score.setStudentId(student.getId());
        score.setCourseId(course.getId());
        List<Score> result = scoreMapper.selectScore(score);
        return Result.success(result, scoreMapper.selectScoreCount(score));
    }

    @Override
    public Result deleteScore(Score score) {
        return Result.success(scoreMapper.deleteScore(score)>0);
    }

    @Override
    public Result updateScore(Score score) {
        return Result.success(scoreMapper.updateScore(score)>0);
    }

    @Override
    public Result getStudentScore(Student student) {
        if (student == null||student.getCode().isEmpty()){
            return Result.error("didn't find student");
        }
        student = studentMapper.selectStudent(student, 1,10).getFirst();
        if (student == null){
            return Result.error("didn't find student");
        }
        Score score = new Score();
        score.setStudentId(student.getId());
        List<Score> result = scoreMapper.selectScore(score);
        int scoreCount = scoreMapper.selectScoreCount(score);
        int scoreSum = 0;
        for (Score s : result){
            scoreSum += s.getScore();
        }
        Result rst = Result.success(scoreSum, scoreCount);
        rst.setMsg(student.getName());
        return rst;
    }
}
