实现了包含试卷管理、考试答题、AI智能阅卷的完整考试流程。 **后端新增功能**: - 试卷管理: 创建试卷、获取试卷列表和详情 - 考试流程: 开始考试、提交答案、查询结果 - AI阅卷: 异步阅卷系统,支持简答题和论述题AI评分 - 实时答题: 题目级别的答案保存和加载 - 数据模型: ExamRecord(考试记录)、ExamUserAnswer(用户答案) **前端新增页面**: - 考试管理页面: 试卷列表展示,支持开始/继续考试 - 答题页面: 左侧题目列表、右侧答题区,支持实时保存 - 成绩查看页面: 展示详细评分结果和AI评语 **技术亮点**: - 按题型固定分值配置(总分100分) - 异步阅卷机制,提交后立即返回 - 答案实时保存,支持断点续答 - AI评分集成,智能评判主观题 - 响应式设计,适配移动端和PC端 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package database
|
|
|
|
import (
|
|
"ankao/internal/models"
|
|
"ankao/pkg/config"
|
|
"fmt"
|
|
"log"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
var DB *gorm.DB
|
|
|
|
// InitDB 初始化数据库连接
|
|
func InitDB() error {
|
|
cfg := config.GetDatabaseConfig()
|
|
dsn := cfg.GetDSN()
|
|
|
|
var err error
|
|
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Info), // 开启SQL日志
|
|
DisableForeignKeyConstraintWhenMigrating: true, // 迁移时禁用外键约束
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to database: %w", err)
|
|
}
|
|
|
|
log.Println("Database connected successfully")
|
|
|
|
// 自动迁移数据库表结构
|
|
err = DB.AutoMigrate(
|
|
&models.User{},
|
|
&models.PracticeQuestion{},
|
|
&models.WrongQuestion{}, // 错题表
|
|
&models.WrongQuestionHistory{}, // 错题历史表
|
|
&models.WrongQuestionTag{}, // 错题标签表
|
|
&models.UserAnswerRecord{}, // 用户答题记录表
|
|
&models.Exam{}, // 考试表(试卷)
|
|
&models.ExamRecord{}, // 考试记录表
|
|
&models.ExamUserAnswer{}, // 用户答案表
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to migrate database: %w", err)
|
|
}
|
|
|
|
log.Println("Database migration completed")
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetDB 获取数据库实例
|
|
func GetDB() *gorm.DB {
|
|
return DB
|
|
}
|