table_append_data_to_table_service.go
3.3 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
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("表不存在")
}
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
}
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &AppendDataToTableLog{
LogEntry: domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx),
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
}
}
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
}
func NewAppendDataToTableService(transactionContext *pgTransaction.TransactionContext) (*AppendDataToTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AppendDataToTableService{
transactionContext: transactionContext,
}, nil
}
}