BookChapterServiceImpl.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.sf.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.sf.dto.resp.BookChapterRespDto;
  4. import com.sf.entity.BookChapter;
  5. import com.sf.mapper.BookChapterMapper;
  6. import com.sf.service.IBookChapterService;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /**
  14. * <p>
  15. * 小说章节 服务实现类
  16. * </p>
  17. *
  18. * @author baomidou
  19. * @since 2024-05-25
  20. */
  21. @Service
  22. public class BookChapterServiceImpl extends ServiceImpl<BookChapterMapper, BookChapter> implements IBookChapterService {
  23. @Autowired
  24. private BookChapterMapper bookChapterMapper;
  25. @Override
  26. public List<BookChapterRespDto> getBookChapterByBookId(Long bookId) {
  27. // select * from book_chapter where book_id = '';
  28. LambdaQueryWrapper<BookChapter> queryWrapper = new LambdaQueryWrapper();
  29. queryWrapper.eq(BookChapter::getBookId, bookId);
  30. List<BookChapter> bookChapters = bookChapterMapper.selectList(queryWrapper);
  31. // 使用普通的方式转换成DTO
  32. List<BookChapterRespDto> chapterRespDtos = new ArrayList<>();
  33. bookChapters.forEach(bookChapter -> {
  34. BookChapterRespDto chapterRespDto = new BookChapterRespDto();
  35. BeanUtils.copyProperties(bookChapter, chapterRespDto);
  36. // 补充不同名的属性
  37. chapterRespDto.setChapterUpdateTime(bookChapter.getUpdateTime());
  38. chapterRespDtos.add(chapterRespDto);
  39. });
  40. // stream流的写法
  41. List<BookChapterRespDto> chapterRespDtos2 = bookChapters.stream().map(bookChapter -> {
  42. return BookChapterRespDto.builder()
  43. .id(bookChapter.getId())
  44. .bookId(bookChapter.getBookId())
  45. .chapterName(bookChapter.getChapterName())
  46. .chapterNum(bookChapter.getChapterNum())
  47. .chapterWordCount(bookChapter.getWordCount())
  48. .chapterUpdateTime(bookChapter.getUpdateTime())
  49. .isVip(bookChapter.getIsVip().intValue())
  50. .build();
  51. }).toList();
  52. return chapterRespDtos2;
  53. }
  54. }