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

import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;

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 java.util.List;
import java.util.Set;

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 q from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 "
    // +
    // " group by q.mainNumber, q.subNumber order by q.mainNumber, q.subNumber")
    // public List<ExamQuestion>
    // findByExamIdAndSubjectCodeAndObjectiveGroupByNumber(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 count(q) from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 and q.mainNumber=?4 and q.subNumber=?5")
    public long countByExamIdAndSubjectCodeAndObjectiveAndMainNumberAndSubNumber(Integer examId, String subjectCode,
            boolean objective, Integer mainNumber, String subNumber);

    // @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.groupNumber!=?4 order by q.mainNumber, q.subNumber")
    public List<ExamQuestion> findByExamIdAndSubjectCodeAndObjectiveAndGroupNumberNotEqual(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);

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

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

    public ExamQuestion findByExamIdAndSubjectCodeAndObjectiveAndMainNumberAndSubNumber(Integer examId,
            String subjectCode, boolean objective, Integer mainNumber, String subNumber);

    @Query(value = "select sum(cast(q.total_score as decimal(10,3))) from eb_exam_question q where q.exam_id=?1 and q.subject_code=?2 and q.is_objective=?3 ", nativeQuery = true)
    public Double sumTotalScore(int examId, String subjectCode, boolean objective);

    @Modifying
    @Query("update ExamQuestion q set q.groupNumber = null"
            + " where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 and q.groupNumber=?4")
    public void resetByExamIdAndSubjectCodeAndObjectiveAndGroupNumber(Integer examId, String subjectCode,
            boolean objective, Integer number);

    @Query("select count(q) from ExamQuestion q where q.examId=?1 and q.subjectCode=?2 and q.objective=?3 and q.groupNumber is null ")
    public long countByExamIdAndSubjectAndObjectiveAndGroupNumberIsNull(int examId, String subjectCode,
            boolean objective);

    @Query("select distinct q.subjectCode from ExamQuestion q where q.examId=?1 and q.objective=?2 and q.groupNumber is null  ")
    public Set<String> FindSubjectCodeByExamIdAndObjectiveAndGroupNumberIsNull(int examId, boolean objective);

    @Query(value = "select sum(cast(q.total_score as decimal(10,3))) from eb_exam_question q where q.exam_id=?1 and q.subject_code=?2 and q.is_objective=?3 and q.group_number=?4 ", nativeQuery = true)
    public Double sumTotalScoreByGroupNumber(int examId, String subjectCode, boolean objective, Integer groupNumber);

}
