导入数据
This commit is contained in:
parent
98d9eaa039
commit
a64110eb38
@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/360EntSecGroup-Skylar/excelize"
|
||||
"github.com/dubbogo/gost/log/logger"
|
||||
"github.com/exhibition-main/api/exhibition"
|
||||
"github.com/exhibition-main/internal/config"
|
||||
@ -267,3 +268,62 @@ func OcrBase64(c *gin.Context) {
|
||||
response.ResponseQuickMsg(c, msg.Ok, "操作成功", res)
|
||||
return
|
||||
}
|
||||
func ImportRecordByExcel(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
logger.Errorf("获取文件失败: %v", err)
|
||||
response.ResponseQuickMsg(c, msg.Fail, msg.INVALID_PARAMS, nil)
|
||||
return
|
||||
}
|
||||
//打开Excel文件
|
||||
f, err := file.Open()
|
||||
if err != nil {
|
||||
logger.Errorf("打开文件失败: %v", err)
|
||||
response.ResponseQuickMsg(c, msg.Fail, "文件打开失败", nil)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
excelFile, err := excelize.OpenReader(f)
|
||||
if err != nil {
|
||||
logger.Errorf("读取Excel失败: %v", err)
|
||||
response.ResponseQuickMsg(c, msg.Fail, "Excel解析失败", nil)
|
||||
return
|
||||
}
|
||||
sheetName := excelFile.GetSheetName(0)
|
||||
rows := excelFile.GetRows(sheetName)
|
||||
if err != nil {
|
||||
logger.Errorf("读取工作表失败: %v", err)
|
||||
response.ResponseQuickMsg(c, msg.Fail, "工作表读取失败", nil)
|
||||
return
|
||||
}
|
||||
|
||||
// 遍历行数据(跳过标题行)
|
||||
var records []*exhibition.RegisterInfo
|
||||
for i, row := range rows {
|
||||
if i == 0 { // 跳过标题行
|
||||
continue
|
||||
}
|
||||
|
||||
if len(row) < 4 { // 确保列数足够
|
||||
continue
|
||||
}
|
||||
record := &exhibition.RegisterInfo{
|
||||
ArtistName: row[0],
|
||||
PhoneNum: row[1],
|
||||
Address: row[2],
|
||||
ArtworkName: row[3],
|
||||
}
|
||||
records = append(records, record)
|
||||
}
|
||||
for _, r := range records {
|
||||
_, err := GrpcExhibitionClientImpl.SaveRegisterRecord(context.Background(), r)
|
||||
if err != nil {
|
||||
response.ResponseQuickMsg(c, msg.Fail, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response.ResponseQuickMsg(c, msg.Ok, "ok", "")
|
||||
return
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user