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.SelectiveGroup;
import cn.com.qmth.stmms.biz.exam.model.SelectiveGroupPK;
import cn.com.qmth.stmms.common.enums.ScorePolicy;

public interface SelectiveGroupDao extends PagingAndSortingRepository<SelectiveGroup, SelectiveGroupPK>,
        JpaSpecificationExecutor<SelectiveGroup> {

    @Query("select q from SelectiveGroup q where q.pk.examId=?1 and q.pk.subjectCode=?2 and q.pk.mainNumber=?3")
    SelectiveGroup findOne(Integer examId, String subjectCode, Integer mainNumber);

    @Modifying(clearAutomatically = true)
    @Query("delete from SelectiveGroup q where q.pk.examId=?1 and q.pk.subjectCode=?2 and q.selectiveIndex=?3 ")
    void deleteByExamIdAndSubjectCodeAndSelectiveIndex(Integer examId, String subjectCode, int selectiveIndex);

    @Query("select q from SelectiveGroup q where q.pk.examId=?1 and q.pk.subjectCode=?2 order by q.pk.mainNumber")
    List<SelectiveGroup> findByExamIdAndSubjectCode(Integer examId, String subjectCode);

    long countByPkExamIdAndPkSubjectCode(int examId, String subjectCode);

    @Modifying(clearAutomatically = true)
    @Query("update SelectiveGroup q set q.scorePolicy=?4 where q.pk.examId=?1 and q.pk.subjectCode=?2 and q.selectiveIndex=?3 ")
    void updateScorePolicy(int examId, String subjectCode, Integer selectiveIndex, ScorePolicy scorePolicy);

}
