mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-09-26 20:11:11 +08:00
144 lines
3.6 KiB
Go
144 lines
3.6 KiB
Go
package model
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/quarkcms/quark-go/pkg/dal/db"
|
||
"github.com/xuri/excelize/v2"
|
||
)
|
||
|
||
// 字段
|
||
type File struct {
|
||
Id int `json:"id" gorm:"autoIncrement"`
|
||
ObjType string `json:"obj_type" gorm:"size:255"`
|
||
ObjId int `json:"obj_id" gorm:"size:11;default:0"`
|
||
FileCategoryId int `json:"file_category_id" gorm:"size:11;default:0"`
|
||
Sort int `json:"sort" gorm:"size:11;default:0"`
|
||
Name string `json:"name" gorm:"size:255;not null"`
|
||
Size string `json:"size" gorm:"size:20;default:0"`
|
||
Ext string `json:"ext" gorm:"size:255"`
|
||
Path string `json:"path" gorm:"size:255;not null"`
|
||
Md5 string `json:"md5" gorm:"size:255;not null"`
|
||
Status int `json:"status" gorm:"size:1;not null;default:1"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
}
|
||
|
||
// 插入数据并返回ID
|
||
func (model *File) InsertGetId(data *File) (id int, Error error) {
|
||
err := db.Client.Create(&data).Error
|
||
|
||
return data.Id, err
|
||
}
|
||
|
||
// 获取文件路径
|
||
func (model *File) GetPath(id interface{}) string {
|
||
http, path := "", ""
|
||
webSiteDomain := (&Config{}).GetValue("WEB_SITE_DOMAIN")
|
||
WebConfig := (&Config{}).GetValue("SSL_OPEN")
|
||
if webSiteDomain != "" {
|
||
if WebConfig == "1" {
|
||
http = "https://"
|
||
} else {
|
||
http = "http://"
|
||
}
|
||
}
|
||
|
||
if getId, ok := id.(string); ok {
|
||
if strings.Contains(getId, "//") && !strings.Contains(getId, "{") {
|
||
return getId
|
||
}
|
||
if strings.Contains(getId, "./") && !strings.Contains(getId, "{") {
|
||
// 如果设置域名,则加上域名前缀
|
||
return http + webSiteDomain + strings.Replace(getId, "./storage/app/public", "/storage", -1)
|
||
}
|
||
// json字符串
|
||
if strings.Contains(getId, "{") {
|
||
var jsonData interface{}
|
||
json.Unmarshal([]byte(getId), &jsonData)
|
||
// 如果为map
|
||
if mapData, ok := jsonData.(map[string]interface{}); ok {
|
||
path = mapData["path"].(string)
|
||
}
|
||
// 如果为数组,返回第一个key的path
|
||
if arrayData, ok := jsonData.([]map[string]interface{}); ok {
|
||
path = arrayData[0]["path"].(string)
|
||
}
|
||
}
|
||
|
||
if strings.Contains(path, "//") {
|
||
return path
|
||
}
|
||
if strings.Contains(path, "./") {
|
||
path = strings.Replace(path, "./storage/app/public", "/storage", -1)
|
||
}
|
||
if path != "" {
|
||
// 如果设置域名,则加上域名前缀
|
||
return http + webSiteDomain + path
|
||
}
|
||
}
|
||
|
||
file := &File{}
|
||
db.Client.Where("id", id).Where("status", 1).First(&file)
|
||
if file.Id != 0 {
|
||
path = file.Path
|
||
if strings.Contains(path, "//") {
|
||
return path
|
||
}
|
||
if strings.Contains(path, "./") {
|
||
path = strings.Replace(path, "./storage/app/public", "/storage", -1)
|
||
}
|
||
if path != "" {
|
||
// 如果设置域名,则加上域名前缀
|
||
return http + webSiteDomain + path
|
||
}
|
||
}
|
||
|
||
return ""
|
||
}
|
||
|
||
// 获取Excel文件数据
|
||
func (model *File) GetExcelData(fileId int) (data [][]interface{}, Error error) {
|
||
filePath := ""
|
||
file := &File{}
|
||
err := db.Client.Where("id", fileId).Where("status", 1).First(&file).Error
|
||
if err != nil {
|
||
return data, err
|
||
}
|
||
if file.Id != 0 {
|
||
filePath = file.Path
|
||
}
|
||
if filePath == "" {
|
||
return data, errors.New("参数错误!")
|
||
}
|
||
|
||
f, err := excelize.OpenFile(filePath)
|
||
if err != nil {
|
||
return data, err
|
||
}
|
||
defer func() {
|
||
if err := f.Close(); err != nil {
|
||
fmt.Println(err)
|
||
}
|
||
}()
|
||
|
||
rows, err := f.GetRows("Sheet1")
|
||
if err != nil {
|
||
return data, err
|
||
}
|
||
|
||
for _, row := range rows {
|
||
getRows := []interface{}{}
|
||
for _, colCell := range row {
|
||
getRows = append(getRows, colCell)
|
||
}
|
||
data = append(data, getRows)
|
||
}
|
||
|
||
return data, err
|
||
}
|