DocumentCleanupServiceImpl.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.smartreview.service.impl;
  2. import com.smartreview.po.Block;
  3. import com.smartreview.po.Conversation;
  4. import com.smartreview.repository.*;
  5. import com.smartreview.service.DocumentCleanupService;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import java.util.List;
  11. /**
  12. * 文档清理服务实现
  13. */
  14. @Slf4j
  15. @Service
  16. @RequiredArgsConstructor
  17. public class DocumentCleanupServiceImpl implements DocumentCleanupService {
  18. private final BlockRepository blockRepository;
  19. private final ChapterRepository chapterRepository;
  20. private final KnowledgePointRepository knowledgePointRepository;
  21. private final QuestionRepository questionRepository;
  22. private final UserAnswerRepository userAnswerRepository;
  23. private final QuestionGenerationTaskRepository questionGenerationTaskRepository;
  24. private final ConversationRepository conversationRepository;
  25. private final ReviewChatMessageRepository reviewChatMessageRepository;
  26. @Override
  27. @Transactional
  28. public void cleanOldDocumentData(String documentId) {
  29. log.info("开始清理文档旧数据: {}", documentId);
  30. // 删除所有Block下的知识点
  31. List<Block> oldBlocks = blockRepository.findByDocumentIdOrderBySortOrder(documentId);
  32. for (Block oldBlock : oldBlocks) {
  33. knowledgePointRepository.deleteByBlockId(oldBlock.getId());
  34. }
  35. // 删除所有Block
  36. blockRepository.deleteByDocumentId(documentId);
  37. // 删除所有章节
  38. chapterRepository.deleteByDocumentId(documentId);
  39. log.info("文档旧数据清理完成: {}", documentId);
  40. }
  41. @Override
  42. @Transactional
  43. public void cleanAllDocumentData(String documentId) {
  44. log.info("开始完全清理文档所有关联数据: {}", documentId);
  45. // 获取所有Block
  46. List<Block> blocks = blockRepository.findByDocumentIdOrderBySortOrder(documentId);
  47. List<Long> blockIds = blocks.stream().map(Block::getId).toList();
  48. if (!blockIds.isEmpty()) {
  49. // 1. 删除用户答案
  50. for (Long blockId : blockIds) {
  51. userAnswerRepository.deleteByBlockId(blockId);
  52. }
  53. log.debug("已删除文档相关的用户答案: documentId={}", documentId);
  54. // 2. 删除题目
  55. for (Long blockId : blockIds) {
  56. questionRepository.deleteByBlockId(blockId);
  57. }
  58. log.debug("已删除文档相关的题目: documentId={}", documentId);
  59. // 3. 删除题目生成任务
  60. for (Long blockId : blockIds) {
  61. questionGenerationTaskRepository.deleteByBlockId(blockId);
  62. }
  63. log.debug("已删除文档相关的题目生成任务: documentId={}", documentId);
  64. // 4. 删除知识点
  65. for (Long blockId : blockIds) {
  66. knowledgePointRepository.deleteByBlockId(blockId);
  67. }
  68. log.debug("已删除文档相关的知识点: documentId={}", documentId);
  69. }
  70. // 5. 删除对话和聊天记录
  71. List<Conversation> conversations = conversationRepository.findByDocumentId(documentId);
  72. for (Conversation conversation : conversations) {
  73. reviewChatMessageRepository.deleteByConversationId(conversation.getId());
  74. }
  75. conversationRepository.deleteByDocumentId(documentId);
  76. log.debug("已删除文档相关的对话和聊天记录: documentId={}", documentId);
  77. // 6. 删除所有Block
  78. blockRepository.deleteByDocumentId(documentId);
  79. log.debug("已删除文档的所有Block: documentId={}", documentId);
  80. // 7. 删除所有章节
  81. chapterRepository.deleteByDocumentId(documentId);
  82. log.debug("已删除文档的所有章节: documentId={}", documentId);
  83. log.info("文档所有关联数据清理完成: {}", documentId);
  84. }
  85. }