You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.2 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Package export 导出组件的封装,一般导出的话csv性能是360插件的xlsx的几十倍
// 但是xlsx可以更加丰富样式,所以没有样式的情况下,用csv性能更高
package export
import (
"context"
"fmt"
"github.com/rs/xid"
"os"
)
type HeaderFieldOne struct {
Field string //数据对应的字段名称
Title string //表头名称
}
type HeaderField []HeaderFieldOne
type Export interface {
FileSuffix() string //文件后缀
ExportFile(data []RowData) error //导出数据
}
type BaseExport struct {
Ctx context.Context
hashMark string //导出任务的唯一hash
Header HeaderField //表头数据
Total int64 //需要导出的总数
filename string //文件名称
fullPath string //文件存储路径
customFile bool //是否自定义文件
}
// Init 实例化导出
// @params
//
// header HeaderField excel表头
// total int64 导出数据的总量条数
// hashMark string 自定义的唯一标识
func (e *BaseExport) Init(ctx context.Context, header HeaderField, total int64, opts ...Option) {
e.Ctx = ctx
e.Total = total
e.Header = header
e.hashMark = BuildHashMark()
//设置参数
for _, opt := range opts {
opt(e)
}
}
// GetHashMark 获取导出的唯一标识
func (e *BaseExport) GetHashMark() string {
return e.hashMark
}
// GetBasePath 获取基础路径
func (e *BaseExport) GetBasePath() string {
path, _ := os.Getwd()
return path + "/tmp/export/"
}
// SetFilename 设置文件名称
func (e *BaseExport) SetFilename(ept Export) {
if e.filename != "" { //已经设置好了就不设置了
return
}
e.filename = e.GetHashMark() + "." + ept.FileSuffix()
e.fullPath = e.GetBasePath() + e.filename
}
// GetFilename 获取文件名称
func (e *BaseExport) GetFilename() string {
return e.filename
}
// GetFullPath 获取文件全路径
func (e *BaseExport) GetFullPath() string {
return e.fullPath
}
// IsCustomerFile 文件是否已经存在
func (e *BaseExport) IsCustomerFile() bool {
return e.customFile
}
// BuildHashMark 初始化当前导出任务的唯一标识
func BuildHashMark() string {
return fmt.Sprintf("ept_%s", xid.New().String())
}