package com.ssj.dao.sys.opentest;

import com.ssj.bean.sys.opentest.OpenTestQuestion;
import com.ssj.bean.sys.opentest.OpenTestResult;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Mount Liu
 * @date 2020/2/11
 */
@Repository
public interface OpenTestResultDao extends JpaRepository<OpenTestResult, String> {

    /**
     * @param userId
     * @return
     */
    List<OpenTestResult> findByUserIdAndIsDeleteOrderByCreateTimeDesc(String userId, int isDelete);

    List<OpenTestResult> findByUserIdAndTypeAndIsDeleteOrderByCreateTimeDesc(String userId, int type, int isDelete);


    /**
     * 查找指定结果
     * @param userId
     * @param grade
     * @param subject
     * @param version
     * @return
     */
    //OpenTestResult findTopByUserIdAndGradeAndSubjectAndVersionAndTypeAndIsDelete(String userId, String grade, String subject, String version, int type, int isDelete);
    OpenTestResult findTopByUserIdAndGradeAndSubjectAndVersionAndSemesterAndTypeAndIsDelete(String userId, String grade, String subject, String version, String semester, int type, int isDelete);

    /**
     * 查找指定结果
     *
     * @param userId
     * @param grade
     * @param subject
     * @param version
     * @param unit
     * @param isDelete
     * @return
     */
    //OpenTestResult findTopByUserIdAndGradeAndSubjectAndVersionAndUnitAndTypeAndIsDelete(String userId, String grade, String subject, String version, String unit, int type, int isDelete);

    OpenTestResult findTopByUserIdAndGradeAndSubjectAndVersionAndUnitAndSemesterAndTypeAndIsDelete(String userId, String grade, String subject, String version, String unit, String semester, int type, int isDelete);


    /**
     * 查找没有版本的指定结果
     *
     * @param userId
     * @param grade
     * @param subject
     * @return
     */
    OpenTestResult findTopByUserIdAndGradeAndSubjectAndIsDelete(String userId, String grade, String subject, int isDelete);

    OpenTestResult findTopByUserIdAndGradeAndSubjectAndVersionAndIsDelete(String userId, String grade, String subject, String version, int isDelete);
    /**
     * 获取超过的人数数量
     * @param grade
     * @param subject
     * @param score
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT sum(case when score = ?3 and create_time > ?4 then 0 else 1 end) total\n" +
            "from tb_test_result \n" +
            "where grade=?1\n" +
            "and subject=?2 \n" +
            "and score>=?3\n" +
            "and is_delete=0")
    int getExceedNum(String grade, String subject, int score, String createTime);

    @Query(nativeQuery = true, value = "SELECT sum(case when score = ?3 and create_time > ?4 then 0 else 1 end) total\n" +
            "from tb_test_result \n" +
            "where grade=?1\n" +
            "and subject=?2 \n" +
            "and score>=?3\n" +
            "and version=?5\n" +
            "and is_delete=0")
    int getExceedNum(String grade, String subject, int score, String createTime, String version);

    @Query(nativeQuery = true, value = "SELECT sum(case when score = ?3 and create_time > ?4 then 0 else 1 end) total\n" +
            "from tb_test_result \n" +
            "where grade=?1\n" +
            "and subject=?2 \n" +
            "and score>=?3\n" +
            "and version=?5\n" +
            "and type=?6\n" +
            "and is_delete=0")
    int getExceedNum(String grade, String subject, int score, String createTime,String version,int type);

    @Query(nativeQuery = true, value = "SELECT sum(case when score = ?3 and create_time > ?4 then 0 else 1 end) total\n" +
            "from tb_test_result \n" +
            "where grade=?1\n" +
            "and subject=?2 \n" +
            "and score>=?3\n" +
            "and version=?5\n" +
            "and unit=?6\n" +
            "and type=?7\n" +
            "and is_delete=0")
    int getExceedNum(String grade, String subject, int score, String createTime,String version,String unit,int type);



    List<OpenTestResult> findByUserIdAndTypeOrderByCreateTime(String userId, int type);

    List<OpenTestResult> findByUserIdAndGradeAndSubjectAndVersionAndUnitAndTypeOrderByCreateTime(String userId, String grade, String subject, String version, String unit, int type);

    List<OpenTestResult> findByUserIdAndGradeAndSubjectAndVersionAndTypeOrderByCreateTime(String userId, String grade, String subject, String version, int type);


}
