file_table_flush_data_table_service.go
6.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
package domainService
import (
"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)
// 通知底层保存、进行回调
response, _ := 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
temporaryFileTableId := table.TableId
// 校验文件对应的表更新
table.TableId = file.FileInfo.TableId
// 追加日志到校验文件
if err = dao.ChangeStepLogOwner(ptr.transactionContext, file.FileId, sourceFile.FileId); err != nil {
return err
}
sourceFile.UpdateFileUrl(url)
if sourceFile, err = fileRepository.Save(sourceFile); err != nil {
return err
}
// 删除中间文件
if err = dao.FileDelete(ptr.transactionContext, file.FileId, domain.TemporaryFile); err != nil {
return err
}
// 删除中间表
if err = dao.TableDelete(ptr.transactionContext, temporaryFileTableId, domain.ExcelTable); err != nil {
return err
}
if _, err = fileRepository.Save(sourceFile); err != nil {
return err
}
// 更新
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err = tableRepository.Save(table)
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 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 {
return tool_funs.ToPinYin(name, "_")
}