package com.ssj.dao.weixin.library.dao;

import com.ssj.bean.weixin.libmy.domain.TbLibSubscribe;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface ISubscribeDao extends JpaRepository<TbLibSubscribe, String> {

	TbLibSubscribe findByUserId(String userId);

	TbLibSubscribe findByUserIdAndStat(String userId, String stat);

	@Query(nativeQuery = true, value = "select *  from tb_lib_subscribe  where vip_id=?1 and stat=?2 and flow_stat = '3' and DATE_FORMAT(order_date,'%Y%m%d')=DATE_FORMAT(NOW(),'%Y%m%d')  limit 1 ")
	TbLibSubscribe findByVipIdAndStat(String vipId, String stat);

	@Query(nativeQuery = true, value = "select * from tb_lib_subscribe where vip_id = ?1 and stat = '1' and flow_stat = '3' and date(ryke_time) = date(now()) ")
	TbLibSubscribe findTodaySignedHistoryByVipId(String vipId);

	/**
	 * 查询今日一条签到签离记录
	 * @param vipId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select *  from tb_lib_subscribe  where vip_id=?1 and flow_stat > 2 and to_days(ryke_time) = to_days(?2)  limit 1 ")
	TbLibSubscribe findTheDayByVipIdAndStat(String vipId, Date date);

	/**
	 * 查询时间段签到签离记录
	 * @param vipId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select *  from tb_lib_subscribe  where vip_id=?1 and flow_stat > 2 and ryke_time between ?2 and ?3")
	List<TbLibSubscribe> findDaysByVipIdAndStat(String vipId, String beginTime, String endTime);
}
