diff --git a/Knowledge_Test_Go/api/v1/questionBank.go b/Knowledge_Test_Go/api/v1/questionBank.go index 1b732a1..b8857f8 100644 --- a/Knowledge_Test_Go/api/v1/questionBank.go +++ b/Knowledge_Test_Go/api/v1/questionBank.go @@ -77,6 +77,8 @@ type QuestionOutputReq struct { 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 } // QuestionOutputRes 题目输出 @@ -132,6 +134,8 @@ type UserScoreOutputReq struct { EndTime string `json:"end_time"` // 结束时间(提交时间范围) Page int `json:"page"` PageSize int `json:"page_size"` + SortField string `json:"sort_field"` // 排序字段:score, created_at + SortOrder string `json:"sort_order"` // 排序方向:asc, desc } // ErrorOutPutUserReq 获取错题用户列表请求 diff --git a/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go b/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go index 707bd4a..9a6acfa 100644 --- a/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go +++ b/Knowledge_Test_Go/internal/logic/knowledge/knowledge.go @@ -89,6 +89,7 @@ func (s *sKnowledgeTest) SubmitAnswers(ctx context.Context, req *v1.SubmitAnswer for i, answer := range req.Answers { questionIds[i] = answer.QuestionId } + // 批量查询题目 var questions []*do.QuestionBank err := dao.QuestionBank.Ctx(ctx). @@ -170,6 +171,14 @@ func (s *sKnowledgeTest) SubmitAnswers(ctx context.Context, req *v1.SubmitAnswer } } + // 计算并更新所有被回答题目的错误率 + if len(allAnsweredQuestionIds) > 0 { + err := s.updateErrorRatesInTransaction(ctx, tx, allAnsweredQuestionIds) + if err != nil { + return err + } + } + // 插入总成绩记录 _, err = tx.Model(dao.TotalScore.Table()).Data(&do.TotalScore{ Jwcode: req.Jwcode, @@ -191,6 +200,27 @@ func (s *sKnowledgeTest) SubmitAnswers(ctx context.Context, req *v1.SubmitAnswer }, nil } +// updateErrorRatesInTransaction 在事务中更新错误率(简化版) +func (s *sKnowledgeTest) updateErrorRatesInTransaction(ctx context.Context, tx gdb.TX, questionIds []int) error { + if len(questionIds) == 0 { + return nil + } + + // 使用GF的Raw方法 + _, err := tx.Model(dao.QuestionBank.Table()). + Where(dao.QuestionBank.Columns().Id, questionIds). + Data(g.Map{ + "error_rate": gdb.Raw("CASE WHEN citation_count = 0 THEN 0 ELSE ROUND((error_count * 100.0) / citation_count) END"), + }). + Update() + + if err != nil { + return fmt.Errorf("更新错误率失败: %w", err) + } + + return nil +} + // ----------------------------------------------------------------------------------------------------------------------------------------------- // GetWrongQuestions 获取错题列表 diff --git a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go index e80c951..dd09517 100644 --- a/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go +++ b/Knowledge_Test_Go/internal/logic/questionBank/questionBank.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" + "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" @@ -25,9 +26,10 @@ func init() { 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"). + 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") + "b.question_type_name", "c.cr_name", "a.error_count", "a.citation_count", "a.error_rate") // 添加 error_rate 字段 // 添加查询条件 if req.Stem != "" { @@ -45,25 +47,38 @@ func (s *sQuestionBank) GetQuestions(ctx context.Context, req *v1.QuestionOutput db = db.Where("a.course_recommendation_id = ?", req.CourseRecommendationId) } + // 添加排序逻辑 + db = s.buildSortCondition(db, req) + err = db.Page(req.Page, req.PageSize).ScanAndCount(&res, &total, false) - if err != nil { - return + return +} + +// buildSortCondition 构建排序条件 +func (s *sQuestionBank) buildSortCondition(db *gdb.Model, req *v1.QuestionOutputReq) *gdb.Model { + // 默认排序(按ID倒序) + if req.SortField == "" { + return db.OrderDesc("a.id") } - // 计算错误率 - for _, question := range res { - question.ErrorRate = calculateErrorRate(question.ErrorCount, question.CitationCount) + // 确定排序方向 + order := "DESC" + if req.SortOrder == "asc" { + order = "ASC" } - return -} -// 计算错误率的辅助函数 -func calculateErrorRate(errorCount, citationCount int) int { - if citationCount == 0 { - return 0 + // 根据排序字段构建排序条件 + switch req.SortField { + case "error_count": + return db.Order("a.error_count " + order) + case "error_rate": + return db.Order("a.error_rate " + order) + case "id": + return db.Order("a.id " + order) + default: + // 默认按ID倒序 + return db.OrderDesc("a.id") } - // 计算百分比(例如:25 表示 25%) - return int(float64(errorCount) / float64(citationCount) * 100) } //------------------------------------------------------------------------------------------------------------------------------- @@ -150,13 +165,38 @@ func (s *sQuestionBank) UserScoreOutput(ctx context.Context, req *v1.UserScoreOu db = db.Where("b.created_at <= ?", req.EndTime) } - // 添加排序,默认按创建时间倒序 - db = db.OrderDesc("b.created_at") + // 添加排序逻辑 + db = s.buildUserScoreSortCondition(db, req) err = db.Page(req.Page, req.PageSize).ScanAndCount(&res, &total, false) return } +// buildUserScoreSortCondition 构建用户成绩排序条件 +func (s *sQuestionBank) buildUserScoreSortCondition(db *gdb.Model, req *v1.UserScoreOutputReq) *gdb.Model { + // 默认排序(按创建时间倒序) + if req.SortField == "" { + return db.OrderDesc("b.created_at") + } + + // 确定排序方向 + order := "DESC" + if req.SortOrder == "asc" { + order = "ASC" + } + + // 根据排序字段构建排序条件 + switch req.SortField { + case "score": + return db.Order("b.score " + order) + case "created_at": + return db.Order("b.created_at " + order) + default: + // 默认按创建时间倒序 + return db.OrderDesc("b.created_at") + } +} + //------------------------------------------------------------------------------------------------------------------------------- // ErrorOutPutUser 错题统计出错用户 @@ -170,6 +210,7 @@ func (s *sQuestionBank) ErrorOutPutUser(ctx context.Context, req *v1.ErrorOutPut err = g.Model("question_record r"). LeftJoin("user u", "r.jwcode = u.jwcode"). Where("r.question_bank_id = ?", req.Id). + Where("r.is_correct = 1"). Fields("r.jwcode", "u.user_name", "u.user_identity", "COUNT(*) as error_count"). Group("r.jwcode", "u.user_name", "u.user_identity"). OrderDesc("error_count").