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

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

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

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

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

    @Query("select s from ExamSubject s where s.pk.examId=?1 and s.status in ?2")
    public List<ExamSubject> findByExamIdAndStatus(int examId, Set<ExamSubjectStatus> status);

    @Query("select s from ExamSubject s where s.status in ?1")
    public List<ExamSubject> findByStatus(Set<ExamSubjectStatus> status);

    @Query("select count(s) from ExamSubject s where s.pk.examId=?1 and s.status in ?2")
    public long countByExamIdAndStatus(int examId, Set<ExamSubjectStatus> status);

    @Modifying
    @Query("update ExamSubject s set s.status=?2 where s.pk.examId=?1")
    public void updateStatusByExamId(int examId, ExamSubjectStatus status);

    @Modifying
    @Query("update ExamSubject s set s.totalScore=s.objectiveScore+s.subjectiveScore where s.pk.examId=?1")
    public void updateTotalScoreByExamId(int examId);

    @Modifying
    @Query("update ExamSubject s set s.totalScore=s.objectiveScore+s.subjectiveScore where s.pk.examId=?1 and s.pk.code=?2")
    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=(select count(s) from ExamStudent es where es.examId=?1 "
            + "and es.subjectCode=?2 and es.upload=true and es.absent=false and es.breach=false) "
            + "where s.pk.examId=?1 and s.pk.code=?2")
    public void updateUploadByExamIdAndCode(Integer examId, String subjectCode);

    @Modifying
    @Query("update ExamSubject s set s.uploadCount=(select count(s) from ExamStudent es where es.examId=?1 "
            + "and es.subjectCode=s.pk.code and es.upload=true and es.absent=false and es.breach=false) "
            + "where s.pk.examId=?1")
    public void updateUploadByExamId(Integer examId);
}
