refactor(backend): incremental architecture evolution (Task 19)

- Add common/ layer for shared capabilities (LLM, RAG, document, middleware)
- Add legacy/ layer for existing business code
- Move files to new structure (controllers, routes, services)
- Update index.ts for new route registration
- System remains fully functional
This commit is contained in:
2025-11-16 15:42:44 +08:00
parent 8a17dc80ae
commit 0c5310fb77
39 changed files with 3904 additions and 353 deletions

View File

@@ -8,6 +8,7 @@ generator client {
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["platform_schema", "aia_schema", "pkb_schema", "asl_schema", "common_schema", "dc_schema", "rvw_schema", "admin_schema", "ssa_schema", "st_schema", "public"]
}
// ==================== 用户模块 ====================
@@ -38,11 +39,15 @@ model User {
documents Document[]
adminLogs AdminLog[]
generalConversations GeneralConversation[]
batchTasks BatchTask[] // Phase 3: 批处理任务
taskTemplates TaskTemplate[] // Phase 3: 任务模板
reviewTasks ReviewTask[] // 稿件审查任务
@@index([email])
@@index([status])
@@index([createdAt])
@@map("users")
@@schema("platform_schema")
}
// ==================== 项目模块 ====================
@@ -66,6 +71,7 @@ model Project {
@@index([createdAt])
@@index([deletedAt])
@@map("projects")
@@schema("aia_schema")
}
// ==================== 对话模块 ====================
@@ -95,6 +101,7 @@ model Conversation {
@@index([createdAt])
@@index([deletedAt])
@@map("conversations")
@@schema("aia_schema")
}
model Message {
@@ -115,6 +122,7 @@ model Message {
@@index([createdAt])
@@index([isPinned])
@@map("messages")
@@schema("aia_schema")
}
// ==================== 知识库模块 ====================
@@ -133,10 +141,12 @@ model KnowledgeBase {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
documents Document[]
batchTasks BatchTask[] // Phase 3: 批处理任务
@@index([userId])
@@index([difyDatasetId])
@@map("knowledge_bases")
@@schema("pkb_schema")
}
model Document {
@@ -154,17 +164,122 @@ model Document {
segmentsCount Int? @map("segments_count")
tokensCount Int? @map("tokens_count")
// Phase 2: 全文阅读模式新增字段
extractionMethod String? @map("extraction_method") // pymupdf/nougat/mammoth/direct
extractionQuality Float? @map("extraction_quality") // 0-1质量分数
charCount Int? @map("char_count") // 字符数
language String? // 检测到的语言 (chinese/english)
extractedText String? @map("extracted_text") @db.Text // 提取的文本内容
uploadedAt DateTime @default(now()) @map("uploaded_at")
processedAt DateTime? @map("processed_at")
knowledgeBase KnowledgeBase @relation(fields: [kbId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
batchResults BatchResult[] // Phase 3: 批处理结果
@@index([kbId])
@@index([userId])
@@index([status])
@@index([difyDocumentId])
@@index([extractionMethod])
@@map("documents")
@@schema("pkb_schema")
}
// ==================== Phase 3: 批处理模块 ====================
// 批处理任务
model BatchTask {
id String @id @default(uuid())
userId String @map("user_id")
kbId String @map("kb_id")
// 任务基本信息
name String // 任务名称(用户可自定义)
templateType String @map("template_type") // 'preset' | 'custom'
templateId String? @map("template_id") // 预设模板ID如'clinical_research'
prompt String @db.Text // 提示词(完整的)
// 执行状态
status String // 'processing' | 'completed' | 'failed' | 'paused'
totalDocuments Int @map("total_documents")
completedCount Int @default(0) @map("completed_count")
failedCount Int @default(0) @map("failed_count")
// 配置
modelType String @map("model_type") // 使用的模型
concurrency Int @default(3) // 固定为3
// 时间统计
startedAt DateTime? @map("started_at")
completedAt DateTime? @map("completed_at")
durationSeconds Int? @map("duration_seconds") // 执行时长(秒)
// 关联
results BatchResult[]
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
knowledgeBase KnowledgeBase @relation(fields: [kbId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@index([userId])
@@index([kbId])
@@index([status])
@@index([createdAt])
@@map("batch_tasks")
@@schema("pkb_schema")
}
// 批处理结果(每篇文献一条)
model BatchResult {
id String @id @default(uuid())
taskId String @map("task_id")
documentId String @map("document_id")
// 执行结果
status String // 'success' | 'failed'
data Json? // 提取的结构化数据(预设模板)或文本(自定义)
rawOutput String? @db.Text @map("raw_output") // AI原始输出备份
errorMessage String? @db.Text @map("error_message") // 错误信息
// 性能指标
processingTimeMs Int? @map("processing_time_ms") // 处理时长(毫秒)
tokensUsed Int? @map("tokens_used") // Token使用量
// 关联
task BatchTask @relation(fields: [taskId], references: [id], onDelete: Cascade)
document Document @relation(fields: [documentId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
@@index([taskId])
@@index([documentId])
@@index([status])
@@map("batch_results")
@@schema("pkb_schema")
}
// 任务模板(暂不实现,预留)
model TaskTemplate {
id String @id @default(uuid())
userId String @map("user_id")
name String
description String?
prompt String @db.Text
outputFields Json // 期望的输出字段定义
isPublic Boolean @default(false) @map("is_public")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@index([userId])
@@map("task_templates")
@@schema("pkb_schema")
}
// ==================== 运营管理模块 ====================
@@ -187,6 +302,7 @@ model AdminLog {
@@index([createdAt])
@@index([action])
@@map("admin_logs")
@@schema("public")
}
// ==================== 通用对话模块 ====================
@@ -208,6 +324,7 @@ model GeneralConversation {
@@index([createdAt])
@@index([updatedAt])
@@map("general_conversations")
@@schema("aia_schema")
}
model GeneralMessage {
@@ -226,4 +343,51 @@ model GeneralMessage {
@@index([conversationId])
@@index([createdAt])
@@map("general_messages")
@@schema("aia_schema")
}
// ==================== 稿件审查模块 ====================
// 稿件审查任务
model ReviewTask {
id String @id @default(uuid())
userId String @map("user_id")
// 文件信息
fileName String @map("file_name")
fileSize Int @map("file_size")
filePath String? @map("file_path")
// 文档内容
extractedText String @map("extracted_text") @db.Text
wordCount Int? @map("word_count")
// 执行状态
status String @default("pending")
// pending, extracting, reviewing_editorial, reviewing_methodology, completed, failed
// 评估结果JSON
editorialReview Json? @map("editorial_review")
methodologyReview Json? @map("methodology_review")
overallScore Float? @map("overall_score")
// 执行信息
modelUsed String? @map("model_used")
startedAt DateTime? @map("started_at")
completedAt DateTime? @map("completed_at")
durationSeconds Int? @map("duration_seconds")
errorMessage String? @map("error_message") @db.Text
// 元数据
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
// 关联
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@index([status])
@@index([createdAt])
@@map("review_tasks")
@@schema("public")
}