You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
61 lines
1.2 KiB
package logic
|
|
|
|
import (
|
|
"main/dao/mysql"
|
|
"main/models"
|
|
"mime/multipart"
|
|
"strconv"
|
|
|
|
"github.com/xuri/excelize/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func ImportStudentInfoLogic(file *multipart.FileHeader) (err error) {
|
|
var s []models.User
|
|
f, err := file.Open()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer f.Close()
|
|
excelFile, err := excelize.OpenReader(f)
|
|
if err != nil {
|
|
return
|
|
}
|
|
sheetList := excelFile.GetSheetList()
|
|
if len(sheetList) == 0 {
|
|
return
|
|
}
|
|
rows, err := excelFile.GetRows(sheetList[0])
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, row := range rows[1:] {
|
|
user_id, err := strconv.ParseInt(row[0], 10, 64)
|
|
if err != nil {
|
|
zap.L().Error("user_id change failed", zap.Error(err))
|
|
continue
|
|
}
|
|
user_score, err := strconv.ParseInt(row[3], 10, 64)
|
|
if err != nil {
|
|
zap.L().Error("user_score change failed", zap.Error(err))
|
|
continue
|
|
}
|
|
var user_gender int
|
|
if row[2] == "男" {
|
|
user_gender = 1
|
|
} else if row[2] == "女" {
|
|
user_gender = 2
|
|
} else {
|
|
user_gender = 0
|
|
}
|
|
student := models.User{
|
|
User_id: user_id,
|
|
User_name: row[1],
|
|
User_gender: user_gender,
|
|
User_score: user_score,
|
|
}
|
|
s = append(s, student)
|
|
}
|
|
return mysql.ImportStudentInfoIntoSql(s)
|
|
}
|