新增智能新建题目功能
This commit is contained in:
@@ -144,6 +144,96 @@ func (c *ExamQuestionController) Create() {
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// BatchCreate
|
||||
// @router /exam-questions/batch [post]
|
||||
func (c *ExamQuestionController) BatchCreate() {
|
||||
tenantId, _ := c.Ctx.Input.GetData("tenantId").(int)
|
||||
var payload struct {
|
||||
Items []struct {
|
||||
QuestionTitle string `json:"question_title"`
|
||||
QuestionType int8 `json:"question_type"`
|
||||
Score float64 `json:"score"`
|
||||
QuestionAnalysis string `json:"question_analysis"`
|
||||
Options []map[string]string `json:"options"`
|
||||
Answer interface{} `json:"answer"`
|
||||
} `json:"items"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &payload); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数错误: " + err.Error(), "data": nil}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
if len(payload.Items) == 0 {
|
||||
c.Data["json"] = map[string]interface{}{"code": 1, "message": "导入数据为空", "data": nil}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
success := 0
|
||||
fails := 0
|
||||
createdIds := make([]int64, 0, len(payload.Items))
|
||||
updatedIds := make([]int64, 0, len(payload.Items))
|
||||
for _, item := range payload.Items {
|
||||
q := &models.ExamQuestion{
|
||||
TenantId: tenantId,
|
||||
QuestionType: item.QuestionType,
|
||||
QuestionTitle: item.QuestionTitle,
|
||||
QuestionAnalysis: item.QuestionAnalysis,
|
||||
Score: item.Score,
|
||||
Status: 1,
|
||||
}
|
||||
var opts []models.ExamQuestionOption
|
||||
for _, o := range item.Options {
|
||||
opts = append(opts, models.ExamQuestionOption{OptionLabel: o["label"], OptionContent: o["content"]})
|
||||
}
|
||||
answerContent := ""
|
||||
switch v := item.Answer.(type) {
|
||||
case string:
|
||||
answerContent = v
|
||||
case []interface{}:
|
||||
for i, it := range v {
|
||||
if s, ok := it.(string); ok {
|
||||
if i == 0 {
|
||||
answerContent = s
|
||||
} else {
|
||||
answerContent += "," + s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果存在完全匹配的题目标题,则覆盖更新;否则新增
|
||||
if existing, err := services.FindExamQuestionByTitle(tenantId, item.QuestionTitle); err == nil && existing != nil {
|
||||
if err := services.UpdateExamQuestion(tenantId, existing.Id, q, opts, answerContent); err != nil {
|
||||
fails++
|
||||
continue
|
||||
}
|
||||
updatedIds = append(updatedIds, existing.Id)
|
||||
success++
|
||||
} else {
|
||||
id, err := services.CreateExamQuestion(tenantId, q, opts, answerContent)
|
||||
if err != nil {
|
||||
fails++
|
||||
continue
|
||||
}
|
||||
createdIds = append(createdIds, id)
|
||||
success++
|
||||
}
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "批量导入完成",
|
||||
"data": map[string]interface{}{
|
||||
"success": success,
|
||||
"failed": fails,
|
||||
"created_ids": createdIds,
|
||||
"updated_ids": updatedIds,
|
||||
},
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// Update
|
||||
// @router /exam-questions/:id [put]
|
||||
func (c *ExamQuestionController) Update() {
|
||||
|
||||
Reference in New Issue
Block a user