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

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

import java.math.BigDecimal;
import java.util.List;

/** 
 * @author  ZhangCaibao 
 * @version 2017年4月24日 上午11:13:35 
 * 类说明 
 */
@Repository
public interface LibSalesPriceDao  extends JpaRepository<LibSalesPrice, String>{

	 public Page<LibSalesPrice> findAll(Specification<LibSalesPrice> specification, Pageable page);
	 
	 
	 @Query(nativeQuery = true, value = " select count(1) from   tb_lib_sales_price  where lib_id = ?1  ")
	 public Integer getLibCount(String libId);
	 
	 @Query(nativeQuery = true, value = " INSERT  into   tb_lib_sales_price ( id,species,type,price,discount,create_time,lib_id ,name,aliases )\n" +
			 "SELECT  ?3,species,type,price,discount ,now(),?1 ,name ,aliases  from  tb_lib_sales_price where lib_id =?2 ")
	 @Modifying
	 public void insertLibSales(String libId,String libType,String id);
	 
	 @Query(nativeQuery = true, value = "update tb_lib_sales_price set discount = ?2  where id = ?1 ")
	 @Modifying
	 public void updatePrice(String id ,BigDecimal price);
	 @Query(nativeQuery = true, value = "update tb_lib_sales_price set name = ?2  where id = ?1 ")
	 @Modifying
	 public void updateName(String id ,String name);
	 
	 @Query(nativeQuery = true, value = " select *  from   tb_lib_sales_price  where lib_id = ?1  ORDER BY   species asc  ,type asc  ")
	 public List<LibSalesPrice> findByLibId(String libId);
	 
	LibSalesPrice findByLibIdAndSpeciesAndType(String libId,String species ,String type);

	List<LibSalesPrice> findByLibIdAndSpecies(String libId, String species);

	List<LibSalesPrice> findByLibIdAndSpeciesOrderByCreateTime(String libId, String species);

	LibSalesPrice findTopBySpecies(String species);

}
