file_table_preview_data_table_service.go
5.1 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
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/api/bytelib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
)
type PreviewDataTableService struct {
FileId int
transactionContext *pgTransaction.TransactionContext
}
// Preview 预览 【data-table】
func (ptr *PreviewDataTableService) Preview(ctx *domain.Context, fileId int, fields []*domain.Field, where domain.Where) (interface{}, error) {
fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
if err != nil {
return nil, fmt.Errorf("校验文件不存在")
}
isSourceFile := true
fileUrl := file.FileInfo.Url
if file.FileType != domain.TemporaryFile.ToString() {
return nil, fmt.Errorf("校验文件不存在")
}
ptr.FileId = file.FileId
fileCache := redis.NewFileCacheService()
if tempFile, _ := fileCache.Get(redis.KeyTemporaryFileInfo(fileId)); tempFile != nil {
isSourceFile = false
fields = tempFile.Fields
} else if file.FileInfo.TableId > 0 {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, _ := tableRepository.FindOne(map[string]interface{}{"tableId": file.FileInfo.TableId})
if table != nil {
fields = table.Fields(false)
}
}
// Load Data From Excel(python api)
byteCore, _ := CreateByteCoreService()
response, err := byteCore.LoadDataTable(domain.ReqLoadDataTable{
FileId: file.FileId,
FileName: file.FileInfo.Name,
Url: file.FileInfo.Url,
Ext: file.FileInfo.Ext,
Where: where,
//FileType: file.FileType,
OriginalTableId: fmt.Sprintf("%v", file.FileId),
IsFromOriginalTable: isSourceFile,
TableFileUrl: fileUrl,
ColumnSchemas: bytelib.DomainFieldsToColumnSchemas(fields),
//QueryParameters: make([]domain.QueryParameter, 0),
SortParameters: make(map[string]interface{}),
})
if err != nil {
return nil, err
}
if len(fields) == 0 {
fields = response.Fields
}
cache := redis.NewFileCacheService()
tempFile, err := cache.Update(redis.KeyTemporaryFileInfo(file.FileId), file, fields, response.Total)
if err != nil {
return nil, err
}
var responseDto = &FilePreviewDto{}
responseDto.Load(file.FileId, response, tempFile)
return responseDto, nil
}
func (ptr *PreviewDataTableService) CreateTemporaryFile(ctx *domain.Context, fileId int) (*domain.File, error) {
fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
if err != nil {
return nil, fmt.Errorf("文件不存在")
}
if !(file.FileType == domain.SourceFile.ToString() || file.FileType == domain.VerifiedFile.ToString()) {
return nil, fmt.Errorf("源文件/校验文件才可以创建临时文件")
}
file = file.CopyTo(domain.TemporaryFile, ctx)
if file, err = fileRepository.Save(file); err != nil {
return nil, err
}
return file, nil
}
type FilePreviewDto struct {
ObjectId int `json:"objectId"`
ObjectType string `json:"objectType"`
TableType string `json:"tableType"`
Fields []*domain.Field `json:"fields"`
Data interface{} `json:"grid"`
PageNumber int `json:"pageNumber"`
InValidCells []domain.InValidCell `json:"inValidCells"`
}
func (d *FilePreviewDto) Load(fileId int, m *domain.DataLoadDataTable, file *redis.TemporaryFileInfo) {
d.ObjectId = fileId
d.ObjectType = domain.ObjectFile
d.TableType = domain.ExcelTable.ToString()
for i, f := range file.Fields {
d.Fields = append(d.Fields, &domain.Field{
Index: i + 1,
Name: f.Name,
SQLName: f.Name,
SQLType: f.SQLType,
})
}
var fields []*domain.Field
mapData := domain.ToFieldData(m.Fields, formatData(m.Data, func(s string) string {
if s == "<NA>" {
return ""
}
return s
}), true)
d.Data = domain.GripData(mapData, int64(m.Total))
d.PageNumber = m.PageNumber
for _, f := range file.Fields {
copyField := f.Copy()
for _, e := range file.ConvertTypeErrors {
if e.FieldName == copyField.Name {
copyField.SQLType = e.ToType
}
}
fields = append(fields, copyField)
}
d.InValidCells = domain.NewInValidCells(fields, mapData)
}
func (ptr *PreviewDataTableService) GetFileId() int {
return ptr.FileId
}
func formatData(data [][]string, f func(s string) string) [][]string {
for i := 0; i < len(data); i++ {
for j := 0; j < len(data[i]); j++ {
data[i][j] = f(data[i][j])
}
}
return data
}
func NewLoadDataTableService(transactionContext *pgTransaction.TransactionContext) (*PreviewDataTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PreviewDataTableService{
transactionContext: transactionContext,
}, nil
}
}