|
|
|
@ -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"). |
|
|
|
|