byte_core_service.go
7.5 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
package domainService
import (
"bytes"
"github.com/beego/beego/v2/client/httplib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"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/excel"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
)
type ByteCoreService struct {
TempDataTable map[int]*domain.DataLoadDataTable
}
// ByteCore 字库底层核心
// 主表、拆分、数据读取等功能
var ByteCore domain.ByteLibService = &WrapByteCoreService{} //&ByteCoreService{}
var _ domain.ByteLibService = (*ByteCoreService)(nil)
func (ptr *ByteCoreService) LoadDataTable(param domain.ReqLoadDataTable) (*domain.DataLoadDataTable, error) {
if v, ok := ptr.load(param.FileId); ok {
return v.Filter(param.Where), nil
}
file, err := httplib.Get(param.Url).Bytes()
if err != nil {
return nil, err
}
reader := bytes.NewReader(file)
var importer *excel.Importer
if param.Ext == domain.XLSX {
importer = excel.NewExcelImport(&excel.XLXSReader{})
} else if param.Ext == domain.XLS {
importer = excel.NewExcelImport(&excel.XLSReader{})
}
data, err := importer.OpenExcelFromIoReader(reader)
if err != nil {
return nil, err
}
cols := importer.Reader().Header().Columns
var response = &domain.DataLoadDataTable{
FileId: param.FileId,
Fields: columnToField(cols),
Data: data,
Total: len(data),
PageNumber: param.PageNumber,
InValidCells: make([]domain.InValidCell, 0),
}
ptr.save(param.FileId, response)
return response.Filter(param.Where), nil
}
func (ptr *ByteCoreService) EditTable(param domain.ReqEditDataTable) (*domain.DataEditDataTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.EditTable(param)
}
func (ptr *ByteCoreService) save(fileId int, dataTable *domain.DataLoadDataTable) {
// Once
if ptr.TempDataTable == nil {
ptr.TempDataTable = make(map[int]*domain.DataLoadDataTable)
}
ptr.TempDataTable[fileId] = dataTable
}
func (ptr *ByteCoreService) load(fileId int) (*domain.DataLoadDataTable, bool) {
v, ok := ptr.TempDataTable[fileId]
return v, ok
}
func columnToField(cols []string) []*domain.Field {
var fields []*domain.Field
for i, col := range cols {
fields = append(fields, &domain.Field{
Name: col,
Index: i + 1,
SQLType: domain.String.ToString(),
})
}
return fields
}
func (ptr *ByteCoreService) SaveTable(param domain.ReqSaveTable) (*domain.DataSaveTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.SaveTable(param)
}
func (ptr *ByteCoreService) GenerateTable(ctx *domain.Context, param domain.ReqGenerateTable) (*domain.DataGenerateTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.GenerateTable(param)
}
func (ptr *ByteCoreService) CopyTable(param domain.ReqCopyTable) (*domain.DataCopyTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.CopyTable(param)
}
func (ptr *ByteCoreService) AppendData(param domain.ReqAppendData) (*domain.DataAppendData, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.AppendData(param)
}
func (ptr *ByteCoreService) DeleteTable(param domain.ReqDeleteTable) (*domain.DataDeleteTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.DeleteTable(param)
}
func (ptr *ByteCoreService) CancelFile(param domain.ReqCancelFile) (*domain.DataCancelFile, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.CancelFile(param)
}
func (ptr *ByteCoreService) EditTableData(param domain.ReqEditTableData) (*domain.DataEditTableData, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.EditTableData(param)
}
func (ptr *ByteCoreService) SplitTable(param domain.ReqSplitTable) (*domain.DataSplitTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.SplitTable(param)
}
func (ptr *ByteCoreService) FieldOptionalValues(param domain.ReqFieldOptionalValues) (*domain.DataFieldOptionalValues, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.FieldOptionalValues(param)
}
func (ptr *ByteCoreService) FormulasGenerate(param domain.ReqFormulasGenerate) (*domain.DataFormulasGenerate, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
//if param.QuerySet.Type == domain.CalculateItem.ToString() {
// if err := starrocks.Exec(starrocks.DB,
// starrocks.CreateViewSql(param.Table.SQLName, param.Table.DataFields,
// starrocks.CalculateItemViewSql(¶m.QueryComponents[0].Formula.FieldExpr),
// )); err != nil {
// return nil, err
// }
// return &domain.DataFormulasGenerate{}, nil
//}
//if param.QuerySet.Type == domain.CalculateTable.ToString() {
// if err := starrocks.Exec(starrocks.DB,
// starrocks.CreateViewSql(param.Table.SQLName, param.Table.DataFields,
// starrocks.CalculateTableViewSql(param.QueryComponents[0].MasterTable.SQLName, ¶m.QueryComponents[0].Aggregation.Aggregation),
// )); err != nil {
// return nil, err
// }
// return &domain.DataFormulasGenerate{}, nil
//}
//if param.QuerySet.Type == domain.CalculateItem.ToString() {
// if param.QuerySet.QueryComponents[0].Formula.ExprMode != 0 {
// err := ptr.ExcelExprCalcPersistence(param.QuerySet.QueryComponents[0].Formula, param, true)
// return &domain.DataFormulasGenerate{}, err
// }
//}
if param.QuerySet.Type == domain.CalculateSet.ToString() {
_, err := param.QuerySetService.(*QuerySetService).LoadCalculateSetData(param.Context.(*domain.Context), param.QuerySet, param.QueryComponents)
if err != nil {
return nil, err
}
return &domain.DataFormulasGenerate{}, nil
}
return apiByteLib.FormulasGenerate(param)
}
func (ptr *ByteCoreService) FormulasClear(param domain.ReqFormulasClear) (*domain.DataFormulasClear, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.FormulasClear(param)
}
func (ptr *ByteCoreService) ExcelExprCalcPersistence(expr *domain.FieldFormulaExpr, param domain.ReqFormulasGenerate, persistence bool) error {
if len(param.QueryComponents) == 0 {
return nil
}
// 加载Tables数据
q := param.QueryComponents[0]
result, err := param.QuerySetService.(*QuerySetService).LoadCalculateItemData(param.Context.(*domain.Context), param.Table, q.Formula)
if err != nil {
return err
}
if persistence {
starrocks.DropView(starrocks.DB, param.Table.SQLName)
starrocks.DropTable(starrocks.DB, param.Table.SQLName)
return starrocks.Exec(starrocks.DB, starrocks.CreateTableSql(param.Table.SQLName, param.Table.Fields(false), result.Data[0]))
}
return nil
}
//////////////
// 字库核心
//////////////
func CreateByteCoreService() (domain.ByteLibService, error) {
return ByteCore, nil
}
type WrapByteCoreService struct {
ByteCoreService
}
func (ptr *WrapByteCoreService) LoadDataTable(param domain.ReqLoadDataTable) (*domain.DataLoadDataTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.LoadDataTable(param)
}
//func (ptr *WrapByteCoreService) GenerateTable(ctx *domain.Context, param bytecore.ReqGenerateTable) (*bytecore.DataGenerateTable, error) {
// return nil, nil
//}