作者 yangfu

feat: app table

... ... @@ -10,4 +10,7 @@ type AppTableFileAppendDataCommand struct {
Data []map[string]string `json:"data"`
AppKey string `json:"appKey"`
// 追加表数据标识 true:往应用表里面追加数据 false:跳过
AppendTableDataFlag bool `json:"appendTableDataFlag"`
}
... ...
... ... @@ -8,4 +8,6 @@ type CreateAppTableFileCommand struct {
Fields []*domain.Field `json:"fields"`
// 数据列表 key:name(字段中文名) value:值(字符串类型)
Data []map[string]string `json:"data"`
// 生成表标识 true:实例化一个表 false:跳过
GenerateTableFlag bool `json:"generateTableFlag"`
}
... ...
... ... @@ -21,6 +21,11 @@ type CreateFileCommand struct {
FileFrom string `json:"-"`
// AppKey
AppKey string `json:"-"`
// 生成表标识 true:实例化一个表 false:跳过
GenerateTableFlag bool `json:"-"`
// name 字段中文名
Fields []*domain.Field `json:"-"`
}
var MaxFileSize = 50 * 1024 * 1024
... ... @@ -35,6 +40,14 @@ func (createFileCommand *CreateFileCommand) Valid(validation *validation.Validat
validation.Error("文件大小超过50M")
return
}
if createFileCommand.GenerateTableFlag {
for _, f := range createFileCommand.Fields {
if err := f.Valid(); err != nil {
validation.Error(err.Error())
return
}
}
}
}
func (createFileCommand *CreateFileCommand) ValidateCommand() error {
... ...
... ... @@ -22,6 +22,10 @@ type FileDto struct {
HeaderRow int `json:"headerRow"`
// 所属应用
AppKey string `json:"appKey"`
// 表ID
TableId int `json:"tableId"`
// 允许表生成标识 1:允许生成分表 0:不允许
AllowTableGenerateFlag int `json:"allowTableGenerateFlag"`
}
func (d *FileDto) Load(f *domain.File) *FileDto {
... ... @@ -36,6 +40,10 @@ func (d *FileDto) Load(f *domain.File) *FileDto {
d.Time = xtime.New(f.UpdatedAt).Local().Format("2006-01-02 15:04:05")
d.HeaderRow = domain.GetHeaderRow(f.FileInfo.HeaderRow)
d.AppKey = f.AppKey
if len(f.AppKey) > 0 {
d.TableId = f.FileInfo.TableId
d.AllowTableGenerateFlag = 1
}
return d
}
... ...
... ... @@ -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())
... ...
... ... @@ -4,6 +4,7 @@ import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib"
"path/filepath"
"strings"
"time"
... ... @@ -24,7 +25,7 @@ type FileService struct {
}
// CreateFile 创建文件服务
func (fileService *FileService) CreateFile(ctx *domain.Context, createFileCommand *command.CreateFileCommand) (interface{}, error) {
func (fileService *FileService) CreateFile(ctx *domain.Context, createFileCommand *command.CreateFileCommand) (*domain.File, error) {
if err := createFileCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
... ... @@ -57,7 +58,7 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman
}
fileRepository, _, _ := factory.FastPgFile(transactionContext, 0)
// 文件名相同进行替换
// 文件名相同,进行替换
if oldFile, findOldFileErr := fileRepository.FindOne(map[string]interface{}{
"context": ctx,
"fileName": fileInfo.Name,
... ... @@ -78,6 +79,41 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 同时生成主表
// 前置需要进行预览、保持文件,才能生成主表
if createFileCommand.GenerateTableFlag {
table := domainService.NewTable(domain.MainTable, fileInfo.Name, createFileCommand.Fields, 0).
WithPrefix(domain.MainTable.ToString()).WithContext(&domain.Context{})
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
if table, err = tableRepository.Save(table); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
domainService.ByteCore.LoadDataTable(domain.ReqLoadDataTable{
FileId: file.FileId,
FileName: file.FileInfo.Name,
Url: file.FileInfo.Url,
Ext: file.FileInfo.Ext,
//Where: where,
OriginalTableId: fmt.Sprintf("%v", file.FileId),
IsFromOriginalTable: true,
TableFileUrl: file.FileInfo.Url,
ColumnSchemas: bytelib.DomainFieldsToColumnSchemas(table.Fields(false)),
SortParameters: make(map[string]interface{}),
})
response, err := domainService.ByteCore.SaveTable(domain.ReqSaveTable{FileId: file.FileId, Table: table})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if _, err = domainService.ByteCore.GenerateTable(ctx, domain.ReqGenerateTable{
file.FileId, response.Url, table,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
file.FileInfo.TableId = table.TableId
if file, err = fileRepository.Save(file); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err = factory.FastLog(transactionContext, domain.CommonLog, file.FileId, &domainService.FileUploadSuccessLog{
LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.SourceFile.ToString(), domain.FileUpload, ctx),
}); err != nil {
... ... @@ -86,7 +122,7 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
return file, nil
}
// GetFile 返回文件服务
... ...
... ... @@ -129,6 +129,7 @@ var ObjectTypeMap = map[string]string{
var (
XLS = ".xls"
XLSX = ".xlsx"
CSV = ".csv"
)
var (
... ...
... ... @@ -98,6 +98,50 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int,
}, nil
}
// AppendDataDirectly 直接追加数据
func (ptr *AppendDataToTableService) AppendDataDirectly(ctx *domain.Context, fileUrl string, table *domain.Table, mappingFields []*domain.MappingField) (interface{}, error) {
defer func() {
//AsyncEvent(domain.NewEventTable(ctx, domain.TableDataImportEvent).WithTable(table))
}()
if !(table.TableType == domain.MainTable.ToString() || table.TableType == domain.SideTable.ToString()) {
return nil, fmt.Errorf("只能追加数据到主表或者副表")
}
var err error
// 通知底层进行追加数据
requestData := domain.ReqAppendData{Table: table, FileId: int(time.Now().Unix()), FileUrl: fileUrl, ExcelTable: table}
if len(mappingFields) > 0 {
for _, m := range mappingFields {
var toField, fromField *domain.Field
var ok bool
toField, ok = table.MatchField(m.MainTableField)
if !ok {
continue
}
if len(m.VerifiedFileFieldName) == 0 {
fromField = &domain.Field{}
} else {
fromField, ok = table.MatchField(&domain.Field{Name: m.VerifiedFileFieldName})
if !ok {
continue
}
}
if fromField.SQLType != "" && !toField.SqlTypeEqual(fromField) {
//return nil, fmt.Errorf("字段【%s】的类型与导入数据表的类型不匹配", toField.Name)
return map[string]interface{}{
"result": fmt.Sprintf("字段【%s】的类型与导入数据表的类型不匹配", toField.Name),
}, nil
}
fromField.SQLType = toField.SQLType // 兼容 INT BIGINT
requestData.To = append(requestData.To, toField)
requestData.From = append(requestData.From, fromField)
}
}
if _, err = ByteCore.AppendData(requestData); err != nil {
return nil, err
}
return nil, nil
}
// PreflightCheck 预检
func (ptr *AppendDataToTableService) PreflightCheck(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
... ...
... ... @@ -34,8 +34,14 @@ func (controller *FileController) CreateAppTableFile() {
return
}
createFileCommand.AppKey = ParseAppKey(controller.BaseController)
data, err := fileService.CreateFile(&domain.Context{}, createFileCommand)
controller.Response(data, err)
// GenerateTableFlag 如果是true,生成主表
if createDigitalAppFileCommand.GenerateTableFlag {
createFileCommand.GenerateTableFlag = true
createFileCommand.Fields = createDigitalAppFileCommand.Fields
}
_, err = fileService.CreateFile(&domain.Context{}, createFileCommand)
controller.Response(struct{}{}, err)
}
func (controller *FileController) DeleteAppTableFile() {
... ... @@ -52,7 +58,17 @@ func (controller *FileController) AppendDataAppTableFile() {
cmd := &command.AppTableFileAppendDataCommand{}
controller.Unmarshal(cmd)
cmd.AppKey = ParseAppKey(controller.BaseController)
data, err := fileService.AppTableFileAppendData(&domain.Context{}, cmd)
var (
data interface{}
err error
)
// AppendDataToTableFlag 如果是true,生成主表 追加数据道表
if cmd.AppendTableDataFlag {
data, err = fileService.AppTableAppendData(&domain.Context{}, cmd)
} else {
data, err = fileService.AppTableFileAppendData(&domain.Context{}, cmd)
}
controller.Response(data, err)
}
... ...