log_service.go
5.5 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
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"
"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(),
}
_, 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 += 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
}
// 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
}