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

public interface ExamSubjectPaperDao
        extends PagingAndSortingRepository<ExamSubjectPaper, Integer>, JpaSpecificationExecutor<ExamSubjectPaper> {

    @Query(value = "select GROUP_CONCAT(b.main_number SEPARATOR ';') mainNumber from ( select p.main_number from eb_exam_subject_paper p where 1=1"
            + " and p.exam_id = ?1 and p.`code` = ?2 GROUP BY p.main_number) b", nativeQuery = true)
    public List<String> getMainNumbersByExamIdAndCode(int examId, String subjectCode);

    @Query(value = "select GROUP_CONCAT(b.main_number SEPARATOR ';') MainNumber from ( select p.main_number from eb_exam_subject_paper p where 1=1"
            + " and p.exam_id = ?1 and p.`code` = ?2 and p.is_objective = ?3) b", nativeQuery = true)
    public List<String> getMainNumbersByExamIdAndCodeAndType(int examId, String subjectCode, int objective);

    @Modifying
    @Query("delete from ExamSubjectPaper q where q.examId=?1 and q.code=?2 and q.objective=?3")
    public void deleteByExamAndCodeAndObjective(int examId, String subjectCode, int objective);

    @Query("select count(*) from ExamSubjectPaper s where s.examId=?1 and s.code=?2 and s.objective=?3")
    Long getExamSubjectPaperCountBy(Integer examId, String subjectCode, int objective);
}
