作者 yangfu

fix: field sqlname padding

  1 +package command
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +type UpdateAppTableFileCommand struct {
  6 + Name string `json:"name"`
  7 + AppKey string `json:"appKey"`
  8 + AddFields []*domain.Field `json:"addFields"`
  9 +}
@@ -40,7 +40,7 @@ func (d *FileDto) Load(f *domain.File) *FileDto { @@ -40,7 +40,7 @@ func (d *FileDto) Load(f *domain.File) *FileDto {
40 d.Time = xtime.New(f.UpdatedAt).Local().Format("2006-01-02 15:04:05") 40 d.Time = xtime.New(f.UpdatedAt).Local().Format("2006-01-02 15:04:05")
41 d.HeaderRow = domain.GetHeaderRow(f.FileInfo.HeaderRow) 41 d.HeaderRow = domain.GetHeaderRow(f.FileInfo.HeaderRow)
42 d.AppKey = f.AppKey 42 d.AppKey = f.AppKey
43 - if len(f.AppKey) > 0 { 43 + if len(f.AppKey) > 0 && f.FileInfo.TableId > 0 {
44 d.TableId = f.FileInfo.TableId 44 d.TableId = f.FileInfo.TableId
45 d.AllowTableGenerateFlag = 1 45 d.AllowTableGenerateFlag = 1
46 } 46 }
@@ -15,6 +15,7 @@ import ( @@ -15,6 +15,7 @@ import (
15 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/apilib" 15 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/apilib"
16 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService" 16 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
17 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel" 17 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel"
  18 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
18 "os" 19 "os"
19 "strings" 20 "strings"
20 "time" 21 "time"
@@ -347,6 +348,134 @@ func (fileService *FileService) AppTableAppendData(ctx *domain.Context, cmd *com @@ -347,6 +348,134 @@ func (fileService *FileService) AppTableAppendData(ctx *domain.Context, cmd *com
347 return struct{}{}, nil 348 return struct{}{}, nil
348 } 349 }
349 350
  351 +func (fileService *FileService) AppTableAppendDataDirect(ctx *domain.Context, cmd *command.AppTableFileAppendDataCommand) (interface{}, error) {
  352 + transactionContext, err := factory.CreateTransactionContext(nil)
  353 + if err != nil {
  354 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  355 + }
  356 + if err := transactionContext.StartTransaction(); err != nil {
  357 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  358 + }
  359 + defer func() {
  360 + transactionContext.RollbackTransaction()
  361 + }()
  362 +
  363 + fileRepository, file, _ := factory.FastPgFile(transactionContext, 0)
  364 + file, err = fileRepository.FindOne(map[string]interface{}{"appKey": cmd.AppKey, "fileName": cmd.Name, "fileType": domain.SourceFile})
  365 + if err == domain.ErrorNotFound {
  366 + return nil, factory.FastError(errors.New("文件不存在"))
  367 + }
  368 + if err != nil {
  369 + return nil, factory.FastError(err)
  370 + }
  371 + if file.FileInfo.TableId == 0 {
  372 + return nil, factory.FastError(errors.New("表不存在"))
  373 + }
  374 + var (
  375 + titles = make([]string, 0)
  376 + table *domain.Table
  377 + )
  378 + _, table, err = factory.FastPgTable(transactionContext, file.FileInfo.TableId)
  379 + if err != nil {
  380 + return nil, factory.FastError(err)
  381 + }
  382 + for _, f := range table.Fields(false) {
  383 + titles = append(titles, f.Name)
  384 + }
  385 + mapNameField := domain.Fields(table.Fields(false)).ToMap()
  386 + for _, f := range cmd.Fields {
  387 + found := false
  388 + for _, column := range titles {
  389 + if column == f.Name {
  390 + found = true
  391 + break
  392 + }
  393 + }
  394 + if !found {
  395 + titles = append(titles, f.Name)
  396 + }
  397 + }
  398 + var mapData = make([]map[string]string, 0)
  399 + for i := range cmd.Data {
  400 + mapItem := make(map[string]string)
  401 + for k, v := range cmd.Data[i] {
  402 + if f, ok := mapNameField[k]; ok {
  403 + mapItem[f.SQLName] = v
  404 + }
  405 + }
  406 + mapData = append(mapData, mapItem)
  407 + }
  408 + editDataService, _ := factory.CreateTableEditDataService(transactionContext)
  409 + _, err = editDataService.BatchAdd(ctx, domain.EditDataRequest{
  410 + TableId: table.TableId,
  411 + Table: table,
  412 + Where: domain.Where{},
  413 + UpdateList: nil,
  414 + AddList: domainService.MapArrayToFieldValues(mapData, table, nil, false),
  415 + RemoveList: nil,
  416 + IgnoreTableType: true,
  417 + })
  418 + if err != nil {
  419 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  420 + }
  421 +
  422 + if err := transactionContext.CommitTransaction(); err != nil {
  423 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  424 + }
  425 + return struct{}{}, nil
  426 +}
  427 +
350 func (fileService *FileService) AppTableFileList(ctx *domain.Context, cmd *query.ListAppTableFileCommand) (interface{}, error) { 428 func (fileService *FileService) AppTableFileList(ctx *domain.Context, cmd *query.ListAppTableFileCommand) (interface{}, error) {
351 return fileService.GetAppFile(ctx, cmd.AppKey, cmd.Name) 429 return fileService.GetAppFile(ctx, cmd.AppKey, cmd.Name)
352 } 430 }
  431 +
  432 +func (fileService *FileService) UpdateAppTableFile(ctx *domain.Context, cmd *command.UpdateAppTableFileCommand) (interface{}, error) {
  433 + transactionContext, err := factory.CreateTransactionContext(nil)
  434 + if err != nil {
  435 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  436 + }
  437 + if err := transactionContext.StartTransaction(); err != nil {
  438 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  439 + }
  440 + defer func() {
  441 + transactionContext.RollbackTransaction()
  442 + }()
  443 +
  444 + fileRepository, file, _ := factory.FastPgFile(transactionContext, 0)
  445 + file, err = fileRepository.FindOne(map[string]interface{}{"appKey": cmd.AppKey, "fileName": cmd.Name, "fileType": domain.SourceFile})
  446 + if err == domain.ErrorNotFound {
  447 + return nil, factory.FastError(errors.New("文件不存在"))
  448 + }
  449 + if err != nil {
  450 + return nil, factory.FastError(err)
  451 + }
  452 + if len(cmd.AddFields) == 0 {
  453 + return nil, nil
  454 + }
  455 + tableRepository, table, _ := factory.FastPgTable(transactionContext, file.FileInfo.TableId)
  456 + if err == domain.ErrorNotFound {
  457 + return nil, factory.FastError(errors.New("文件表不存在"))
  458 + }
  459 + builder := domainService.NewDataFieldsBuilder()
  460 + for i, _ := range cmd.AddFields {
  461 + if _, ok := table.MatchField(cmd.AddFields[i]); ok {
  462 + return nil, factory.FastError(errors.New("字段已存在"))
  463 + }
  464 + }
  465 + for _, f := range cmd.AddFields {
  466 + dataField := builder.NewDataField(f.Name, f.SQLType, domain.MainTableField)
  467 + table.DataFields = append(table.DataFields, dataField)
  468 + if err = starrocks.AddTableColumn(starrocks.DB, table.SQLName, dataField); err != nil {
  469 + return nil, factory.FastError(err)
  470 + }
  471 + }
  472 +
  473 + if table, err = tableRepository.Save(table); err != nil {
  474 + return nil, factory.FastError(err)
  475 + }
  476 +
  477 + if err := transactionContext.CommitTransaction(); err != nil {
  478 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  479 + }
  480 + return struct{}{}, nil
  481 +}
@@ -68,6 +68,14 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -68,6 +68,14 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
68 if !ok { 68 if !ok {
69 return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name)) 69 return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name))
70 } 70 }
  71 + // 字段只传name时,补齐sqlName
  72 + for i, c := range cmd.Where.Conditions {
  73 + if c.Field != nil && c.Field.SQLName == "" {
  74 + if v, ok := table.MatchField(c.Field); ok {
  75 + cmd.Where.Conditions[i].Field.SQLName = v.SQLName
  76 + }
  77 + }
  78 + }
