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

import com.ssj.bean.weixin.libmy.domain.TbLibBookBorrow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface IBookBorrowDao extends JpaRepository<TbLibBookBorrow, String> {
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_book_borrow  where lib_id=?1 and user_id =?2 and vip_id=?3 and isbn13 = ?4 and type=?5 and stat=1  limit 1 ")
	TbLibBookBorrow findByLibIdAndUserIdAndIsbnAndType(String libId,String userId,String vipId,String isbn13,Integer  type );
	@Query(nativeQuery = true, value = "select *  from tb_lib_book_borrow  where lib_id=?1  and vip_id=?2 and isbn13 = ?3 and type=?4  and stat=1 limit 1 ")
	public TbLibBookBorrow findByLibIdAndVipIdAndIsbn13(String libId,String vipId,String isbn13,Integer type);
	@Query(nativeQuery = true, value = "select *  from tb_lib_book_borrow  where lib_id=?4  and vip_id=?3 and isbn13 = ?2 and type=4  and user_id=?1 and stat=1  limit 1 ")
	TbLibBookBorrow findIsHelp(String userId, String isbn13, String vipId, String libId);
	
	@Query(nativeQuery = true, value = "update tb_lib_book_borrow set is_finish=2 where lib_id=?1  and isbn13=?2 and type=?3 ")
	@Modifying
	void updateLibBorrow(String libId, String isbn13, int i);

	@Query(nativeQuery = true, value = "insert into tb_lib_book_info(id,author_intro,summary,price,author,publisher,isbn13,isbn10,douban_image,\n" +
			"pubdate,local_image,create_time,update_time,pages,book_tag,book_age,book_stock_remain,book_stock,\n" +
			"catalog,lib_id,title,org_code,is_serial,csisbn,bookcase,start_age,end_age,keyword,book_gr,keyword_feel,parent_theme,child_theme,grade,section)\n" +
			"select UUID(),t.author_intro,t.summary,t.price,t.author,t.publisher,t.isbn13,t.isbn10,t.douban_image,\n" +
			"t.pubdate,t.local_image,t.create_time,t.update_time,t.pages,t.book_tag,t.book_age,1,1,\n" +
			"t.catalog,?1,t.title,?3,t.is_serial,t.csisbn,t.bookcase,t.start_age,t.end_age,t.keyword,t.book_gr,t.keyword_feel,t.parent_theme,t.child_theme,t.grade,t.section\n" +
			"from tb_lib_book_info t \n" +
			"inner join tb_lib_book_detail t13 on t.id = t13.info_id "+
			"left join tb_lib_book_info t12 on t.isbn13 = t12.isbn13 and t12.lib_id=?1\n" +
			"where t.lib_id=?2 and t12.id is null and t13.id in (?4)")
	@Modifying
	void insertInlibNotBook(String inlibId, String outlibId, String orgCode, List<String> idsStr);

	@Query(nativeQuery = true, value = "update tb_lib_book_detail INNER JOIN (\n" +
			"SELECT t.id,t12.id as info_id from tb_lib_book_detail t \n" +
			"INNER JOIN tb_lib_book_info t11 on t.info_id = t11.id\n" +
			"inner join tb_lib_book_info t12 on t11.isbn13 = t12.isbn13 and t12.lib_id = ?1\n" +
			"where t.id in (?3) and t.lib_id=?2 \n" +
			")t13 on tb_lib_book_detail.id = t13.id set lib_id=?1 ,tb_lib_book_detail.info_id=t13.info_id")
	@Modifying
	void updateInlibNotBook(String inlibId, String outlibId, List<String> idsStr);

	@Query(nativeQuery = true, value = "update tb_lib_book_info INNER JOIN(\n" +
			"SELECT count(1) as total,count(if(user_id is null,true,null)) as cost_total,info_id from tb_lib_book_detail \n" +
			"where lib_id=?1\n" +
			"GROUP BY info_id\n" +
			") t on tb_lib_book_info.id = t.info_id set tb_lib_book_info.book_stock=t.total,tb_lib_book_info.book_stock_remain=t.cost_total")
	@Modifying
	void updateBookStock(String libId);




	@Query(nativeQuery = true, value = "call proc_update_book_stock(:inParam1)")
	@Modifying
	void callProcBookStock(@Param("inParam1") String libId);

	@Query(nativeQuery = true, value = "call proc_update_book_detail(:inParam1,:inParam2,:inParam3)")
	@Modifying

	void callProcBookDetail(@Param("inParam1") String inLibId,@Param("inParam2") String outLibId,@Param("inParam3") String bookIds);
}
