file_table_flush_data_table_service.go
7.0 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
package domainService
import (
"bytes"
"fmt"
"github.com/google/uuid"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"time"
)
type FlushDataTableService struct {
FileId int
transactionContext *pgTransaction.TransactionContext
}
// Flush 保存表 【data-table】
func (ptr *FlushDataTableService) Flush(ctx *domain.Context, fileId int, table *domain.Table) (interface{}, error) {
fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
if err != nil {
return nil, fmt.Errorf("临时文件不存在")
}
sourceFile, err := fileRepository.FindOne(map[string]interface{}{"fileId": file.SourceFileId})
if err != nil {
return nil, fmt.Errorf("源文件不存在")
}
// New Table
table = NewTable(domain.ExcelTable, file.FileInfo.Name, table.DataFields, table.RowCount).WithContext(ctx)
// 通知底层保存、进行回调
var response *domain.DataSaveTable
response, err = ByteCore.SaveTable(domain.ReqSaveTable{FileId: fileId})
if err != nil {
return nil, err
}
// 来自源文件的
// 临时文件 -》校验文件
var newUrl string
if response != nil {
newUrl = response.Url
}
log.Logger.Info("更新文件地址", map[string]interface{}{"from_url": file.FileInfo.Url, "to_url": newUrl, "sourceFileId": file.SourceFileId})
switch sourceFile.FileType {
case domain.SourceFile.ToString():
if err = ptr.flushSourceFile(ctx, table, file, sourceFile, fileRepository, newUrl); err != nil {
return nil, err
}
case domain.VerifiedFile.ToString():
if err = ptr.flushVerifiedFile(ctx, table, file, sourceFile, fileRepository, newUrl); err != nil {
return nil, err
}
}
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, file.FileId, &FileVerifyLog{
LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.VerifiedFile.ToString(), domain.FileVerify, ctx),
Total: table.RowCount,
}); err != nil {
return nil, err
}
return struct{}{}, nil
}
func (ptr *FlushDataTableService) flushSourceFile(ctx *domain.Context, table *domain.Table, file *domain.File, sourceFile *domain.File, fileRepository domain.FileRepository, url string) error {
var err error
// 新增
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err = tableRepository.Save(table)
if err != nil {
return err
}
file.FileInfo.TableId = table.TableId
file.FileType = domain.VerifiedFile.ToString()
file.UpdateFileUrl(url)
if file, err = fileRepository.Save(file); err != nil {
return err
}
_, files, err := fileRepository.Find(map[string]interface{}{"context": ctx, "sourceFileId": sourceFile.FileId, "fileType": domain.VerifiedFile.ToString(), "notInFileIds": []int{file.FileId}})
if err != nil {
return err
}
deleteFileService, _ := NewDeleteFileService(ptr.transactionContext)
if err = deleteFileService.DeleteFiles(ctx, files...); err != nil {
return err
}
return nil
}
func (ptr *FlushDataTableService) flushVerifiedFile(ctx *domain.Context, table *domain.Table, file *domain.File, sourceFile *domain.File, fileRepository domain.FileRepository, url string) error {
var err error
// 追加日志到校验文件
if err = dao.ChangeStepLogOwner(ptr.transactionContext, file.FileId, sourceFile.FileId); err != nil {
return err
}
sourceFile.UpdateFileUrl(url)
if _, err = fileRepository.Save(sourceFile); err != nil {
return err
}
// 删除中间文件
if err = dao.FileDelete(ptr.transactionContext, file.FileId, domain.TemporaryFile); err != nil {
return err
}
// 更新表信息
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
sourceTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": sourceFile.FileInfo.TableId})
if err != nil {
return err
}
sourceTable.DataFields = table.DataFields
sourceTable.DataFieldIndex = table.DataFieldIndex
sourceTable.UpdatedAt = time.Now()
_, err = tableRepository.Save(sourceTable)
if err != nil {
return err
}
return nil
}
func NewFlushDataTableService(transactionContext *pgTransaction.TransactionContext) (*FlushDataTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &FlushDataTableService{
transactionContext: transactionContext,
}, nil
}
}
func NewTable(tableType domain.TableType, fileName string, dataFields []*domain.Field, rowCount int) *domain.Table {
var table = &domain.Table{}
// New Table
table.TableType = tableType.ToString()
table.Name = fileName
table.SQLName = pin(fileName) //SQLTableName()
table.PK = PK()
table.DataFieldIndex = len(dataFields)
for i, field := range dataFields {
table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i+1))
}
table.ManualFields = make([]*domain.Field, 0)
table.CreatedAt = time.Now()
table.UpdatedAt = time.Now()
table.RowCount = rowCount
return table
}
func NewCopyTable(tableType domain.TableType, fileName string, dataFields []*domain.Field, rowCount int) *domain.Table {
var table = &domain.Table{}
// New Table
table.TableType = tableType.ToString()
table.Name = fileName
table.SQLName = pin(fileName) //SQLTableName()
table.PK = PK()
table.DataFieldIndex = len(dataFields)
table.DataFields = dataFields
table.ManualFields = make([]*domain.Field, 0)
table.CreatedAt = time.Now()
table.UpdatedAt = time.Now()
table.RowCount = rowCount
return table
}
func SQLTableName() string {
id, _ := uuid.NewUUID()
return id.String()
}
func PK() *domain.Field {
return &domain.Field{
Index: 0,
Name: "序号",
SQLName: "id",
SQLType: domain.String.ToString(),
Description: "主键",
Flag: domain.PKField,
}
}
func DataField(name string, sqlType string, flag int, index int) *domain.Field {
return &domain.Field{
Index: index,
Name: name,
SQLName: fmt.Sprintf("%v_c%d", pin(name), index), //fieldName(index),
SQLType: sqlType,
Description: "",
Flag: flag,
}
}
func pin(name string) string {
pinyin := tool_funs.ToPinYin(name, "_")
newPinyin := bytes.NewBuffer(nil)
firstCharIsLetter := false
for i := range pinyin {
b := pinyin[i]
if !firstCharIsLetter {
firstCharIsLetter = isLetters(b)
}
if !firstCharIsLetter {
continue
}
if !(isDigital(b) || isLetters(b) || b == byte('_')) {
continue
}
newPinyin.WriteByte(b)
}
return newPinyin.String()
}
func isDigital(b byte) bool {
return b >= byte('0') && b <= byte('9')
}
func isLetters(b byte) bool {
return (b >= byte('a') && b <= byte('z')) || (b >= byte('A') && b <= byte('Z'))
}