package com.ssj.dao.sys.cardinfo.dao;


import com.ssj.bean.sys.cardinfo.domain.Card;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
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;
@Repository
public interface CardDao extends JpaRepository<Card, String>{
	
	public Page<Card> findAll(Specification<Card> specification, Pageable page);
	
	@Query(nativeQuery = true, value = "delete from tb_card_award  where card_id =?1  ")
	@Modifying
	public Integer deleteCardAward(String cardId);

	@Query(nativeQuery = true, value = " SELECT  count(1)  from  tb_card_award  where award_status = 0 and card_id = ?1 ")
	public Integer getCountCardAward(String cardId);
	@Query(nativeQuery = true, value = " SELECT  count(1)  from  tb_card_award  where award_status = 0 and card_id = ?1  and  award_place in (?2) ")
	public Integer getCountCardInPlace(String cardId, String place);
	/**
	 *  计算活动iD,用户ID 开始时间和结束时间   用户的抽奖记录
	 * @param cardId
	 * @param userId
	 * @param begenTime
	 * @param endTime
	 * @return
	 */
	@Query(nativeQuery = true, value = "SELECT  count(1) from  tb_card_award_log  t  where  t.card_id = ?1 and  t.user_id = ?2 "
			+ "and t.create_time  BETWEEN ?3 AND ?4   ")
	public Integer  getCardAwardLog(String cardId, String userId, String begenTime, String endTime);
	/**
	 * 计算活动iD,用户ID    用户的抽奖记录
	 * @param cardId
	 * @param userId
	 * @return
	 */
	@Query(nativeQuery = true, value = "SELECT  count(1) from  tb_card_award_log  t  where  t.card_id = ?1 and  t.user_id = ?2 " )
	public Integer  getCardAwardLog(String cardId, String userId);
	
	/**
	 * 判断用户是否中奖，
	 * @param cardId
	 * @param userId
	 * @return
	 */
	@Query(nativeQuery = true, value = "SELECT  count(1) from  tb_card_award_log  t  where t.stat = 1 and  t.card_id = ?1 and  t.user_id = ?2 " )
	public Integer  getUserZJLog(String cardId, String userId);
	
	
}
