package com.ssj.dao.weixin.zuoyb.dao;

import com.ssj.bean.weixin.zyb.domain.ZuoybDay;
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.List;


/** 
 * @author  ZhangCaibao 
 * @version 2018年5月6日 上午11:09:04 
 * 类说明 
 */
@Repository
public interface ZuoybDayDao  extends JpaRepository<ZuoybDay, String>{
    /*
     * 查询当天是否有记录
     */
	@Query(nativeQuery = true, value = "SELECT t.* from zyb_day t WHERE t.vip_id=?1 and t.type=?2 and TO_DAYS(t.that_day) = TO_DAYS(now()) ")
	public ZuoybDay getVipIdAndType(String vipId,int type);
	@Query(nativeQuery = true, value = "SELECT t.* from zyb_day t WHERE t.vip_id=?1 and t.type=?2 and t.that_day = ?3 ")
	public ZuoybDay getVipIdAndType(String vipId,int type,String date);
	
	@Query(nativeQuery = true, value = "SELECT t.* from zyb_day t WHERE t.vip_id=?1 order by that_day desc  ")
	public List<ZuoybDay> getVipId(String vipId);
	@Query(nativeQuery = true, value = "SELECT t.* from zyb_day t WHERE t.vip_id=?1 and t.type=?2 and  TO_DAYS(t.that_day) = TO_DAYS(?3)   ")
	public ZuoybDay getVipIdAndDateAndType(String vipId,int type, String date);
	
	@Query(nativeQuery = true, value = "SELECT t.id, t.that_day,t.vip_id ,t.create_time ,group_concat(t.type) as type,t.num from zyb_day t"
			+ " WHERE t.vip_id=?1 and t.that_day >= ?2 and t.that_day <= ?3 "
			+ " GROUP BY t.that_day   order by t.that_day desc  ")
	public List<ZuoybDay> getVipIdAndStartDateAndEndDate(String vipId,String startDate,String endDate);
	
	@Query(nativeQuery = true, value = "update zyb_day set num = num-1 where vip_id = ?1 and type=?3 and that_day in (" + 
			"select course_time from kmt_course where plan_status<5 and lesson_service_id= ?2) ")
	@Modifying
	public void updateByVipIdAndType(String vipId,String lessonServiceId,int type);
	
	@Query(nativeQuery = true, value = "DELETE FROM zyb_day where vip_id = ?1 and type=?3 and num = 0 and that_day in (" + 
			"select course_time from kmt_course where plan_status<5 and lesson_service_id= ?2) ")
	@Modifying
	public void deleteByVipIdAndType(String vipId,String lessonServiceId,int type);
}
