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

import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.model.ExamSubjectPK;

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

public interface ExamSubjectDao extends PagingAndSortingRepository<ExamSubject, ExamSubjectPK>,
        JpaSpecificationExecutor<ExamSubject> {

    @Query("select s from ExamSubject s where s.pk.examId=?1")
    public List<ExamSubject> findByExamId(int examId);

    @Query("select s from ExamSubject s where s.pk.examId=?1 and s.pk.code in (?2)")
    public List<ExamSubject> findByExamIdAndSubjectCodeIn(int examId, Set<String> codes);

    @Query("select s.level from ExamSubject s where s.pk.examId=?1 and s.level is not null group by s.level")
    public List<String> findDistinctLevel(int examId);

    @Query("select s.category from ExamSubject s where s.pk.examId=?1 and s.category is not null group by s.category")
    public List<String> findDistinctCategory(int examId);

    @Query("select count(s) from ExamSubject s where s.pk.examId=?1")
    public long countByExamId(int examId);

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

    @Query("select s from ExamSubject s where s.pk.examId=?1 and s.uploadCount>?2")
    public List<ExamSubject> findByExamIdAndUploadCountGt(int examId, int uploadCount);

    @Modifying
    @Query(value = "update eb_exam_subject s set s.total_score=cast(s.objective_score as decimal(10,3))+cast(s.subjective_score as decimal(10,3)) where s.exam_id=?1", nativeQuery = true)
    public void updateTotalScoreByExamId(int examId);

    @Modifying
    @Query(value = "update eb_exam_subject s set s.total_score=cast(s.objective_score as decimal(10,3))+cast(s.subjective_score as decimal(10,3)) where s.exam_id=?1 and s.code=?2", nativeQuery = true)
    public void updateTotalScoreByExamIdAndCode(int examId, String code);

    @Modifying
    @Query("update ExamSubject s set s.objectiveScore=?3 where s.pk.examId=?1 and s.pk.code=?2")
    public void updateObjectiveScore(int examId, String code, double score);

    @Modifying
    @Query("update ExamSubject s set s.subjectiveScore=?3 where s.pk.examId=?1 and s.pk.code=?2")
    public void updateSubjectiveScore(int examId, String code, double score);

    @Modifying
    @Query("delete ExamSubject s where s.pk.examId=?1")
    void deleteByExamId(int examId);

    @Modifying
    @Query("update ExamSubject s set s.uploadCount=?3 where s.pk.examId=?1 and s.pk.code=?2")
    public void updateUploadCount(Integer examId, String subjectCode, int count);

    @Query("select s from ExamSubject s where s.pk.examId=?1 and s.subjectiveScore>?2")
    public List<ExamSubject> listSubjectiveScore(int examId, double subjectiveScore);

    @Modifying
    @Query("update ExamSubject s set s.sliceConfig=?3 where s.pk.examId=?1 and s.pk.code=?2")
    public void updateSliceConfig(Integer examId, String subjectCode, String configString);

    @Modifying
    @Query("update ExamSubject s set s.trialCount=?3 where s.pk.examId=?1 and s.pk.code=?2")
    public void updateTrialCount(int examId, String subjectCode, int trialCount);

    @Modifying
    @Query("update ExamSubject s set s.inspectRound=s.inspectRound+1 where s.pk.examId=?1 and s.pk.code=?2")
    public void nextInspectRound(int examId, String subjectCode);

    @Modifying
    @Query("update ExamSubject s set s.selective=?3 where s.pk.examId=?1 and s.pk.code=?2")
    public void updateSelective(int examId, String subjectCode, boolean selective);

    @Query("select DISTINCT(s.totalScore) from ExamSubject s where s.pk.examId=?1 order by s.totalScore")
    public List<Double> distinctTotalScore(int examId);

    @Modifying
    @Query("update ExamSubject s set s.inspectRound=1 where s.pk.examId=?1 and s.pk.code=?2")
	public void resetInspect(int examId, String subjectCode);

}
