package com.ssj.dao.conch.conch.dao;

import com.ssj.bean.conch.conch.domain.HomeworkRanking;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface HomeworkRankingDao extends JpaRepository<HomeworkRanking, String>{

	/**
	 * 根据学生id查询小塾排行榜
	 * @param vipId 学生id
	 */
	HomeworkRanking findByVipId(String vipId);

	@Query(nativeQuery = true, value = "select user_name, img_url, ranking_num, rowno as `index`, derver, vip_id " +
			"from (select a.child_name      as     user_name, " +
			"             a.img_url, " +
			"             a.derver, " +
			"             a.vip_id, " +
			"             a.ranking_num, " +
			"             (@rowno \\:= @rowno + 1) as rowno " +
			"      from (select tb_lib_vip.child_name, " +
			"                   tb_lib_vip.img_url, " +
			"                   tb_lib_vip.derver, " +
			"                   scon_homework_ranking.vip_id, " +
			"                   scon_homework_ranking.ranking_num, " +
			"                   scon_homework_ranking.update_time " +
			"            from scon_homework_ranking, " +
			"                 tb_lib_vip " +
			"            where scon_homework_ranking.vip_id = tb_lib_vip.id) as a, " +
			"           (select @rowno \\:= 0) as row " +
			"      order by a.ranking_num desc, a.update_time desc) as temp " +
			"where temp.derver = ?1")
    List<Object[]> findByDerver(String derver);

	@Query(nativeQuery = true, value = "select user_name, img_url, ranking_num, rowno as `index`, derver, vip_id " +
			"from (select a.child_name     as      user_name, " +
			"             a.img_url, " +
			"             a.ranking_num, " +
			"             a.derver, " +
			"             a.vip_id, " +
			"             (@rowno \\:= @rowno + 1) as rowno " +
			"      from (select tb_lib_vip.child_name, " +
			"                   tb_lib_vip.img_url, " +
			"                   tb_lib_vip.derver, " +
			"                   scon_homework_ranking.vip_id, " +
			"                   scon_homework_ranking.ranking_num, " +
			"                   scon_homework_ranking.update_time " +
			"            from scon_homework_ranking, " +
			"                 tb_lib_vip " +
			"            where scon_homework_ranking.ranking_num > 0 and scon_homework_ranking.vip_id = tb_lib_vip.id) as a, " +
			"           (select @rowno \\:= 0) as row " +
			"      order by a.ranking_num desc, a.update_time desc " +
			"      limit 50) as temp ")
	List<Object[]> findPrevious50();
}
