1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
| package com.qiniu.service;
import com.qiniu.entity.Book; import com.qiniu.entity.Chapter; import com.qiniu.repository.BookRepository; import com.qiniu.repository.ChapterRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.TimeUnit;
@Slf4j @Service public class BookManagementService { @Autowired private BookRepository bookRepository; @Autowired private ChapterRepository chapterRepository; @Autowired private QiniuFileService qiniuFileService; @Autowired private StringRedisTemplate redisTemplate; private static final String BOOK_CACHE_KEY = "book:cache:"; private static final String CHAPTER_CACHE_KEY = "chapter:cache:";
@Transactional public Book createBook(Book book) { try { log.info("创建书籍: {}", book.getTitle()); book.setBookId(generateBookId()); book.setStatus("DRAFT"); book.setCreateTime(LocalDateTime.now()); book.setUpdateTime(LocalDateTime.now()); Book savedBook = bookRepository.save(book); log.info("书籍创建成功: {}", savedBook.getBookId()); return savedBook; } catch (Exception e) { log.error("创建书籍失败", e); throw new RuntimeException("创建书籍失败", e); } }
@Transactional public String uploadBookCover(String bookId, MultipartFile coverFile) { try { log.info("上传书籍封面: {}", bookId); Optional<Book> bookOpt = bookRepository.findByBookId(bookId); if (!bookOpt.isPresent()) { throw new RuntimeException("书籍不存在"); } Book book = bookOpt.get(); String folder = "books/" + bookId + "/cover"; Map<String, Object> uploadResult = qiniuFileService.uploadFileWithDetails(coverFile, folder); if ((Boolean) uploadResult.get("success")) { String coverUrl = (String) uploadResult.get("fileUrl"); book.setCoverUrl(coverUrl); book.setUpdateTime(LocalDateTime.now()); bookRepository.save(book); clearBookCache(bookId); log.info("书籍封面上传成功: {}", coverUrl); return coverUrl; } else { throw new RuntimeException("封面上传失败: " + uploadResult.get("error")); } } catch (Exception e) { log.error("上传书籍封面失败", e); throw new RuntimeException("上传书籍封面失败", e); } }
@Transactional public Chapter createChapter(String bookId, Chapter chapter) { try { log.info("创建章节: {}, 书籍: {}", chapter.getTitle(), bookId); Optional<Book> bookOpt = bookRepository.findByBookId(bookId); if (!bookOpt.isPresent()) { throw new RuntimeException("书籍不存在"); } chapter.setChapterId(generateChapterId()); chapter.setBookId(bookId); chapter.setStatus("DRAFT"); chapter.setCreateTime(LocalDateTime.now()); chapter.setUpdateTime(LocalDateTime.now()); if (chapter.getChapterOrder() == null) { Integer maxOrder = chapterRepository.findMaxChapterOrderByBookId(bookId); chapter.setChapterOrder(maxOrder != null ? maxOrder + 1 : 1); } Chapter savedChapter = chapterRepository.save(chapter); clearBookCache(bookId); clearChapterCache(bookId); log.info("章节创建成功: {}", savedChapter.getChapterId()); return savedChapter; } catch (Exception e) { log.error("创建章节失败", e); throw new RuntimeException("创建章节失败", e); } }
@Transactional public String uploadChapterFile(String chapterId, MultipartFile file) { try { log.info("上传章节文件: {}", chapterId); Optional<Chapter> chapterOpt = chapterRepository.findByChapterId(chapterId); if (!chapterOpt.isPresent()) { throw new RuntimeException("章节不存在"); } Chapter chapter = chapterOpt.get(); String folder = "books/" + chapter.getBookId() + "/chapters"; Map<String, Object> uploadResult = qiniuFileService.uploadFileWithDetails(file, folder); if ((Boolean) uploadResult.get("success")) { String filePath = (String) uploadResult.get("filePath"); String fileUrl = (String) uploadResult.get("fileUrl"); Long fileSize = (Long) uploadResult.get("fileSize"); String fileType = (String) uploadResult.get("fileType"); chapter.setFilePath(filePath); chapter.setFileSize(fileSize); chapter.setFileType(fileType); chapter.setUpdateTime(LocalDateTime.now()); chapterRepository.save(chapter); clearChapterCache(chapter.getBookId()); log.info("章节文件上传成功: {}", fileUrl); return fileUrl; } else { throw new RuntimeException("章节文件上传失败: " + uploadResult.get("error")); } } catch (Exception e) { log.error("上传章节文件失败", e); throw new RuntimeException("上传章节文件失败", e); } }
public List<Chapter> getBookChapters(String bookId) { try { log.info("获取书籍章节列表: {}", bookId); String cacheKey = CHAPTER_CACHE_KEY + bookId; String cachedData = redisTemplate.opsForValue().get(cacheKey); if (cachedData != null) { log.info("从缓存获取章节列表: {}", bookId); return chapterRepository.findByBookIdOrderByChapterOrder(bookId); } List<Chapter> chapters = chapterRepository.findByBookIdOrderByChapterOrder(bookId); redisTemplate.opsForValue().set(cacheKey, "cached", 30, TimeUnit.MINUTES); log.info("获取章节列表成功,数量: {}", chapters.size()); return chapters; } catch (Exception e) { log.error("获取书籍章节列表失败", e); throw new RuntimeException("获取书籍章节列表失败", e); } }
public Book getBookDetail(String bookId) { try { log.info("获取书籍详情: {}", bookId); String cacheKey = BOOK_CACHE_KEY + bookId; String cachedData = redisTemplate.opsForValue().get(cacheKey); if (cachedData != null) { log.info("从缓存获取书籍详情: {}", bookId); return bookRepository.findByBookId(bookId).orElse(null); } Optional<Book> bookOpt = bookRepository.findByBookId(bookId); if (bookOpt.isPresent()) { Book book = bookOpt.get(); redisTemplate.opsForValue().set(cacheKey, "cached", 30, TimeUnit.MINUTES); log.info("获取书籍详情成功: {}", book.getTitle()); return book; } else { log.warn("书籍不存在: {}", bookId); return null; } } catch (Exception e) { log.error("获取书籍详情失败", e); throw new RuntimeException("获取书籍详情失败", e); } }
public String generateChapterDownloadUrl(String chapterId, long expireInSeconds) { try { log.info("生成章节下载链接: {}", chapterId); Optional<Chapter> chapterOpt = chapterRepository.findByChapterId(chapterId); if (!chapterOpt.isPresent()) { throw new RuntimeException("章节不存在"); } Chapter chapter = chapterOpt.get(); if (chapter.getFilePath() == null) { throw new RuntimeException("章节文件不存在"); } String downloadUrl = qiniuFileService.generatePrivateDownloadUrl( chapter.getFilePath(), expireInSeconds); log.info("章节下载链接生成成功: {}", chapterId); return downloadUrl; } catch (Exception e) { log.error("生成章节下载链接失败", e); throw new RuntimeException("生成章节下载链接失败", e); } }
private void clearBookCache(String bookId) { try { String cacheKey = BOOK_CACHE_KEY + bookId; redisTemplate.delete(cacheKey); log.info("清除书籍缓存: {}", bookId); } catch (Exception e) { log.error("清除书籍缓存失败", e); } }
private void clearChapterCache(String bookId) { try { String cacheKey = CHAPTER_CACHE_KEY + bookId; redisTemplate.delete(cacheKey); log.info("清除章节缓存: {}", bookId); } catch (Exception e) { log.error("清除章节缓存失败", e); } }
private String generateBookId() { return "book_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000); }
private String generateChapterId() { return "chapter_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000); } }
|