table_preview.go
4.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/cache"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)
func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command.TablePreviewCommand) (interface{}, error) {
if cmd.TableId == 0 {
return &dto.TablePreviewDto{}, nil
}
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var dataTable *domain.DataTable
var table *domain.Table
var cacheMiss bool
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
if err != nil {
return nil, factory.FastError(err)
}
// 字段只传name时,补齐sqlName
for i, c := range cmd.Where.Conditions {
if c.Field != nil && c.Field.SQLName == "" {
if v, ok := table.MatchField(c.Field); ok {
cmd.Where.Conditions[i].Field.SQLName = v.SQLName
cmd.Where.Conditions[i].Field.SQLType = v.SQLType
}
}
}
// 方案 计算项 计算集 做缓存
if cmd.UseCache && table.AssertTableType(domain.SchemaTable, domain.CalculateItem, domain.CalculateSet) {
if d, ok := cache.GetDataTable(table.TableId); ok {
dataTable = d
} else {
cacheMiss = true
}
}
response := &dto.TablePreviewDto{HiddenData: cmd.HiddenData, TableType: table.TableType, Name: table.Name, TableId: table.TableId}
if dataTable == nil {
switch table.TableType {
case domain.CalculateSet.ToString():
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
querySet, err := querySetRepository.FindOne(map[string]interface{}{"BindTableId": table.TableId})
if err != nil {
return nil, factory.FastError(err)
}
svr, _ := factory.FastQuerySetServices(transactionContext)
dataTable, err = svr.LoadCalculateSetData(ctx, querySet, querySet.QueryComponents)
if err != nil {
return nil, factory.FastError(err)
}
data, total := dataTable.FilterByWhere(cmd.Where)
response.Fields = dataTable.Fields
response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, data, false), total)
default:
var options = starrocks.QueryOptions{
Table: table,
TableName: table.SQLName,
Select: table.Fields(true),
}
options.SetCondition(cmd.Where.Conditions).SetDefaultOrder()
options.SetOffsetLimit(cmd.Where.PageNumber, cmd.Where.PageSize)
dataTable, err = factory.FastDataTable(options)
if err != nil {
return nil, factory.FastError(err)
}
// 计算项只返回第一个值
// if table.TableType==domain.CalculateItem.ToString() && dataTable!=nil && len(dataTable.Data)>0{
// dataTable.Data = dataTable.Data[:1]
// }
response.Load(table, dataTable, domain.ObjectMetaTable)
}
} else {
switch table.TableType {
case domain.CalculateSet.ToString():
data, total := dataTable.FilterByWhere(cmd.Where)
response.Fields = dataTable.Fields
response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, data, false), total)
default:
response.Load(table, dataTable, domain.ObjectMetaTable)
}
}
if cacheMiss && dataTable != nil {
// 存储缓存
cache.SetDataTable(table.TableId, dataTable)
}
return response, nil
}
func pageData(pageNumber, pageSize int, data [][]string) [][]string {
if pageNumber == 0 || pageSize == 0 {
return data
}
offset := (pageNumber - 1) * pageSize
if len(data) < offset {
return [][]string{}
}
if len(data) < offset+pageSize {
pageSize = len(data) - offset
}
log.Logger.Info(fmt.Sprintf("%v %v %v", len(data), offset, pageSize))
return data[offset : offset+pageSize]
}