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.TrialLibrary;

public interface TrialLibraryDao extends JpaRepository<TrialLibrary, Integer>, JpaSpecificationExecutor<TrialLibrary> {

    // List<TrialLibrary> findByExamIdAndSubjectCode(Integer examId, String
    // subjectCode, Pageable page);

    // List<TrialLibrary> findByExamIdAndSubjectCodeAndGroupNumber(Integer
    // examId, String subjectCode,
    // Integer groupNumber, Pageable page);

    @Query("select l from TrialLibrary l where l.examId=?1 and l.subjectCode=?2 and l.groupNumber=?3 "
            + "and l.markerId is null ")
    List<TrialLibrary> findUnMarked(Integer examId, String subjectCode, Integer groupNumber, Pageable page);

    @Query("select count(l) from TrialLibrary l where l.examId=?1 and l.subjectCode=?2 and l.groupNumber=?3 "
            + "and l.markerId is not null ")
    long countMarked(Integer examId, String subjectCode, Integer groupNumber);

    // @Query("select l from TrialLibrary l where l.studentId=?1 order by l.groupNumber ")
    // List<TrialLibrary> findByStudentId(Integer studentId);

    // List<TrialLibrary> findByStudentIdAndGroupNumber(Integer studentId,
    // Integer groupNumber);

    @Query("select count(*) from TrialLibrary f where f.examId=?1 and f.subjectCode=?2 and f.groupNumber=?3")
    long countByExamIdAndSubjectCodeAndGroupNumber(Integer examId, String subjectCode, Integer groupNumber);

    // @Query("select count(*) from TrialLibrary f where f.studentId=?1 and f.groupNumber=?2")
    // long countByStudentIdAndGroupNumber(Integer studentId, Integer
    // groupNumber);

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

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

    @Query("select l from TrialLibrary l where l.examId=?1 and l.subjectCode=?2 and l.groupNumber=?3 "
            + "and l.markerId=?4 and l.markerScore!=-1  ")
    List<TrialLibrary> findByExamIdAndSubjectCodeAndGroupNumberAndMarkerId(Integer examId, String subjectCode,
            Integer groupNumber, Integer markerId, Pageable page);

    @Query("select l from TrialLibrary l where l.examId=?1 and l.subjectCode=?2 and l.groupNumber=?3 "
            + "and l.markerId=?4 and l.secretNumber like ?5 and l.markerScore!=-1  ")
    List<TrialLibrary> findByExamIdAndSubjectCodeAndGroupNumberAndMarkerIdAndSecretNumber(Integer examId,
            String subjectCode, Integer groupNumber, Integer markerId, String secretNumber, Pageable query);

    @Query("select l from TrialLibrary l where l.examId=?1 and l.subjectCode=?2 and l.groupNumber=?3 "
            + "and l.markerId=?4 and l.markerScore=?5 and l.markerScore!=-1  ")
    List<TrialLibrary> findByExamIdAndSubjectCodeAndGroupNumberAndMarkerIdAndMarkerScore(Integer examId,
            String subjectCode, Integer groupNumber, Integer markerId, Double markerScore, Pageable query);

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

    @Modifying(clearAutomatically = true)
    @Query("update TrialLibrary m set m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null "
            + "where m.markerId=?1 ")
    void resetByMarkerId(Integer markerId);

    @Modifying(clearAutomatically = true)
    @Query("update TrialLibrary m set m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null "
            + "where m.id=?1")
    void resetById(Integer id);

    @Query("select f from TrialLibrary f where f.markerId=?1")
    List<TrialLibrary> findByMarkerId(Integer markerId);

    @Modifying(clearAutomatically = true)
    @Query("update TrialLibrary m set m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null "
            + " where m.examId=?1 and m.subjectCode=?2 and m.groupNumber=?3")
    void resetByExamIdAndSubjectCodeAndGroupNumber(Integer examId, String subjectCode, Integer number);

}
