excel-export/biz/export/csv_exporter.go

87 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package export
import (
"fmt"
"log"
"sync"
"time"
)
type CsvExporter struct {
mFetcher DataFetcher
file FileAdapter
count int
wg *sync.WaitGroup
}
func NewCsvExporter(fetcher DataFetcher, file FileAdapter) DataExporter {
return &CsvExporter{
mFetcher: fetcher,
file: file,
}
}
func (ee *CsvExporter) Fetcher(fetcher DataFetcher) {
ee.mFetcher = fetcher
}
func (ee *CsvExporter) File(file FileAdapter) {
ee.file = file
}
func (ee *CsvExporter) WaitGroup(wg *sync.WaitGroup) {
ee.wg = wg
}
func (ee *CsvExporter) Export(sql, pk string) error {
begin := time.Now()
data, err := ee.mFetcher.Fetch(sql)
if err != nil {
return fmt.Errorf("数据获取错误:%w", err)
}
duration := time.Now().Sub(begin)
if duration.Seconds() > 10 {
log.Printf("数据获取耗时:%s \n", duration.String())
}
ee.count = len(data.Data)
if ee.count > 0 {
//异步导出数据到csv文件中
go ee.exportToCsv(data)
} else {
ee.wg.Done()
}
return nil
}
func (ee *CsvExporter) exportToCsv(data *Data) {
begin := time.Now()
ee.file.Open()
ee.file.WriteTitle(data.Title)
for _, val := range data.Data {
row := val
ee.file.Write(row)
}
ee.file.Close()
ee.wg.Done()
duration := time.Now().Sub(begin)
if duration.Seconds() > 10 {
log.Printf("csv输出耗时%s \n", duration.String())
}
}
func (ee *CsvExporter) Count() int {
return ee.count
}
func (ee *CsvExporter) getPkIndex(titles []string, pk string) int {
for i, title := range titles {
if title == pk {
return i
}
}
return -1
}