|
@@ -0,0 +1,222 @@
|
|
|
+package com.sf.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.sf.common.constant.DatabaseConsts;
|
|
|
+import com.sf.common.resp.RestResp;
|
|
|
+import com.sf.dto.req.ChapterAddReqDto;
|
|
|
+import com.sf.dto.resp.BookChapterRespDto;
|
|
|
+import com.sf.dto.resp.BookContentAboutRespDto;
|
|
|
+import com.sf.dto.resp.BookInfoRespDto;
|
|
|
+import com.sf.manager.cache.BookCacheManager;
|
|
|
+import com.sf.mapper.BookChapterMapper;
|
|
|
+import com.sf.mapper.BookContentMapper;
|
|
|
+import com.sf.mapper.BookInfoMapper;
|
|
|
+import com.sf.po.BookChapter;
|
|
|
+import com.sf.po.BookContent;
|
|
|
+import com.sf.po.BookInfo;
|
|
|
+import com.sf.service.IBookInfoService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 小说信息 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author Qing
|
|
|
+ * @since 2023-12-01
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class BookInfoServiceImpl extends ServiceImpl<BookInfoMapper, BookInfo> implements IBookInfoService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookInfoMapper bookInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookChapterMapper bookChapterMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookContentMapper bookContentMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookCacheManager bookCacheManager;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RestResp<Void> saveBookChapter(ChapterAddReqDto dto) {
|
|
|
+ // 校验该作品是否属于当前作家
|
|
|
+ BookInfo bookInfo = bookInfoMapper.selectById(dto.getBookId());
|
|
|
+// if (!Objects.equals(bookInfo.getAuthorId(), UserHolder.getAuthorId())) {
|
|
|
+// return RestResp.fail(ErrorCodeEnum.USER_UN_AUTH);
|
|
|
+// }
|
|
|
+ // 1) 保存章节相关信息到小说章节表
|
|
|
+ // a) 查询最新章节号
|
|
|
+ int chapterNum = 0;
|
|
|
+ QueryWrapper<BookChapter> chapterQueryWrapper = new QueryWrapper<>();
|
|
|
+ chapterQueryWrapper.eq(DatabaseConsts.BookChapterTable.COLUMN_BOOK_ID, dto.getBookId())
|
|
|
+ .orderByDesc(DatabaseConsts.BookChapterTable.COLUMN_CHAPTER_NUM)
|
|
|
+ .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
|
|
|
+ BookChapter bookChapter = bookChapterMapper.selectOne(chapterQueryWrapper);
|
|
|
+ if (Objects.nonNull(bookChapter)) {
|
|
|
+ chapterNum = bookChapter.getChapterNum() + 1;
|
|
|
+ }
|
|
|
+ // b) 设置章节相关信息并保存
|
|
|
+ BookChapter newBookChapter = BookChapter.builder().build();
|
|
|
+ newBookChapter.setBookId(dto.getBookId());
|
|
|
+ newBookChapter.setChapterName(dto.getChapterName());
|
|
|
+ newBookChapter.setChapterNum(chapterNum);
|
|
|
+ newBookChapter.setWordCount(dto.getChapterContent().length());
|
|
|
+ newBookChapter.setIsVip(dto.getIsVip().byteValue());
|
|
|
+ newBookChapter.setCreateTime(LocalDateTime.now());
|
|
|
+ newBookChapter.setUpdateTime(LocalDateTime.now());
|
|
|
+ bookChapterMapper.insert(newBookChapter);
|
|
|
+
|
|
|
+ // 2) 保存章节内容到小说内容表
|
|
|
+ BookContent bookContent = BookContent.builder().build();
|
|
|
+ bookContent.setContent(dto.getChapterContent());
|
|
|
+ bookContent.setChapterId(newBookChapter.getId());
|
|
|
+ bookContent.setCreateTime(LocalDateTime.now());
|
|
|
+ bookContent.setUpdateTime(LocalDateTime.now());
|
|
|
+ bookContentMapper.insert(bookContent);
|
|
|
+
|
|
|
+ // 3) 更新小说表最新章节信息和小说总字数信息
|
|
|
+ // a) 更新小说表关于最新章节的信息
|
|
|
+ BookInfo newBookInfo = new BookInfo();
|
|
|
+ newBookInfo.setId(dto.getBookId());
|
|
|
+ newBookInfo.setLastChapterId(newBookChapter.getId());
|
|
|
+ newBookInfo.setLastChapterName(newBookChapter.getChapterName());
|
|
|
+ newBookInfo.setLastChapterUpdateTime(LocalDateTime.now());
|
|
|
+ newBookInfo.setWordCount(bookInfo.getWordCount() + newBookChapter.getWordCount());
|
|
|
+ newBookChapter.setUpdateTime(LocalDateTime.now());
|
|
|
+ bookInfoMapper.updateById(newBookInfo);
|
|
|
+ // b) 清除小说信息缓存
|
|
|
+// bookInfoCacheManager.evictBookInfoCache(dto.getBookId());
|
|
|
+ return RestResp.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RestResp<BookInfoRespDto> getBookById(Long bookId) {
|
|
|
+ // 查询基础信息
|
|
|
+ BookInfo bookInfo = bookInfoMapper.selectById(bookId);
|
|
|
+ // 查询首章ID
|
|
|
+ QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper
|
|
|
+ .eq(DatabaseConsts.BookChapterTable.COLUMN_BOOK_ID, bookId)
|
|
|
+ .orderByAsc(DatabaseConsts.BookChapterTable.COLUMN_CHAPTER_NUM)
|
|
|
+ .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
|
|
|
+ BookChapter firstBookChapter = bookChapterMapper.selectOne(queryWrapper);
|
|
|
+ // 组装响应对象
|
|
|
+ BookInfoRespDto dto = BookInfoRespDto.builder()
|
|
|
+ .id(bookInfo.getId())
|
|
|
+ .bookName(bookInfo.getBookName())
|
|
|
+ .bookDesc(bookInfo.getBookDesc())
|
|
|
+ .bookStatus(bookInfo.getBookStatus().intValue())
|
|
|
+ .authorId(bookInfo.getAuthorId())
|
|
|
+ .authorName(bookInfo.getAuthorName())
|
|
|
+ .categoryId(bookInfo.getCategoryId())
|
|
|
+ .categoryName(bookInfo.getCategoryName())
|
|
|
+ .commentCount(bookInfo.getCommentCount())
|
|
|
+ .firstChapterId(firstBookChapter.getId())
|
|
|
+ .lastChapterId(bookInfo.getLastChapterId())
|
|
|
+ .picUrl(bookInfo.getPicUrl())
|
|
|
+ .visitCount(bookInfo.getVisitCount())
|
|
|
+ .wordCount(bookInfo.getWordCount())
|
|
|
+ .build();
|
|
|
+ return RestResp.ok(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
|
|
|
+// log.debug("userId:{}", UserHolder.getUserId());
|
|
|
+ // 查询章节信息
|
|
|
+// BookChapter bookChapter = bookChapterMapper.selectById(chapterId);
|
|
|
+// BookChapterRespDto bookChapterDto = BookChapterRespDto.builder()
|
|
|
+// .id(chapterId)
|
|
|
+// .bookId(bookChapter.getBookId())
|
|
|
+// .chapterNum(bookChapter.getChapterNum().intValue())
|
|
|
+// .chapterName(bookChapter.getChapterName())
|
|
|
+// .chapterWordCount(bookChapter.getWordCount())
|
|
|
+// .chapterUpdateTime(bookChapter.getUpdateTime())
|
|
|
+// .isVip(bookChapter.getIsVip().intValue())
|
|
|
+// .build();
|
|
|
+
|
|
|
+// BookChapterRespDto bookChapterDto = bookCacheManager.getBookChapterNew(chapterId);
|
|
|
+// BookChapterRespDto bookChapterDto = new ObjectMapper().convertValue(object,BookChapterRespDto.class);
|
|
|
+// BookChapterRespDto bookChapterDto = JSONObject.toJavaObject(object,BookChapterRespDto.class);
|
|
|
+
|
|
|
+// String objectJson = bookCacheManager.getBookChapter(chapterId);
|
|
|
+// BookChapterRespDto bookChapterDto = new Gson().fromJson(objectJson,BookChapterRespDto.class);
|
|
|
+//
|
|
|
+ BookChapterRespDto bookChapterDto = bookCacheManager.getBookChapterNew(chapterId);
|
|
|
+
|
|
|
+ // 查询章节内容
|
|
|
+ String content = bookCacheManager.getBookContent(chapterId);
|
|
|
+// QueryWrapper<BookContent> contentQueryWrapper = new QueryWrapper<>();
|
|
|
+// contentQueryWrapper.eq(DatabaseConsts.BookContentTable.COLUMN_CHAPTER_ID, chapterId)
|
|
|
+// .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
|
|
|
+// BookContent bookContent = bookContentMapper.selectOne(contentQueryWrapper);
|
|
|
+// String content = bookContent.getContent();
|
|
|
+
|
|
|
+ // 查询小说信息
|
|
|
+// BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(bookChapter.getBookId());
|
|
|
+ // 查询基础信息
|
|
|
+ Long id = bookChapterDto.getBookId();
|
|
|
+ BookInfo bookInfo = bookInfoMapper.selectById(id);
|
|
|
+ // 查询首章ID
|
|
|
+ QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper
|
|
|
+ .eq(DatabaseConsts.BookChapterTable.COLUMN_BOOK_ID, id)
|
|
|
+ .orderByAsc(DatabaseConsts.BookChapterTable.COLUMN_CHAPTER_NUM)
|
|
|
+ .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
|
|
|
+ BookChapter firstBookChapter = bookChapterMapper.selectOne(queryWrapper);
|
|
|
+ // 组装响应对象
|
|
|
+ BookInfoRespDto bookInfoDto = BookInfoRespDto.builder()
|
|
|
+ .id(bookInfo.getId())
|
|
|
+ .bookName(bookInfo.getBookName())
|
|
|
+ .bookDesc(bookInfo.getBookDesc())
|
|
|
+ .bookStatus(bookInfo.getBookStatus().intValue())
|
|
|
+ .authorId(bookInfo.getAuthorId())
|
|
|
+ .authorName(bookInfo.getAuthorName())
|
|
|
+ .categoryId(bookInfo.getCategoryId())
|
|
|
+ .categoryName(bookInfo.getCategoryName())
|
|
|
+ .commentCount(bookInfo.getCommentCount())
|
|
|
+ .firstChapterId(firstBookChapter.getId())
|
|
|
+ .lastChapterId(bookInfo.getLastChapterId())
|
|
|
+ .picUrl(bookInfo.getPicUrl())
|
|
|
+ .visitCount(bookInfo.getVisitCount())
|
|
|
+ .wordCount(bookInfo.getWordCount())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 组装数据并返回
|
|
|
+ return RestResp.ok(BookContentAboutRespDto.builder()
|
|
|
+ .bookInfo(bookInfoDto)
|
|
|
+ .chapterInfo(bookChapterDto)
|
|
|
+ .bookContent(content)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RestResp<List<BookChapterRespDto>> listChapters(Long bookId) {
|
|
|
+ QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq(DatabaseConsts.BookChapterTable.COLUMN_BOOK_ID, bookId)
|
|
|
+ .orderByAsc(DatabaseConsts.BookChapterTable.COLUMN_CHAPTER_NUM);
|
|
|
+ List<BookChapter> bookChapters = bookChapterMapper.selectList(queryWrapper);
|
|
|
+// return RestResp.ok(null);
|
|
|
+ return RestResp.ok(bookChapters.stream().map(v -> BookChapterRespDto.builder()
|
|
|
+ .id(v.getId())
|
|
|
+ .bookId(bookId)
|
|
|
+ .chapterName(v.getChapterName())
|
|
|
+ .chapterNum(v.getChapterNum().intValue())
|
|
|
+ .chapterWordCount(v.getWordCount())
|
|
|
+ .isVip(v.getIsVip().intValue())
|
|
|
+ .build()).toList());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|