table_row_data_edit_service.go
3.6 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
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() {
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
}
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
}