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

import java.util.List;

import org.springframework.data.domain.Pageable;
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.TrialHistory;

public interface TrialHistoryDao extends JpaRepository<TrialHistory, Integer>, JpaSpecificationExecutor<TrialHistory> {

    List<TrialHistory> findByPkLibraryId(Integer libraryId);

    TrialHistory findByPkLibraryIdAndPkMarkerId(Integer libraryId, Integer markerId);

    List<TrialHistory> findByExamIdAndSubjectCodeAndGroupNumberAndPkMarkerId(Integer examId, String subjectCode,
            Integer groupNumber, Integer markerId, Pageable page);

    @Query("select count(*) from TrialHistory f where f.pk.libraryId=?1")
    long countByLibraryId(Integer libraryId);

    @Query("select count(*) from TrialHistory f where f.pk.markerId=?1")
    long countByMarkerId(Integer markerId);

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

    @Modifying
    @Query("delete TrialHistory m where m.pk.libraryId=?1")
    void deleteByLibraryId(Integer libraryId);

    @Modifying
    @Query("delete TrialHistory m where m.studentId=?1")
    void deleteByStudentId(Integer studentId);

    @Modifying
    @Query("delete TrialHistory m where m.pk.markerId=?1")
    void deleteByMarkerId(Integer markerId);

}
