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

import com.ssj.bean.sys.imlibuser.TbClassStudent;
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 com.ssj.bean.sys.imlibuser.TbClassTeacher;

import java.util.List;


@Repository
public interface TbClassTeacherDao extends JpaRepository<TbClassTeacher, String> {

	@Query(nativeQuery = true, value = " SELECT * from tb_class_teacher t where t.class_id=?1 AND t.teacher_id=?2 AND t.`status`=1 LIMIT 1 ")
	TbClassTeacher getTbLibImTeacherByClassId(String classId,String teacherId);

	@Query(nativeQuery = true, value = " SELECT * from tb_class_teacher t where t.class_id=?1 AND t.subject=?2 AND t.`status`=1 LIMIT 1 ")
	TbClassTeacher getClassSubjectTeacher(String classId,String subject);

	@Query(nativeQuery=true,value="select count(distinct teacher_id)\n" +
			"from tb_class_teacher\n" +
			"where class_id = ?1\n" +
			"  and status = 1\n" +
			"  and exists(\n" +
			"        select 1\n" +
			"        from tb_lib_manager tlm\n" +
			"        where teacher_id = tlm.user_id\n" +
			"          and tlm.state = 1\n" +
			"          and tlm.lib_id = ?2\n" +
			"    )\n" +
			"  and lib_id = ?2\n" +
			"  and teacher_id is not null ")
	int getClassTeacherListByClassId(String classId,String libId);



	@Query(nativeQuery = true, value = " SELECT * from tb_class_teacher t where t.class_id=?1 AND t.teacher_id=?2 order by status desc LIMIT 1 ")
	TbClassTeacher getTbLibImTeacherByClassIdIgoreStatus(String classId,String teacherId);

	@Query(nativeQuery = true, value = " SELECT * from tb_class_teacher t where t.lib_id=?1 AND t.teacher_id=?2 and status = 1")
	List<TbClassTeacher> getTbLibImTeacherBylibId(String libId, String teacherId);

	@Modifying
	@Query(nativeQuery=true,value="delete from tb_class_teacher where lib_id=?1 and status=1 and class_id=?2 ")
	void deleteClassTeacherByClassIdAndLibId(String libId,String classId);

}
