package com.ssj.dao.course.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ssj.bean.course.domain.CourseSeriesOrder;


/** 
 * @author  ZhangCaibao 
 * @version 2018年10月18日 下午3:32:45 
 * 类说明 
 */
@Repository
public interface CourseSeriesOrderDao extends JpaRepository<CourseSeriesOrder, String> {

	@Query(nativeQuery = true, value = " SELECT t.* FROM tb_course_series_order t where t.stat=1 and t.lib_id=?1 and t.course_series_id=?2 and  TO_DAYS(t.end_time)  >=  TO_DAYS(now()) LIMIT 1  ")
	public CourseSeriesOrder findCourseSeriesBylibId(String libId, String courseSeriesId);
	
	@Query(nativeQuery = true, value = " SELECT count(t.id) FROM tb_course_series_order t where  t.order_no=?1   ")
	public int findCourseSeriesByOrderNo(String orderNo);
	
	@Query(nativeQuery = true, value = " SELECT t.* FROM tb_course_series_order t where t.stat=1 and t.lib_id=?1  and  TO_DAYS(t.end_time)  >=  TO_DAYS(now())  ORDER BY  t.create_time desc    ")
	 List<CourseSeriesOrder> findByLibId(String libId);
	
	@Query(nativeQuery = true, value = " SELECT count(t.id) FROM tb_course_series_order t where t.stat=1 and t.lib_id=?1  and  TO_DAYS(t.end_time)  >=  TO_DAYS(now()) and  TO_DAYS(t.start_time)  <=  TO_DAYS(now())   ")
	 Integer getCurrentCourserSize(String libId);
	
	@Query(nativeQuery = true, value ="SELECT  sum(1) from (\n" +
			"SELECT  count(1) from   tb_course_series_order  r  JOIN\n" +
			"tb_course_series_join j on  r.course_series_id = j.course_series_id and r.lib_id = j.lib_id \n" +
			"where r.stat =1 and  TO_DAYS(r.end_time)  >=  TO_DAYS(now())\n" +
			"and r.lib_id=?1 and  r.course_series_id =?2  \n" +
			"GROUP BY j.vip_id ) a ")
	Integer getCurrentStudentSize(String libId,String courseSeriesId);
	
	@Query(nativeQuery = true, value ="SELECT  sum(1) from (\n" +
			"SELECT  count(1) from   tb_course_series_order  r  JOIN\n" +
			"tb_course_series_join j on  r.course_series_id = j.course_series_id  and r.lib_id = j.lib_id \n" +
			"where r.stat =1 and  TO_DAYS(r.end_time)  >=  TO_DAYS(now())\n" +
			"and r.lib_id=?1 \n" +
			"GROUP BY j.vip_id ) a ")
	Integer getCurrentStudentSize(String libId);
}
