drone-test/api/upload_excel.go

95 lines
2.0 KiB
Go
Raw Normal View History

2022-06-15 10:31:40 +08:00
package api
import (
"docDemo/client"
"docDemo/model"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/xuri/excelize/v2"
"strings"
)
type scaleApi struct{}
func ScaleApi() *scaleApi {
return &scaleApi{}
}
func (scaleApi) AddScale(ctx *gin.Context) {
var p model.AddQuestion
if ctx.ShouldBind(&p) != nil {
fmt.Println("参数错误")
return
}
open, err := p.File.Open()
if err != nil {
fmt.Println("文件打开失败", err)
return
}
file, err := excelize.OpenReader(open)
defer file.Close()
if err != nil {
return
}
rows, err := file.GetRows("Sheet1")
if err != nil {
return
}
for _, row := range rows[1:3] {
if row[0] == "" {
break
}
var ques []map[string]string
if len(row) > 2 {
//ques = append(ques, map[string]string{"A": row[2]})
//ques = append(ques, map[string]string{"B": row[3]})
//ques = append(ques, map[string]string{"C": row[4]})
//ques = append(ques, map[string]string{"D": row[5]})
for i := range rows {
ques = append(ques, map[string]string{row[i]: row[3]})
}
}
awssJson, err := json.Marshal(ques)
if err != nil {
return
}
var Questions model.Question
Questions.Content = row[1]
Questions.Answer = string(awssJson)
Questions.Analysis = "A"
Questions.Subject = "大学语文"
if strings.Contains(row[0], "单选") || strings.Contains(row[0], "单项") {
Questions.SubjectType = "single"
}
if strings.Contains(row[0], "多选") || strings.Contains(row[0], "多项") {
Questions.SubjectType = "mutil"
}
if strings.Contains(row[0], "判断") {
Questions.SubjectType = "judge"
}
if strings.Contains(row[0], "应用") || strings.Contains(row[0], "简答") {
Questions.SubjectType = "application"
}
err = client.MySQL.Table("sys_question_bank").Create(&Questions).Error
if err != nil {
fmt.Println("导入题库失败:", err)
}
}
}
func (scaleApi) Test(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"message": "drone-Test",
})
return
}