Files
wrong-notebook/prisma/schema.prisma
margrop 5f0b5b120d feat: 新增多题录入批次:服务端分题草稿、逐题识别任务与 AI 分题接口
- 新增 ImportBatch 表(迁移只做加法),ImportTask 增加所属批次、题目标识与文字任务原文;
  文字任务按「文字 AI 解题」相同的年级规则与候选知识点解析,题干保留用户确认的原文
- 批次接口:创建(幂等)、读取(整页图按需返回)、带版本号保存草稿、开始、重试、按识别结果批量入库、丢弃、已读;
  开始时在一个事务内抢占草稿并建出全部任务,提交后才逐题计额度并在后台执行,失败整体回滚、批次仍可编辑
- 本地文字分题规则:按同一写法连续递增的题号分题,选项与小问留在原题,标题/姓名栏列为未计入内容,
  公共材料只作用于所属题组;编号像小问时保守地按一道题,超出 60 题或 2 万字时不静默截断
- 四个提供商新增 splitQuestions:文字只返回行号范围、由原文拼回;图片随请求发送真实图片,区域按 0~1000 校验;
  JSON 截断、越界、重叠、空结果统一校验,模型不支持图片时给出明确错误
- 批次页:分题预览(选择、编辑、拆分、合并、移动、删除、撤销、原图区域)、统一进度与逐题结果,只重试失败的题;
  识别任务列表把批次作为「一批 N 题」显示,逐题校对保存后回到本批
- 备份升级到第 6 版,导出未完成的批次及批次内任务,导入不调用 AI,旧版文件照常导入
2026-11-03 14:06:26 +08:00

