Browse Source

后台题目查询,更新,修改,删除接口

wangguixi/feature-20251107144650-股票知识测评
wangguixi 2 months ago
parent
commit
a1b0b17c11
  1. 58
      Knowledge_Test_Go/api/v1/questionBank.go
  2. 30
      Knowledge_Test_Go/internal/cmd/cmd.go
  3. 63
      Knowledge_Test_Go/internal/controller/questionBank.go
  4. 1
      Knowledge_Test_Go/internal/logic/knowledge/knowledge.go
  5. 99
      Knowledge_Test_Go/internal/logic/questionBank/questionBank.go

58
Knowledge_Test_Go/api/v1/questionBank.go

@ -51,21 +51,11 @@ type GetUserScoresRes struct {
Score int `json:"score"` 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 错题详情 // GetWrongQuestionsRes 错题详情
type GetWrongQuestionsRes struct { 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 成绩输出 // TotalScoreOutput 成绩输出
@ -75,3 +65,45 @@ type TotalScoreOutput struct {
Score int `json:"score"` Score int `json:"score"`
CreatedAt string `json:"createdAt"` 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"`
}

30
Knowledge_Test_Go/internal/cmd/cmd.go

@ -29,20 +29,22 @@ var (
group.POST("/knowledge/wrong-questions", controller.NewKnowledgeTest().GetWrongQuestions) // 获取错题列表 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() s.Run()
return nil return nil

63
Knowledge_Test_Go/internal/controller/questionBank.go

@ -1 +1,64 @@
package controller 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,
})
}

1
Knowledge_Test_Go/internal/logic/knowledge/knowledge.go

@ -26,6 +26,7 @@ func (s *sKnowledgeTest) GetQuestions(ctx context.Context, req *v1.GetQuestionsR
return return
} }
// -----------------------------------------------------------------------------------------------------------------------------------------------
// GetUserScores 获取用户成绩 // GetUserScores 获取用户成绩
func (s *sKnowledgeTest) GetUserScores(ctx context.Context, req *v1.GetUserScoresReq) (res []*v1.GetUserScoresRes, err error) { 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) err = g.Model("total_score").Fields("score").Where("jwcode=?", req.Jwcode).Ctx(ctx).Scan(&res)

99
Knowledge_Test_Go/internal/logic/questionBank/questionBank.go

@ -4,6 +4,7 @@ import (
v1 "Knowledge_Test_Go/api/v1" v1 "Knowledge_Test_Go/api/v1"
"Knowledge_Test_Go/internal/service" "Knowledge_Test_Go/internal/service"
"context" "context"
"fmt"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
) )
@ -15,8 +16,102 @@ func init() {
} }
// GetQuestions 获取题目列表 // 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) 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 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
}
//-------------------------------------------------------------------------------------------------------------------------------
Loading…
Cancel
Save