|
|
package service
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"github.com/beego/beego/v2/client/httplib"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel"
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
|
...
|
...
|
@@ -207,3 +211,68 @@ func (fileService *FileService) AppendDataToTable(ctx *domain.Context, cmd *comm |
|
|
}
|
|
|
return result, nil
|
|
|
}
|
|
|
|
|
|
// ExportFile 文件下载
|
|
|
func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.ExportFileCommand) (interface{}, error) {
|
|
|
if err := cmd.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
_, file, err := factory.FastPgFile(transactionContext, cmd.FileId)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
var response = struct {
|
|
|
Url string `json:"url"`
|
|
|
Ext string `json:"ext"`
|
|
|
FileName string `json:"fileName"`
|
|
|
}{}
|
|
|
if file.FileType == domain.SourceFile.ToString() {
|
|
|
response.Url = file.FileInfo.Url
|
|
|
response.Ext = file.FileInfo.Ext
|
|
|
response.FileName = file.FileInfo.Name
|
|
|
return response, nil
|
|
|
}
|
|
|
_, table, err := factory.FastPgTable(transactionContext, file.FileInfo.TableId)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
f, err := httplib.Get(file.FileInfo.Url).Bytes()
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
reader := bytes.NewReader(f)
|
|
|
var importer *excel.Importer = excel.NewExcelImportByFile(file.FileInfo.Ext)
|
|
|
data, err := importer.OpenExcelFromIoReader(reader)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
filename := fmt.Sprintf("%v_%v.xlsx", file.FileInfo.Name, time.Now().Format("060102150405"))
|
|
|
path := fmt.Sprintf("public/%v", filename)
|
|
|
writerTo := excel.NewXLXSWriterTo(importer.Reader().Header().Columns, data)
|
|
|
writerTo.ToInterfaces = domain.MakeToInterfaces(table.DataFields)
|
|
|
if err := writerTo.Save(path); err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
response.Url = domain.DownloadUrl(filename)
|
|
|
response.FileName = filename
|
|
|
response.Ext = file.FileInfo.Ext
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return response, nil
|
|
|
} |
...
|
...
|
|