package cn.com.qmth.stmms.biz.exam.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import cn.com.qmth.stmms.biz.exam.model.SubjectiveScore;
import cn.com.qmth.stmms.biz.exam.model.SubjectiveScorePK;
import cn.com.qmth.stmms.common.enums.SubjectiveStatus;

public interface SubjectiveScoreDao extends JpaRepository<SubjectiveScore, SubjectiveScorePK>,
        PagingAndSortingRepository<SubjectiveScore, SubjectiveScorePK>, JpaSpecificationExecutor<SubjectiveScore> {

    @Query("select s from SubjectiveScore s where s.pk.studentId=?1 order by s.pk.mainNumber, s.pk.subNumber")
    List<SubjectiveScore> findByStudentId(Integer studentId);

    @Query("select s from SubjectiveScore s where s.pk.studentId=?1 and s.groupNumber=?2 order by s.pk.mainNumber, s.pk.subNumber")
    List<SubjectiveScore> findByStudentIdAndGroupNumber(Integer studentId, Integer groupNumber);

    @Query("select s from SubjectiveScore s where s.pk.studentId=?1 and s.pk.mainNumber=?2 and s.pk.subNumber=?3")
    SubjectiveScore findByStudentIdAndMainNumberAndSubNumber(Integer studentId, Integer mainNumber, String subNumber);

    @Modifying(clearAutomatically = true)
    @Query("delete SubjectiveScore s where s.pk.studentId=?1")
    void deleteByStudentId(Integer studentId);

    @Modifying(clearAutomatically = true)
    @Query("delete SubjectiveScore s where s.pk.studentId=?1 and s.groupNumber=?2")
    void deleteByStudentIdAndGroupNumber(Integer studentId, Integer groupNumber);

    @Modifying(clearAutomatically = true)
    @Query("delete SubjectiveScore s where s.examId=?1 and s.subjectCode=?2 and s.groupNumber=?3")
    void deleteByExamIdAndSubjectCodeAndGroupNumber(Integer examId, String subjectCode, Integer groupNumber);

    @Modifying(clearAutomatically = true)
    @Query("delete SubjectiveScore s where s.examId=?1 and s.subjectCode=?2 and s.groupNumber=?3 and not exists "
            + "(select g.pk.studentId from MarkGroupStudent g where g.pk.studentId=s.pk.studentId and g.status=?4)")
    void deleteByStudentGroupStatus(Integer examId, String subjectCode, Integer groupNumber, SubjectiveStatus status);

    @Query("select s from SubjectiveScore s where s.pk.studentId=?1 and s.uncalculate=?2 order by s.pk.mainNumber, s.pk.subNumber")
    List<SubjectiveScore> findByStudentIdAndUncalculate(Integer studentId, Boolean uncalculate);

    @Modifying(clearAutomatically = true)
    @Query("update SubjectiveScore s set s.rejected=?4 where s.pk.studentId=?1 and s.pk.mainNumber=?2 and s.pk.subNumber=?3")
    void updateRejected(Integer studentId, Integer mainNumber, String subNumber, boolean rejected);

    @Modifying(clearAutomatically = true)
    @Query("update SubjectiveScore s set s.rejected=?3 where s.pk.studentId=?1 and s.groupNumber=?2")
    void updateRejected(Integer studentId, Integer groupNumber, boolean rejected);

    @Modifying(clearAutomatically = true)
    @Query("delete SubjectiveScore s where s.examId=?1 and s.subjectCode=?2")
    void deleteByExamIdAndSubjectCode(Integer examId, String subjectCode);
}
