package com.ssj.dao.weixin.library.dao;

import com.ssj.bean.weixin.libmy.domain.LibVipBind;
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 LibVipBindDao extends JpaRepository<LibVipBind, String>{
	
	@Query(nativeQuery = true, value = " SELECT * from tb_lib_vip_bind t where t.master_id=?2 AND t.vip_id =?1 AND IF(?3='',t.teacher_id=?3,1=1) ")
	List<LibVipBind>  findByVipIdAndMasterId(String vipId, String masterId,String teacherId);
	@Query(nativeQuery = true, value = " SELECT * from tb_lib_vip_bind t where t.user_id =?1 LIMIT 1 ")
	LibVipBind  findByUserId(String userId);

	List<LibVipBind> findByVipIdInAndMasterId(String[] vipIds, String masterId);
	
	@Query(nativeQuery = true, value = " SELECT * from tb_lib_vip_bind t where t.vip_id =?1 LIMIT 1 ")
	LibVipBind findByVipId(String studentId);

	List<LibVipBind> findByMasterId(String masterId);
	
	public int countByVipIdAndMasterIdIsNotNull(String vipId);

	/**
	 * 查询会员科目绑定
	 * @param vipId
	 * @param subject
	 * @return
	 */
	List<LibVipBind> findByVipIdAndSubject(String vipId, String subject);

	/**
	 * 查询老师下绑定学生数量
	 * @param teacherId
	 * @return
	 */
	@Query(nativeQuery = true, value = " SELECT count(distinct vip_id) from tb_lib_vip_bind where teacher_id = ?1 " +
			"    and (vip_id is not null or vip_id != '')")
	int countByTeacherIdAndVipIdIsNotNull(String teacherId);
}
