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 in (1,2)")
	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);

	@Query(nativeQuery = true,value = "select create_time from tb_class where id=?1 and create_time is not null order by create_time limit 1")
	String findClassCreateTime(String classId);

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

    @Query(nativeQuery = true, value = "select t1.class_id\n" +
            "    from tb_class_student t1,\n" +
            "         tb_class t2\n" +
            "    where 1 = 1\n" +
            "      and t1.vip_id = ?1\n" +
            "      and t1.lib_id = ?2\n" +
            "      and t1.status = 2\n" +
            "      and t2.status = 1\n" +
            "      and t1.class_id = t2.id\n" +
            "    order by t1.create_time desc\n" +
            "    limit 1")
    String findRecentOwnedClassId(String vipId, String libId);

    List<TbClassStudent> findByLibIdAndVipIdInAndStatus(String libId, List<String> vipIds, int status);

}
