query_set_service_load_calculate_set.go
12.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package domainService
import (
"fmt"
"sort"
"strings"
"github.com/zeromicro/go-zero/core/collection"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)
const DefaultExpandNum = 1000
const MaxExpandNum = 5000
func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain.QuerySet, queryComponents []*domain.QueryComponent) (*domain.DataTable, error) {
var (
res = &domain.DataTable{}
dataTables = make(map[int]*domain.DataTable)
err error
)
if len(queryComponents) == 0 {
return res, nil
}
// 加载Tables数据
q := queryComponents[0]
cells := q.Layout.LayoutCells()
if len(cells) == 0 {
return res, nil
}
dataTables, err = ptr.LoadDataTables(ctx, cells)
if err != nil {
return nil, err
}
// 设置数据
dt := &DataLayoutDataTable{
DataTable: res,
MapDataTables: dataTables,
unprocessed: cells,
}
for i := range cells {
blockData, length := dt.BlockData(cells[i])
cells[i].Length = length
cells[i].BlockData = blockData
}
// 根据数据修改偏移
CellsLocationAdjust(cells)
// 数据布局
res, err = DataLayout(res, cells)
if err != nil {
return nil, err
}
// 数据持久化
return res, nil
}
func CellsLocationAdjust(cells []*domain.LayoutCell) {
xMin := 0
xMax := 0
yMin := 0
yMax := 0
if len(cells) > 0 {
xMin = cells[0].X
xMax = cells[0].X
yMin = cells[0].Y
yMax = cells[0].Y
}
for i := 1; i <= len(cells)-1; i++ {
cell := cells[i]
xMin = min(xMin, cell.X)
xMax = max(xMax, cell.X+cell.Length)
yMin = min(yMin, cell.Y)
yMax = max(yMax, cell.Y+cell.Length)
}
for j := yMin; j <= yMax; j++ {
move := 0
var c *domain.LayoutCell
for _, cell := range cells {
if cell.Y != j {
continue
}
if cell.Direction != domain.DirectionRight {
continue
}
//move = cell.Length
//c = cell
if max(move, cell.Length) != move {
c = cell
}
move = max(move, cell.Length)
//break
}
ChangeLocation(cells, domain.DirectionRight, j, move, c)
}
for i := xMin; i <= xMax; i++ {
move := 0
var c *domain.LayoutCell
for _, cell := range cells {
if cell.X != i {
continue
}
if cell.Direction != domain.DirectionDown {
continue
}
//move = cell.Length
//c = cell
if max(move, cell.Length) != move && c == nil {
c = cell
}
move = max(move, cell.Length)
//break
}
ChangeLocation(cells, domain.DirectionDown, i, move, c)
}
}
func CellsLocationAdjustV1(cells []*domain.LayoutCell) {
yList := cellsRange(cells, "y")
xList := cellsRange(cells, "x")
for i := 0; i < len(yList); i++ {
j := yList[i]
move := 0
var c *domain.LayoutCell
for _, cell := range cells {
if cell.Y != j {
continue
}
if cell.Direction != domain.DirectionRight {
continue
}
if max(move, cell.Length) != move {
c = cell
}
move = max(move, cell.Length)
}
ChangeLocation(cells, domain.DirectionRight, j, move, c)
}
for j := 0; j < len(xList); j++ {
i := xList[j]
move := 0
var c *domain.LayoutCell
for _, cell := range cells {
if cell.X != i {
continue
}
if cell.Direction != domain.DirectionDown {
continue
}
if max(move, cell.Length) != move && c == nil {
c = cell
}
move = max(move, cell.Length)
}
ChangeLocation(cells, domain.DirectionDown, i, move, c)
}
}
func cellsRange(cells []*domain.LayoutCell, direction string) []int {
list := collection.NewSet()
for i := range cells {
if strings.ToLower(direction) == "x" {
list.Add(cells[i].X)
} else {
list.Add(cells[i].Y)
}
}
sortList := list.KeysInt()
sort.Ints(sortList)
return sortList
}
func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move int, c *domain.LayoutCell) {
// log.Logger.Info("修改定位点")
if move == 0 {
return
}
for _, cell := range cells {
switch direction {
case domain.DirectionRight:
if cell.Y > position {
cell.Y += move - 1
}
case domain.DirectionDown:
if cell.X > position && cell.Y >= c.Y {
cell.X += move - 1
}
}
// log.Logger.Info(fmt.Sprintf("%s %s X:%d Y:%d", cell.Data.Field.SQLName, cell.Direction, cell.X, cell.Y))
}
}
func FastTable(table *domain.Table) (*domain.DataTable, error) {
var err error
var options = starrocks.QueryOptions{
Table: table,
TableName: table.SQLName,
Select: table.Fields(false),
}
options.SetDefaultOrder()
options.SetOffsetLimit(1, 10000)
var dataTable *domain.DataTable
dataTable, err = FastDataTable(options)
if err != nil {
return nil, err
}
return dataTable, nil
}
func FastDataTable(options starrocks.QueryOptions) (*domain.DataTable, error) {
var err error
// 待优化分批下载,压缩
var dataTable *domain.DataTable
dataTable, err = starrocks.Query(options, starrocks.WrapQueryFuncWithDB(starrocks.DB))
if err != nil {
return nil, err
}
dataTable.Total, err = starrocks.WrapQueryCountWithDB(options, starrocks.DB)()
if err != nil {
return nil, err
}
return dataTable, nil
}
func (ptr *QuerySetService) LoadDataTables(ctx *domain.Context, cells []*domain.LayoutCell) (map[int]*domain.DataTable, error) {
//var (
// dataTables = make(map[int]*domain.DataTable)
// tableRepository, _ = repository.NewTableRepository(ptr.transactionContext)
//)
tableIds := collection.NewSet()
for _, cell := range cells {
if cell.Data == nil || cell.Data.TableField == nil || cell.Data.TableField.TableId == 0 {
continue
}
tableIds.AddInt(cell.Data.TableField.TableId)
}
//if len(tableIds.KeysInt()) > 0 {
// _, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": tableIds.KeysInt()})
// if err != nil {
// return nil, err
// }
// for _, t := range tables {
// if _, ok := dataTables[t.TableId]; ok {
// continue
// }
// dataTable, err := FastTable(t)
// if err != nil {
// log.Logger.Error(err.Error())
// return nil, fmt.Errorf("获取【%s】出现异常:%s", t.Name, err.Error())
// }
// dataTable.Fields = t.DataFields
// dataTables[t.TableId] = dataTable
// }
//}
return ptr.loadDataTables(ctx, tableIds.KeysInt())
}
func (ptr *QuerySetService) loadDataTables(ctx *domain.Context, tableIds []int) (map[int]*domain.DataTable, error) {
var (
dataTables = make(map[int]*domain.DataTable)
tableRepository, _ = repository.NewTableRepository(ptr.transactionContext)
)
if len(tableIds) > 0 {
_, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": tableIds})
if err != nil {
return nil, err
}
for _, t := range tables {
if _, ok := dataTables[t.TableId]; ok {
continue
}
dataTable, err := FastTable(t)
if err != nil {
log.Logger.Error(err.Error())
return nil, fmt.Errorf("获取【%s】出现异常:%s", t.Name, err.Error())
}
dataTable.Fields = t.DataFields
dataTables[t.TableId] = dataTable
}
}
return dataTables, nil
}
func DataLayout(res *domain.DataTable, cells []*domain.LayoutCell) (*domain.DataTable, error) {
dt := &DataLayoutDataTable{
DataTable: res,
unprocessed: cells,
}
dt.Init(DefaultExpandNum)
for {
if len(dt.unprocessed) == 0 {
break
}
cell := dt.unprocessed[0]
dt.unprocessed = dt.unprocessed[1:]
if err := dt.Expand(cell, cell.Length); err != nil {
return nil, err
}
dt.addByLocation(cell, cell.BlockData)
dt.processed = append(dt.processed, cell)
dt.LastCell = cell
}
dt.Shrink()
return dt.DataTable, nil
}
type DataLayoutDataTable struct {
PointBegin *Location
PointEnd *Location
MaxX int
MaxY int
DataTable *domain.DataTable
MapDataTables map[int]*domain.DataTable
processed []*domain.LayoutCell
unprocessed []*domain.LayoutCell
LastCell *domain.LayoutCell
}
type Location struct {
X int
Y int
}
func NewLocation(x, y int) *Location {
return &Location{
X: x,
Y: y,
}
}
func (l *Location) UpdateX(x int) {
if l.X <= x {
l.X = x
}
}
func (l *Location) UpdateY(y int) {
if l.Y <= y {
l.Y = y
}
}
func (l *Location) Update(x int, y int, compare func(int, int) int) {
if x != 0 {
l.X = compare(l.X, x)
}
if y != 0 {
l.Y = compare(l.Y, y)
}
}
func (d *DataLayoutDataTable) addByLocation(cell *domain.LayoutCell, blockData []string) {
if d.PointBegin == nil {
d.PointBegin = NewLocation(cell.X, cell.Y)
d.PointEnd = NewLocation(cell.X, cell.Y)
}
switch cell.Direction {
case domain.DirectionRight:
for i := range blockData {
d.DataTable.Data[cell.X][cell.Y+i] = blockData[i]
}
d.PointEnd.Update(cell.X, cell.Y+len(blockData)-1, max)
case domain.DirectionDown:
for i := range blockData {
d.DataTable.Data[cell.X+i][cell.Y] = blockData[i]
}
d.PointEnd.Update(cell.X+len(blockData)-1, cell.Y, max)
case domain.DirectionNone:
d.DataTable.Data[cell.X][cell.Y] = safeBlockData(blockData, 0)
d.PointEnd.Update(cell.X, cell.Y, max)
}
}
func safeBlockData(data []string, index int) string {
if len(data) < index+1 {
return ""
}
return data[index]
}
func (d *DataLayoutDataTable) changeUnProcessedLocation(lastCell *domain.LayoutCell, length int) {
for _, cell := range d.unprocessed {
switch lastCell.Direction {
case domain.DirectionRight:
if cell.X >= lastCell.X && cell.Y > lastCell.Y {
cell.Y += length - 1
}
case domain.DirectionDown:
if cell.X > lastCell.X && cell.Y >= lastCell.Y {
cell.X += length - 1
}
}
}
}
func (d *DataLayoutDataTable) BlockData(cells *domain.LayoutCell) ([]string, int) {
var block = make([]string, 0)
if cells.Type == domain.CellTypeText {
data := []string{cells.Data.Text}
return data, 1
}
table, ok := d.MapDataTables[cells.Data.TableField.TableId]
if !ok {
return block, 0
}
values := table.Values(&domain.Field{SQLName: cells.Data.TableField.FieldSqlName, SQLType: cells.Data.TableField.FieldSQLType})
if len(values) == 0 {
return block, 0
}
if cells.Data.TableField != nil && strings.HasPrefix(cells.Data.TableField.TableSqlName, strings.ToLower(domain.CalculateItem.ToString())) {
return values[:1], 1
}
if cells.Direction == domain.DirectionNone {
return values[:1], 1
}
return values, len(values)
}
func (d *DataLayoutDataTable) Shrink() {
x := d.PointEnd.X - d.PointBegin.X
y := d.PointEnd.Y - d.PointBegin.Y
data := make([][]string, x+1)
for i := range data {
data[i] = make([]string, y+1)
}
iData := 0
for i := d.PointBegin.X; i <= d.PointEnd.X; i++ {
jData := 0
for j := d.PointBegin.Y; j <= d.PointEnd.Y; j++ {
data[iData][jData] = d.DataTable.Data[i][j]
jData++
}
iData++
}
d.DataTable.Data = data
for i := 0; i <= y; i++ {
d.DataTable.Fields = append(d.DataTable.Fields, &domain.Field{
Name: fmt.Sprintf("列%d", i),
SQLName: fmt.Sprintf("col%d", i),
SQLType: domain.String.ToString(),
})
}
}
func (d *DataLayoutDataTable) Expand(cell *domain.LayoutCell, length int) error {
if !d.CellOutRange(cell, length) {
return nil
}
expandLength := DefaultExpandNum
if expandLength < length {
expandLength = length
}
if d.MaxX+expandLength > MaxExpandNum+DefaultExpandNum {
return fmt.Errorf("布局元素超过限制 %d", MaxExpandNum)
}
copyData := make([][]string, d.MaxX+length)
for i := range copyData {
copyData[i] = make([]string, d.MaxY+length)
}
copy(copyData, d.DataTable.Data)
d.DataTable.Data = copyData
return nil
}
func (d *DataLayoutDataTable) Init(length int) {
initData := make([][]string, length)
for i := range initData {
initData[i] = make([]string, length)
}
d.DataTable.Data = initData
d.AddRange(length)
}
func (d *DataLayoutDataTable) AddRange(length int) {
d.MaxY += length
d.MaxX += length
}
func (d *DataLayoutDataTable) CellOutRange(cell *domain.LayoutCell, length int) bool {
if cell.X+length > d.MaxX {
return true
}
if cell.Y+length > d.MaxY {
return true
}
return false
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
func max(a, b int) int {
if a < b {
return b
}
return a
}