|
|
package domain
|
|
|
|
|
|
import "strings"
|
|
|
import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
type DataTable struct {
|
|
|
Fields []*Field `json:"fields"`
|
...
|
...
|
@@ -113,7 +115,81 @@ 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
|
|
|
}
|
|
|
} |
...
|
...
|
|