log_service.go
7.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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"
"strings"
"time"
)
type PGLogService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewPGLogService(transactionContext *pgTransaction.TransactionContext) (*PGLogService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PGLogService{
transactionContext: transactionContext,
}, nil
}
}
func FastLog(transactionContext *pgTransaction.TransactionContext, logType domain.LogType, sourceId int, logEntry Log) error {
logService, _ := NewPGLogService(transactionContext)
return logService.Log(logType, sourceId, logEntry)
}
func (ptr *PGLogService) Log(logType domain.LogType, sourceId int, logEntry Log) error {
logRepository, _ := repository.NewLogRepository(ptr.transactionContext)
entry := logEntry.Entry()
log := &domain.Log{
LogType: logType.ToString(),
SourceId: sourceId,
Entry: &entry,
ObjectName: entry.ObjectName,
ObjectType: domain.EnumsDescription(domain.ObjectTypeMap, entry.ObjectType),
OperationType: domain.EnumsDescription(domain.OperationTypeMap, entry.OperationType),
Content: logEntry.Content(),
OperatorName: entry.OperatorName,
CreatedAt: time.Now(),
Context: logEntry.Context(),
}
if v, ok := logEntry.Context().GetValue(domain.ContextWithLogLevel); ok {
log.Entry.Level = string(v.(domain.LogLevel))
}
if v, ok := logEntry.Context().GetValue(domain.ContextWithLogLevel); ok {
log.Entry.Error = v.(string)
}
_, err := logRepository.Save(log)
return err
}
func (ptr *PGLogService) NewLogEntry() domain.LogEntry {
return domain.LogEntry{}
}
type Log interface {
Content() string
Entry() domain.LogEntry
Context() *domain.Context
}
var _ Log = (*FileUploadSuccessLog)(nil)
// 1.1文件上传成功
type FileUploadSuccessLog struct {
domain.LogEntry
}
func (l *FileUploadSuccessLog) Content() string {
return fmt.Sprintf("上传成功")
}
// 1.2文件上传失败
type FileUploadFailLog struct {
domain.LogEntry
Reason string
}
func (l *FileUploadFailLog) Content() string {
return fmt.Sprintf("上传失败,失败原因:%s", l.Reason)
}
// 2.文件校验
type FileVerifyLog struct {
domain.LogEntry
// 错误信息
Errors []string
// 记录数
Total int
}
func (l *FileVerifyLog) Content() string {
msg := fmt.Sprintf("校验完成,共计%d条记录 ", l.Total)
if len(l.Errors) > 0 {
msg += fmt.Sprintf("存在%v条报错", len(l.Errors))
}
return msg
}
// 3.主表生成日志
type GenerateMainTableLog struct {
domain.LogEntry
// 表名
TableName string
// 文件名
FileName string
}
func (l *GenerateMainTableLog) Content() string {
msg := fmt.Sprintf("来源校验文件:%v", l.FileName)
return msg
}
// 4.主表拆分
type SpiltMainTableLog struct {
domain.LogEntry
Reserve []*domain.Field
Delete []*domain.Field
Add []*domain.Field
// 表名
SourceTableName string
}
func (l *SpiltMainTableLog) Content() string {
var msg string
msg += fmt.Sprintf("来源表:%v", l.SourceTableName)
msg += l.makeMsg(" 删除字段", l.Delete)
msg += l.makeMsg(" 保留字段", l.Reserve)
msg += l.makeMsg(" 添加字段", l.Add)
return msg
}
func (l *SpiltMainTableLog) makeMsg(title string, fields []*domain.Field) string {
if len(l.fieldNames(fields)) > 0 {
return fmt.Sprintf("%s: %s ", title, strings.Join(l.fieldNames(fields), "、"))
}
return ""
}
func (l *SpiltMainTableLog) fieldNames(fields []*domain.Field) []string {
names := make([]string, 0)
for _, f := range fields {
names = append(names, f.Name)
}
return names
}
// 5.分表编辑
type SubTableEditLog struct {
domain.LogEntry
Reserve []*domain.Field
Delete []*domain.Field
Add []*domain.Field
}
func (l *SubTableEditLog) Content() string {
var msg string
msg = "分表编辑 "
msg += l.makeMsg("删除字段", l.Delete)
msg += l.makeMsg("保留字段", l.Reserve)
msg += l.makeMsg("添加字段", l.Add)
return msg
}
func (l *SubTableEditLog) makeMsg(title string, fields []*domain.Field) string {
if len(l.fieldNames(fields)) > 0 {
return fmt.Sprintf("%s: %s ", title, strings.Join(l.fieldNames(fields), "、"))
}
return ""
}
func (l *SubTableEditLog) fieldNames(fields []*domain.Field) []string {
names := make([]string, 0)
for _, f := range fields {
names = append(names, f.Name)
}
return names
}
// 6.表复制日志
type CopyTableLog struct {
domain.LogEntry
// 表名
SourceTableName string
}
func (l *CopyTableLog) Content() string {
msg := fmt.Sprintf("来源表:%v", l.SourceTableName)
return msg
}
// 7.编辑记录
type RowAddLog struct {
domain.LogEntry
}
func (l *RowAddLog) Content() string {
msg := fmt.Sprintf("新增行数据")
return msg
}
type RowUpdateLog struct {
domain.LogEntry
FieldValue []*domain.FieldValue
Where domain.Where
Number int
}
func (l *RowUpdateLog) Content() string {
change := ""
//index := l.Number + l.Where.Offset()
for _, f := range l.FieldValue {
if f.OldValue != f.Value {
//change += fmt.Sprintf("%v字段%v行的值从%v更改为%v;", f.Field.Name, index, f.OldValue, f.Value)
change += fmt.Sprintf("【%v】字段的值从“%v”更改为“%v”;", f.Field.Name, f.OldValue, f.Value)
}
}
if len(change) == 0 {
return "更新数据内容"
}
msg := fmt.Sprintf("更改数据内容:%v", change)
return msg
}
type RowRemoveLog struct {
domain.LogEntry
DeleteRowCount int
Where domain.Where
}
func (l *RowRemoveLog) Content() string {
index := l.DeleteRowCount
//msg := fmt.Sprintf("删除%v行数据;筛选件:%v",index,"")
msg := fmt.Sprintf("删除%v行数据;", index)
filters := make([]string, 0)
inArgs := func(args []string) string {
return strings.Join(args, "、")
}
for _, c := range l.Where.Conditions {
if len(c.In) > 0 {
filters = append(filters, fmt.Sprintf("【%v】 包含 %v", c.Field.Name, inArgs(starrocks.ArrayInterfaceToString(c.In))))
}
if len(c.Ex) > 0 {
filters = append(filters, fmt.Sprintf("【%v】 不包含 %v", c.Field.Name, inArgs(starrocks.ArrayInterfaceToString(c.Ex))))
}
}
if len(filters) > 0 {
msg += "筛选件:" + strings.Join(filters, "|")
}
return msg
}
// 8.表删除日志
type DeleteTableLog struct {
domain.LogEntry
// 表名
SourceTableName string
RowCount int
SubTables []*domain.Table
}
func (l *DeleteTableLog) Content() string {
msg := fmt.Sprintf("共计%v条数据", l.RowCount)
var tables []string
for _, t := range l.SubTables {
tables = append(tables, t.Name+"分表")
}
if len(tables) > 0 {
msg += fmt.Sprintf(",(存在分表)同步删除%s", strings.Join(tables, "/"))
}
return msg
}
// 9.数据追加日志
type AppendDataToTableLog struct {
domain.LogEntry
Table *domain.Table
File *domain.File
RowCount int
SubTables []*domain.Table
}
func (l *AppendDataToTableLog) Content() string {
msg := fmt.Sprintf("来源文件:%v校验文件,导入成功%v条,目标表单:%v", l.File.FileInfo.Name, l.RowCount, l.Table.Name)
var tables []string
for _, t := range l.SubTables {
tables = append(tables, t.Name+"分表")
}
if len(tables) > 0 {
msg += fmt.Sprintf(",关联更新%s", strings.Join(tables, "/"))
}
return msg
}
/*步骤日志*/
type ExcelTableEditLog struct {
domain.LogEntry
// 操作名称
OperateName string
// 操作列
ProcessFields []*domain.Field
}
func (l *ExcelTableEditLog) Content() string {
fieldsName := make([]string, 0)
for _, f := range l.ProcessFields {
fieldsName = append(fieldsName, fmt.Sprintf("【%v】", f.Name))
}
msg := fmt.Sprintf("%v:%v", l.OperateName, strings.Join(fieldsName, "、"))
return msg
}