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

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import cn.com.qmth.stmms.biz.distributpaper.model.FormallyLibrary;
import cn.com.qmth.stmms.common.enums.LibraryStatus;

public interface FormallyLibraryDao extends PagingAndSortingRepository<FormallyLibrary, Integer>,
        JpaSpecificationExecutor<FormallyLibrary> {

    List<FormallyLibrary> findByExamIdAndBlockId(Integer examId, Integer blockId);

    List<FormallyLibrary> findByExamIdAndBlockIdAndStatus(Integer examId, Integer blockId, LibraryStatus status);

    List<FormallyLibrary> findByExamIdAndBlockIdAndStatusAndIsChecked(Integer examId, Integer blockId,
            LibraryStatus status, boolean isChecked);

    List<FormallyLibrary> findByExamIdAndBlockIdAndGetter(Integer examId, Integer blockId, Integer getter);

    List<FormallyLibrary> findByBlockIdAndSecretNo(Integer blockId, Integer secretNo);

    List<FormallyLibrary> findByExamIdAndBlockIdAndGetterAndStatus(int examId, int blockId, int getter,
            LibraryStatus status);

    @Query("select count(*) from FormallyLibrary f where f.examId=?1 and f.subjectCode=?2 and f.blockId=?3")
    Long getCountByExamIdAndSubjectCodeAndBlockId(Integer examId, String subjectCode, Integer blockId);

    List<FormallyLibrary> findByExamIdAndSubjectCodeAndStatus(Integer examId, String subjectCode, LibraryStatus status);

    @Query("select count(*) from FormallyLibrary f where f.examId=?1 and f.subjectCode=?2 and f.blockId=?3 and f.getter=?4 and f.status=?5")
    Long endingCount(Integer examId, String subjectCode, Integer blockId, Integer getterId, LibraryStatus status);

    List<FormallyLibrary> findByExamIdAndSubjectCodeAndBlockIdAndSecretNoAndStatus(int examId, String subjectCode, int blockId, int secretNo, LibraryStatus status);

    List<FormallyLibrary> findByExamIdAndSubjectCodeAndBlockId(int examId, String subjectCode, int blockId);

    @Query("select count(*) from FormallyLibrary f where f.examId=?1 and f.subjectCode=?2 and f.blockId=?3 and f.status=?4")
    Long getCountByExamIdAndSubjectCodeAndBlockIdAndStatus(Integer examId, String subjectCode, Integer blockId,
            LibraryStatus status);

    @Query("select f from FormallyLibrary f where f.examId=?1 and f.subjectCode=?2 and f.blockId=?3 and f.status=?4 and f.getter=?5")
    List<FormallyLibrary> endTask(Integer examId, String subjectCode, Integer blockId, LibraryStatus status, Integer id1);

    @Modifying
    @Query("update FormallyLibrary f set f.status=?2 where f.id=?1")
    void updateStatusById(int id, LibraryStatus status);

}
