refactor: 后端仓库层收口(A1:课程/产品/素材)+ 收进工作区既有对象化重构
本提交含两部分。第一部分是本轮工作;第二部分是此前一直留在工作区、
从未提交的对象化重构,与第一部分在文件上互相咬合(internal/repository
整个包都是未跟踪状态,且 api 层已有文件引用它),无法拆成两个可编译的提交。
一、仓库层收口 A1 批(本轮工作)
把 api 层手写的 store.DB 查询收进具名仓库方法,只给真正获益的对象做方法,
不机械包裹全量。本批迁移 22 处裸查询(courses.go 9 / media.go 12 / products.go 1),
新增方法:
- MediaFileRepo.ListByBind / ListForAudit / MarkExtracted
- KnowledgeChunkRepo.CountByMediaFile
- ProductRepo.GetVisibleByID
两条业务口径改由仓库单点持有,避免各处手写漂移:
「只有 approved 素材出现在课程详情」与「已停用产品不在课程详情露出」。
修掉两个真实缺陷:
- ProductRepo.GetByID 缺 Where 条件。此前 GET /api/products/{id} 对任意 id 都返回
第一条产品、对不存在的 id 返回 200,且 PUT /api/products/{id} 会覆盖第一条产品
—— 数据损坏级。全仓扫描确认这是唯一一处同型写法。
- ProductRepo.Delete 写 status="deleted",而 DELETE 处理器文档与回包都声称
"inactive",接口在说谎;管理员用 status=all 拉列表会看到前端不认识的状态。
已对齐为 inactive(与 CourseRepo.Delete 一致)。
删除 8 个零调用且列名不存在的死方法(一调即 SQL 报错):
- media_file 上的 file_path / file_type / approval_status 三列并不存在,
GetByPath / ListByType / UpdateStatus 全废
- knowledge_chunk 上的 space_id 列不存在(模型早已改为 knowledge_space_key),
List / Total / ListBySpaceIDs / DeleteBySpace / SearchByVector 全废
取舍边界:能对当前 schema 跑通的死方法保留,跑不通的删或修。
CourseRepo.List 补齐 status=all 档(此前传给它会当作 status='all' 过滤出空列表)。
该方法此前零调用,现与产品列表语义对齐。
验证:go build ./... 与 go test ./... 全绿;另用真实 HTTP 请求验证 34 项
(课程 17 / 产品 3 / 素材 14),跑在数据库副本与独立 KB_DATA_DIR 上,
含 multipart 真上传 → 审批 → pdftotext 提取 → 分片入库的完整链路。
二、此前未提交的对象化重构(非本轮工作)
- 新增 internal/repository 仓库层、connectors、skills、specialists、xapps、jsonutil,
model/task_record|task_run|task_artifact、api/task_runtime|action_definition|chat_message
- 删除 api/app_definition、connectors、my_app_center、notification、office_skill、
export_docx|pptx|xlsx、official_account_* 等,随 XApp/Skill/Specialist/Connector
可插拔打包方向(AR10/AR11)调整
- 资产目录归位:backend-go/knowledge_source → assets/knowledge/source、
training_materials → assets/training/materials;README 内相对路径同步加深两级;
deploy env 补 ASSET_ROOT_DIR 并改 KNOWLEDGE_SOURCE_DIR / TRAINING_MATERIALS_DIR
- 前端新增 skills/ specialists/ connectors/ xapps/ 目录与对应页面
验证:前端 npm run build 通过(7.26s)。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -16,10 +16,13 @@ import (
|
||||
"eai_agentplatform/backend/internal/auth"
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
"eai_agentplatform/backend/internal/repository"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
var mistakeRepo repository.MistakeRecordRepo
|
||||
|
||||
// Option 题目选项
|
||||
type Option struct {
|
||||
Key string `json:"key"`
|
||||
@@ -213,11 +216,6 @@ func CreatePaper(c *gin.Context) {
|
||||
web.Fail(c, web.NewBadRequest("创建考试失败"))
|
||||
return
|
||||
}
|
||||
// 发布正式考试:通知全员(提醒及时参加)
|
||||
if p.Type == "formal" {
|
||||
notifyAllEmployees("exam_publish", "新考试发布",
|
||||
fmt.Sprintf("管理员发布了正式考试「%s」,请及时参加", p.Name), "/exam/formal")
|
||||
}
|
||||
web.OK(c, p)
|
||||
}
|
||||
|
||||
@@ -306,12 +304,8 @@ func ExamList(c *gin.Context) {
|
||||
out := make([]row, 0, len(items))
|
||||
for _, p := range items {
|
||||
st := "available"
|
||||
if p.Type == "formal" && u != nil {
|
||||
var n int64
|
||||
store.DB.Model(&model.ExamRecord{}).Where("user_id = ? AND paper_id = ?", u.ID, p.ID).Count(&n)
|
||||
if n > 0 {
|
||||
st = "completed"
|
||||
}
|
||||
if p.Type == "formal" && u != nil && examRecordRepo.HasTaken(u.ID, p.ID) {
|
||||
st = "completed"
|
||||
}
|
||||
out = append(out, row{
|
||||
ID: p.ID, Name: p.Name, Type: p.Type, Domain: p.Domain,
|
||||
@@ -322,9 +316,9 @@ func ExamList(c *gin.Context) {
|
||||
web.OK(c, out)
|
||||
}
|
||||
|
||||
// ExamCover GET /api/exam/cover?id={paperId}
|
||||
// ExamCover GET /api/exam/cover?paper_id={paper_id}
|
||||
func ExamCover(c *gin.Context) {
|
||||
id64, err := strconv.ParseUint(c.Query("id"), 10, 64)
|
||||
id64, err := strconv.ParseUint(c.Query("paper_id"), 10, 64)
|
||||
if err != nil || id64 == 0 {
|
||||
web.Fail(c, web.NewBadRequest("无效的 id"))
|
||||
return
|
||||
@@ -564,13 +558,9 @@ func ExamStart(c *gin.Context) {
|
||||
web.Fail(c, web.NewNotFoundError("考试不存在或已停用"))
|
||||
return
|
||||
}
|
||||
if p.Type == "formal" && u != nil {
|
||||
var n int64
|
||||
store.DB.Model(&model.ExamRecord{}).Where("user_id = ? AND paper_id = ?", u.ID, p.ID).Count(&n)
|
||||
if n > 0 {
|
||||
web.Fail(c, web.NewConflictError("已参加过该正式考试"))
|
||||
return
|
||||
}
|
||||
if p.Type == "formal" && u != nil && examRecordRepo.HasTaken(u.ID, p.ID) {
|
||||
web.Fail(c, web.NewConflictError("已参加过该正式考试"))
|
||||
return
|
||||
}
|
||||
questions, err := pickQuestions(p)
|
||||
if err != nil {
|
||||
@@ -817,13 +807,9 @@ func ExamSubmit(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 正式考不可重复交卷
|
||||
if stype == "formal" && u != nil {
|
||||
var n int64
|
||||
store.DB.Model(&model.ExamRecord{}).Where("user_id = ? AND paper_id = ?", u.ID, p.ID).Count(&n)
|
||||
if n > 0 {
|
||||
web.Fail(c, web.NewConflictError("已参加过该正式考试"))
|
||||
return
|
||||
}
|
||||
if stype == "formal" && u != nil && examRecordRepo.HasTaken(u.ID, p.ID) {
|
||||
web.Fail(c, web.NewConflictError("已参加过该正式考试"))
|
||||
return
|
||||
}
|
||||
|
||||
ids := splitIDs(qidsStr)
|
||||
@@ -931,9 +917,6 @@ func ExamSubmit(c *gin.Context) {
|
||||
if passed {
|
||||
awardPoints(u.ID, "formal_pass", ptFormalPass, "paper", p.ID)
|
||||
issueCertificate(u, rec)
|
||||
notifyUser(u.ID, "exam_pass", "考试通过",
|
||||
fmt.Sprintf("恭喜通过「%s」考试,成绩 %d 分,已颁发合格证书", p.Name, score),
|
||||
"/exam/my-certificates")
|
||||
}
|
||||
} else if stype == "self_test" && u != nil {
|
||||
// 自测不落分、不存档(P03),仅计一次练习积分
|
||||
@@ -960,19 +943,17 @@ func splitIDs(s string) []uint {
|
||||
// ExamRecordList GET /api/exam/record?page=&size= —— 我的考试记录
|
||||
func ExamRecordList(c *gin.Context) {
|
||||
u := middleware.CurrentUser(c)
|
||||
q := store.DB.Model(&model.ExamRecord{}).Where("user_id = ?", u.ID)
|
||||
var items []model.ExamRecord
|
||||
if err := q.Order("submitted_at DESC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询考试记录失败"))
|
||||
return
|
||||
items := examRecordRepo.ListByUser(u.ID)
|
||||
if items == nil {
|
||||
items = []model.ExamRecord{}
|
||||
}
|
||||
web.OK(c, items)
|
||||
}
|
||||
|
||||
// ExamRecordDetail GET /api/exam/record/{recordId} —— 记录详情回溯
|
||||
// ExamRecordDetail GET /api/exam/record/{record_id} —— 记录详情回溯
|
||||
func ExamRecordDetail(c *gin.Context) {
|
||||
u := middleware.CurrentUser(c)
|
||||
id, ok := parseID(c, "recordId")
|
||||
id, ok := parseID(c, "record_id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -997,28 +978,22 @@ func ExamRecordDetail(c *gin.Context) {
|
||||
|
||||
// ============ 错题本(学员自助) ============
|
||||
|
||||
// recordMistake 答错入本:按 (user_id, question_id, source) 去重,再次答错则更新并重置为未掌握。
|
||||
func recordMistake(userID, questionID uint, q model.Question, userAns any, correct []string, source string) {
|
||||
// mistakePayload 组装错题本记录:作答与正确答案以 JSON 落库,
|
||||
// 与 mistakeView 的反序列化、MyMistakes 的出参形状对齐。
|
||||
func mistakePayload(userID, questionID uint, source string, q model.Question, userAns any, correct []string) model.MistakeRecord {
|
||||
userAnsJSON, _ := json.Marshal(userAns)
|
||||
correctJSON, _ := json.Marshal(correct)
|
||||
var rec model.MistakeRecord
|
||||
err := store.DB.Where("user_id = ? AND question_id = ? AND source = ?", userID, questionID, source).First(&rec).Error
|
||||
if err != nil {
|
||||
store.DB.Create(&model.MistakeRecord{
|
||||
UserID: userID, QuestionID: questionID, Source: source,
|
||||
QuestionType: q.Type, QuestionStem: q.Stem,
|
||||
UserAnswer: string(userAnsJSON), CorrectAnswer: string(correctJSON),
|
||||
Explanation: q.Explanation, Resolved: false,
|
||||
})
|
||||
return
|
||||
return model.MistakeRecord{
|
||||
UserID: userID, QuestionID: questionID, Source: source,
|
||||
QuestionType: q.Type, QuestionStem: q.Stem,
|
||||
UserAnswer: string(userAnsJSON), CorrectAnswer: string(correctJSON),
|
||||
Explanation: q.Explanation,
|
||||
}
|
||||
rec.QuestionType = q.Type
|
||||
rec.QuestionStem = q.Stem
|
||||
rec.UserAnswer = string(userAnsJSON)
|
||||
rec.CorrectAnswer = string(correctJSON)
|
||||
rec.Explanation = q.Explanation
|
||||
rec.Resolved = false
|
||||
store.DB.Save(&rec)
|
||||
}
|
||||
|
||||
// recordMistake 答错入本:按 (user_id, question_id, source) 去重,再次答错则更新并重置为未掌握。
|
||||
func recordMistake(userID, questionID uint, q model.Question, userAns any, correct []string, source string) {
|
||||
mistakeRepo.RecordWrong(mistakePayload(userID, questionID, source, q, userAns, correct))
|
||||
}
|
||||
|
||||
// mistakeView 错题出参(answer 反序列化,便于前端直接展示)
|
||||
@@ -1038,11 +1013,7 @@ type mistakeView struct {
|
||||
// MyMistakes GET /api/exam/mistakes —— 我的错题本
|
||||
func MyMistakes(c *gin.Context) {
|
||||
u := middleware.CurrentUser(c)
|
||||
var items []model.MistakeRecord
|
||||
if err := store.DB.Where("user_id = ?", u.ID).Order("updated_at DESC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询错题本失败"))
|
||||
return
|
||||
}
|
||||
items := mistakeRepo.ListByUser(u.ID)
|
||||
out := make([]mistakeView, 0, len(items))
|
||||
for _, it := range items {
|
||||
var userAns any
|
||||
@@ -1069,8 +1040,8 @@ func ResolveMistake(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var rec model.MistakeRecord
|
||||
if err := store.DB.First(&rec, id).Error; err != nil {
|
||||
rec, found := mistakeRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("错题记录不存在"))
|
||||
return
|
||||
}
|
||||
@@ -1088,7 +1059,7 @@ func ResolveMistake(c *gin.Context) {
|
||||
}
|
||||
wasResolved := rec.Resolved
|
||||
rec.Resolved = target
|
||||
if err := store.DB.Save(&rec).Error; err != nil {
|
||||
if !mistakeRepo.Update(&rec) {
|
||||
web.Fail(c, web.NewBadRequest("更新错题状态失败"))
|
||||
return
|
||||
}
|
||||
@@ -1112,18 +1083,7 @@ func MistakePractice(c *gin.Context) {
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
|
||||
q := store.DB.Where("user_id = ?", u.ID)
|
||||
if req.Source != "" {
|
||||
q = q.Where("source = ?", req.Source)
|
||||
}
|
||||
if req.OnlyUnresolved {
|
||||
q = q.Where("resolved = ?", false)
|
||||
}
|
||||
var recs []model.MistakeRecord
|
||||
if err := q.Order("updated_at DESC").Find(&recs).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询错题失败"))
|
||||
return
|
||||
}
|
||||
recs := mistakeRepo.ListForPractice(u.ID, req.Source, req.OnlyUnresolved)
|
||||
if len(recs) == 0 {
|
||||
web.Fail(c, web.NewBadRequest("暂无可重练的错题"))
|
||||
return
|
||||
@@ -1187,26 +1147,16 @@ func MistakePractice(c *gin.Context) {
|
||||
|
||||
// touchMistakeOnPractice 错题重练判分后同步错题状态:答对置为已掌握(加积分),答错重置为未掌握。
|
||||
func touchMistakeOnPractice(userID, questionID uint, q model.Question, userAns any, correct []string, resolved bool) {
|
||||
var recs []model.MistakeRecord
|
||||
store.DB.Where("user_id = ? AND question_id = ?", userID, questionID).Find(&recs)
|
||||
if len(recs) == 0 {
|
||||
payload := mistakePayload(userID, questionID, "", q, userAns, correct)
|
||||
flipped, touched := mistakeRepo.TouchOnPractice(payload, resolved)
|
||||
if !touched {
|
||||
// 防御性兜底:理论上重练题目均来自错题本,这里创建一条
|
||||
recordMistake(userID, questionID, q, userAns, correct, "re_practice")
|
||||
payload.Source = "re_practice"
|
||||
mistakeRepo.RecordWrong(payload)
|
||||
return
|
||||
}
|
||||
userAnsJSON, _ := json.Marshal(userAns)
|
||||
correctJSON, _ := json.Marshal(correct)
|
||||
for i := range recs {
|
||||
was := recs[i].Resolved
|
||||
recs[i].Resolved = resolved
|
||||
recs[i].QuestionType = q.Type
|
||||
recs[i].QuestionStem = q.Stem
|
||||
recs[i].UserAnswer = string(userAnsJSON)
|
||||
recs[i].CorrectAnswer = string(correctJSON)
|
||||
recs[i].Explanation = q.Explanation
|
||||
store.DB.Save(&recs[i])
|
||||
if resolved && !was {
|
||||
awardPoints(userID, "mistake_resolved", ptMistakeResolved, "mistake", recs[i].ID)
|
||||
}
|
||||
// 仅在「未掌握 → 已掌握」时加分,避免反复重练刷分
|
||||
for _, id := range flipped {
|
||||
awardPoints(userID, "mistake_resolved", ptMistakeResolved, "mistake", id)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user