Files
wrong-notebook/prisma/schema.prisma
margrop 00763232b5 feat: 试卷按题型分大题显示与打印
- ExamQuestion 新增 questionType(创建时快照来源错题题型),ExamPaper 新增 groupByType 开关(默认关闭),生成正式迁移
- 新增纯函数 src/lib/exam/exam-sections.ts:按 QUESTION_TYPES 顺序分组,中文「一、选择题」/ 英文 "Part I. Single Choice" 标题,全卷连续题号,分值小计;快照为空回退关联错题当前题型,仍为空归入「其他」
- PATCH /api/exams/:id 支持 { groupByType },与配图、分值更新互斥;GET 详情返回每题 questionType 与 errorItemQuestionType
- 试卷详情页新增「按题型分大题」开关,开启后分组显示大题标题(题数与分值小计);打印与导出 HTML 同样分组,大题标题加粗带边框适合黑白打印;关闭时与旧版一致
- 备份版本升至 11:导出/导入 groupByType 与 questionType,旧版文件缺省 false/null
- 测试:exam-sections 单测、PATCH 集成测试、打印组件分组测试、备份往返与旧版兼容
- 文档:wiki 试卷页(中英文)与备份页版本说明,AGENTS.md 版本号
2026-11-04 13:00:14 +08:00

363 lines
16 KiB
Plaintext

// 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
// 题型,取值来自 src/lib/question/question-type.ts 的 QUESTION_TYPES
questionType String?
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
mode String @default("generate") // generate 举一反三新题 | original 原题直出
requestedCount Int
// 是否按题型分大题显示与打印,默认关闭(兼容旧试卷)
groupByType Boolean @default(false)
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),为空时按全局留白档位
score Float? // 每题分值(最多一位小数,0~1000),空表示未设分值
// 创建时从来源错题快照的题型;旧数据为 null,分组时回退读取关联错题当前题型
questionType String?
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])
}