package com.ssj.dao.course.dao;

import com.ssj.bean.course.domain.CourseSeriesJoin;
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.Date;
import java.util.List;


/**
 * @author  ZhangCaibao
 * @version 2018年10月18日 下午3:29:14
 * 类说明
 */
@Repository
public interface CourseSeriesJoinDao extends JpaRepository<CourseSeriesJoin, String>{

	@Query(nativeQuery = true, value = " SELECT count(1) FROM tb_course_series_join t where t.course_series_id=?1 and t.lib_id=?2 and t.vip_id=?3  ")
	int findCourseSeriesJoinBycourseDetailsIdAndLibId(String courseDetailsId,String libId,String vipId);

	@Query(nativeQuery = true, value = " UPDATE tb_course_series_join set begin_time=NOW(),state=1 where course_details_id=?2 and lib_id=?1 and state=0 and TO_DAYS(open_time)=TO_DAYS(now())")
	@Modifying
	void updateBeginTime(String libId, String courseDetailId);

	@Query(nativeQuery = true, value = " UPDATE tb_course_series_join set end_time=NOW(),duration=round((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(begin_time))/60) where course_details_id=?1 and lib_id=?2  and state=1 and TO_DAYS(open_time)=TO_DAYS(now()) ")
	@Modifying
	void courseUpdateTime(String courseDetailId, String libId);

	@Query(nativeQuery = true, value = " UPDATE tb_course_series_join set state=2 where course_details_id=?1 and lib_id=?3 and vip_id=?2  and TO_DAYS(open_time)=TO_DAYS(now())")
	@Modifying
	void updateStateSend(String courseDetailsId, String vipId, String libId);
	@Query(nativeQuery = true, value = " UPDATE tb_course_series_join set open_time=?4,num=?3  where course_details_id=?1 and lib_id=?2  ")
	@Modifying
	public void updateJoinDate(String courseDetailsId, String libId,int num ,Date date);
	@Query(nativeQuery = true, value = " select  * from  tb_course_series_join  where course_details_id=?1 and lib_id=?2  and vip_id=?3 ")
	public CourseSeriesJoin findBycourseDetailsIdAndLibIdAndNum(String courseDetailsId, String libId,String vipId );
	@Query(nativeQuery = true, value = "SELECT  vip_id  from   tb_course_series_join where lib_id =?1\n" +
			"and course_series_id=?2\n" +
			"GROUP BY vip_id ")
	public List<String> findByVipIdAndCourseSeriesIdAndLibId(String libId,String courseSeriesId);

	@Query(nativeQuery = true, value = " delete from  tb_course_series_join  where course_series_id=?2 and lib_id=?1 and vip_id=?3  ")
	@Modifying
	void delByAnyReq(String libId, String courseSeriesId, String vipId);

	@Query(nativeQuery = true, value = " select  * from  tb_course_series_join  where course_series_id=?2 and lib_id=?1  limit 1 ")
	CourseSeriesJoin findOneDataByAny(String libId, String courseSeriesId);

	@Query(nativeQuery = true, value = " UPDATE tb_course_series_join set open_time=?2,num=?4 where course_details_id=?1 and lib_id=?3 and state=0  ")
	@Modifying
	void updateDetailsData(String courseDetailId, String newTime,String libId,int num);

	@Query(nativeQuery = true, value = " SELECT COUNT(DISTINCT vip_id)  FROM tb_course_series_join t where t.course_series_id=?2 and t.lib_id=?1   ")
	int findStudentCount(String libId, String courseSeriesId);

	@Query(nativeQuery = true, value = " select  * from  tb_course_series_join  where course_details_id=?2 and lib_id=?1  and state=0 ")
	List<CourseSeriesJoin> findbyLibJoinList(String libId, String seriesId);

	@Query(nativeQuery = true, value = "  select  * from  tb_course_series_join  where course_series_id=?2 and lib_id=?1  and vip_id=?3 and num<>?4 and open_time>?5 order by num asc ")
	List<CourseSeriesJoin> findbyLibAndVip(String libId, String seriesId, String vipId,int num,Date openTime);

	@Query(nativeQuery = true, value = " select  * from  tb_course_series_join  where course_details_id=?2 and lib_id=?1  limit 1 ")
	CourseSeriesJoin findOneDataByDetail(String libId, String detailId);

	@Query(nativeQuery = true, value = " select  * from  tb_course_series_join  where course_series_id=?2 and lib_id=?1 and open_time <?3 order by open_time desc limit 1 ")
	CourseSeriesJoin findOneDataByDetailAndTime(String libId, String seriesId, Date openTime);

	@Query(nativeQuery = true, value = " UPDATE tb_course_series_join set num=?3 where course_details_id=?1 and lib_id=?2   ")
	@Modifying
	void updateJoinDate2(String courseDetailsId, String libId, int num);
}