package cn.com.qmth.stmms.biz.exam.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.exam.model.SubjectiveBlock;

public interface SubjectiveBlockDao extends PagingAndSortingRepository<SubjectiveBlock, Integer>,
        JpaSpecificationExecutor<SubjectiveBlock> {

    @Query("select count(o) from SubjectiveBlock o where o.examId=?1 and o.subjectCode=?2")
    public long countByExamIdAndSubjectCode(int examId, String subjectCode);

    @Query("select count(ms) from SubjectiveBlock ms where ms.id in ( select m.id from SubjectiveBlock m where m.examId=?1 group by m.subjectCode)")
    public long countSubjectByExamId(int examId);

    @Query("select o from SubjectiveBlock o where o.examId=?1 and o.subjectCode=?2 order by o.number")
    public List<SubjectiveBlock> findByExamIdAndSubjectCode(int examId, String subjectCode);

    public List<SubjectiveBlock> findByExamId(int examId);

    public List<SubjectiveBlock> findByExamIdAndSubjectCodeAndNumber(int examId, String subjectCode, int number);

    @Modifying
    @Query("delete from SubjectiveBlock s where s.examId=?1 and s.subjectCode=?2")
    public void deleteByExamIdAndSubjectCode(int examId, String subjectCode);

}
