Merge branch 'wwq' into dev
This commit is contained in:
commit
f5db257a20
@ -23,6 +23,7 @@ func cronRouter(r *gin.RouterGroup) {
|
||||
cron.POST("exportExcelExecutionResult", cronService.ExportExcelExecutionResult)
|
||||
cron.POST("getListExecutionRecord", cronService.GetListExecutionRecord)
|
||||
cron.POST("getScheduleTaskStatus", cronService.GetScheduleTaskStatus)
|
||||
cron.POST("getImportData", cronService.GetImportData)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -231,7 +231,7 @@ func UpdateWorkVideo(ctx *gin.Context) {
|
||||
FileName: "",
|
||||
})
|
||||
if errs != nil {
|
||||
service.Error(ctx, err)
|
||||
service.Error(ctx, errs)
|
||||
return
|
||||
}
|
||||
if fileResp.SecurityStatus == "high" {
|
||||
|
||||
@ -7,10 +7,16 @@ import (
|
||||
"fonchain-fiee/api/cron"
|
||||
"fonchain-fiee/pkg/service"
|
||||
"fonchain-fiee/pkg/utils"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/xuri/excelize/v2"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
@ -205,3 +211,109 @@ func GetScheduleTaskStatus(c *gin.Context) {
|
||||
}
|
||||
service.Success(c, res)
|
||||
}
|
||||
|
||||
func GetImportData(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
service.Error(c, errors.New("操作失败"))
|
||||
}
|
||||
}()
|
||||
|
||||
// 导入excel
|
||||
excelFile, err := c.FormFile("excel")
|
||||
if err != nil {
|
||||
service.Error(c, errors.New("缺少excel文件"))
|
||||
return
|
||||
}
|
||||
|
||||
// 创建临时文件
|
||||
tempDir := "tmp"
|
||||
if err = os.MkdirAll(tempDir, 0755); err != nil {
|
||||
service.Error(c, errors.New("创建临时目录失败"))
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// 保存excel
|
||||
excelPath := filepath.Join(tempDir, "excel.xlsx")
|
||||
if err = c.SaveUploadedFile(excelFile, excelPath); err != nil {
|
||||
service.Error(c, errors.New("保存excel文件失败"))
|
||||
return
|
||||
}
|
||||
|
||||
// 读取excel
|
||||
readExcelResult, err := readExcel(excelPath)
|
||||
if err != nil {
|
||||
service.Error(c, fmt.Errorf("读取excel失败: %v", err))
|
||||
return
|
||||
}
|
||||
if len(readExcelResult) == 0 {
|
||||
service.Error(c, errors.New("请检查excel文件"))
|
||||
return
|
||||
}
|
||||
service.Success(c, readExcelResult)
|
||||
|
||||
}
|
||||
|
||||
type excelData struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func readExcel(excelPath string) ([]excelData, error) {
|
||||
//打开excel
|
||||
f, err := excelize.OpenFile(excelPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
//读取第一页
|
||||
sheetName := f.GetSheetName(0)
|
||||
if sheetName == "" {
|
||||
return nil, errors.New("excel文件中没有工作表")
|
||||
}
|
||||
|
||||
//读取数据
|
||||
rows, err := f.GetRows(sheetName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取工作表失败: %v", err)
|
||||
}
|
||||
|
||||
if len(rows) <= 1 {
|
||||
return nil, errors.New("excel文件没有数据行(只有表头或为空)")
|
||||
}
|
||||
var result []excelData
|
||||
for i := 1; i < len(rows); i++ { // 从第2行开始(跳过表头)
|
||||
row := rows[i]
|
||||
if len(row) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
idStr := getCellValue(f, sheetName, i, 0)
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := getCellValue(f, sheetName, i, 1)
|
||||
data := excelData{
|
||||
Id: id,
|
||||
Name: name,
|
||||
}
|
||||
result = append(result, data)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
func getCellValue(f *excelize.File, sheetName string, rowIndex, colIndex int) string {
|
||||
colName, _ := excelize.ColumnNumberToName(colIndex + 1)
|
||||
cell := fmt.Sprintf("%s%d", colName, rowIndex+1)
|
||||
|
||||
value, err := f.GetCellValue(sheetName, cell)
|
||||
if err != nil {
|
||||
log.Printf("读取单元格 %s 失败: %v", cell, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user