package com.sf.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sf.dto.resp.BookChapterRespDto;
import com.sf.entity.BookChapter;
import com.sf.mapper.BookChapterMapper;
import com.sf.service.IBookChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
*
* 小说章节 服务实现类
*
*
* @author baomidou
* @since 2024-05-25
*/
@Service
public class BookChapterServiceImpl extends ServiceImpl implements IBookChapterService {
@Autowired
private BookChapterMapper bookChapterMapper;
@Override
public List getBookChapterByBookId(Long bookId) {
// select * from book_chapter where book_id = '';
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(BookChapter::getBookId, bookId);
List bookChapters = bookChapterMapper.selectList(queryWrapper);
// 使用普通的方式转换成DTO
List chapterRespDtos = new ArrayList<>();
bookChapters.forEach(bookChapter -> {
BookChapterRespDto chapterRespDto = new BookChapterRespDto();
BeanUtils.copyProperties(bookChapter, chapterRespDto);
// 补充不同名的属性
chapterRespDto.setChapterUpdateTime(bookChapter.getUpdateTime());
chapterRespDtos.add(chapterRespDto);
});
// stream流的写法
List chapterRespDtos2 = bookChapters.stream().map(bookChapter -> {
return BookChapterRespDto.builder()
.id(bookChapter.getId())
.bookId(bookChapter.getBookId())
.chapterName(bookChapter.getChapterName())
.chapterNum(bookChapter.getChapterNum())
.chapterWordCount(bookChapter.getWordCount())
.chapterUpdateTime(bookChapter.getUpdateTime())
.isVip(bookChapter.getIsVip().intValue())
.build();
}).toList();
return chapterRespDtos2;
}
}