package com.ssj.dao.weixin.library.dao;

import com.ssj.bean.weixin.libmy.domain.LibBookLocking;
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年11月16日 上午11:16:59 
 * 类说明 
 */
@Repository
public interface LibBookLockingDao extends JpaRepository<LibBookLocking, String> {

	
	
	@Query(nativeQuery = true, value = "SELECT  * from  tb_lib_book_locking " +
			" where  isbn13 =?1 and stat =1 and in_lib_id=?2")
	LibBookLocking findLibBookLockingBybookDetailId(String bookDetailId,String inLibid);
	@Query(nativeQuery = true, value = "SELECT  count(1) from  tb_lib_book_locking " +
			" where  (in_lib_id =?1 or out_lib_id=?1) and stat >=2  and  NOW() BETWEEN create_time and  date_add(create_time,interval 60 day)")
	public int getReadNum(String libId);
	
	@Query(nativeQuery = true, value = "SELECT  ifnull(sum(book_size),0)  from  tb_lib_book_locking " +
			" where  in_lib_id =?1  and stat =1   ")
	public int getBookInExchangeNum(String libId);
	@Query(nativeQuery = true, value = "SELECT  count(1) from  tb_lib_book_locking " +
			" where  out_lib_id =?1  and stat <3 ")
	public int getBookOutExchangeNum(String libId);
	
	@Query(nativeQuery = true, value = " update   tb_lib_book_locking set stat=2 ,update_time=now()  where out_lib_id=?1 and type =1 and stat=1  ")
	@Modifying
	public int updateLibBookLockingOutAndLibId(String libId); 
	
	@Query(nativeQuery = true, value = " update   tb_lib_book_locking set stat=2 ,update_time=now()  where in_lib_id=?1 and type =2 and stat=1  ")
	@Modifying
	public int updateLibBookLockingInAndLibId(String libId); 
	@Query(nativeQuery = true, value = "UPDATE tb_lib_book_info \n" +
			"SET book_stock_remain = book_stock_remain-1\n" +
			"where  id in (\n" +
			"SELECT book_info_id from  tb_lib_book_locking where \n" +
			"in_lib_id =?1\n" +
			"AND type = 2\n" +
			"AND stat = 1\n" +
			")")
	@Modifying
	public int updateLibBookStockRemainInAndLibId(String libId); 
	@Query(nativeQuery = true, value = "UPDATE tb_lib_book_info \n" +
			"SET book_stock_remain = book_stock_remain-1\n" +
			"where  id in (\n" +
			"SELECT book_info_id from  tb_lib_book_locking where \n" +
			"out_lib_id =?1\n" +
			"AND type = 2\n" +
			"AND stat = 1\n" +
			")")
	@Modifying
	public int updateLibBookStockRemainOutAndLibId(String libId);
	
	@Query(nativeQuery = true, value = " update   tb_lib_book_locking set stat=3 ,update_time=now(),batch_id=?3  where out_lib_id=?1 and type =?2 and stat=2   ")
	@Modifying
	void updateDataStat(String libId, int type,String batchId);
	
	@Query(nativeQuery = true, value = " update   tb_lib_book_locking set stat=3 ,update_time=now(),batch_id=?3  where in_lib_id=?1 and type =?2 and stat=2  ")
	@Modifying
	void updateDataStat2(String libId, int type,String batchId);
	
	@Query(nativeQuery = true, value = "SELECT  count(1) from  tb_lib_book_locking where  in_lib_id=?1 and (stat =2 or stat =3) and type=2 and out_lib_id=?2 ")
	int findLastNum(String inlibId, String outLibId);

	@Query(nativeQuery = true, value = " update   tb_lib_book_locking set stat=?2 ,update_time=now()  where in_lib_id=?1  and stat=2   ")
	@Modifying
	void updateDataStatByLibId(String libId, int stat);

	@Query(nativeQuery = true, value = "SELECT  * from  tb_lib_book_locking " +
			" where  book_info_id =?1 and stat =1 and in_lib_id=?2 limit 1 ")
    LibBookLocking findByInfoIdAndLibId(String str, String libId);

	@Query(nativeQuery = true, value = "SELECT  * from  tb_lib_book_locking " +
			" where   in_lib_id=?1 and stat=2 ")
	List<LibBookLocking> findLibLockingList(String searchLibId);
}
