...
|
...
|
@@ -6,12 +6,14 @@ import ( |
|
|
"fmt"
|
|
|
"github.com/beego/beego/v2/client/httplib"
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
pgTransaction "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/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/apilib"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel"
|
|
|
"os"
|
|
|
"strings"
|
...
|
...
|
@@ -83,6 +85,34 @@ func saveFile(name string, title []string, dataList [][]string, toInterfaces fun |
|
|
return response, nil
|
|
|
}
|
|
|
|
|
|
func saveCsvFile(name string, title []string, dataList [][]string, toInterfaces func([]string) []interface{}) (FileUpload, error) {
|
|
|
var (
|
|
|
response = FileUpload{}
|
|
|
err error
|
|
|
)
|
|
|
var writerTo = excel.NewCSVWriterTo(title, dataList)
|
|
|
filename := fmt.Sprintf("%v_%v.csv", name, time.Now().Format("060102150405"))
|
|
|
path := fmt.Sprintf("public/%v", filename)
|
|
|
if err = writerTo.Save(path); err != nil {
|
|
|
return response, factory.FastError(err)
|
|
|
}
|
|
|
api := apilib.NewApiAuthLib(constant.OPEN_API_HOST)
|
|
|
uploadResponse, err := api.Upload(apilib.RequestUpload{
|
|
|
UploadFileMap: map[string]string{"file": path},
|
|
|
})
|
|
|
if err != nil {
|
|
|
return response, err
|
|
|
}
|
|
|
if stat, err := os.Stat(path); err == nil {
|
|
|
response.FileSize = stat.Size()
|
|
|
}
|
|
|
response.Url = domain.ConvertInternalFileUrlToPublic(uploadResponse.Path)
|
|
|
response.FileName = name
|
|
|
response.Ext = domain.CSV
|
|
|
|
|
|
return response, nil
|
|
|
}
|
|
|
|
|
|
type FileUpload struct {
|
|
|
Url string `json:"url"`
|
|
|
Ext string `json:"ext"`
|
...
|
...
|
@@ -146,7 +176,10 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd |
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
reader := bytes.NewReader(f)
|
|
|
var importer *excel.Importer = excel.NewExcelImportByFile(file.FileInfo.Ext)
|
|
|
var (
|
|
|
importer *excel.Importer = excel.NewExcelImportByFile(file.FileInfo.Ext)
|
|
|
appendTableDataList = make([][]string, 0)
|
|
|
)
|
|
|
data, err := importer.OpenExcelFromIoReader(reader)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
...
|
...
|
@@ -183,14 +216,17 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd |
|
|
}
|
|
|
}
|
|
|
data = append(data, row)
|
|
|
if cmd.AppendTableDataFlag {
|
|
|
appendTableDataList = append(appendTableDataList, row)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//if !cmd.AppendTableDataFlag {
|
|
|
// 上传文件
|
|
|
fileUpload, err := saveFile(cmd.Name, titles, data, nil)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
// 更新文件
|
|
|
file.FileInfo.Url = fileUpload.Url
|
|
|
file.FileInfo.FileSize = int(fileUpload.FileSize)
|
...
|
...
|
@@ -199,6 +235,111 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd |
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
//}
|
|
|
//else if cmd.AppendTableDataFlag && file.FileInfo.TableId != 0 { // 追加数据到应用表
|
|
|
//var table *domain.Table
|
|
|
//if _, table, err = factory.FastPgTable(transactionContext, file.FileInfo.TableId); err != nil {
|
|
|
// return nil, factory.FastError(err)
|
|
|
//}
|
|
|
//// 上传文件
|
|
|
//fileUpload, err := saveCsvFile(cmd.Name, titles, appendTableDataList, nil)
|
|
|
//if err != nil {
|
|
|
// return nil, factory.FastError(err)
|
|
|
//}
|
|
|
//appendDataToTableService, _ := domainService.NewAppendDataToTableService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
//var mappingFields = make([]*domain.MappingField, 0)
|
|
|
//for _, f := range cmd.Fields {
|
|
|
// mappingFields = append(mappingFields, &domain.MappingField{
|
|
|
// MainTableField: &domain.Field{Name: f.Name},
|
|
|
// VerifiedFileFieldName: f.Name,
|
|
|
// })
|
|
|
//}
|
|
|
//if _, err = appendDataToTableService.AppendDataDirectly(ctx, fileUpload.Url, table, mappingFields); err != nil {
|
|
|
// return nil, factory.FastError(err)
|
|
|
//}
|
|
|
//}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return struct{}{}, nil
|
|
|
}
|
|
|
|
|
|
func (fileService *FileService) AppTableAppendData(ctx *domain.Context, cmd *command.AppTableFileAppendDataCommand) (interface{}, 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()
|
|
|
}()
|
|
|
|
|
|
fileRepository, file, _ := factory.FastPgFile(transactionContext, 0)
|
|
|
file, err = fileRepository.FindOne(map[string]interface{}{"appKey": cmd.AppKey, "fileName": cmd.Name, "fileType": domain.SourceFile})
|
|
|
if err == domain.ErrorNotFound {
|
|
|
return nil, factory.FastError(errors.New("文件不存在"))
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
if file.FileInfo.TableId == 0 {
|
|
|
return nil, factory.FastError(errors.New("表不存在"))
|
|
|
}
|
|
|
var (
|
|
|
appendTableDataList = make([][]string, 0)
|
|
|
titles = make([]string, 0)
|
|
|
table *domain.Table
|
|
|
)
|
|
|
_, table, err = factory.FastPgTable(transactionContext, file.FileInfo.TableId)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
for _, f := range table.Fields(false) {
|
|
|
titles = append(titles, f.Name)
|
|
|
}
|
|
|
for _, f := range cmd.Fields {
|
|
|
found := false
|
|
|
for _, column := range titles {
|
|
|
if column == f.Name {
|
|
|
found = true
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
if !found {
|
|
|
titles = append(titles, f.Name)
|
|
|
}
|
|
|
}
|
|
|
for i := range cmd.Data {
|
|
|
row := make([]string, 0)
|
|
|
for _, filed := range titles {
|
|
|
if v, ok := cmd.Data[i][filed]; ok {
|
|
|
row = append(row, v)
|
|
|
} else {
|
|
|
row = append(row, "")
|
|
|
}
|
|
|
}
|
|
|
appendTableDataList = append(appendTableDataList, row)
|
|
|
}
|
|
|
|
|
|
// 上传文件
|
|
|
fileUpload, err := saveCsvFile(cmd.Name, titles, appendTableDataList, nil)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
appendDataToTableService, _ := domainService.NewAppendDataToTableService(transactionContext.(*pgTransaction.TransactionContext))
|
|
|
var mappingFields = make([]*domain.MappingField, 0)
|
|
|
for _, f := range cmd.Fields {
|
|
|
mappingFields = append(mappingFields, &domain.MappingField{
|
|
|
MainTableField: &domain.Field{Name: f.Name},
|
|
|
VerifiedFileFieldName: f.Name,
|
|
|
})
|
|
|
}
|
|
|
if _, err = appendDataToTableService.AppendDataDirectly(ctx, fileUpload.Url, table, mappingFields); err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
|