package cn.edu.ecnu.stu.bookstore.mapper;

import cn.edu.ecnu.stu.bookstore.pojo.Book;
import cn.edu.ecnu.stu.bookstore.pojo.vo.NewOrderVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Mapper
public interface BookMapper {

    @Insert("insert into t_book(book_id, store_id, title, author, publisher," +
            "original_title, translator, pub_year, pages, price, binding, isbn, stock_level)" +
            "values (#{book.bookId}, #{book.storeId}, #{book.title}, #{book.author}, #{book.publisher}, #{book.originalTitle}, " +
            "#{book.translator}, #{book.pubYear}, #{book.pages}, #{book.price}, #{book.binding}, #{book.isbn}, #{book.stockLevel})")
    int insert(@Param("book") Book book);

    @Select("select tag from t_book_tag where book_id = #{book_id}")
    List<String> selectTags(@Param("book_id") String bookId);

    int insertTags(@Param("book_id") String bookId, @Param("tags") List<String> tags);

    @Select("select picture from t_book_picture where book_id = #{book_id}")
    List<String> selectPictureUrl(@Param("book_id") String bookId);

    int insertPictures(@Param("book_id") String bookId, @Param("pictures") List<String> pictures);

    @Insert("insert into t_book_detail(book_id, author_intro, book_intro, content) values " +
            "(#{book.bookId}, #{book.authorIntro}, #{book.bookIntro}, #{book.content})")
    int insertBookDetail(@Param("book") Book book);

    @Select("select count(*) from t_book where store_id = #{storeId} and book_id = #{bookId}")
    int checkBook(@Param("storeId") String storeId, @Param("bookId") String bookId);

    @Update("update t_book set stock_level = stock_level + #{addStockLevel} where store_id = #{storeId} and book_id = #{bookId}")
    void addStockLevel(@Param("storeId") String storeId, @Param("bookId") String bookId, @Param("addStockLevel") int addStockLevel);

    List<Book> batchSelect(@Param("storeId") String storeId, @Param("books") Set<String> books);

    @Update("update t_book set stock_level = stock_level - #{count} where book_id = #{bookId}")
    int minusStockLevel(@Param("bookId") String bookId, @Param("count") int count);
}
