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

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

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.libmy.domain.BookDetail;


@Repository
public interface IBookDetailDao extends JpaRepository<BookDetail, String> {
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_book_detail  where info_id = ?1 and stat = 1 ")
	List<BookDetail>  bookDetailByInfoId(String infoId);

	BookDetail findById(String id);
	
	@Query(nativeQuery = true, value = "insert into tb_order_details(id,create_time,user_id,author,title,book_detail_id,money,is_close,manager_userid,vip_id)\n" +
										"select UUID(),SYSDATE(),?3,b.author,b.title,a.id,?2,'2',?4,?5\n" +
										"from tb_lib_book_detail a\n" +
										"left join tb_lib_book_info b on a.info_id = b.id\n" +
										"where a.id =?1 ")
	@Modifying
	void saveOrderDetail(String id, int money, String userId, String userId2, String vipId);

	@Query(nativeQuery = true,value = "update tb_lib_book_detail set type=2,vip_id=?4,user_id=?2, borrowed_time=NOW() , back_book_time=DATE_ADD(NOW(),INTERVAL ?3 day) where id=?1")
	@Modifying
	void updateDetailStat(String id, String userId, int borrowDay, String vipId);
	
	@Query(nativeQuery = true,value = "update tb_lib_book_detail set type=1,user_id=null,vip_id=null,borrowed_time=null,back_book_time=null where id=?1")
	@Modifying
	void backBook(String id);

	@Query(nativeQuery = true,value = "update tb_order_details set is_close='1',update_time=NOW() where book_detail_id =?1 and is_close='2'")
	@Modifying
	void updateOrderStat(String id);



	@Query(nativeQuery = true,value = "update tb_lib_book_detail set type=2,user_id=?2,back_book_time=date_add(back_book_time, interval 20 day) where id=?1")
	@Modifying
	void updateDetailStat2(String id, String userId);

	@Query(nativeQuery = true, value = "insert into tb_lib_user_history(id,user_id,author,title,book_detail_id,money,detail_id,order_type,start_time,end_time)\n" +
			"select UUID(),t.user_id,t.author,t.title,t.book_detail_id,?2,t.id,2,t11.back_book_time,date_add(t11.back_book_time, interval 20 day)\n" +
			"from tb_order_details t\n" +
			"left join tb_lib_book_detail t11 on t.book_detail_id = t11.id and t.user_id = t11.user_id\n" +
			"where t.is_close='2' and t.user_id=?3 and t11.id=?1")
	@Modifying
	void saveContinueOrder(String id, int i, String userId);
	
	@Query(nativeQuery = true, value = "insert into tb_lib_user_history(id,user_id,author,title,book_detail_id,money,detail_id,order_type,start_time,end_time,vip_id)\n" +
			"select UUID(),t.user_id,t.author,t.title,t.book_detail_id,?2,t.id,1,t.create_time,t11.back_book_time,t.vip_id\n" +
			"from tb_order_details t\n" +
			"left join tb_lib_book_detail t11 on t.book_detail_id = t11.id and t.user_id = t11.user_id\n" +
			"where t.is_close='2' and t.user_id=?3 and t11.id=?1")
	@Modifying
	void saveCommonOrder(String id, int i, String userId);
	
	
	@Query(nativeQuery = true, value = "insert into tb_lib_user_history(id,user_id,author,title,book_detail_id,money,detail_id,order_type,start_time,end_time)\n" +
			"select UUID(),t.user_id,t.author,t.title,t.book_detail_id,?2,t.id,3,t11.back_book_time,NOW()\n" +
			"from tb_order_details t\n" +
			"left join tb_lib_book_detail t11 on t.book_detail_id = t11.id and t.user_id = t11.user_id\n" +
			"where t.is_close='2' and t.user_id =?3 and t11.id=?1")
	@Modifying
	void saveOverdueOrder(String id, BigDecimal i, String userId);


	@Query(nativeQuery = true, value = "select count(1) from tb_lib_book_detail  where lib_id = ?1")
	Integer getLibBkDetailCount(String libId);
	
	@Query(nativeQuery = true, value = "select count(1) from tb_lib_book_detail  where lib_id = ?1 and info_id is not null and user_id is null ")
	Integer getLibBkInCount(String libId);

	@Query(nativeQuery = true, value = "select count(1) from tb_lib_book_detail  where  vip_id =?1 and lib_id = ?2")
	public Integer borrowedByVipId(String vipId,String libId);

	@Query(nativeQuery = true, value = "update tb_lib_book_info set book_stock_remain=IFNULL(book_stock_remain,book_stock)+(?2) where id=?1")
	@Modifying
	void updateLibStock(String infoId, int i);

	@Query(nativeQuery = true, value = "update tb_lib_book_borrow set stat=2 where isbn13=?1 and type=3 and lib_id=?3 and vip_id=?2 and stat=1 ")
	@Modifying
	void updateBorrowData(String isbn13, String vipId, String libId);

	@Query(nativeQuery = true, value = "select count(1) from tb_lib_book_detail  where   lib_id = ?1 and info_id is not null")
	public Integer getbooksByLibId(String libId);
	@Query(nativeQuery = true, value = "select count(1) from tb_lib_book_detail  where   lib_id = ?1 and info_id is not null and user_id is not null")
	public Integer getbooksBorrowingByLibId(String libId);

	@Query(nativeQuery = true, value = "select *  from tb_lib_book_detail  where recovery_type = 2 and isscan=2 and lib_id=?1")
	List<BookDetail> findInList(String libId);

	@Query(nativeQuery = true, value = "update tb_lib_book_detail set recovery_type=?2 where  lib_id=?1 and isscan=1 and info_id is not null ")
	@Modifying
	void updateStep2(String libId,int type);

	@Query(nativeQuery = true, value = "update tb_lib_book_detail set recovery_type=?2 where  lib_id=?1  and info_id is not null ")
	@Modifying
	void updateStep(String libId, int type);
}
