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

import java.util.List;
import java.util.Set;

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 cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.model.ExamSubjectPK;

public interface ExamSubjectDao
        extends JpaRepository<ExamSubject, ExamSubjectPK>, JpaSpecificationExecutor<ExamSubject> {
	
	@Modifying
    @Query(value = "delete es from eb_exam_subject es "
    		+ "where es.exam_id =?1 "
    		+ "and not exists (select s.id from eb_exam_student s where s.exam_id=es.exam_id and s.subject_code=es.code) "
    		+ "and not exists (select q.id from eb_exam_question q where q.exam_id=es.exam_id and q.subject_code=es.code)", nativeQuery = true)
    public void deleteNoStudentByExamId(int examId);
	
    @Modifying
    @Query(value = "update eb_exam_subject es "
    		+ "set es.upload_count=(select count(*) from eb_exam_student s "
    		+ "where s.exam_id=es.exam_id and s.subject_code=es.code and s.is_upload=1 and s.is_absent=0 and s.is_breach=0) "
    		+ "where es.exam_id=?1", nativeQuery = true)
    public void updateUploadCountByExamId(int examId);

    @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);

    @Query("select s from ExamSubject s where s.pk.examId=?1 and s.objectiveScore>?2")
    List<ExamSubject> listObjectiveScore(Integer examId, double objectiveScoreGt);
    
    @Query(value = "select count(1) from eb_exam_subject t "
    		+ "where t.exam_id =?1  and t.total_score=0", nativeQuery = true)
    long getZeroScoreCount(Integer examId);

    @Query(value = "select count(*) from eb_exam_subject t left join eb_mark_group f on t.exam_id=f.exam_id and t.code=f.subject_code "
    		+ "where t.exam_id =?1  and t.subjective_score is not null and t.subjective_score>0 and f.exam_id is null", nativeQuery = true)
	public long getNoGroupCount(Integer id);
}
