search_table_data_logic.go
4.9 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
package table
import (
"context"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
"sort"
"strconv"
"strings"
)
type SearchTableDataLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSearchTableDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchTableDataLogic {
return &SearchTableDataLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SearchTableDataLogic) SearchTableData(req *types.SearchTableDataRequest) (resp interface{}, err error) {
if req.ObjectId == 0 {
resp = map[string]interface{}{
"objectId": 0,
"fields": nil,
"total": 0,
"data": make([]string, 0),
"local": false,
}
return
}
resp, err = l.getLocal(req)
if err == nil {
return resp, nil
}
return l.getRemote(req)
}
// getLocal 获取本地数据
func (l *SearchTableDataLogic) getLocal(req *types.SearchTableDataRequest) (interface{}, error) {
conn := l.svcCtx.DefaultDBConn()
conditions := make([]*domain.TableDataCondition, 0)
_ = copier.Copy(&conditions, req.Condition)
//查询表数据
objectTable, err := l.svcCtx.ObjectTableRepository.FindOneByTableId(l.ctx, conn, req.ObjectId)
if err != nil || objectTable.Id <= 0 {
return nil, xerr.NewErrMsg("表不存在")
}
if !objectTable.IsLocal {
return nil, xerr.NewErrMsg("未保存到本地存储")
}
//查询表字段
objectField, err := l.svcCtx.ObjectFieldRepository.FindOne(l.ctx, conn, int64(req.ObjectId))
if err != nil || objectField.Id <= 0 {
return nil, xerr.NewErrMsg("表字段不存在")
}
//替换中文字段
for _, item := range conditions {
replace := false
for _, field := range objectField.Fields {
if field.Name == item.FieldName {
item.FieldName = field.SQLName
replace = true
}
}
if !replace {
return nil, xerr.NewErrMsg("字段" + item.FieldName + "不存在")
}
}
//表数据
total, list, err := l.svcCtx.ObjectTableDataRepository.Find(l.ctx, conn, req.ObjectId, &domain.ObjectTableDataQuery{
Page: req.PageNumber,
Size: req.PageSize,
Conditions: conditions,
})
if err != nil {
return nil, xerr.NewErrMsg("查询表数据失败")
}
return map[string]interface{}{
"objectId": req.ObjectId,
"fields": removeIdField(objectField.Fields),
"total": total,
"data": list,
"local": true,
}, nil
}
// getRemote 获取远程字库数据
func (l *SearchTableDataLogic) getRemote(req *types.SearchTableDataRequest) (resp interface{}, err error) {
tableDataPreviewRequest := &bytelib.TableDataPreviewRequest{
Token: req.Token,
ObjectType: bytelib.ObjectMetaTable,
ObjectId: int64(req.ObjectId),
Where: newWhere(req.Condition).WithPageSize(req.PageNumber, req.PageSize),
}
var (
response bytelib.TablePreviewResponse
)
response, err = l.svcCtx.ByteMetadataService.TableDataPreview(l.ctx, tableDataPreviewRequest)
if err != nil {
return resp, xerr.NewErr(err)
}
//排序
orderField := ""
orderBy := ""
fieldType := "string"
for _, item := range req.Condition {
if item.Order != "" {
orderField = item.FieldName
orderBy = item.Order
break
}
}
for _, item := range response.Fields {
if orderField == item.SQLName {
fieldType = item.SQLType
}
}
fieldType = strings.ToLower(fieldType)
if orderField != "" && orderBy != "" {
sort.Slice(response.Grid.List, func(i, j int) bool {
if _, ok := response.Grid.List[i][orderField]; ok {
if _, ok := response.Grid.List[j][orderField]; ok {
if fieldType == "bigint" || fieldType == "int" || fieldType == "float" {
idata, _ := strconv.ParseFloat(response.Grid.List[i][orderField], 64)
jdata, _ := strconv.ParseFloat(response.Grid.List[j][orderField], 64)
if strings.ToLower(orderBy) == "asc" {
return idata < jdata
} else {
return idata > jdata
}
} else {
if strings.ToLower(orderBy) == "asc" {
return response.Grid.List[i][orderField] < response.Grid.List[j][orderField]
} else {
return response.Grid.List[i][orderField] > response.Grid.List[j][orderField]
}
}
}
}
return true
})
}
resp = map[string]interface{}{
"objectId": response.ObjectId,
"fields": removeIdField(response.Fields),
"total": response.Grid.Total,
"data": response.Grid.List,
"local": false,
}
return
}
func removeIdField(fields []*bytelib.Field) []*bytelib.Field {
var result = make([]*bytelib.Field, 0)
for _, f := range fields {
if f.SQLName == "id" {
continue
}
result = append(result, f)
}
return result
}