|
|
|
@ -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 != "" { |
|
|
|
|