package cn.com.qmth.stmms.biz.mark.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 cn.com.qmth.stmms.biz.mark.model.HeaderTrack;
import cn.com.qmth.stmms.biz.mark.model.HeaderTrackPK;

public interface HeaderTrackDao extends JpaRepository<HeaderTrack, HeaderTrackPK>,
        JpaSpecificationExecutor<HeaderTrack> {

    List<HeaderTrack> findByPkStudentIdAndGroupNumber(Integer studentId, Integer groupNumber);

    List<HeaderTrack> findByPkStudentIdAndPkQuestionNumberOrderByPkNumberAsc(Integer studentId, String questionNumber);

    @Modifying(clearAutomatically = true)
    @Query("delete from HeaderTrack t where t.pk.studentId=?1")
    void deleteByPkStudentId(Integer studentId);

    @Modifying(clearAutomatically = true)
    @Query("delete from HeaderTrack t where t.pk.studentId=?1 and t.groupNumber=?2 ")
    void deleteByPkStudentIdAndGroupNumber(Integer studentId, Integer groupNumber);

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

    @Query("select count(t) from HeaderTrack t where t.pk.studentId=?1 and t.pk.questionNumber=?2 and t.unanswered =?3")
    int countByStudentIdAndQuestionNumberAndUnanswered(Integer studentId, String questionNumber, boolean unanswered);

}
