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

public interface MarkTrackDao extends JpaRepository<MarkTrack, MarkTrackPK>, JpaSpecificationExecutor<MarkTrack> {

    List<MarkTrack> findByPkLibraryId(Integer libraryId);

    List<MarkTrack> findByPkLibraryIdAndPkQuestionNumberOrderByPkNumberAsc(Integer libraryId, String questionNumber);

    // List<MarkTrack> findByStudentId(Integer studentId);

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

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

    // @Modifying(clearAutomatically = true)
    // @Query("delete from MarkTrack t where t.pk.libraryId in (select m.id from MarkLibrary m where m.markerId=?1  and m.status!=?2 and m.status not in (?3) )")
    // void deleteByMarkerId(Integer markerId, LibraryStatus arbitrated,
    // LibraryStatus... waitArbitrate);

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

    @Modifying(clearAutomatically = true)
    @Query("delete from MarkTrack t where t.pk.libraryId=?1 and t.pk.questionNumber=?2")
    void deleteByLibraryIdAndQuestionNumber(Integer id, String questionNumber);

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

}
