package com.ssj.dao.sys.opentest;

import com.ssj.bean.sys.opentest.OpenTestQuestion;
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 OpenTestQuestionDao extends JpaRepository<OpenTestQuestion, String> {

    List<OpenTestQuestion> findBySubjectAndGradeAndVersionAndUpdateVersionOrderBySortNo(String subject, String grade, String version, Integer updateVersion);

    List<OpenTestQuestion> findBySubjectAndGradeAndVersionAndUpdateVersionAndTypeAndSemesterOrderBySortNo(String subject, String grade, String version, Integer updateVersion, int type, String semester);


    List<OpenTestQuestion> findBySubjectAndGradeAndUpdateVersionOrderBySortNo(String subject, String grade, Integer updateVersion);

    /**
     * 获取题目的最新版本
     *
     * @return
     */
    @Query(nativeQuery = true, value = "select update_version from tb_test_question order by update_version desc limit 1")
    Integer findLatestUpdateVersion();

    @Query(nativeQuery = true, value = "select update_version from tb_test_question where type=?1 order by update_version desc limit 1")
    Integer findLatestUpdateVersion(int type);

    List<OpenTestQuestion> findBySubjectAndGradeAndVersionAndUnitAndUpdateVersionAndTypeAndSemesterOrderBySortNo(String subject, String grade, String version, String unit, Integer updateVersion, int type, String semester);


    //@Query(nativeQuery = true,value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and unit=?4 and type=?5 and update_version !=-1 order by sort_no desc limit 1")
    //OpenTestQuestion findOpenTestQuestionBySubjectAndGradeAndVersionAndUnitAndType(String subject,String grade,String version,String unit,int type);

    //@Query(nativeQuery = true,value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and unit=?4 and type=?5 and update_version !=-1 and option_content is not  null order by question_num desc limit 1")
    //OpenTestQuestion findOpenTestQuestionBySubjectAndGradeAndVersionAndUnitAndTypeAndQuestion(String subject,String grade,String version,String unit,int type);

    //@Query(nativeQuery = true,value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and unit=?4 and type=?5 and update_version !=-1 order by sort_no desc ")
    //List<OpenTestQuestion> findOpenTestQuestionListBySubjectAndGradeAndVersionAndUnitAndType(String subject,String grade,String version,String unit,int type);

    @Query(nativeQuery = true,value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and unit=?4 and type=?5 and update_version = 0 and option_content is not  null  and sort_no>=?6 " +
            "and semester = ?7 " +
            "order by sort_no limit 1")
    OpenTestQuestion findQuestion(String subject,String grade,String version,String unit,int type,int sortNo,String semester);

    @Query(nativeQuery = true,value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and type=?4 and update_version = 0 and option_content is not  null  and sort_no>=?5 " +
            "and semester = ?6 " +
            "order by sort_no limit 1")
    OpenTestQuestion findQuestion(String subject,String grade,String version,int type,int sortNo,String semester);

    @Query(nativeQuery = true, value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and unit=?4 and type=?5 and update_version =0 and semester = ?6 order by sort_no desc limit 1")
    OpenTestQuestion findQuestion(String subject, String grade, String version, String unit, int type, String semester);

    @Query(nativeQuery = true, value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and type=?4 and update_version =0 and semester = ?5 order by sort_no desc limit 1")
    OpenTestQuestion findQuestion(String subject, String grade, String version, int type, String semester);

    @Query(nativeQuery = true, value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and unit=?4 and type=?5 and semester = ?6 and update_version =0 and option_content is not  null order by question_num desc limit 1")
    OpenTestQuestion findQuestionOrderByQuestionNum(String subject, String grade, String version, String unit, int type, String semester);

    @Query(nativeQuery = true, value = "select  * from tb_test_question where subject =?1 and grade=?2 and version=?3 and type=?4 and semester = ?5 and update_version =0 and option_content is not  null order by question_num desc limit 1")
    OpenTestQuestion findQuestionOrderByQuestionNum(String subject, String grade, String version, int type, String semester);
}
