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.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import cn.com.qmth.stmms.biz.exam.model.AnswerCard;
import cn.com.qmth.stmms.biz.exam.model.AnswerCardPK;
import cn.com.qmth.stmms.common.enums.CardSource;

public interface AnswerCardDao extends PagingAndSortingRepository<AnswerCard, AnswerCardPK>,
        JpaSpecificationExecutor<AnswerCard> {

    @Query("select s from AnswerCard s where s.pk.examId=?1 and s.pk.number=?2")
    AnswerCard findByExamIdAndNumber(Integer examId, Integer number);

    @Query("select q from AnswerCard q where q.pk.examId=?1 order by q.pk.number")
    List<AnswerCard> findByExamId(Integer examId);

    @Query(value = "select e.* from eb_answer_card e left join eb_answer_card_subject s on e.exam_id=s.exam_id and e.number=s.card_number where e.exam_id = ?1 and e.source=?3 and e.code is null and s.subject_code=?2", nativeQuery = true)
    AnswerCard findByExamIdAndSubjectCodeAndCodeIsNull(Integer examId, String subjectCode, CardSource source);

    @Query("select s from AnswerCard s where s.pk.examId=?1 and s.code=?2")
    AnswerCard findByExamIdAndCode(Integer examId, String code);

}
