package com.ssj.dao.sys.situation.dao;

import com.ssj.bean.sys.situation.domain.LearningSituation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ISituationDao extends JpaRepository<LearningSituation, String> {
    /**
     * 查询学生和学生id为空即所有人的学情数据
     * @param studentId
     * @return
     */
    List<LearningSituation> findByStudentIdOrStudentIdIsNull(String studentId);

    /**
     * 查询学生和学生id为空即所有人的某科学情数据
     * @param courseName 科目
     * @param studentId
     * @return
     */
    @Query(nativeQuery = true, value="select * from scon_learning_situation where course_name=?1 " +
            " and (student_id = ?2 or student_id is null)")
    List<LearningSituation> findByCourseNameAndStudentId(String courseName, String studentId);

    @Transactional
    @Modifying
    @Query(value = "truncate table scon_learning_situation",nativeQuery = true)
    public void truncateTable();

    @Query(nativeQuery = true, value="select * from scon_learning_situation where type = ?2 and (student_id = ?1 or student_id is null) order by degree_percent desc")
	List<LearningSituation> findByStudentIdOrStudentIdIsNullAndType(String vipId, Integer type);

    @Query(nativeQuery = true, value="SELECT ls.* FROM scon_learning_situation_new ls WHERE ls.course_name = ?1 AND ls.student_id = ?2 AND ls.type = ?3 AND ls.semester = ?4 AND ls.grade = ?5 ORDER BY degree_percent ")
	List<LearningSituation> findByCourseNameAndStudentIdAndType(String subject, String vipId, Integer type, Integer semester, String grade);

	List<LearningSituation> findByCourseNameAndGradeAndSemesterAndTypeAndStudentIdIsNull(String subject, String grade,
			Integer semester, Integer type);
}
