table_append_data_to_table_service.go
4.8 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
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"
"time"
)
type AppendDataToTableService struct {
transactionContext *pgTransaction.TransactionContext
}
// AppendData 追加数据 【data-table】
func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) {
fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
if err != nil {
return nil, fmt.Errorf("文件不存在")
}
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
if err != nil {
return nil, fmt.Errorf("表不存在")
}
defer func() {
AsyncEvent(domain.NewEventTable(ctx, domain.TableDataImportEvent).WithTable(table))
}()
excelTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": file.FileInfo.TableId})
if err != nil {
return nil, fmt.Errorf("文件未校验")
}
if !(table.TableType == domain.MainTable.ToString() || table.TableType == domain.SideTable.ToString()) {
return nil, fmt.Errorf("只能追加数据到主表或者副表")
}
var subTables []*domain.Table
_, subTables, err = tableRepository.Find(map[string]interface{}{"parentId": tableId, "tableTypes": []string{domain.SubTable.ToString()}})
if err != nil {
return nil, err
}
// 更新表数据
table.RowCount += excelTable.RowCount
table.UpdatedAt = time.Now()
if _, err = tableRepository.Save(table); err != nil {
return nil, err
}
// 日志
entry := domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx)
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &AppendDataToTableLog{
LogEntry: (&entry).WithAppendFileId(fileId),
File: file,
Table: table,
SubTables: subTables,
RowCount: excelTable.RowCount,
}); err != nil {
return nil, err
}
// 通知底层进行追加数据
requestData := domain.ReqAppendData{Table: table, FileId: fileId, FileUrl: file.FileInfo.Url, ExcelTable: excelTable}
if len(mappingFields) > 0 {
for _, m := range mappingFields {
var toField, fromField *domain.Field
var ok bool
toField, ok = table.MatchField(m.MainTableField)
if !ok {
continue
}
if len(m.VerifiedFileFieldName) == 0 {
fromField = &domain.Field{}
// continue
} else {
fromField, ok = excelTable.MatchField(&domain.Field{Name: m.VerifiedFileFieldName})
if !ok {
continue
}
}
if fromField.SQLType != "" && !toField.SqlTypeEqual(fromField) {
//return nil, fmt.Errorf("字段【%s】的类型与导入数据表的类型不匹配", toField.Name)
return map[string]interface{}{
"result": fmt.Sprintf("字段【%s】的类型与导入数据表的类型不匹配", toField.Name),
}, nil
}
fromField.SQLType = toField.SQLType // 兼容 INT BIGINT
requestData.To = append(requestData.To, toField)
requestData.From = append(requestData.From, fromField)
}
}
if _, err = ByteCore.AppendData(requestData); err != nil {
return nil, err
}
return map[string]interface{}{
"result": fmt.Sprintf("源数据%v条,成功追加%v条;", excelTable.RowCount, excelTable.RowCount),
}, nil
}
// PreflightCheck 预检
func (ptr *AppendDataToTableService) PreflightCheck(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
if err != nil {
return nil, fmt.Errorf("表不存在")
}
inSourceId := []int{table.TableId}
if table.ParentId != 0 {
inSourceId = append(inSourceId, table.ParentId)
}
logRepository, _ := repository.NewLogRepository(ptr.transactionContext)
_, logs, err := logRepository.Find(map[string]interface{}{
"inSourceId": inSourceId,
"inOperationType": []string{domain.GenerateMainTable.ToString(), domain.AppendData.ToString()},
"limit": 500,
})
if err != nil {
return nil, err
}
for _, log := range logs {
if log.Entry.AppendFileId == fileId {
return map[string]interface{}{
"fileAppended": true,
}, nil
}
}
return map[string]interface{}{
"fileAppended": false,
}, nil
}
func NewAppendDataToTableService(transactionContext *pgTransaction.TransactionContext) (*AppendDataToTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AppendDataToTableService{
transactionContext: transactionContext,
}, nil
}
}