package com.ssj.dao.sys.opentest;

import com.ssj.bean.sys.opentest.OpenTestUnit;
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/5/15
 */
@Repository
public interface OpenTestUnitDao extends JpaRepository<OpenTestUnit, String> {

    @Query(nativeQuery = true, value = "select * from (select *\n" +
            "from tb_test_unit\n" +
            "where type = ?1\n" +
            "group by grade,subject,version_name,unit) t\n" +
            "order by t.grade_sort,t.subject,t.version_type,t.sort_no")
    List<OpenTestUnit> findAllUnit(int type);

    @Query(nativeQuery = true, value = "select * from (select *\n" +
            "from tb_test_unit where is_valid=1 and type = ?1\n" +
            "group by grade,subject,version_name,unit) t\n" +
            "order by t.grade_sort,t.subject,t.version_type,t.sort_no")
    List<OpenTestUnit> findValidUnit(int type);

    @Query(nativeQuery = true, value = "select *\n" +
            "from tb_test_unit where grade=?1 and subject=?2 and version_name =?3 and unit = ?4 and semester=?5 and type = ?6\n")
    List<OpenTestUnit> findUnit(String grade, String subject, String versionName, String unit, int semester, int type);

    @Query(nativeQuery = true, value = "select *\n" +
            "from tb_test_unit where grade=?1 and subject=?2 and version_name =?3 and semester=?4 and type = ?5\n")
    List<OpenTestUnit> findUnit(String grade, String subject, String versionName, int semester, int type);

    List<OpenTestUnit> findByGradeAndSubjectAndVersionNameAndUnit(String grade, String subject, String versionName, String unit);

    List<OpenTestUnit> findByGradeAndSubjectAndVersionNameAndUnitAndIsValidAndSemesterAndType(String grade, String subject, String versionName, String unit, int isValid, int semester, int type);
    List<OpenTestUnit> findByGradeAndSubjectAndVersionNameAndIsValidAndSemesterAndType(String grade, String subject, String versionName, int isValid, int semester, int type);


    List<OpenTestUnit> findByHowIdIsNullAndType(int type);

    @Query(nativeQuery = true, value = "select *\n" +
            "from tb_test_unit where grade=?1 and subject=?2 and version_name =?3 and unit regexp ?4 and semester=?5 and type = ?6\n")
    List<OpenTestUnit> findUnitRegexp(String grade, String subject, String versionName, String unit, int semester, int type);
}
