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 java.util.List;


@Repository
public interface TbClassStudentDao extends JpaRepository<TbClassStudent, String> {

	@Query(nativeQuery = true, value = " SELECT * from tb_class_student t where t.class_id=?1 AND t.vip_id=?2 AND t.`status`=1 LIMIT 1 ")
	TbClassStudent getTbClassTeacherByVipId(String classId,String vipId);

	@Query(nativeQuery = true,value = "select count(vip_id)  from tb_class_student where class_id=?1 and status=1 and lib_id=?2 ")
	int getTbClassStudentByClassId(String classId,String libId);

	List<TbClassStudent> findByVipId(String vipId);

	@Modifying
	@Query(nativeQuery = true,value = "update tb_class_student set class_id=?1 where vip_id in ?2 and lib_id=?3 and status=1")
	void updateTbClassStudentByVipIdsAndClassIdAndlibId(String classId,List<String>vipIds,String libId);

	TbClassStudent findTopByLibIdAndVipIdAndStatus(String libId, String vipId, int status);

	List<TbClassStudent> findByVipIdAndStatus(String vipId, int status);

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

	@Query(nativeQuery = true,value = "update tb_class_student set status=0 where vip_id=?1 and lib_id=?2")
	@Modifying
	void updateByVipIdAndLibId(String vipId,String libId);


	@Modifying
	@Query(nativeQuery = true,value = "update tb_class_student set class_id=?1 where vip_id=?2 and lib_id=?3")
	void updateTbClassStudentByVipIdAndClassIdAndLibId(String classId,String vipId,String libId);


	@Query(nativeQuery = true,value = "SELECT * from tb_class_student t where t.vip_id=?1 AND t.lib_id=?2 AND t.`status`=1 LIMIT 1 ")
	TbClassStudent findByVipIdAndLibId(String vipId,String libId);

	@Query(nativeQuery = true, value = "SELECT vip_id from tb_class_student where status=1 and class_id in ?1")
	List<String> findByClassIds(List<String> classIds);



}
