table_row_data_edit_service.go
5.4 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)
type TableEditDataService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewTableEditDataService(transactionContext *pgTransaction.TransactionContext) (*TableEditDataService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &TableEditDataService{
transactionContext: transactionContext,
}, nil
}
}
// RowEdit 行数据编辑
func (ptr *TableEditDataService) RowEdit(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
}
}
if table.TableType != domain.SideTable.ToString() && !request.IgnoreTableType {
return nil, fmt.Errorf("副表才允许编辑数据")
}
defer func() {
AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
}()
for _, l := range request.AddList {
if addErr := ptr.add(ctx, table, l, request.Where); addErr != nil {
log.Logger.Error(addErr.Error())
}
}
if len(request.RemoveList) > 0 {
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowRemoveLog{
LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
DeleteRowCount: len(request.RemoveList),
}); err != nil {
return nil, err
}
if err = starrocks.BatchDelete(starrocks.DB, table.SQLName, request.RemoveList); err != nil {
return nil, err
}
}
for _, l := range request.UpdateList {
if e := ptr.update(ctx, table, l, request.Where); e != nil {
log.Logger.Error(e.Error())
}
}
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
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowAddLog{
LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
}); err != nil {
return err
}
// 添加记录
if err = starrocks.Insert(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
return err
}
return nil
}
func (ptr *TableEditDataService) remove(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
var err error
// 删除记录
if err = starrocks.Delete(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
return err
}
return nil
}
func (ptr *TableEditDataService) update(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
var err error
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowUpdateLog{
LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
FieldValue: list.FieldValues,
Number: list.Number,
}); err != nil {
return err
}
// 更新记录
if err = starrocks.Update(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
return err
}
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
}