...
|
...
|
@@ -4,14 +4,14 @@ 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"
|
|
|
"github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/dto"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
|
|
|
)
|
|
|
|
...
|
...
|
@@ -44,6 +44,54 @@ func (fileService *FileService) FilePreview(ctx *domain.Context, loadDataTableCo |
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
func (fileService *FileService) ResetHeaderRow(ctx *domain.Context, loadDataTableCommand *command.ResetTableHeaderCommand) (interface{}, error) {
|
|
|
if err := loadDataTableCommand.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()
|
|
|
}()
|
|
|
cache := redis.NewFileCacheService()
|
|
|
temporaryFile, err := cache.Get(redis.KeyTemporaryFileInfo(loadDataTableCommand.FileId))
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
loadDataTableService, _ := factory.CreateLoadDataTableService(transactionContext)
|
|
|
data, err := loadDataTableService.RePreview(ctx, loadDataTableCommand.FileId, temporaryFile.Fields, loadDataTableCommand.Where)
|
|
|
// 处理错误
|
|
|
level := domain.LevelInfo
|
|
|
errMsg := ""
|
|
|
if err != nil {
|
|
|
level = domain.LevelError
|
|
|
errMsg = err.Error()
|
|
|
}
|
|
|
if logErr := domainService.FastLog(transactionContext.(*pg.TransactionContext),
|
|
|
domain.VerifiedStepLog, temporaryFile.FileId, &domainService.ExcelTableResetHeaderLog{
|
|
|
LogEntry: domain.NewLogEntry(temporaryFile.FileName, domain.VerifiedFile.ToString(), domain.FileVerify,
|
|
|
ctx.WithValue(domain.ContextWithLogLevel, level).
|
|
|
WithValue(domain.ContextWithLogMsg, errMsg)),
|
|
|
HeaderRow: domain.GetHeaderRow(loadDataTableCommand.HeaderRow),
|
|
|
}); logErr != nil {
|
|
|
return nil, logErr
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
// PrepareTemporaryFile 准备临时文件
|
|
|
func (fileService *FileService) PrepareTemporaryFile(ctx *domain.Context, cmd *command.PrepareTemporaryFileCommand) (interface{}, error) {
|
|
|
if err := cmd.ValidateCommand(); err != nil {
|
...
|
...
|
@@ -104,6 +152,13 @@ func (fileService *FileService) EditDataTable(ctx *domain.Context, editDataTable |
|
|
if editDataTableCommand.Action == "remove-column" && len(temporaryFile.Fields) == len(editDataTableCommand.ProcessFields) {
|
|
|
return nil, factory.FastError(fmt.Errorf("请至少保留一个数据列"))
|
|
|
}
|
|
|
if editDataTableCommand.Action == "rename-column" {
|
|
|
targetColumn := editDataTableCommand.ProcessFieldNames[0]
|
|
|
newColumnName := editDataTableCommand.Params["newColumnName"].(string)
|
|
|
if len(temporaryFile.MatchFields([]string{newColumnName})) > 0 && newColumnName != targetColumn {
|
|
|
return nil, factory.FastError(fmt.Errorf("已存在相同名称,修改无效"))
|
|
|
}
|
|
|
}
|
|
|
// allowAction := func(fields []*domain.Field, action string) error {
|
|
|
// for _, f := range fields {
|
|
|
// if f.SQLType != string(domain.String) &&
|
...
|
...
|
@@ -148,10 +203,14 @@ func (fileService *FileService) FlushDataTable(ctx *domain.Context, flushDataTab |
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
if err = temporaryFile.Valid(); err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
if _, err := flushDataTableService.Flush(ctx, flushDataTableCommand.ObjectId, &domain.Table{
|
|
|
DataFields: temporaryFile.Fields,
|
|
|
RowCount: temporaryFile.Total,
|
|
|
HeaderRow: temporaryFile.HeaderRow,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
...
|
...
|
@@ -266,11 +325,7 @@ func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.Exp |
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
var response = struct {
|
|
|
Url string `json:"url"`
|
|
|
Ext string `json:"ext"`
|
|
|
FileName string `json:"fileName"`
|
|
|
}{}
|
|
|
var response = FileUpload{}
|
|
|
if file.FileType == domain.SourceFile.ToString() {
|
|
|
response.Url = file.FileInfo.Url
|
|
|
response.Ext = domain.XLSX
|
...
|
...
|
@@ -282,7 +337,7 @@ func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.Exp |
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
f, err := httplib.Get(file.FileInfo.Url).Bytes()
|
|
|
f, err := httplib.Get(domain.ConvertFileUrlToInternal(file.FileInfo.Url)).Bytes()
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
...
|
...
|
@@ -292,17 +347,46 @@ func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.Exp |
|
|
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 {
|
|
|
|
|
|
response, err = saveFile(file.FileInfo.Name, importer.Reader().Header().Columns, data, domain.MakeToInterfaces(table.DataFields))
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
response.Url = domain.DownloadUrl(filename)
|
|
|
response.FileName = file.FileInfo.Name
|
|
|
response.Ext = domain.XLSX
|
|
|
//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)
|
|
|
//}
|
|
|
//
|
|
|
//var (
|
|
|
// config = utils.RouterConfig{
|
|
|
// OssEndPoint: "oss-cn-hangzhou.aliyuncs-internal.com",
|
|
|
// AccessKeyID: "LTAI4Fz1LUBW2fXp6QWaJHRS",
|
|
|
// AccessKeySecret: "aLZXwK8pgrs10Ws03qcN7NsrSXFVsg",
|
|
|
// BuckName: "byte-bank",
|
|
|
// }
|
|
|
// key = fmt.Sprintf("byte-bank/%v/%v", time.Now().Format("2006-01-02"), filename)
|
|
|
//)
|
|
|
//bucket, bucketErr := utils.NewBucket(config)
|
|
|
//if bucketErr == nil && bucket != nil {
|
|
|
// log.Logger.Info(fmt.Sprintf("end-point:%v key:%v", config.OssEndPoint, key))
|
|
|
// f, _ := os.Open(path)
|
|
|
// if err = utils.CreateObjects(bucket, utils.Object{
|
|
|
// Key: key,
|
|
|
// Value: f,
|
|
|
// }); err != nil {
|
|
|
// log.Logger.Error(err.Error())
|
|
|
// } else {
|
|
|
// response.Url = domain.ConvertInternalFileUrlToPublic(fmt.Sprintf("https://%v.%v/%v", config.BuckName, config.OssEndPoint, key))
|
|
|
// }
|
|
|
//}
|
|
|
//if len(response.Url) == 0 {
|
|
|
// response.Url = domain.DownloadUrl(filename)
|
|
|
//}
|
|
|
//response.FileName = file.FileInfo.Name
|
|
|
//response.Ext = domain.XLSX
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
|