From eed295bd3c3254d4ffd72e136c566d5df03cad37 Mon Sep 17 00:00:00 2001 From: lijikun Date: Wed, 19 Nov 2025 10:04:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=BA=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Knowledge_Test_Go/api/v1/questionBank.go | 30 +++++++------- Knowledge_Test_Go/internal/cmd/cmd.go | 10 ++--- .../internal/logic/knowledge/knowledge.go | 6 +-- .../internal/logic/questionBank/questionBank.go | 47 +++++++++------------- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/Knowledge_Test_Go/api/v1/questionBank.go b/Knowledge_Test_Go/api/v1/questionBank.go index c8ebacc..d6ead50 100644 --- a/Knowledge_Test_Go/api/v1/questionBank.go +++ b/Knowledge_Test_Go/api/v1/questionBank.go @@ -74,11 +74,11 @@ type GetWrongQuestionsRes struct { type QuestionOutputReq struct { Stem string `json:"stem"` QuestionTypeId string `json:"question_type_id"` - CourseRecommendationId string `json:"course+recommendation_id"` + CourseRecommendationId string `json:"course_recommendation_id"` Page int `json:"page"` PageSize int `json:"page_size"` - SortField string `json:"sort_field"` // 排序字段:error_count, error_rate, id - SortOrder string `json:"sort_order"` // 排序方向:asc, desc + SortField string `json:"sort_field"` // 排序字段:error_count, error_rate, id + SortOrder string `json:"sort_order" d:"DESC"` // 排序方向:asc, desc } // QuestionOutputRes 题目输出 @@ -89,12 +89,12 @@ type QuestionOutputRes struct { B string `json:"B"` C string `json:"C"` D string `json:"D"` - CorrectAnswer string `json:"correctAnswer"` - QuestionTypeName string `json:"questionTypeName"` - CrName string `json:"CrName"` - CitationCount int `json:"citationCount"` - ErrorCount int `json:"errorCount"` - ErrorRate int `json:"errorRate"` + CorrectAnswer string `json:"correct_answer"` + QuestionTypeName string `json:"question_type_name"` + CrName string `json:"cr_name"` + CitationCount int `json:"citation_count"` + ErrorCount int `json:"error_count"` + ErrorRate int `json:"error_rate"` } // QuestionUpdateReq 修改题目请求 @@ -122,16 +122,16 @@ type UserScoreOutputRes struct { UserIdentity string `json:"user_identity"` Jwcode int `json:"jwcode"` Score int `json:"score"` - CreatedAt string `json:"createdAt"` + CreatedAt string `json:"created_at"` } // UserScoreOutputReq 获取用户成绩列表请求 type UserScoreOutputReq struct { - UserName string `json:"user_name"` // 用户名(精准查询) - UserIdentity string `json:"user_identity"` // 用户身份(过滤) - Jwcode string `json:"jwcode"` // 精网号(精准查询) - StartTime string `json:"start_time"` // 开始时间(提交时间范围) - EndTime string `json:"end_time"` // 结束时间(提交时间范围) + UserName string `json:"user_name"` // 用户名(精准查询) + UserIdentity string `json:"user_identity"` // 用户身份(过滤) + Jwcode string `json:"jwcode" v:"integer|length:8"` // 精网号(精准查询) + StartTime string `json:"start_time"` // 开始时间(提交时间范围) + EndTime string `json:"end_time"` // 结束时间(提交时间范围) Page int `json:"page"` PageSize int `json:"page_size"` SortField string `json:"sort_field"` // 排序字段:score, created_at diff --git a/Knowledge_Test_Go/internal/cmd/cmd.go b/Knowledge_Test_Go/internal/cmd/cmd.go index 25d9087..bf99fc9 100644 --- a/Knowledge_Test_Go/internal/cmd/cmd.go +++ b/Knowledge_Test_Go/internal/cmd/cmd.go @@ -23,11 +23,11 @@ var ( ghttp.MiddlewareHandlerResponse, ghttp.MiddlewareCORS, ) - group.POST("/knowledge/questions", controller.NewKnowledgeTest().GetQuestions) // 获取题目列表 - group.POST("/knowledge/submit", controller.NewKnowledgeTest().SubmitAnswers) // 提交答案 - group.POST("/knowledge/scores", controller.NewKnowledgeTest().GetUserScores) // 获取用户成绩 - group.POST("/knowledge/wrong-questions", controller.NewKnowledgeTest().GetWrongQuestions) // 获取错题列表 - group.POST("/knowledge/course", controller.NewKnowledgeTest().GetCourse) // 获取课程 + group.POST("/knowledge/questions", controller.NewKnowledgeTest().GetQuestions) // 获取题目列表 + group.POST("/knowledge/submit", controller.NewKnowledgeTest().SubmitAnswers) // 提交答案 + group.POST("/knowledge/scores", controller.NewKnowledgeTest().GetUserScores) // 获取用户成绩 + group.POST("/knowledge/wrongQuestions", controller.NewKnowledgeTest().GetWrongQuestions) // 获取错题列表 + group.POST("/knowledge/course", controller.NewKnowledgeTest().GetCourse) // 获取课程 }) // 后台路由组 diff --git a/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go b/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go index 9927058..8790ef1 100644 --- a/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go +++ b/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go @@ -225,9 +225,9 @@ func (s *sKnowledgeTest) updateErrorRatesInTransaction(ctx context.Context, tx g // GetWrongQuestions 获取错题列表 func (s *sKnowledgeTest) GetWrongQuestions(ctx context.Context, req *v1.GetWrongQuestionsReq) (res []*v1.GetWrongQuestionsRes, total int, err error) { - db := g.Model("question_record b"). + err = g.Model("question_record b"). RightJoin("question_bank a", "a.id=b.question_bank_id"). - Where("b.jwcode=?", req.Jwcode).Fields("a.id", "a.stem", "a.a", "a.b", "a.c", "a.d", "a.correct_answer", "b.user_answer") - err = db.Page(req.Page, req.PageSize).ScanAndCount(&res, &total, false) + Where("b.jwcode=?", req.Jwcode).Fields("a.id", "a.stem", "a.a", "a.b", "a.c", "a.d", "a.correct_answer", "b.user_answer"). + Page(req.Page, req.PageSize).ScanAndCount(&res, &total, false) return } diff --git a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go index 621c5f5..e6c76e4 100644 --- a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go +++ b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go @@ -6,6 +6,7 @@ import ( "Knowledge_Test_Go/utility/response" "context" "fmt" + "github.com/gogf/gf/v2/util/gconv" "regexp" "strconv" @@ -25,27 +26,27 @@ func init() { // GetQuestions 获取题目列表 func (s *sQuestionBank) GetQuestions(ctx context.Context, req *v1.QuestionOutputReq) (res []*v1.QuestionOutputRes, total int, err error) { - db := g.Model("question_bank a"). - LeftJoin("question_type b", "a.question_type_id = b.id"). - LeftJoin("course_recommend c", "a.course_recommendation_id = c.id"). - Where("a.isdel = 0"). - Fields("a.id", "a.stem", "a.a", "a.b", "a.c", "a.d", "a.correct_answer", - "b.question_type_name", "c.cr_name", "a.error_count", "a.citation_count", "a.error_rate") // 添加 error_rate 字段 + db := g.Model("question_bank qb"). //! 别名尽量别用a, b, c之类的 + LeftJoin("question_type qt", "qb.question_type_id = qt.id"). + LeftJoin("course_recommend cr", "qb.course_recommendation_id = cr.id"). + Where("qb.isdel = 0"). + Fields("qb.id", "qb.stem", "qb.a", "qb.b", "qb.c", "qb.d", "qb.correct_answer", + "qt.question_type_name", "cr.cr_name", "qb.error_count", "qb.citation_count", "qb.error_rate") // 添加 error_rate 字段 // 添加查询条件 if req.Stem != "" { // 对题干进行模糊查询 - db = db.Where("a.stem LIKE ?", "%"+req.Stem+"%") + db = db.Where("qb.stem LIKE ?", "%"+req.Stem+"%") } if req.QuestionTypeId != "" { // 对题目类型ID进行精准查询 - db = db.Where("a.question_type_id = ?", req.QuestionTypeId) + db = db.Where("qb.question_type_id = ?", req.QuestionTypeId) } if req.CourseRecommendationId != "" { // 对课程推荐ID进行精准查询 - db = db.Where("a.course_recommendation_id = ?", req.CourseRecommendationId) + db = db.Where("qb.course_recommendation_id = ?", req.CourseRecommendationId) } // 添加排序逻辑 @@ -59,26 +60,20 @@ func (s *sQuestionBank) GetQuestions(ctx context.Context, req *v1.QuestionOutput func (s *sQuestionBank) buildSortCondition(db *gdb.Model, req *v1.QuestionOutputReq) *gdb.Model { // 默认排序(按ID倒序) if req.SortField == "" { - return db.OrderDesc("a.id") - } - - // 确定排序方向 - order := "DESC" - if req.SortOrder == "asc" { - order = "ASC" + return db.OrderDesc("a.created_at") //! 尽量不要用id进行排序 } // 根据排序字段构建排序条件 switch req.SortField { case "error_count": - return db.Order("a.error_count " + order) + return db.Order("a.error_count " + req.SortOrder) case "error_rate": - return db.Order("a.error_rate " + order) + return db.Order("a.error_rate " + req.SortOrder) case "id": - return db.Order("a.id " + order) + return db.Order("a.id " + req.SortOrder) default: // 默认按ID倒序 - return db.OrderDesc("a.id") + return db.OrderDesc("a.created_at") } } @@ -100,14 +95,12 @@ func (s *sQuestionBank) QuestionUpdate(ctx context.Context, req *v1.QuestionUpda if req.Id == 0 { // 新增记录 - result, err := g.Model("question_bank").Data(data).Insert() + result, err := g.Model("question_bank").Data(data).InsertAndGetId() if err != nil { return "新增失败", err } - // 获取新增的ID - id, _ := result.LastInsertId() - return fmt.Sprintf("新增成功,ID: %d", id), nil + return fmt.Sprintf("新增成功,ID: %d", gconv.Int(result)), nil } else { // 更新记录 _, err := g.Model("question_bank").Where("id = ?", req.Id).Data(data).Update() @@ -140,9 +133,9 @@ func (s *sQuestionBank) QuestionDel(ctx context.Context, req *v1.QuestionDelReq) // UserScoreOutput 获取用户成绩列表 func (s *sQuestionBank) UserScoreOutput(ctx context.Context, req *v1.UserScoreOutputReq) (res []*v1.UserScoreOutputRes, total int, err error) { - db := g.Model("user a"). - RightJoin("total_score b", "a.jwcode = b.jwcode"). - Fields("a.id", "a.user_name", "a.user_identity", "a.jwcode", "b.created_at", "b.score") + db := g.Model("user a"). //! 别名尽量别用a, b, c之类的 + RightJoin("total_score b", "a.jwcode = b.jwcode"). + Fields("a.id", "a.user_name", "a.user_identity", "a.jwcode", "b.created_at", "b.score") // 添加查询条件 if req.UserName != "" {