作者 yangfu

fix: field sqlname padding

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"`
}
... ...
... ... @@ -40,7 +40,7 @@ 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 {
if len(f.AppKey) > 0 && f.FileInfo.TableId > 0 {
d.TableId = f.FileInfo.TableId
d.AllowTableGenerateFlag = 1
}
... ...
... ... @@ -15,6 +15,7 @@ import (
"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"
... ... @@ -347,6 +348,134 @@ func (fileService *FileService) AppTableAppendData(ctx *domain.Context, cmd *com
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())
}
return struct{}{}, nil
}
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
}
... ...
... ... @@ -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,12 +102,14 @@ 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"`
Table *Table
UpdateList []*FieldValues `json:"updateList"`
RemoveList []*FieldValues `json:"removeList"`
AddList []*FieldValues `json:"addList"`
Where Where `json:"where"`
TableId int `json:"tableId"`
Table *Table
UpdateList []*FieldValues `json:"updateList"`
RemoveList []*FieldValues `json:"removeList"`
AddList []*FieldValues `json:"addList"`
Where Where `json:"where"`
IgnoreTableType bool
}
... ...
... ... @@ -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))
... ...
... ... @@ -64,7 +64,7 @@ func (controller *FileController) AppendDataAppTableFile() {
)
// AppendDataToTableFlag 如果是true,生成主表 追加数据道表
if cmd.AppendTableDataFlag {
data, err = fileService.AppTableAppendData(&domain.Context{}, cmd)
data, err = fileService.AppTableAppendDataDirect(&domain.Context{}, cmd)
} else {
data, err = fileService.AppTableFileAppendData(&domain.Context{}, cmd)
}
... ... @@ -81,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")
}
... ...