package com.ssj.dao.course.dao;

import java.util.Date;
import java.util.List;

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 com.ssj.bean.course.domain.CourseSeriesSetting;


/** 
 * @author  ZhangCaibao 
 * @version 2018年10月18日 下午3:36:28 
 * 类说明 
 */
@Repository
public interface CourseSeriesSettingDao extends JpaRepository<CourseSeriesSetting, String> {

	@Query(nativeQuery = true, value = " select  *  from   tb_course_series_setting  where course_series_id =?1 and lib_id=?2 ")
	List<CourseSeriesSetting>  fingByCourseSeriesIdAndLibId(String courseSeriesId,String libId);
	@Query(nativeQuery = true, value = " select  *  from   tb_course_series_setting  where course_series_id =?1 and lib_id=?2 LIMIT 1 ")
	CourseSeriesSetting  fingByCourseSeriesIdAndLibIdLimit(String courseSeriesId,String libId);
	
	
	@Modifying
	@Query(nativeQuery = true, value = " delete from  tb_course_series_setting  where course_series_id =?1 and lib_id=?2 ")
	void deleteByByCourseSeriesIdAndLibId(String courseSeriesId,String libId);
	
	@Query(nativeQuery = true, value = " UPDATE tb_course_series_setting set end_time=?4,course_size=?3  where course_series_id=?2 and lib_id=?1  ")
	@Modifying
	void updateSetData(String libId, String courseSeriesId, int size, Date endTime);
	
	@Query(nativeQuery = true, value = " SELECT * from tb_course_series_setting t where t.open_time=?3 and t.teacher_id=?2 and t.lib_id=?1  ")
	CourseSeriesSetting findSetByUserIdAndLibId(String userId, String courseSeriesId, String libId);
	
	//@Query(nativeQuery = true, value = "  select count(1) from tb_course_series_setting t  inner join tb_course_series t11 on t.course_series_id = t11.id   inner join tb_lib_manager t12 on t.lib_id = t12.lib_id and t.teacher_id = t12.user_id "
	//	+ " where t.teacher_id=?3 and t.lib_id=?2  and  t11.state=1  and t12.state=1 and TO_DAYS(NOW())<=to_days(t.end_time)  and (TO_DAYS(?1)-TO_DAYS(t.open_time))%(t.days+1)=0  and TO_DAYS(NOW())>=TO_DAYS(t.open_time)  ")
	
	@Query(nativeQuery = true, value = " SELECT  count(1) from tb_course_series_setting t LEFT JOIN tb_course_series t11 on t.course_series_id = t11.id  inner join tb_lib_manager t12 on t.lib_id = t12.lib_id and t.teacher_id = t12.user_id "
			+ "where t.teacher_id=?3 and t.lib_id=?2 and  t11.state=1  and t12.state=1 AND t.end_time is NOT NULL and TO_DAYS(?1)>=TO_DAYS(t.open_time)  AND TO_DAYS(?1)<=to_days(t.end_time) "
			+ " AND t.course_date LIKE concat('%', ?1, '%')  ")
	int findTeacherHasLesson(String timesNow, String libId, String userId);
	
	@Query(nativeQuery = true, value = " update tb_course_series_setting set course_date =?2,end_time=?4 where course_series_id=?3 and lib_id=?1  ")
	@Modifying
	void updateSettingOneDay(String libId, String newTime, String seriesId, Date openTime);
}
