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

import java.util.List;

import org.springframework.data.domain.Sort;
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.ObjectiveBlock;

public interface ObjectiveBlockDao extends PagingAndSortingRepository<ObjectiveBlock, Integer>,
        JpaSpecificationExecutor<ObjectiveBlock> {

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

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

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

    public List<ObjectiveBlock> findByExamIdAndSubjectCode(int examId, String subjectCode, Sort sort);

    public List<ObjectiveBlock> findByExamId(int examId);

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