package com.ssj.dao.weixin.sales.dao;

import com.ssj.bean.weixin.sales.domain.LibVipCode;
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;

import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2017年6月6日 下午4:06:00 
 * 类说明 
 */
@Repository
public interface LibVipCodeDao  extends JpaRepository<LibVipCode, String>{

	 public Page<LibVipCode> findAll(Specification<LibVipCode> specification, Pageable page);
	 
	 @Query(nativeQuery = true, value = " select  *  from  tb_lib_vip_code  where vip_code = ?1 and lib_id = ?2  LIMIT 1 ")
	 LibVipCode  findByVipCode(String vipCode,String libId);
	 @Query(nativeQuery = true, value = " select  *  from  tb_lib_vip_code  where vip_code = ?1 and lib_id = ?2 and type !=2 LIMIT 1 ")
	 LibVipCode  findByVipCodeAndType(String vipCode,String libId);
	 
	 @Query(nativeQuery = true, value = " INSERT  into   tb_lib_vip_code ( id,vip_code,type,lib_id,discount,create_time  )\n" +
			 " values (  UUID(),FLOOR(RAND()*?3 + ?3),1,?1,?2,now() )")
	 @Modifying
	 public void inserLibVipCode(String libId,String discount,int rand);
	 
	 
	 @Query(nativeQuery = true, value = "select  *  from  tb_lib_vip_code  where lib_id = ?1 and type = ?2 and discount=?3 LIMIT 0,?4 ")
	 List<LibVipCode> queryLibVipCodeBy(String libId, Integer type,String discount,Integer size);
	 
	 @Query(nativeQuery = true, value = "select  *  from  tb_lib_vip_code  where type <> 2 ")
	 List<LibVipCode> queryLibVipCodeByType();
	 
	 
}