71 if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField { 79 if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
72 return empty, nil 80 return empty, nil
73 } 81 }
@@ -30,6 +30,16 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command @@ -30,6 +30,16 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
30 if err != nil { 30 if err != nil {
31 return nil, factory.FastError(err) 31 return nil, factory.FastError(err)
32 } 32 }
  33 +
  34 + // 字段只传name时,补齐sqlName
  35 + for i, c := range cmd.Where.Conditions {
  36 + if c.Field != nil && c.Field.SQLName == "" {
  37 + if v, ok := table.MatchField(c.Field); ok {
  38 + cmd.Where.Conditions[i].Field.SQLName = v.SQLName
  39 + }
  40 + }
  41 + }
  42 +
33 // 方案 计算项 计算集 做缓存 43 // 方案 计算项 计算集 做缓存
34 if cmd.UseCache && table.AssertTableType(domain.SchemaTable, domain.CalculateItem, domain.CalculateSet) { 44 if cmd.UseCache && table.AssertTableType(domain.SchemaTable, domain.CalculateItem, domain.CalculateSet) {
35 if d, ok := cache.GetDataTable(table.TableId); ok { 45 if d, ok := cache.GetDataTable(table.TableId); ok {
@@ -102,12 +102,14 @@ type EditTableRequest struct { @@ -102,12 +102,14 @@ type EditTableRequest struct {
102 102
103 type TableEditDataService interface { 103 type TableEditDataService interface {
104 RowEdit(ctx *Context, request EditDataRequest) (interface{}, error) 104 RowEdit(ctx *Context, request EditDataRequest) (interface{}, error)
  105 + BatchAdd(ctx *Context, request EditDataRequest) (interface{}, error)
105 } 106 }
106 type EditDataRequest struct { 107 type EditDataRequest struct {
107 - TableId int `json:"tableId"`  
108 - Table *Table  
109 - UpdateList []*FieldValues `json:"updateList"`  
110 - RemoveList []*FieldValues `json:"removeList"`  
111 - AddList []*FieldValues `json:"addList"`  
112 - Where Where `json:"where"` 108 + TableId int `json:"tableId"`
  109 + Table *Table
  110 + UpdateList []*FieldValues `json:"updateList"`
  111 + RemoveList []*FieldValues `json:"removeList"`
  112 + AddList []*FieldValues `json:"addList"`
  113 + Where Where `json:"where"`
  114 + IgnoreTableType bool
113 } 115 }
@@ -35,7 +35,7 @@ func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.Edi @@ -35,7 +35,7 @@ func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.Edi
35 } 35 }
36 } 36 }
37 37
38 - if table.TableType != domain.SideTable.ToString() { 38 + if table.TableType != domain.SideTable.ToString() && !request.IgnoreTableType {
39 return nil, fmt.Errorf("副表才允许编辑数据") 39 return nil, fmt.Errorf("副表才允许编辑数据")
40 } 40 }
41 defer func() { 41 defer func() {
@@ -68,6 +68,31 @@ func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.Edi @@ -68,6 +68,31 @@ func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.Edi
68 return nil, nil 68 return nil, nil
69 } 69 }
70 70
  71 +// BatchAdd 行数据批量添加
  72 +func (ptr *TableEditDataService) BatchAdd(ctx *domain.Context, request domain.EditDataRequest) (interface{}, error) {
  73 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  74 + var table *domain.Table = request.Table
  75 + var err error
  76 + if table == nil {
  77 + table, err = tableRepository.FindOne(map[string]interface{}{"tableId": request.TableId})
  78 + if err != nil {
  79 + return nil, err
  80 + }
  81 + }
  82 +
  83 + defer func() {
  84 + AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
  85 + }()
  86 +
  87 + for _, l := range request.AddList {
  88 + // 添加记录
  89 + if err = starrocks.Insert(starrocks.DB, table.SQLName, l.FieldValues); err != nil {
  90 + log.Logger.Error(fmt.Sprintf("添加记录错误:%v", err.Error()))
  91 + }
  92 + }
  93 + return nil, nil
  94 +}
  95 +
71 func (ptr *TableEditDataService) add(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error { 96 func (ptr *TableEditDataService) add(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
72 var err error 97 var err error
73 98
@@ -113,3 +138,41 @@ func (ptr *TableEditDataService) update(ctx *domain.Context, table *domain.Table @@ -113,3 +138,41 @@ func (ptr *TableEditDataService) update(ctx *domain.Context, table *domain.Table
113 } 138 }
114 return nil 139 return nil
115 } 140 }
  141 +
  142 +func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues {
  143 + var result = make([]*domain.FieldValues, 0)
  144 + //history := dto.ToFieldDataByPK(table, dataTable)
  145 + mapField := domain.Fields(table.Fields(true)).ToMapBySqlName()
  146 + for _, m := range list {
  147 + var fieldValues = &domain.FieldValues{
  148 + FieldValues: make([]*domain.FieldValue, 0),
  149 + }
  150 + //matchItem, ok := history[m[domain.DefaultPkField]]
  151 + //if mustMatch {
  152 + // if !ok {
  153 + // continue
  154 + // }
  155 + //}
  156 + if _, ok := m[domain.DefaultPkField]; !ok {
  157 + m[domain.DefaultPkField] = ""
  158 + }
  159 + for key, value := range m {
  160 + field, ok := mapField[key]
  161 + if !ok || field.Flag == domain.ManualField {
  162 + continue
  163 + }
  164 + fieldValue := &domain.FieldValue{
  165 + Field: field,
  166 + Value: value,
  167 + }
  168 + //if mustMatch {
  169 + // if oldValue, ok := matchItem[key]; ok {
  170 + // fieldValue.OldValue = oldValue
  171 + // }
  172 + //}
  173 + fieldValues.FieldValues = append(fieldValues.FieldValues, fieldValue)
  174 + }
  175 + result = append(result, fieldValues)
  176 + }
  177 + return result
  178 +}
@@ -172,3 +172,15 @@ insert into {{.ViewName}} Values {{.Values}} @@ -172,3 +172,15 @@ insert into {{.ViewName}} Values {{.Values}}
172 }) 172 })
173 return html.UnescapeString(buf.String()) 173 return html.UnescapeString(buf.String())
174 } 174 }
  175 +
  176 +func AddTableColumn(db *gorm.DB, tableName string, filed *domain.Field) error {
  177 + tx := db.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", tableName, filed.SQLName, convertFiledSQLType(filed.SQLType)))
  178 + return tx.Error
  179 +}
  180 +
  181 +func convertFiledSQLType(sqlType string) string {
  182 + if sqlType == domain.Float.ToString() || sqlType == domain.DECIMAL279.ToString() {
  183 + return domain.DECIMAL279.ToString()
  184 + }
  185 + return sqlType
  186 +}
