package com.ssj.dao.sys.imlibuser.dao;

import com.ssj.bean.sys.imlibuser.TbClass;
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 java.util.List;


@Repository
public interface TbClassDao extends JpaRepository<TbClass, String> {

	@Query(nativeQuery = true, value = " SELECT * from tb_class t where t.school=?1 AND t.grade=?2 AND t.class_name=?3 LIMIT 1 ")
	TbClass getTbClassBy(String school,String grade,String className);

	@Query(nativeQuery=true,value=" select * from tb_class where lib_id=?1 and status in(1,2) order by status desc,create_time  ")
	List<TbClass> findTbClassListByLibId(String libId);

	@Query(nativeQuery = true, value = " SELECT * from tb_class t where t.lib_id=?1 AND t.class_name=?2 LIMIT 1 ")
	TbClass getTbClassByLibId(String libId,String className);
	
	@Query(nativeQuery = true, value = " SELECT * from tb_class t where t.lib_id=?1 AND t.grade=?2 AND t.class_name=?3 LIMIT 1 ")
	TbClass getTbClassGradeByLibId(String libId,String grade,String className);

	@Query(nativeQuery = true, value = " SELECT * from tb_class t where t.lib_id=?1 AND t.status = 2 order by create_time LIMIT 1 ")
	TbClass getDefaultTbClassByLibId(String libId);

	@Query(nativeQuery=true,value=" select * from tb_class where lib_id=?1 and status !=0 and school is not null")
	List<TbClass> findTbClassSchoolNameByLibId(String libId);

	@Query(nativeQuery = true,value = "select * from tb_class where lib_id=?1 and status !=0 and class_name =?2")
	List<TbClass> findTbClassByLibIdAndClassName(String libId,String className);

	TbClass findTopByLibIdAndStatus(String libId, int status);

	@Modifying
	@Query(nativeQuery = true,value = "update tb_class set class_name= ?2 where id=?1 ")
	void updateByClassIdAndClassName(String classId,String className);


}
