2024-12-13 17:56:54 +08:00
|
|
|
package com.guwan.backend.service.impl;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.guwan.backend.entity.Book;
|
|
|
|
import com.guwan.backend.mapper.BookMapper;
|
|
|
|
import com.guwan.backend.service.BookService;
|
2024-12-20 15:10:17 +08:00
|
|
|
import com.mongoplus.conditions.query.LambdaQueryChainWrapper;
|
2024-12-13 17:56:54 +08:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Service
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class BookServiceImpl implements BookService {
|
|
|
|
|
|
|
|
private final BookMapper bookMapper;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
public Book addBook(Book book) {
|
2024-12-20 15:10:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
// new LambdaQueryWrapper<>()
|
|
|
|
|
|
|
|
// new LambdaQueryChainWrapper<>()
|
|
|
|
|
2024-12-13 17:56:54 +08:00
|
|
|
bookMapper.insert(book);
|
|
|
|
return book;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
public Book updateBook(Book book) {
|
|
|
|
bookMapper.updateById(book);
|
|
|
|
return book;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
public void deleteBook(Long id) {
|
|
|
|
bookMapper.deleteById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Book getBookById(Long id) {
|
|
|
|
return bookMapper.selectById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Book getBookByIsbn(String isbn) {
|
|
|
|
return bookMapper.selectOne(
|
|
|
|
new LambdaQueryWrapper<Book>()
|
|
|
|
.eq(Book::getIsbn, isbn)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPage<Book> getBookList(Integer pageNum, Integer pageSize, String keyword) {
|
|
|
|
LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
if (keyword != null && !keyword.isEmpty()) {
|
|
|
|
wrapper.like(Book::getTitle, keyword)
|
|
|
|
.or()
|
|
|
|
.like(Book::getAuthor, keyword)
|
|
|
|
.or()
|
|
|
|
.like(Book::getDescription, keyword);
|
|
|
|
}
|
|
|
|
return bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPage<Book> getBooksByCategory(String category, Integer pageNum, Integer pageSize) {
|
|
|
|
LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
wrapper.eq(Book::getCategory, category);
|
|
|
|
return bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
|
|
|
|
}
|
|
|
|
}
|