package com.ssj.dao.weixin.sales.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.weixin.sales.domain.WorkDay;


@Repository
public interface WorkDayDao  extends JpaRepository<WorkDay, String>{
	
	 //得到多少天后的工作日是几号，跳过了法定假期，寒假期间。
	 @Query(nativeQuery = true, value = "SELECT * from tb_work_day t where t.date>?1 AND t.lib_id=?3 AND (t.work_day=1 OR t.work_day is NULL OR t.work_day=3)  ORDER BY t.date asc LIMIT ?2,1  ")
	 public WorkDay getEndWorkDay(Date curDate,Integer day,String LibId);
	 
	//计算月还有多少天工作日
	@Query(nativeQuery = true, value = "SELECT * from tb_work_day t WHERE t.date>=?1 AND t.lib_id=?2  AND t.date<date_add(curdate()-day(curdate())+1,interval 1 month) AND (t.work_day=1 OR t.work_day is NULL OR t.work_day=4) ORDER BY t.date asc ")
   public List<WorkDay> getMonthWorkDay(Date curDate,String LibId);
	
	//下一个工作日
	@Query(nativeQuery = true, value = "SELECT * from tb_work_day t where t.date>=?1 AND t.lib_id=?2  AND (t.work_day=1 OR t.work_day is NULL)  ORDER BY t.date asc LIMIT 0,1  ")
	public WorkDay getNextWorkDay(Date curDate,String LibId);
	
	//深圳新开馆插入日历表，SSJ为公共的日历数据
	@Query(nativeQuery = true, value = " insert into tb_work_day(id, date, work_day,lib_id) select UUID(),date, work_day,?1 from tb_work_day t where t.lib_id='SSJ' ")
	@Modifying
	public void insertWorkDayByLibId(String libId);
	
	//查询是否有自定制的日历数据，没有则查询SSJ的数据
	@Query(nativeQuery = true, value = " SELECT COUNT(1) FROM `tb_work_day`  t where t.lib_id=?1  ")
	public Integer countWorkDayByLibId(String libId);

	
	//查询所有之前配置的寒假期
	@Query(nativeQuery = true, value = " SELECT * FROM `tb_work_day`  t where t.work_day=4 AND t.lib_id=?1 ORDER BY t.date desc  ")
	public List<WorkDay> findWorkDayByLibId(String libId);
	
	
	//两日期之间的日期
	@Query(nativeQuery = true, value = " SELECT * FROM `tb_work_day`  t where  t.lib_id=?1 AND TO_DAYS(?2)<=TO_DAYS(t.date) AND TO_DAYS(?3)>=TO_DAYS(t.date) ORDER BY t.date asc  ")
	public List<WorkDay> findWorkDayByDate(String libId,String datestart,String dateend);
	
	
}
