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

import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.common.enums.MarkStatus;

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

public interface MarkerDao extends PagingAndSortingRepository<Marker, Integer>, JpaSpecificationExecutor<Marker> {

    @Query("select count(m) from Marker m where m.examId=?1")
    public long countByExamId(int examId);

    @Query("select count(m) from Marker m where m.examId=?1 and m.subjectCode=?2 and m.groupNumber=?3")
    public long countByExamIdAndSubjectCodeAndGroupNumber(int examId, String subjectCode, int number);

    public List<Marker> findByExamIdAndSubjectCodeAndGroupNumber(int examId, String subjectCode, int number,
            Pageable page);

    @Modifying
    @Query("delete from Marker m where m.examId=?1 and m.subjectCode=?2 and m.groupNumber=?3")
    public void deleteByExamIdAndSubjectCodeAndGroupNumber(int examId, String subjectCode, int number);

    @Modifying
    @Query("delete from Marker m where m.examId=?1 and m.subjectCode=?2")
    public void deleteByExamIdAndSubjectCode(int examId, String subjectCode);

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

    @Modifying
    @Query("update Marker m set m.finishCount=null, m.validCount=null, m.avgSpeed=null, m.avgScore=null, m.stdevScore=null "
            + "where m.id=?1")
    public void resetById(Integer id);

    @Modifying
    @Query("update Marker m set m.finishCount=?2, m.validCount=?3, m.avgSpeed=?4, m.avgScore=?5, m.stdevScore=?6 "
            + "where m.id=?1")
    public void updateQualityById(Integer id, int finishCount, int validCount, double avgSpeed, double avgScore,
            double stdevScore);

    @Modifying
    @Query("update Marker m set m.finishCount=null, m.validCount=null, m.avgSpeed=null, m.avgScore=null, m.stdevScore=null "
            + "where m.examId=?1 and m.subjectCode=?2 and m.groupNumber=?3")
    public void resetByExamIdAndSubjectCodeAndGroupNumber(Integer examId, String subjectCode, Integer groupNumber);

    @Query(value = "select e.* from eb_marker e,m_library m where 1=1 and e.id = m.marker_id and m.student_id =?1", nativeQuery = true)
    public List<Marker> findByStudentId(int studentId);

    public Marker findByExamIdAndSubjectCodeAndGroupNumberAndUserId(int examId, String subjectCode, Integer number,
            Integer userId);

    @Query("select m from Marker m where m.examId=?1 and m.userId=?2 and exists "
            + "(select g.pk.number from MarkGroup g where g.pk.examId=?1 and g.pk.subjectCode=m.subjectCode and g.status in (?3)) "
            + "order by m.subjectCode, m.groupNumber")
    public List<Marker> findByExamIdAndUserIdAndGroupStatus(Integer examId, Integer userId, MarkStatus... groupStatus);

    @Query("select count(m) from Marker m where m.userId=?1")
    public long countByUserId(Integer userId);

    public List<Marker> findByExamIdAndSubjectCodeAndUserIdAndEnable(Integer examId, String subjectCode,
            Integer userId, boolean enable);

    @Modifying
    @Query(value = "update eb_marker m set m.reject_count=?2 where m.id=?1", nativeQuery = true)
    public void updateRejectCountById(Integer markerId, Integer rejectCount);

    @Modifying
    @Query(value = "update Marker m set m.enable=?2 where m.userId=?1")
    public void updateEnableByUserId(Integer userId, boolean enable);

    public List<Marker> findByExamIdAndSubjectCode(Integer examId, String subjectCode);

    @Modifying
    @Query(value = "update eb_marker m set m.reject_count=ifnull(m.reject_count,0)+1 where m.id=?1", nativeQuery = true)
	public void addRejectCountById(Integer markerId);

}
