Browse Source

建议

lijikun/hotfix-20251118170922-股票知识测评修改代码问题
lijikun 2 months ago
parent
commit
eed295bd3c
  1. 30
      Knowledge_Test_Go/api/v1/questionBank.go
  2. 10
      Knowledge_Test_Go/internal/cmd/cmd.go
  3. 6
      Knowledge_Test_Go/internal/logic/knowledge/knowledge.go
  4. 47
      Knowledge_Test_Go/internal/logic/questionBank/questionBank.go

30
Knowledge_Test_Go/api/v1/questionBank.go

@ -74,11 +74,11 @@ type GetWrongQuestionsRes struct {
type QuestionOutputReq struct { type QuestionOutputReq struct {
Stem string `json:"stem"` Stem string `json:"stem"`
QuestionTypeId string `json:"question_type_id"` QuestionTypeId string `json:"question_type_id"`
CourseRecommendationId string `json:"course+recommendation_id"`
CourseRecommendationId string `json:"course_recommendation_id"`
Page int `json:"page"` Page int `json:"page"`
PageSize int `json:"page_size"` 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 题目输出 // QuestionOutputRes 题目输出
@ -89,12 +89,12 @@ type QuestionOutputRes struct {
B string `json:"B"` B string `json:"B"`
C string `json:"C"` C string `json:"C"`
D string `json:"D"` 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 修改题目请求 // QuestionUpdateReq 修改题目请求
@ -122,16 +122,16 @@ type UserScoreOutputRes struct {
UserIdentity string `json:"user_identity"` UserIdentity string `json:"user_identity"`
Jwcode int `json:"jwcode"` Jwcode int `json:"jwcode"`
Score int `json:"score"` Score int `json:"score"`
CreatedAt string `json:"createdAt"`
CreatedAt string `json:"created_at"`
} }
// UserScoreOutputReq 获取用户成绩列表请求 // UserScoreOutputReq 获取用户成绩列表请求
type UserScoreOutputReq struct { 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"` Page int `json:"page"`
PageSize int `json:"page_size"` PageSize int `json:"page_size"`
SortField string `json:"sort_field"` // 排序字段:score, created_at SortField string `json:"sort_field"` // 排序字段:score, created_at

10
Knowledge_Test_Go/internal/cmd/cmd.go

@ -23,11 +23,11 @@ var (
ghttp.MiddlewareHandlerResponse, ghttp.MiddlewareHandlerResponse,
ghttp.MiddlewareCORS, 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) // 获取课程
}) })
// 后台路由组 // 后台路由组

6
Knowledge_Test_Go/internal/logic/knowledge/knowledge.go

@ -225,9 +225,9 @@ func (s *sKnowledgeTest) updateErrorRatesInTransaction(ctx context.Context, tx g
// GetWrongQuestions 获取错题列表 // GetWrongQuestions 获取错题列表
func (s *sKnowledgeTest) GetWrongQuestions(ctx context.Context, req *v1.GetWrongQuestionsReq) (res []*v1.GetWrongQuestionsRes, total int, err error) { 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"). 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 return
} }

47
Knowledge_Test_Go/internal/logic/questionBank/questionBank.go

@ -6,6 +6,7 @@ import (
"Knowledge_Test_Go/utility/response" "Knowledge_Test_Go/utility/response"
"context" "context"
"fmt" "fmt"
"github.com/gogf/gf/v2/util/gconv"
"regexp" "regexp"
"strconv" "strconv"
@ -25,27 +26,27 @@ func init() {
// GetQuestions 获取题目列表 // GetQuestions 获取题目列表
func (s *sQuestionBank) GetQuestions(ctx context.Context, req *v1.QuestionOutputReq) (res []*v1.QuestionOutputRes, total int, err error) { 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 != "" { if req.Stem != "" {
// 对题干进行模糊查询 // 对题干进行模糊查询
db = db.Where("a.stem LIKE ?", "%"+req.Stem+"%")
db = db.Where("qb.stem LIKE ?", "%"+req.Stem+"%")
} }
if req.QuestionTypeId != "" { if req.QuestionTypeId != "" {
// 对题目类型ID进行精准查询 // 对题目类型ID进行精准查询
db = db.Where("a.question_type_id = ?", req.QuestionTypeId)
db = db.Where("qb.question_type_id = ?", req.QuestionTypeId)
} }
if req.CourseRecommendationId != "" { if req.CourseRecommendationId != "" {
// 对课程推荐ID进行精准查询 // 对课程推荐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 { func (s *sQuestionBank) buildSortCondition(db *gdb.Model, req *v1.QuestionOutputReq) *gdb.Model {
// 默认排序(按ID倒序) // 默认排序(按ID倒序)
if req.SortField == "" { 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 { switch req.SortField {
case "error_count": case "error_count":
return db.Order("a.error_count " + order)
return db.Order("a.error_count " + req.SortOrder)
case "error_rate": case "error_rate":
return db.Order("a.error_rate " + order)
return db.Order("a.error_rate " + req.SortOrder)
case "id": case "id":
return db.Order("a.id " + order)
return db.Order("a.id " + req.SortOrder)
default: default:
// 默认按ID倒序 // 默认按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 { 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 { if err != nil {
return "新增失败", err return "新增失败", err
} }
// 获取新增的ID
id, _ := result.LastInsertId()
return fmt.Sprintf("新增成功,ID: %d", id), nil
return fmt.Sprintf("新增成功,ID: %d", gconv.Int(result)), nil
} else { } else {
// 更新记录 // 更新记录
_, err := g.Model("question_bank").Where("id = ?", req.Id).Data(data).Update() _, 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 获取用户成绩列表 // UserScoreOutput 获取用户成绩列表
func (s *sQuestionBank) UserScoreOutput(ctx context.Context, req *v1.UserScoreOutputReq) (res []*v1.UserScoreOutputRes, total int, err error) { 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 != "" { if req.UserName != "" {

Loading…
Cancel
Save