优化题库代码
This commit is contained in:
@@ -6,7 +6,7 @@ runmode = dev
|
||||
# MySQL - 远程连接配置
|
||||
mysqluser = gotest
|
||||
mysqlpass = 2nZhRdMPCNZrdzsd
|
||||
mysqlurls = 192.168.31.10:3306
|
||||
mysqlurls = 212.64.112.158:3388
|
||||
mysqldb = gotest
|
||||
|
||||
# ORM配置
|
||||
|
||||
@@ -24,6 +24,8 @@ func (c *ExamQuestionController) GetList() {
|
||||
tenantId, _ := c.Ctx.Input.GetData("tenantId").(int)
|
||||
keyword := c.GetString("keyword")
|
||||
typeStr := c.GetString("type")
|
||||
bankId, _ := c.GetInt64("bank_id", 0)
|
||||
|
||||
var qtype *int8
|
||||
if typeStr != "" {
|
||||
if iv, err := strconv.Atoi(typeStr); err == nil {
|
||||
@@ -31,12 +33,15 @@ func (c *ExamQuestionController) GetList() {
|
||||
qtype = &v
|
||||
}
|
||||
}
|
||||
|
||||
page, _ := c.GetInt("page", 1)
|
||||
pageSize, _ := c.GetInt("pageSize", 10)
|
||||
|
||||
list, total, err := services.GetExamQuestions(services.QuestionListParams{
|
||||
TenantId: tenantId,
|
||||
Keyword: keyword,
|
||||
QuestionType: qtype,
|
||||
BankId: bankId,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
})
|
||||
@@ -45,7 +50,7 @@ func (c *ExamQuestionController) GetList() {
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
// build minimal DTO to limit fields in response
|
||||
|
||||
items := make([]map[string]interface{}, 0, len(list))
|
||||
for _, q := range list {
|
||||
if q == nil {
|
||||
@@ -59,6 +64,7 @@ func (c *ExamQuestionController) GetList() {
|
||||
"score": q.Score,
|
||||
})
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "ok",
|
||||
@@ -101,6 +107,7 @@ func (c *ExamQuestionController) Create() {
|
||||
QuestionAnalysis string `json:"question_analysis"`
|
||||
Options []map[string]string `json:"options"`
|
||||
Answer interface{} `json:"answer"`
|
||||
BankId int64 `json:"bank_id"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &payload); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数错误: " + err.Error(), "data": nil}
|
||||
@@ -115,6 +122,9 @@ func (c *ExamQuestionController) Create() {
|
||||
Score: payload.Score,
|
||||
Status: 1,
|
||||
}
|
||||
if payload.BankId > 0 {
|
||||
q.BankId = payload.BankId
|
||||
}
|
||||
var opts []models.ExamQuestionOption
|
||||
for _, o := range payload.Options {
|
||||
opts = append(opts, models.ExamQuestionOption{OptionLabel: o["label"], OptionContent: o["content"]})
|
||||
@@ -149,6 +159,7 @@ func (c *ExamQuestionController) Create() {
|
||||
func (c *ExamQuestionController) BatchCreate() {
|
||||
tenantId, _ := c.Ctx.Input.GetData("tenantId").(int)
|
||||
var payload struct {
|
||||
BankId int64 `json:"bank_id"`
|
||||
Items []struct {
|
||||
QuestionTitle string `json:"question_title"`
|
||||
QuestionType int8 `json:"question_type"`
|
||||
@@ -182,6 +193,9 @@ func (c *ExamQuestionController) BatchCreate() {
|
||||
Score: item.Score,
|
||||
Status: 1,
|
||||
}
|
||||
if payload.BankId > 0 {
|
||||
q.BankId = payload.BankId
|
||||
}
|
||||
var opts []models.ExamQuestionOption
|
||||
for _, o := range item.Options {
|
||||
opts = append(opts, models.ExamQuestionOption{OptionLabel: o["label"], OptionContent: o["content"]})
|
||||
@@ -251,6 +265,7 @@ func (c *ExamQuestionController) Update() {
|
||||
QuestionAnalysis string `json:"question_analysis"`
|
||||
Options []map[string]string `json:"options"`
|
||||
Answer interface{} `json:"answer"`
|
||||
BankId int64 `json:"bank_id"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &payload); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数错误: " + err.Error(), "data": nil}
|
||||
@@ -258,6 +273,9 @@ func (c *ExamQuestionController) Update() {
|
||||
return
|
||||
}
|
||||
q := &models.ExamQuestion{QuestionType: payload.QuestionType, QuestionTitle: payload.QuestionTitle, QuestionAnalysis: payload.QuestionAnalysis, Score: payload.Score}
|
||||
if payload.BankId > 0 {
|
||||
q.BankId = payload.BankId
|
||||
}
|
||||
var opts []models.ExamQuestionOption
|
||||
for _, o := range payload.Options {
|
||||
opts = append(opts, models.ExamQuestionOption{OptionLabel: o["label"], OptionContent: o["content"]})
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
type ExamQuestion struct {
|
||||
Id int64 `orm:"column(id);auto" json:"id"`
|
||||
TenantId int `orm:"column(tenant_id)" json:"tenant_id"`
|
||||
BankId int64 `orm:"column(bank_id);null" json:"bank_id"`
|
||||
QuestionType int8 `orm:"column(question_type)" json:"question_type"`
|
||||
QuestionTitle string `orm:"column(question_title);size(1000)" json:"question_title"`
|
||||
QuestionAnalysis string `orm:"column(question_analysis);size(2000);null" json:"question_analysis"`
|
||||
|
||||
+11
-1
@@ -13,6 +13,7 @@ type QuestionListParams struct {
|
||||
TenantId int
|
||||
Keyword string
|
||||
QuestionType *int8
|
||||
BankId int64
|
||||
Page int
|
||||
PageSize int
|
||||
}
|
||||
@@ -42,6 +43,9 @@ func GetExamQuestions(params QuestionListParams) ([]*models.ExamQuestion, int64,
|
||||
if params.QuestionType != nil {
|
||||
qs = qs.Filter("question_type", *params.QuestionType)
|
||||
}
|
||||
if params.BankId > 0 {
|
||||
qs = qs.Filter("bank_id", params.BankId)
|
||||
}
|
||||
total, err := qs.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -121,7 +125,13 @@ func UpdateExamQuestion(tenantId int, id int64, q *models.ExamQuestion, options
|
||||
existing.QuestionTitle = q.QuestionTitle
|
||||
existing.QuestionAnalysis = q.QuestionAnalysis
|
||||
existing.Score = q.Score
|
||||
if _, err := o.Update(&existing, "QuestionType", "QuestionTitle", "QuestionAnalysis", "Score"); err != nil {
|
||||
// 可选更新题库归属
|
||||
fields := []string{"QuestionType", "QuestionTitle", "QuestionAnalysis", "Score"}
|
||||
if q.BankId > 0 {
|
||||
existing.BankId = q.BankId
|
||||
fields = append(fields, "BankId")
|
||||
}
|
||||
if _, err := o.Update(&existing, fields...); err != nil {
|
||||
return err
|
||||
}
|
||||
// soft delete old options and answer
|
||||
|
||||
Reference in New Issue
Block a user