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

import java.util.List;

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

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

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

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

    @Query("select sum(q.totalScore) from ExamQuestion q where q.pk.examId=?1 and q.pk.subjectCode=?2 and q.pk.objective=?3")
    public Double sumScoreByExamIdAndSubjectCodeAndObjective(int examId, String subjectCode, boolean objective);

    @Modifying
    @Query("delete from ExamQuestion q where q.pk.examId=?1")
    public void deleteByExamId(int examId);

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

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

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

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

}
