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

import java.util.List;

import org.springframework.data.domain.Pageable;
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.mark.model.MarkLibrary;
import cn.com.qmth.stmms.common.enums.LibraryStatus;

public interface MarkLibraryDao
        extends PagingAndSortingRepository<MarkLibrary, Integer>, JpaSpecificationExecutor<MarkLibrary> {

    List<MarkLibrary> findByExamId(int examId, Pageable page);

    List<MarkLibrary> findByExamIdAndSubjectCode(int examId, String subjectCode, Pageable page);

    List<MarkLibrary> findByExamIdAndStatus(int examId, LibraryStatus status, Pageable page);

    List<MarkLibrary> findByExamIdAndSubjectCodeAndStatus(int examId, String subjectCode, LibraryStatus status,
            Pageable page);

    List<MarkLibrary> findByExamIdAndMarkerId(int examId, int markerId, Pageable page);

    List<MarkLibrary> findByExamIdAndExamNumber(int examId, String examNumber);

    List<MarkLibrary> findByStudentId(int studentId);

    @Query("select distinct l.campusId from MarkLibrary l where l.examId=?1 and l.tags is not null")
    List<Integer> findTagCampusId(int examId);

    @Query("select distinct l.subjectCode from MarkLibrary l where l.examId=?1 and l.tags is not null")
    List<String> findTagSubjectCode(int examId);

    @Modifying
    @Query("update MarkLibrary m set m.status=?2, m.tags=null, m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null where m.examId=?1")
    void resetByExamId(int examId, LibraryStatus status);

    @Modifying
    @Query("update MarkLibrary m set m.status=?3, m.tags=null, m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null where m.examId=?1 and m.subjectCode=?2")
    void resetByExamIdAndSubjectCode(int examId, String subjectCode, LibraryStatus status);

    @Modifying
    @Query("update MarkLibrary m set m.status=?2, m.tags=null, m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null where m.markerId=?1")
    void resetByMarkerId(int markerId, LibraryStatus status);

    @Modifying
    @Query("update MarkLibrary m set m.status=?2, m.tags=null, m.markerId=null, m.markerTime=null, m.markerScore=null, m.markerScoreList=null where m.id=?1")
    void resetById(int id, LibraryStatus status);

    @Query("select count(*) from MarkLibrary f where f.examId=?1")
    long countByExamId(int examId);

    @Query("select count(*) from MarkLibrary f where f.examId=?1 and f.subjectCode=?2")
    long countByExamIdAndSubjectCode(int examId, String subjectCode);

    @Query("select count(*) from MarkLibrary f where f.examId=?1 and f.subjectCode=?2 and f.status=?3")
    long countByExamIdAndSubjectCodeAndStatus(int examId, String subjectCode, LibraryStatus status);

    @Query("select f.markerId, count(*) as markerCount from MarkLibrary f where f.examId=?1 and f.status=?2 group by f.markerId")
    List<Object[]> countByMarkerAndStatus(int examId, LibraryStatus status);

    @Modifying
    @Query("delete MarkLibrary m where m.examId=?1")
    void deleteByExamId(int examId);

    @Query(value = "select max(f.markerScore),min(f.markerScore),avg(f.markerScore),max(f.markerDuration),min(f.markerDuration),avg(f.markerDuration) from MarkLibrary f where f.markerId = ?1 and f.subjectCode = ?2 ")
    List<Object[]> getMarkerInfo(int markerId, String subjectCode);

    @Query(value = "select f.markerScore from MarkLibrary f where f.markerId = ?1 and f.subjectCode = ?2 ")
    List<Double> getAllScore(int markerId, String subjectCode);

    @Query(value = "select f.subjectCode from MarkLibrary f where f.markerId = ?1 and f.status = ?2 GROUP BY f.subjectCode")
    List<String> findCodeListByMidGBCode(int markerId, LibraryStatus status);

    @Query("select count(*) from MarkLibrary f where f.markerId=?1 and f.status=?2")
    long countByMidAndStatus(int markerId, LibraryStatus status);

    @Query("select count(*) from MarkLibrary f where f.markerId=?1 and f.subjectCode = ?2 and f.status=?3")
    long countByMidAndSubjectCodeStatus(int markerId, String subjectCode, LibraryStatus status);

    @Query(value = "select max(f.markerScore),min(f.markerScore),avg(f.markerScore) from MarkLibrary f where f.markerId = ?1 and f.status = ?2 ")
    List<Object[]> getMarkerInfo(int markerId, LibraryStatus status);

}
