From a1b0b17c11657a104fa398e6a6b060a9c9ee2065 Mon Sep 17 00:00:00 2001 From: wangguixi Date: Wed, 12 Nov 2025 14:57:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E9=A2=98=E7=9B=AE=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2,=E6=9B=B4=E6=96=B0,=E4=BF=AE=E6=94=B9,=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Knowledge_Test_Go/api/v1/questionBank.go | 58 ++++++++++--- Knowledge_Test_Go/internal/cmd/cmd.go | 30 ++++--- .../internal/controller/questionBank.go | 63 ++++++++++++++ .../internal/logic/knowledge/knowledge.go | 1 + .../internal/logic/questionBank/questionBank.go | 99 +++++++++++++++++++++- 5 files changed, 222 insertions(+), 29 deletions(-) diff --git a/Knowledge_Test_Go/api/v1/questionBank.go b/Knowledge_Test_Go/api/v1/questionBank.go index 35f8601..5510091 100644 --- a/Knowledge_Test_Go/api/v1/questionBank.go +++ b/Knowledge_Test_Go/api/v1/questionBank.go @@ -51,21 +51,11 @@ type GetUserScoresRes struct { Score int `json:"score"` } -// QuestionOutput 题目输出 -type QuestionOutput struct { - Id int `json:"id"` - Stem string `json:"stem"` - A string `json:"A"` - B string `json:"B"` - C string `json:"C"` - D string `json:"D"` -} - // GetWrongQuestionsRes 错题详情 type GetWrongQuestionsRes struct { - QuestionOutput `json:"question"` - UserAnswer string `json:"userAnswer" dc:"用户答案"` - CorrectAnswer string `json:"correctAnswer" dc:"正确答案"` + GetQuestionsRes `json:"question"` + UserAnswer string `json:"userAnswer" dc:"用户答案"` + CorrectAnswer string `json:"correctAnswer" dc:"正确答案"` } // TotalScoreOutput 成绩输出 @@ -75,3 +65,45 @@ type TotalScoreOutput struct { Score int `json:"score"` CreatedAt string `json:"createdAt"` } + +//------------------------------------------------------------------------------------------------------------ + +// QuestionOutputReq 获取题目列表请求 +type QuestionOutputReq struct { + Stem string `json:"stem"` + QuestionTypeId string `json:"question_type_id"` + CourseRecommendationId string `json:"course+recommendation_id"` + Page int `json:"page"` + PageSize int `json:"page_size"` +} + +// QuestionOutputRes 题目输出 +type QuestionOutputRes struct { + Id int `json:"id"` + Stem string `json:"stem"` + A string `json:"A"` + 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"` +} + +type QuestionUpdateReq struct { + Id int `json:"id"` + Stem string `json:"stem"` + A string `json:"A"` + B string `json:"B"` + C string `json:"C"` + D string `json:"D"` + CorrectAnswer string `json:"correct_answer"` + QuestionTypeId string `json:"question_type_id"` + CourseRecommendationId string `json:"course+recommendation_id"` +} +type QuestionDelReq struct { + Id int `json:"id"` +} diff --git a/Knowledge_Test_Go/internal/cmd/cmd.go b/Knowledge_Test_Go/internal/cmd/cmd.go index 966b57d..477128e 100644 --- a/Knowledge_Test_Go/internal/cmd/cmd.go +++ b/Knowledge_Test_Go/internal/cmd/cmd.go @@ -29,20 +29,22 @@ var ( group.POST("/knowledge/wrong-questions", controller.NewKnowledgeTest().GetWrongQuestions) // 获取错题列表 }) - //// 后台路由组 - //s.Group("/admin", func(group *ghttp.RouterGroup) { - // group.Middleware( - // ghttp.MiddlewareHandlerResponse, - // ) - // - // kt := controller.NewKnowledgeTest() - // qb := controller.NewQuestionBank() - // - // group.GET("/users", kt.AdminUserList) - // group.GET("/wrong-statistics", kt.AdminWrongStatistics) - // group.GET("/questions", qb.AdminQuestionList) - // group.POST("/questions/update", qb.UpdateQuestion) - //}) + // 后台路由组 + s.Group("/admin", func(group *ghttp.RouterGroup) { + group.Middleware( + ghttp.MiddlewareHandlerResponse, + ) + group.POST("/questions/get", controller.NewQuestionBank().GetQuestions) + group.POST("/questions/update", controller.NewQuestionBank().QuestionUpdate) + group.POST("/questions/del", controller.NewQuestionBank().QuestionDel) + //kt := controller.NewKnowledgeTest() + //qb := controller.NewQuestionBank() + + //group.GET("/users", kt.AdminUserList) + //group.GET("/wrong-statistics", kt.AdminWrongStatistics) + //group.GET("/questions", qb.AdminQuestionList) + //group.POST("/questions/update", qb.UpdateQuestion) + }) s.Run() return nil diff --git a/Knowledge_Test_Go/internal/controller/questionBank.go b/Knowledge_Test_Go/internal/controller/questionBank.go index b0b429f..2472e9b 100644 --- a/Knowledge_Test_Go/internal/controller/questionBank.go +++ b/Knowledge_Test_Go/internal/controller/questionBank.go @@ -1 +1,64 @@ package controller + +import ( + "Knowledge_Test_Go/api/v1" + "Knowledge_Test_Go/internal/service" + "Knowledge_Test_Go/utility/response" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" +) + +type cQuestionBank struct{} + +func NewQuestionBank() *cQuestionBank { + return &cQuestionBank{} +} + +// GetQuestions 获取题目列表 +func (c *cQuestionBank) GetQuestions(r *ghttp.Request) { + ctx := r.GetCtx() + var req *v1.QuestionOutputReq + if err := r.Parse(&req); err != nil { + response.JsonExit(r, 400, err.Error()) + } + res, total, err := service.QuestionBank().GetQuestions(ctx, req) + if err != nil { + response.JsonExit(r, 400, err.Error()) + } + response.JsonExit(r, 200, "success", g.Map{ + "list": res, + "total": total, + }) +} + +// QuestionUpdate 获取题目列表 +func (c *cQuestionBank) QuestionUpdate(r *ghttp.Request) { + ctx := r.GetCtx() + var req *v1.QuestionUpdateReq + if err := r.Parse(&req); err != nil { + response.JsonExit(r, 400, err.Error()) + } + res, err := service.QuestionBank().QuestionUpdate(ctx, req) + if err != nil { + response.JsonExit(r, 400, err.Error()) + } + response.JsonExit(r, 200, "success", g.Map{ + "list": res, + }) +} + +func (c *cQuestionBank) QuestionDel(r *ghttp.Request) { + ctx := r.GetCtx() + var req *v1.QuestionDelReq + if err := r.Parse(&req); err != nil { + response.JsonExit(r, 400, err.Error()) + } + res, err := service.QuestionBank().QuestionDel(ctx, req) + if err != nil { + response.JsonExit(r, 400, err.Error()) + } + response.JsonExit(r, 200, "success", g.Map{ + "list": res, + }) +} diff --git a/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go b/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go index 1e936ef..c146a60 100644 --- a/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go +++ b/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go @@ -26,6 +26,7 @@ func (s *sKnowledgeTest) GetQuestions(ctx context.Context, req *v1.GetQuestionsR return } +// ----------------------------------------------------------------------------------------------------------------------------------------------- // GetUserScores 获取用户成绩 func (s *sKnowledgeTest) GetUserScores(ctx context.Context, req *v1.GetUserScoresReq) (res []*v1.GetUserScoresRes, err error) { err = g.Model("total_score").Fields("score").Where("jwcode=?", req.Jwcode).Ctx(ctx).Scan(&res) diff --git a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go index eb2c9a5..e88e3e5 100644 --- a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go +++ b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go @@ -4,6 +4,7 @@ import ( v1 "Knowledge_Test_Go/api/v1" "Knowledge_Test_Go/internal/service" "context" + "fmt" "github.com/gogf/gf/v2/frame/g" ) @@ -15,8 +16,102 @@ func init() { } // GetQuestions 获取题目列表 -func (s *sQuestionBank) GetQuestions(ctx context.Context, req *v1.GetQuestionsReq) (res []*v1.GetQuestionsRes, total int, err error) { - db := g.Model("question_bank a").Fields("a.id", "a.stem", "a.a", "a.b", "a.c", "a.d") +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"). + 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") + + // 添加查询条件 + if req.Stem != "" { + // 对题干进行模糊查询 + db = db.Where("a.stem LIKE ?", "%"+req.Stem+"%") + } + + if req.QuestionTypeId != "" { + // 对题目类型ID进行精准查询 + db = db.Where("a.question_type_id = ?", req.QuestionTypeId) + } + + if req.CourseRecommendationId != "" { + // 对课程推荐ID进行精准查询 + db = db.Where("a.course_recommendation_id = ?", req.CourseRecommendationId) + } + err = db.Page(req.Page, req.PageSize).ScanAndCount(&res, &total, false) + if err != nil { + return + } + + // 计算错误率 + for _, question := range res { + question.ErrorRate = calculateErrorRate(question.ErrorCount, question.CitationCount) + } return } + +// 计算错误率的辅助函数 +func calculateErrorRate(errorCount, citationCount int) int { + if citationCount == 0 { + return 0 + } + // 计算百分比(例如:25 表示 25%) + return int(float64(errorCount) / float64(citationCount) * 100) +} + +//------------------------------------------------------------------------------------------------------------------------------- + +// QuestionUpdate 修改题目 +func (s *sQuestionBank) QuestionUpdate(ctx context.Context, req *v1.QuestionUpdateReq) (res string, err error) { + // 准备数据 + data := g.Map{ + "stem": req.Stem, + "a": req.A, + "b": req.B, + "c": req.C, + "d": req.D, + "correct_answer": req.CorrectAnswer, + "question_type_id": req.QuestionTypeId, + "course_recommendation_id": req.CourseRecommendationId, + } + + if req.Id == 0 { + // 新增记录 + result, err := g.Model("question_bank").Data(data).Insert() + if err != nil { + return "新增失败", err + } + + // 获取新增的ID + id, _ := result.LastInsertId() + return fmt.Sprintf("新增成功,ID: %d", id), nil + } else { + // 更新记录 + _, err := g.Model("question_bank").Where("id = ?", req.Id).Data(data).Update() + if err != nil { + return "更新失败", err + } + + // 检查是否真的更新了记录 + count, _ := g.Model("question_bank").Where("id = ?", req.Id).Count() + if count == 0 { + return "更新失败,记录不存在", nil + } + + return fmt.Sprintf("更新成功,ID: %d", req.Id), nil + } +} + +//------------------------------------------------------------------------------------------------------------------------------- + +// QuestionDel 删除题目 +func (s *sQuestionBank) QuestionDel(ctx context.Context, req *v1.QuestionDelReq) (res string, err error) { + _, err = g.Model("question_bank").Where("id = ?", req.Id).Data("isdel", 1).Update() + if err != nil { + return "删除失败", err + } + return "删除成功", err +} + +//-------------------------------------------------------------------------------------------------------------------------------