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

import java.util.List;

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

public interface ExamQuestionDao extends JpaRepository<ExamQuestion, Integer>, JpaSpecificationExecutor<ExamQuestion> {

    @Query("select q from ExamQuestion q where q.examId=?1 and  q.subjectCode=?2 and q.objective=?3 "
            + "order by q.paperType, q.mainNumber, q.subNumber")
    public List<ExamQuestion> findByExamIdAndSubjectCodeAndObjective(Integer examId, String subjectCode,
            boolean objective);

    @Query("select count(q) from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 and q.mainNumber=?4")
    public long countByExamIdAndSubjectCodeAndObjectiveAndMainNumber(Integer examId, String subjectCode,
            boolean objective, Integer mainNumber);

    // @Query("select q from ExamQuestion q where q.examId=?1 and
    // q.subjectCode=?2 and q.objective=?3 "
    // + "and q.paperType=?4 order by q.mainNumber, q.subNumber")
    public List<ExamQuestion> findByExamIdAndSubjectCodeAndObjectiveAndPaperType(Integer examId, String subjectCode,
            boolean objective, String paperType, Sort sort);

    @Query("select q from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3"
            + "  and q.mainNumber=?4 order by q.subNumber")
    public List<ExamQuestion> findByExamIdAndSubjectCodeAndObjectiveAndMainNumber(Integer examId, String subjectCode,
            boolean objective, Integer mainNumber);

    @Query("select q from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3"
            + "  and q.groupNumber=?4 order by q.mainNumber, q.subNumber")
    public List<ExamQuestion> findByExamIdAndSubjectCodeAndObjectiveAndGroupNumber(Integer examId, String subjectCode,
            boolean objective, Integer groupNumber);

    // @Query("select q from ExamQuestion q where q.examId=?1 and
    // q.subjectCode=?2 and q.objective=?3 "
    // + "and q.paperType=?4 and q.mainNumber=?5 order by q.subNumber")
    public List<ExamQuestion> findByExamIdAndSubjectCodeAndObjectiveAndPaperTypeAndMainNumber(Integer examId,
            String subjectCode, boolean objective, String paperType, Integer mainNumber, Sort sort);

    @Modifying
    @Query("update ExamQuestion q set q.mainTitle=?5 "
            + "where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 and q.mainNumber=?4")
    public void updateMainTitle(Integer examId, String subjectCode, boolean objective, Integer mainNumber,
            String mainTitle);

    @Modifying
    @Query("delete from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3")
    public void deleteByExamIdAndSubjectCodeAndObjective(Integer examId, String subjectCode, boolean objective);

    @Modifying
    @Query("delete from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 and q.groupNumber=?4")
    public void deleteByExamIdAndSubjectCodeAndObjectiveAndGroupNumber(Integer examId, String subjectCode,
            boolean objective, Integer groupNumber);

    @Query("select DISTINCT q.paperType from ExamQuestion q where q.examId=?1 and  q.subjectCode=?2 and q.objective=?3 and q.paperType is not null "
            + "order by q.paperType")
    public List<String> getPaperType(int examId, String subjectCode, boolean objective);

}
