78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"excel_export/biz/config"
|
|
"excel_export/biz/db"
|
|
"excel_export/biz/export"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Excel struct {
|
|
conf *config.Config
|
|
}
|
|
|
|
func NewExcel(conf *config.Config) *Excel {
|
|
return &Excel{
|
|
conf: conf,
|
|
}
|
|
}
|
|
|
|
func (e *Excel) Export(sysName, jobName string, begin, end time.Time, batch int) error {
|
|
job, dbStr, err := config.GetJob(e.conf, sysName, jobName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d, err := db.NewDb(dbStr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return e.JobHandler(job, d, map[string]interface{}{
|
|
"begin": begin,
|
|
"end": end,
|
|
"last": 0,
|
|
}, batch)
|
|
}
|
|
|
|
func (e *Excel) JobHandler(job config.Job, d export.DataFetcher, params map[string]interface{}, batch int) error {
|
|
|
|
f := export.NewExcel(job.File, job.Size, map[string]string{
|
|
"begin": params["begin"].(time.Time).Format("20060102"),
|
|
"end": params["end"].(time.Time).Format("20060102"),
|
|
})
|
|
|
|
for i, task := range job.Tasks {
|
|
fmt.Printf("执行导出任务:%d\n", i+1)
|
|
if err := e.TaskExport(d, task, params, f, batch); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *Excel) TaskExport(d export.DataFetcher, t config.Task, params map[string]interface{}, f export.FileAdapter, batch int) error {
|
|
//todo 最多分1000个批次进行处理
|
|
f.Open()
|
|
defer f.Close()
|
|
for i := 0; i < 1000; i++ {
|
|
sql := t.GetSql(params)
|
|
|
|
e := export.NewExcelExporter(d, f)
|
|
|
|
e.Export(sql+" limit "+strconv.Itoa(batch), t.PK)
|
|
|
|
count, last := e.Last()
|
|
fmt.Printf("已导出 %d 条数据\n", batch*i+count)
|
|
if count < batch {
|
|
break
|
|
}
|
|
|
|
params["last"] = last
|
|
time.Sleep(time.Microsecond * 30)
|
|
}
|
|
return nil
|
|
}
|