row_edit.go
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
)
func (tableService *TableService) RowEdit(ctx *domain.Context, cmd *command.RowEditCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.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()
}()
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 (tableService *TableService) RowEditV2(ctx *domain.Context, cmd *command.RowEditCommandV2) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.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()
}()
var table *domain.Table
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
if err != nil {
return nil, factory.FastError(err)
}
var options = starrocks.QueryOptions{
TableName: table.SQLName,
Select: table.Fields(true),
}
options.SetCondition(cmd.Where.Conditions).SetDefaultOrder()
options.SetOffsetLimit(cmd.Where.PageNumber, cmd.Where.PageSize)
var dataTable *domain.DataTable
dataTable, err = factory.FastDataTable(options)
if err != nil {
return nil, factory.FastError(err)
}
editDataService, _ := factory.CreateTableEditDataService(transactionContext)
_, err = editDataService.RowEdit(ctx, domain.EditDataRequest{
TableId: cmd.TableId,
Table: table,
Where: cmd.Where,
UpdateList: MapArrayToFieldValues(cmd.UpdateList, table, dataTable, true),
AddList: MapArrayToFieldValues(cmd.AddList, table, dataTable, false),
RemoveList: MapArrayToFieldValues(cmd.RemoveList, table, dataTable, 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 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
}