package cn.edu.ecnu.stu.bookstore.service.impl;

import cn.edu.ecnu.stu.bookstore.mapper.BookMapper;
import cn.edu.ecnu.stu.bookstore.pojo.Book;
import cn.edu.ecnu.stu.bookstore.utils.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    private List<Book> switchPicture(List<Book> books) {
        for (Book book : books) {
            book.setPictures(book.getPictures().stream().map(ImageUtil::convertImageToBase64Str).collect(Collectors.toList()));
        }
        return books;
    }

    public List<Book> getBookByTitle(String title, String storeId, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        return switchPicture(bookMapper.getBookByTitle(title, storeId, start, pageSize));
    }

    public List<Book> getBookByAuthor(String author, String storeId, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        return switchPicture(bookMapper.getBookByAuthor(author, storeId, start, pageSize));

    }

    public List<Book> getBookByTag(String tag, String storeId, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        return switchPicture(bookMapper.getBookByTag(tag, storeId, start, pageSize));

    }

    public List<Book> getBookByContent(String content, String storeId, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        return switchPicture(bookMapper.getBookByContent(content, storeId, start, pageSize));

    }
}
