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.AnswerCardSubject;
import cn.com.qmth.stmms.common.enums.CardSource;

public interface AnswerCardSubjectDao extends PagingAndSortingRepository<AnswerCardSubject, Integer>,
        JpaSpecificationExecutor<AnswerCardSubject> {

    @Query("select s from AnswerCardSubject s where s.examId=?1 and s.cardNumber=?2 and s.subjectCode=?3 ")
    AnswerCardSubject findByExamIdAndCardNumberAndSubjectCode(Integer examId, Integer number, String subjectCode);

    @Query("select q from AnswerCardSubject q where q.examId=?1 and q.cardNumber=?2")
    List<AnswerCardSubject> findByExamIdAndCardNumber(Integer examId, Integer number);

    @Modifying
    @Query("delete AnswerCardSubject s where s.examId=?1 and s.cardNumber=?2 ")
    void deleteByExamIdAndCardNumber(Integer examId, Integer number);

    AnswerCardSubject findByExamIdAndSubjectCode(Integer examId, String subjectCode);

    @Query(value = "select count(s.id) from eb_answer_card e 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=?4 and e.code != ?3 and s.subject_code=?2", nativeQuery = true)
    int countByExamIdAndSubjectCodeAndNotAnswerCode(int examId, String subjectCode, String code, CardSource sourse);

}
