data_table.go
4.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
package domain
import (
"strings"
)
type DataTable struct {
Fields []*Field `json:"fields"`
Data [][]string `json:"data"`
Total int64 `json:"total"`
}
type Where struct {
PageNumber int `json:"pageNumber"`
PageSize int `json:"pageSize"`
HeaderRow int `json:"headerRow"` // 行号 默认:0
Conditions []Condition `json:"conditions"`
}
func (w Where) Offset() int {
if w.PageNumber == 0 && w.PageSize == 0 {
return 0
}
return (w.PageNumber - 1) * w.PageSize
}
func (w Where) ValidSql() (err error) {
for _, c := range w.Conditions {
if err = SqlDetections(c.Like); err != nil {
return err
}
//if err = SqlDetections(c.In...); err != nil {
// return err
//}
//if err = SqlDetections(c.Ex...); err != nil {
// return err
//}
if err = SqlDetections(c.Order); err != nil {
return err
}
for _, r := range c.Range {
if err = SqlDetections(r.Val); err != nil {
return err
}
}
}
return
}
type Condition struct {
Field *Field `json:"field"`
Like string `json:"like"`
In []interface{} `json:"in"`
Ex []interface{} `json:"ex"`
Range []RangStruct `json:"range"`
Order string `json:"order"`
}
func (t *DataTable) OptionalValue(args ...string) []string {
var values = make([]string, 0)
match := ""
filedType := ""
if len(args) > 0 {
match = args[0]
}
if len(args) > 1 {
filedType = args[1]
}
if len(t.Data) > 0 && len(t.Data[0]) == 1 {
for i := range t.Data {
if len(t.Data[i]) == 0 {
continue
}
if len(match) > 0 && !strings.Contains(t.Data[i][0], match) {
continue
}
if filedType == Float.ToString() || filedType == Date.ToString() {
values = append(values, RoundFieldValue(&Field{SQLType: filedType}, t.Data[i][0]))
continue
}
values = append(values, t.Data[i][0])
}
}
return values
}
func (t *DataTable) Values(f *Field) []string {
var res = make([]string, 0)
index := -1
for i := range t.Fields {
if t.Fields[i].SQLName == f.SQLName {
index = i
break
}
if t.Fields[i].Name == f.SQLName {
index = i
break
}
}
if index < 0 {
return res
}
for i := range t.Data {
for j := range t.Data[i] {
if j == index {
res = append(res, RoundFieldValue(f, t.Data[i][j]))
break
}
}
}
return res
}
func (t *DataTable) MatchFields(from []*Field) []*Field {
return from
}
func (t *DataTable) FilterByWhere(where Where) (response [][]string, length int64) {
pageNumber, pageSize := where.PageNumber, where.PageSize
response = make([][]string, 0)
wrapperFilters := func(data []string, next ...func([]string) bool) ([]string, bool) {
ok := true
for i := range next {
if !next[i](data) {
ok = false
}
if !ok {
break
}
}
if !ok {
return nil, false
}
return data, true
}
filterFuncList := make([]func([]string) bool, 0)
for _, c := range where.Conditions {
var index = 0
var filed *Field
for i, f := range t.Fields {
if f.Name == c.Field.Name {
index = i
filed = f
break
}
}
if filed == nil {
continue
}
if len(c.In) > 0 {
filterFuncList = append(filterFuncList, WithInFilter(c.In, index))
}
}
for _, data := range t.Data {
if v, ok := wrapperFilters(data, filterFuncList...); ok {
response = append(response, v)
}
}
length = int64(len(response))
response = pageData(pageNumber, pageSize, response)
return
}
type RangStruct struct {
Op string `json:"op"`
Val interface{} `json:"val"`
}
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
}
return data[offset : offset+pageSize]
}
func WithInFilter(in []interface{}, index int) func(data []string) bool {
return func(data []string) bool {
if len(data) < index {
return false
}
for _, item := range in {
if data[index] == item.(string) {
return true
}
}
return false
}
}