@@ -58,7 +58,7 @@ func init() { @@ -58,7 +58,7 @@ func init() {
58 web.InsertFilter("/*", web.BeforeRouter, RequestCostBefore()) 58 web.InsertFilter("/*", web.BeforeRouter, RequestCostBefore())
59 web.InsertFilter("/*", web.BeforeExec, controllers.BlacklistFilter(controllers.BlacklistRouters)) 59 web.InsertFilter("/*", web.BeforeExec, controllers.BlacklistFilter(controllers.BlacklistRouters))
60 web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(true)) // filters.CreateRequstLogFilter(Logger) 60 web.InsertFilter("/*", web.BeforeExec, CreateRequestLogFilter(true)) // filters.CreateRequstLogFilter(Logger)
61 - if constant.SERVICE_ENV == "dev" { //|| web.BConfig.RunMode =="test" 61 + if constant.SERVICE_ENV == "test" { //|| web.BConfig.RunMode =="test"
62 web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false)) 62 web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(Logger), web.WithReturnOnOutput(false))
63 } 63 }
64 web.InsertFilter("/*", web.AfterExec, RequestCostAfter(150), web.WithReturnOnOutput(false)) 64 web.InsertFilter("/*", web.AfterExec, RequestCostAfter(150), web.WithReturnOnOutput(false))
@@ -64,7 +64,7 @@ func (controller *FileController) AppendDataAppTableFile() { @@ -64,7 +64,7 @@ func (controller *FileController) AppendDataAppTableFile() {
64 ) 64 )
65 // AppendDataToTableFlag 如果是true,生成主表 追加数据道表 65 // AppendDataToTableFlag 如果是true,生成主表 追加数据道表
66 if cmd.AppendTableDataFlag { 66 if cmd.AppendTableDataFlag {
67 - data, err = fileService.AppTableAppendData(&domain.Context{}, cmd) 67 + data, err = fileService.AppTableAppendDataDirect(&domain.Context{}, cmd)
68 } else { 68 } else {
69 data, err = fileService.AppTableFileAppendData(&domain.Context{}, cmd) 69 data, err = fileService.AppTableFileAppendData(&domain.Context{}, cmd)
70 } 70 }
@@ -81,6 +81,15 @@ func (controller *FileController) ListAppTableFile() { @@ -81,6 +81,15 @@ func (controller *FileController) ListAppTableFile() {
81 controller.Response(data, err) 81 controller.Response(data, err)
82 } 82 }
83 83
  84 +func (controller *FileController) UpdateAppTableFile() {
  85 + fileService := service.NewFileService(nil)
  86 + cmd := &command.UpdateAppTableFileCommand{}
  87 + controller.Unmarshal(cmd)
  88 + cmd.AppKey = ParseAppKey(controller.BaseController)
  89 + data, err := fileService.UpdateAppTableFile(&domain.Context{}, cmd)
  90 + controller.Response(data, err)
  91 +}
  92 +
84 func (controller *FileController) UpdateFile() { 93 func (controller *FileController) UpdateFile() {
85 fileService := service.NewFileService(nil) 94 fileService := service.NewFileService(nil)
86 updateFileCommand := &command.UpdateFileCommand{} 95 updateFileCommand := &command.UpdateFileCommand{}
@@ -12,4 +12,5 @@ func init() { @@ -12,4 +12,5 @@ func init() {
12 web.Router("/api/app-table-file/delete", &controllers.FileController{}, "Post:DeleteAppTableFile") 12 web.Router("/api/app-table-file/delete", &controllers.FileController{}, "Post:DeleteAppTableFile")
13 web.Router("/api/app-table-file/append-data", &controllers.FileController{}, "Post:AppendDataAppTableFile") 13 web.Router("/api/app-table-file/append-data", &controllers.FileController{}, "Post:AppendDataAppTableFile")
14 web.Router("/api/app-table-file/list", &controllers.FileController{}, "Post:ListAppTableFile") 14 web.Router("/api/app-table-file/list", &controllers.FileController{}, "Post:ListAppTableFile")
  15 + web.Router("/api/app-table-file/update", &controllers.FileController{}, "Post:UpdateAppTableFile")
15 } 16 }