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

import com.ssj.bean.sys.cardinfo.domain.CardAward;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface CardAwardDao extends JpaRepository<CardAward, String>{

	
	@Query(nativeQuery = true, value = " SELECT  t.*    from  tb_card_award t  where t.award_status = 0 and t.card_id =?1 ")
	public List<CardAward> getCardAwardListByCardId(String cardId);
	
	@Query(nativeQuery = true, value = " SELECT  t.*    from  tb_card_award t  where t.award_status = 0 and t.card_id =?1  and  award_place in ?2  order by rand() limit 1 ")
	public CardAward getCardAwardListByCardIdPracelimit(String cardId, List<Integer> prace);
	
	public CardAward  findByUserIdAndCardId(String userId, String cardId);
	
	/**
	 *  随机获取一个奖品
	* @param cardId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年9月21日 下午2:17:47 
	* @CardAward
	 */
	@Query(nativeQuery = true, value = "select * from (\n" +
			"( SELECT  *    from  tb_card_award   where award_status = 0 and card_id =?1   order by rand() limit 1)\n" +
			" union \n" +
			"(SELECT  *    from  tb_card_award   where award_status = 0 and card_id =?1   order by rand() limit 1) \n" +
			"union \n" +
			"(SELECT  *    from  tb_card_award   where award_status = 0 and card_id =?1   order by rand() limit 1)\n" +
			"union \n" +
			"(SELECT  *    from  tb_card_award   where award_status = 0 and card_id =?1  order by rand() limit 1)\n" +
			"union \n" +
			"(SELECT  *    from  tb_card_award   where award_status = 0 and card_id =?1   order by rand() limit 1)\n" +
			"union \n" +
			"(SELECT  *    from  tb_card_award   where award_status = 0 and card_id =?1   order by rand() limit 1)\n" +
			")\n" +
			" b order by rand() limit 1;")
	public CardAward findByCardIdRand(String cardId);
}
