作者 yangfu

Merge branch 'test'

... ... @@ -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"`
}
... ...
package command
import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
type UpdateAppTableFileCommand struct {
Name string `json:"name"`
AppKey string `json:"appKey"`
AddFields []*domain.Field `json:"addFields"`
}
... ...
... ... @@ -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 && f.FileInfo.TableId > 0 {
d.TableId = f.FileInfo.TableId
d.AllowTableGenerateFlag = 1
}
return d
}
... ...
... ... @@ -6,13 +6,16 @@ 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"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"os"
"strings"
"time"
... ... @@ -83,6 +86,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 +177,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 +217,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 +236,188 @@ 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())
}
return struct{}{}, nil
}
func (fileService *FileService) AppTableAppendDataDirect(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 (
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)
}
mapNameField := domain.Fields(table.Fields(false)).ToMap()
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)
}
}
var mapData = make([]map[string]string, 0)
for i := range cmd.Data {
mapItem := make(map[string]string)
for k, v := range cmd.Data[i] {
if f, ok := mapNameField[k]; ok {
mapItem[f.SQLName] = v
}
}
mapData = append(mapData, mapItem)
}
editDataService, _ := factory.CreateTableEditDataService(transactionContext)
_, err = editDataService.BatchAdd(ctx, domain.EditDataRequest{
TableId: table.TableId,
Table: table,
Where: domain.Where{},
UpdateList: nil,
AddList: domainService.MapArrayToFieldValues(mapData, table, nil, false),
RemoveList: nil,
IgnoreTableType: true,
})
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())
... ... @@ -209,3 +428,54 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd
func (fileService *FileService) AppTableFileList(ctx *domain.Context, cmd *query.ListAppTableFileCommand) (interface{}, error) {
return fileService.GetAppFile(ctx, cmd.AppKey, cmd.Name)
}
func (fileService *FileService) UpdateAppTableFile(ctx *domain.Context, cmd *command.UpdateAppTableFileCommand) (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 len(cmd.AddFields) == 0 {
return nil, nil
}
tableRepository, table, _ := factory.FastPgTable(transactionContext, file.FileInfo.TableId)
if err == domain.ErrorNotFound {
return nil, factory.FastError(errors.New("文件表不存在"))
}
builder := domainService.NewDataFieldsBuilder()
for i, _ := range cmd.AddFields {
if _, ok := table.MatchField(cmd.AddFields[i]); ok {
return nil, factory.FastError(errors.New("字段已存在"))
}
}
for _, f := range cmd.AddFields {
dataField := builder.NewDataField(f.Name, f.SQLType, domain.MainTableField)
table.DataFields = append(table.DataFields, dataField)
if err = starrocks.AddTableColumn(starrocks.DB, table.SQLName, dataField); err != nil {
return nil, factory.FastError(err)
}
}
if table, err = tableRepository.Save(table); 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
}
... ...
... ... @@ -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 返回文件服务
... ...
... ... @@ -68,6 +68,14 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
if !ok {
return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name))
}
// 字段只传name时,补齐sqlName
for i, c := range cmd.Where.Conditions {
if c.Field != nil && c.Field.SQLName == "" {
if v, ok := table.MatchField(c.Field); ok {
cmd.Where.Conditions[i].Field.SQLName = v.SQLName
}
}
}
if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
return empty, nil
}
... ...
... ... @@ -30,6 +30,16 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
if err != nil {
return nil, factory.FastError(err)
}
// 字段只传name时,补齐sqlName
for i, c := range cmd.Where.Conditions {
if c.Field != nil && c.Field.SQLName == "" {
if v, ok := table.MatchField(c.Field); ok {
cmd.Where.Conditions[i].Field.SQLName = v.SQLName
}
}
}
// 方案 计算项 计算集 做缓存
if cmd.UseCache && table.AssertTableType(domain.SchemaTable, domain.CalculateItem, domain.CalculateSet) {
if d, ok := cache.GetDataTable(table.TableId); ok {
... ...
... ... @@ -102,6 +102,7 @@ type EditTableRequest struct {
type TableEditDataService interface {
RowEdit(ctx *Context, request EditDataRequest) (interface{}, error)
BatchAdd(ctx *Context, request EditDataRequest) (interface{}, error)
}
type EditDataRequest struct {
TableId int `json:"tableId"`
... ... @@ -110,4 +111,5 @@ type EditDataRequest struct {
RemoveList []*FieldValues `json:"removeList"`
AddList []*FieldValues `json:"addList"`
Where Where `json:"where"`
IgnoreTableType bool
}
... ...
... ... @@ -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)
... ...
... ... @@ -35,7 +35,7 @@ func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.Edi
}
}
if table.TableType != domain.SideTable.ToString() {
if table.TableType != domain.SideTable.ToString() && !request.IgnoreTableType {
return nil, fmt.Errorf("副表才允许编辑数据")
}
defer func() {
... ... @@ -68,6 +68,31 @@ func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.Edi
return nil, nil
}
// BatchAdd 行数据批量添加
func (ptr *TableEditDataService) BatchAdd(ctx *domain.Context, request domain.EditDataRequest) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
var table *domain.Table = request.Table
var err error
if table == nil {
table, err = tableRepository.FindOne(map[string]interface{}{"tableId": request.TableId})
if err != nil {
return nil, err
}
}
defer func() {
AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
}()
for _, l := range request.AddList {
// 添加记录
if err = starrocks.Insert(starrocks.DB, table.SQLName, l.FieldValues); err != nil {
log.Logger.Error(fmt.Sprintf("添加记录错误:%v", err.Error()))
}
}
return nil, nil
}
func (ptr *TableEditDataService) add(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
var err error
... ... @@ -113,3 +138,41 @@ func (ptr *TableEditDataService) update(ctx *domain.Context, table *domain.Table
}
return nil
}
func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues {
var result = make([]*domain.FieldValues, 0)
//history := dto.ToFieldDataByPK(table, dataTable)
mapField := domain.Fields(table.Fields(true)).ToMapBySqlName()
for _, m := range list {
var fieldValues = &domain.FieldValues{
FieldValues: make([]*domain.FieldValue, 0),
}
//matchItem, ok := history[m[domain.DefaultPkField]]
//if mustMatch {
// if !ok {
// continue
// }
//}
if _, ok := m[domain.DefaultPkField]; !ok {
m[domain.DefaultPkField] = ""
}
for key, value := range m {
field, ok := mapField[key]
if !ok || field.Flag == domain.ManualField {
continue
}
fieldValue := &domain.FieldValue{
Field: field,
Value: value,
}
//if mustMatch {
// if oldValue, ok := matchItem[key]; ok {
// fieldValue.OldValue = oldValue
// }
//}
fieldValues.FieldValues = append(fieldValues.FieldValues, fieldValue)
}
result = append(result, fieldValues)
}
return result
}
... ...
... ... @@ -172,3 +172,15 @@ insert into {{.ViewName}} Values {{.Values}}
})
return html.UnescapeString(buf.String())
}
func AddTableColumn(db *gorm.DB, tableName string, filed *domain.Field) error {
tx := db.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", tableName, filed.SQLName, convertFiledSQLType(filed.SQLType)))
return tx.Error
}
func convertFiledSQLType(sqlType string) string {
if sqlType == domain.Float.ToString() || sqlType == domain.DECIMAL279.ToString() {
return domain.DECIMAL279.ToString()
}
return sqlType
}
... ...
... ... @@ -58,7 +58,7 @@ func init() {
web.InsertFilter("/*", web.BeforeRouter, RequestCostBefore())
web.InsertFilter("/*", web.BeforeExec, controllers.BlacklistFilter(controllers.BlacklistRouters))
web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(true)) // filters.CreateRequstLogFilter(Logger)
if constant.SERVICE_ENV == "dev" { //|| web.BConfig.RunMode =="test"
if constant.SERVICE_ENV == "test" { //|| web.BConfig.RunMode =="test"
web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
}
web.InsertFilter("/*", web.AfterExec, RequestCostAfter(150), web.WithReturnOnOutput(false))
... ...
... ... @@ -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.AppTableAppendDataDirect(&domain.Context{}, cmd)
} else {
data, err = fileService.AppTableFileAppendData(&domain.Context{}, cmd)
}
controller.Response(data, err)
}
... ... @@ -65,6 +81,15 @@ func (controller *FileController) ListAppTableFile() {
controller.Response(data, err)
}
func (controller *FileController) UpdateAppTableFile() {
fileService := service.NewFileService(nil)
cmd := &command.UpdateAppTableFileCommand{}
controller.Unmarshal(cmd)
cmd.AppKey = ParseAppKey(controller.BaseController)
data, err := fileService.UpdateAppTableFile(&domain.Context{}, cmd)
controller.Response(data, err)
}
func (controller *FileController) UpdateFile() {
fileService := service.NewFileService(nil)
updateFileCommand := &command.UpdateFileCommand{}
... ...
... ... @@ -12,4 +12,5 @@ func init() {
web.Router("/api/app-table-file/delete", &controllers.FileController{}, "Post:DeleteAppTableFile")
web.Router("/api/app-table-file/append-data", &controllers.FileController{}, "Post:AppendDataAppTableFile")
web.Router("/api/app-table-file/list", &controllers.FileController{}, "Post:ListAppTableFile")
web.Router("/api/app-table-file/update", &controllers.FileController{}, "Post:UpdateAppTableFile")
}
... ...