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.TrialTrack;
import cn.com.qmth.stmms.biz.mark.model.TrialTrackPK;

public interface TrialTrackDao extends JpaRepository<TrialTrack, TrialTrackPK>, JpaSpecificationExecutor<TrialTrack> {

    List<TrialTrack> findByPkLibraryId(Integer libraryId);

    List<TrialTrack> findByStudentId(Integer studentId);

    @Modifying
    @Query("delete from TrialTrack t where t.pk.libraryId=?1")
    void deleteByLibraryId(Integer libraryId);

    @Modifying
    @Query("delete from TrialTrack t where t.pk.libraryId=?1 and t.pk.markerId=?2")
    void deleteByLibraryIdAndMarkerId(Integer libraryId, Integer markerId);

    @Modifying
    @Query("delete from TrialTrack t where t.studentId=?1")
    void deleteByStudentId(Integer studentId);

    @Modifying
    @Query("delete from TrialTrack t where t.pk.markerId=?1")
    void deleteByMarkerId(Integer markerId);

    @Modifying
    @Query("delete from TrialTrack 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 TrialTrack t where t.studentId=?1 and t.pk.questionNumber=?2 and t.unanswered =?3")
    Integer countByStudentIdAndQuestionNumberAndUnanswered(Integer studentId, String questionNumber, boolean unanswered);

}
