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

public interface ExamQuestionDao extends PagingAndSortingRepository<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 count(q) from ExamQuestion q where q.pk.examId=?1 and q.pk.subjectCode=?2")
    public long countByExamIdAndSubjectCode(int examId, String subjectCode);

    @Query("select count(q) from ExamQuestion q where q.pk.examId=?1 and q.pk.subjectCode=?2 and q.pk.objective=?3")
    public long countByExamIdAndSubjectCodeAndObjective(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 mianNumber);
    

    @Query(value="SELECT case  when sum(q.total_score) != 'null' then sum(q.total_score) else 0 end as score from eb_exam_question q where 1=1 and q.exam_id =?1 and q.subject_code=?2",nativeQuery = true)
    public double scoreByExamIdAndSubjectCode(int examId, String subjectCode);
    
    @Query(value="SELECT case  when sum(q.total_score) != 'null' then sum(q.total_score) else 0 end as score from eb_exam_question q where 1=1 and q.exam_id =?1 and q.subject_code=?2 and q.is_objective=?3",nativeQuery = true)
    public double scoreByExamIdAndSubjectCodeAndObjective(int examId, String subjectCode,boolean objective);
    
    @Query(value="SELECT case  when sum(q.total_score) != 'null' then sum(q.total_score) else 0 end as score from eb_exam_question q where 1=1 and q.exam_id =?1 and q.subject_code=?2 and q.main_number=?3 and q.is_objective=?4",nativeQuery = true)
    public double scoreByExamIdAndSubjectCodeAndMainNumberAndObjective(int examId, String subjectCode,int mainNumber,boolean objective);
}