355 lines
15 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-musl-openssl-3.0.x"]
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
password String
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
educationStage String? // primary, junior_high, senior_high, university
enrollmentYear Int?
gradeRecognitionMode String @default("fixed") // 录入年级规则:fixed 固定,ai 按题目识别
fixedGradeSemester String? // 固定年级学期,未指定时沿用账户当前年级
role String @default("user") // "admin" or "user"
isActive Boolean @default(true) // 用户是否启用
confirmBeforeImport Boolean @default(false) // 导入题目前是否进入人工校对页
questionDuplicateThreshold Float @default(90) // 百分比,相似度严格大于此值时判重
masteryThreshold Int @default(50) // 百分比,掌握概率达到此值视为已掌握
enableGeogebra Boolean @default(false) // 是否显示 GeoGebra 动态演示
enableSvgAnimation Boolean @default(true) // 是否显示 SVG 动画讲解
themePreference String @default("system") // 界面主题:system 跟随系统、light 浅色、dark 深色
errorItems ErrorItem[]
subjects Subject[]
practiceRecords PracticeRecord[]
customTags KnowledgeTag[] // User's custom tags
examPapers ExamPaper[]
aiUsages AiUsage[]
paperGroups PaperGroup[]
importTasks ImportTask[]
importBatches ImportBatch[]
}
// 统一知识点标签表 (支持无限层级)
model KnowledgeTag {
id String @id @default(cuid())
name String // 标签名,如 "勾股定理"
subject String // 学科,如 "math", "english"
// 层级控制 (邻接表模式)
parentId String?
parent KnowledgeTag? @relation("TagHierarchy", fields: [parentId], references: [id], onDelete: SetNull)
children KnowledgeTag[] @relation("TagHierarchy")
// 排序与编码
order Int @default(0) // 用于保持教材顺序
code String? // 如 "1.2.1"
// 区分系统预设 vs 用户自定义
isSystem Boolean @default(false)
userId String? // 如果是用户自定义标签,绑定 userId
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
// 标签体系:标识标签所属的数据源/体系,不同体系可并存于同一学科
tagSet String @default("default")
// 与错题的多对多关系
errorItems ErrorItem[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// 联合索引:确保同一用户(或系统)下同一父节点、同一体系下的标签名不重复
@@unique([subject, name, userId, parentId, tagSet])
// 索引:加速父子查询
@@index([parentId])
@@index([subject])
@@index([tagSet])
}
model Subject {
id String @id @default(cuid())
name String
stage String @default("primary") // 学段:primary / junior_high / senior_high,仅创建时设置
subjectKey String? // 学科:math / english / physics 等,仅创建时设置;为空时按名称推断
tagSet String @default("default") // 标签体系:default=内置体系,其他值为导入的备选体系
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
errorItems ErrorItem[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([name, userId])
@@index([userId])
}
model PaperGroup {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
key String? // 系统预留:'a' | 'b' | 'other';自定义为 null
name String // 显示名:A卷/B卷/单元测试/期中/期末…
order Int @default(0)
isActive Boolean @default(true) // 停用后不出现下拉里,但历史数据仍保留
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
errorItems ErrorItem[]
@@unique([userId, key])
@@unique([userId, name])
@@index([userId, order])
}
model ErrorItem {
id String @id @default(cuid())
shortId String? @unique // 6 位 base58 短 ID,创建后不变
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
subjectId String?
subject Subject? @relation(fields: [subjectId], references: [id], onDelete: Cascade)
originalImageUrl String
ocrText String?
// AI Analysis
questionText String? // Parsed question
answerText String? // AI provided answer
analysis String? // AI analysis/explanation
wrongAnswerText String? // Student's wrong answer/work if visible
mistakeAnalysis String? // Analysis of why the wrong answer happened
mistakeStatus String? // not_attempted, wrong_attempt, unknown
knowledgePoints String? // [DEPRECATED] JSON string - kept for migration only
geogebraCommands String? // GeoGebra commands for interactive demo (JSON or newline-separated)
svgAnimation String? // AI 生成的 SVG 动画讲解(已净化),中文版
svgAnimationEn String? // 英文版 SVG 动画讲解(已净化),界面为英文时使用
// New: many-to-many relation with KnowledgeTag
tags KnowledgeTag[]
// User Metadata
source String? // e.g. "Midterm Exam"
errorType String? // e.g. "Calculation", "Concept"
userNotes String?
masteryLevel Int @default(0) // 0: New, 1: Reviewing, 2: Mastered
gradeSemester String? // e.g. "Junior High Grade 2, 1st Semester"
paperLevel String? // 现在存 PaperGroup.id
paperGroup PaperGroup? @relation(fields: [paperLevel], references: [id], onDelete: SetNull)
difficultyStars Int? // 难度星级 1~5(参考高思导引星级),未评定时为空
hasFigure Boolean @default(false) // 识别时判断题目是否有配图
figureSvg String? // AI 用 SVG 重绘的题目配图(已净化),显示和打印时使用
figureScale Int @default(100) // 配图显示与打印比例,10~200%
figureAlign String @default("center") // 配图显示与打印位置:left | center | right
answerSpacePx Int? // 打印时手动设置的题下留白高度(0~600 px),为空时按全局留白档位
lastPractice String? // 最近一次生成的举一反三题目与解答(JSON),再次进入练习页时直接显示
// 根据举一反三历史推测的掌握情况
masteryProbability Float? // 0~1
masteryReason String?
masterySource String? // ai | rule
masteryEvaluatedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
reviewSchedules ReviewSchedule[]
practiceRecords PracticeRecord[]
examQuestions ExamQuestion[]
importTasks ImportTask[]
// 列表、统计都按用户取错题并按时间排序;SQLite 不会自动给外键建索引
@@index([userId, createdAt])
@@index([subjectId])
}
model ReviewSchedule {
id String @id @default(cuid())
errorItemId String
errorItem ErrorItem @relation(fields: [errorItemId], references: [id], onDelete: Cascade)
scheduledFor DateTime
completedAt DateTime?
isCorrect Boolean? // Result of the review
createdAt DateTime @default(now())
@@index([errorItemId])
}
model PracticeRecord {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
subject String? // e.g. "Math", "Physics"
difficulty String? // "easy", "medium", "hard", "harder"
isCorrect Boolean?
// 举一反三记录:关联原题,保存生成的题目与结果
errorItemId String?
errorItem ErrorItem? @relation(fields: [errorItemId], references: [id], onDelete: Cascade)
result String? // correct | wrong | not_attempted
questionText String?
answerText String?
analysis String?
wrongAnswerText String? // 本次作答快照,避免重复导入时覆盖原题
mistakeAnalysis String?
originalImageUrl String?
figureSvg String? // 举一反三新题的 SVG 配图
source String @default("practice") // practice | manual | exam | duplicate_import
// 试卷题目可多次录入结果,每次一条记录
examQuestionId String?
examQuestion ExamQuestion? @relation(fields: [examQuestionId], references: [id], onDelete: SetNull)
createdAt DateTime @default(now())
@@index([errorItemId, createdAt])
@@index([examQuestionId])
@@index([userId, createdAt])
}
// 随机错题试卷
model ExamPaper {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
title String
status String @default("generating") // generating | ready | partial | failed
scope String // unmastered | mastered | all
difficulty String // easy | medium | hard | harder
requestedCount Int
subjectIds String // JSON 数组
tagNames String? // JSON 数组
resultsRecordedAt DateTime?
questions ExamQuestion[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId, createdAt])
}
model ExamQuestion {
id String @id @default(cuid())
paperId String
paper ExamPaper @relation(fields: [paperId], references: [id], onDelete: Cascade)
order Int
errorItemId String?
errorItem ErrorItem? @relation(fields: [errorItemId], references: [id], onDelete: SetNull)
sourceShortId String // 来源题目短 ID 快照,原题删除后仍保留
status String @default("pending") // pending | ready | failed
questionText String?
answerText String?
analysis String?
figureSvg String? // 新题的 SVG 配图
figureScale Int @default(100) // 配图显示与打印比例,10~200%
figureAlign String @default("center") // 配图显示与打印位置:left | center | right
answerSpacePx Int? // 打印时手动设置的题下留白高度(0~600 px),为空时按全局留白档位
error String?
practiceRecordId String? // [DEPRECATED] 旧版一对一关联,部署校验要求保留已有列,改用 PracticeRecord.examQuestionId
practiceRecords PracticeRecord[]
@@index([paperId, order])
}
// 用户发起的 AI 调用记录,用于每日额度和用量统计(不含后台自动的掌握概率评估)
model AiUsage {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
kind String // analyze | reanswer | practice | grade | figure | latex | geogebra | animation | exam | retag
createdAt DateTime @default(now())
@@index([userId, createdAt])
}
// 拍照识别导入任务:上传后由服务端识别并保存结果,离开页面、锁屏或服务重启后仍可取回,不重复调用 AI
model ImportTask {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
clientRequestId String? // 前端生成的幂等键:同一用户重复提交时返回同一任务
mode String // review 待人工校对 | auto 识别后直接入库(创建时按「导入前人工确认」决定)
status String @default("analyzing") // analyzing 识别中 | review 待校对 | saving 保存中 | saved 已保存 | failed 失败
language String @default("zh")
subjectId String? // 识别时参考的错题本(决定候选知识点)
targetSubjectId String? // 固定放入的错题本;为空时按识别学科匹配或在校对页选择
paperLevel String? // 指定的卷别分组
imageData String // 压缩后的题目图片(data URL),入库后清空,原图保存在错题中
imageInfo String? // 图片格式、像素与体积(JSON),不含图片内容
result String? // 识别结果(JSON),即待校对草稿;入库失败可直接重试保存
errorItemId String?
errorItem ErrorItem? @relation(fields: [errorItemId], references: [id], onDelete: SetNull)
batchId String? // 所属多题批次;为空表示单独上传的任务
batch ImportBatch? @relation(fields: [batchId], references: [id], onDelete: Cascade)
batchItemKey String? // 批次草稿中对应题目的标识
inputText String? // 文字任务的题目原文(含公共材料);图片任务为空
duplicate Boolean @default(false) // 入库时判为重复题,已记入原题的做错记录
error String? // 失败原因的错误码
timings String? // 各阶段耗时(JSON,毫秒)
attempts Int @default(1)
runnerId String? // 执行识别或保存的服务进程,用于判断服务重启导致的中断
heartbeatAt DateTime? // 执行中定时刷新,多实例部署时据此判断中断
readAt DateTime? // 用户查看结果的时间,为空时显示未读提示
finishedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([userId, clientRequestId])
@@index([userId, status])
@@index([errorItemId])
@@index([batchId])
}
/// 多题录入批次:一段多题文字或一张多题照片。分题结果作为草稿保存,用户确认后为每道题建一个识别任务。
model ImportBatch {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
clientRequestId String? // 前端生成的幂等键
source String // text 文字 | image 照片
splitter String // local 本地规则 | ai 模型分题
status String @default("draft") // splitting 分题中 | draft 待确认 | started 已开始 | failed 分题失败
mode String? // 开始时按「导入前人工确认」决定:review 逐题校对 | auto 直接入库
language String @default("zh")
subjectId String?
targetSubjectId String?
paperLevel String?
sourceText String? // 原始文字
imageData String @default("") // 整页图片(data URL)
imageInfo String?
draft String? // 分题草稿(JSON)
version Int @default(1) // 草稿版本,保存时校验,避免多处编辑互相覆盖
error String?
attempts Int @default(1)
runnerId String?
heartbeatAt DateTime?
readAt DateTime?
startedAt DateTime?
finishedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
tasks ImportTask[]
@@unique([userId, clientRequestId])
@@index([userId, status])
}