|
|
@@ -3,6 +3,8 @@ package com.lingyue.graph.listener;
|
|
|
import com.lingyue.common.event.DocumentParsedEvent;
|
|
|
import com.lingyue.document.entity.Document;
|
|
|
import com.lingyue.document.repository.DocumentRepository;
|
|
|
+import com.lingyue.graph.service.DocumentBlockGeneratorService;
|
|
|
+import com.lingyue.graph.service.DocumentBlockGeneratorService.BlockDTO;
|
|
|
import com.lingyue.graph.service.GraphNerService;
|
|
|
import com.lingyue.graph.service.NerToBlockService;
|
|
|
import com.lingyue.graph.service.NerToBlockService.TextElementDTO;
|
|
|
@@ -36,6 +38,7 @@ public class DocumentParsedEventListener {
|
|
|
|
|
|
private final GraphNerService graphNerService;
|
|
|
private final NerToBlockService nerToBlockService;
|
|
|
+ private final DocumentBlockGeneratorService blockGeneratorService;
|
|
|
private final RestTemplate restTemplate;
|
|
|
private final DocumentRepository documentRepository;
|
|
|
|
|
|
@@ -212,10 +215,17 @@ public class DocumentParsedEventListener {
|
|
|
List<Map<String, Object>> relations = (List<Map<String, Object>>) nerResponse.get("relations");
|
|
|
int relationCount = graphNerService.saveRelationsToGraph(relations, tempIdToNodeId);
|
|
|
|
|
|
- // 5. 将 NER 结果转换为 TextElement 格式(用于结构化文档)
|
|
|
- List<TextElementDTO> textElements = nerToBlockService.convertToTextElements(text, entities);
|
|
|
- log.debug("NER 结果已转换为 TextElement: documentId={}, elementCount={}",
|
|
|
- documentId, textElements.size());
|
|
|
+ // 5. 生成并保存文档块结构
|
|
|
+ try {
|
|
|
+ List<BlockDTO> blocks = blockGeneratorService.generateBlocks(documentId, text, entities);
|
|
|
+ if (!blocks.isEmpty()) {
|
|
|
+ // 调用 document-service 保存 blocks
|
|
|
+ saveBlocksToDocumentService(documentId, blocks);
|
|
|
+ log.info("文档块生成并保存完成: documentId={}, blockCount={}", documentId, blocks.size());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("生成或保存文档块失败(不影响主流程): documentId={}, error={}", documentId, e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
long processingTime = System.currentTimeMillis() - startTime;
|
|
|
|
|
|
@@ -395,6 +405,71 @@ public class DocumentParsedEventListener {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // ==================== 文档块保存 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用 document-service 保存文档块
|
|
|
+ */
|
|
|
+ private void saveBlocksToDocumentService(String documentId, List<BlockDTO> blocks) {
|
|
|
+ try {
|
|
|
+ String url = "http://localhost:" + serverPort + "/api/v1/documents/" + documentId + "/blocks/batch";
|
|
|
+
|
|
|
+ // 将 BlockDTO 转换为 Map 列表
|
|
|
+ List<Map<String, Object>> blockMaps = new ArrayList<>();
|
|
|
+ for (BlockDTO block : blocks) {
|
|
|
+ Map<String, Object> blockMap = new HashMap<>();
|
|
|
+ blockMap.put("blockId", block.getBlockId());
|
|
|
+ blockMap.put("documentId", block.getDocumentId());
|
|
|
+ blockMap.put("parentId", block.getParentId());
|
|
|
+ blockMap.put("children", block.getChildren());
|
|
|
+ blockMap.put("blockIndex", block.getBlockIndex());
|
|
|
+ blockMap.put("blockType", block.getBlockType());
|
|
|
+ blockMap.put("charStart", block.getCharStart());
|
|
|
+ blockMap.put("charEnd", block.getCharEnd());
|
|
|
+
|
|
|
+ // 转换 elements
|
|
|
+ if (block.getElements() != null) {
|
|
|
+ List<Map<String, Object>> elementMaps = new ArrayList<>();
|
|
|
+ for (TextElementDTO el : block.getElements()) {
|
|
|
+ Map<String, Object> elMap = new HashMap<>();
|
|
|
+ elMap.put("type", el.getType());
|
|
|
+ elMap.put("content", el.getContent());
|
|
|
+ elMap.put("entityId", el.getEntityId());
|
|
|
+ elMap.put("entityText", el.getEntityText());
|
|
|
+ elMap.put("entityType", el.getEntityType());
|
|
|
+ elMap.put("confirmed", el.getConfirmed());
|
|
|
+ elMap.put("url", el.getUrl());
|
|
|
+ elMap.put("refDocId", el.getRefDocId());
|
|
|
+ elMap.put("refDocTitle", el.getRefDocTitle());
|
|
|
+ elementMaps.add(elMap);
|
|
|
+ }
|
|
|
+ blockMap.put("elements", elementMaps);
|
|
|
+ }
|
|
|
+
|
|
|
+ blockMaps.add(blockMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> request = new HashMap<>();
|
|
|
+ request.put("blocks", blockMaps);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);
|
|
|
+
|
|
|
+ ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
|
|
|
+
|
|
|
+ if (response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ log.info("文档块保存成功: documentId={}, blockCount={}", documentId, blocks.size());
|
|
|
+ } else {
|
|
|
+ log.warn("文档块保存失败: documentId={}, status={}", documentId, response.getStatusCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用 document-service 保存块失败: documentId={}, error={}", documentId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// ==================== 任务进度更新 ====================
|
|
|
|
|
|
/**
|