Compare commits
No commits in common. "master" and "v0.1.2" have entirely different histories.
|
@ -84,14 +84,9 @@ func BuildConditions(ctx context.Context, r *http.Request, query *Query, schemas
|
||||||
}
|
}
|
||||||
//如果是多选的话,直接使用IN操作
|
//如果是多选的话,直接使用IN操作
|
||||||
columnName := row.Column + "[]"
|
columnName := row.Column + "[]"
|
||||||
if qs.Has(columnName) {
|
if qs.Has(columnName) && len(qs[columnName]) > 1 {
|
||||||
if len(qs[columnName]) > 1 {
|
|
||||||
query.AndFilterWhere(newConditionWithOperator("IN", row.Column, qs[columnName]))
|
query.AndFilterWhere(newConditionWithOperator("IN", row.Column, qs[columnName]))
|
||||||
continue
|
continue
|
||||||
} else if len(qs[columnName]) == 1 {
|
|
||||||
query.AndFilterWhere(newCondition(row.Column, qs[columnName][0]))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
formValue = qs.Get(row.Column)
|
formValue = qs.Get(row.Column)
|
||||||
switch row.Format {
|
switch row.Format {
|
||||||
|
|
43
rest.go
43
rest.go
|
@ -687,49 +687,6 @@ func ModelTypes[T any](ctx context.Context, db *gorm.DB, model any, domainName,
|
||||||
return values, nil
|
return values, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModelTiers 查询指定模型的层级数据
|
|
||||||
func ModelTiers[T comparable](ctx context.Context, db *gorm.DB, model any, domainName, parentColumn, labelColumn, valueColumn string) (values []*types.TierValue[T], err error) {
|
|
||||||
tx := db.WithContext(ctx)
|
|
||||||
result := make([]map[string]any, 0, 10)
|
|
||||||
if domainName == "" {
|
|
||||||
err = tx.Model(model).Select(parentColumn, labelColumn, valueColumn).Scan(&result).Error
|
|
||||||
} else {
|
|
||||||
err = tx.Model(model).Select(parentColumn, labelColumn, valueColumn).Where("domain=?", domainName).Scan(&result).Error
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
values = make([]*types.TierValue[T], 0, len(result))
|
|
||||||
for _, pairs := range result {
|
|
||||||
feed := &types.TierValue[T]{}
|
|
||||||
for k, v := range pairs {
|
|
||||||
if k == labelColumn {
|
|
||||||
if s, ok := v.(string); ok {
|
|
||||||
feed.Label = s
|
|
||||||
} else {
|
|
||||||
feed.Label = fmt.Sprint(s)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if k == valueColumn {
|
|
||||||
if p, ok := v.(T); ok {
|
|
||||||
feed.Value = p
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if k == parentColumn {
|
|
||||||
if p, ok := v.(T); ok {
|
|
||||||
feed.Parent = p
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
values = append(values, feed)
|
|
||||||
}
|
|
||||||
var none T
|
|
||||||
return recursiveTier(none, values), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFieldValue 获取模型某个字段的值
|
// GetFieldValue 获取模型某个字段的值
|
||||||
func GetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string) any {
|
func GetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string) any {
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -124,15 +124,6 @@ type (
|
||||||
Value T `json:"value"`
|
Value T `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//TierValue 层级数据
|
|
||||||
TierValue[T comparable] struct {
|
|
||||||
Label string `json:"label"`
|
|
||||||
Value T `json:"value"`
|
|
||||||
Parent T `json:"-"`
|
|
||||||
Used bool `json:"-"`
|
|
||||||
Children []*TierValue[T] `json:"children"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NestedValue 层级数据
|
// NestedValue 层级数据
|
||||||
NestedValue[T any] struct {
|
NestedValue[T any] struct {
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
|
@ -249,17 +240,3 @@ type (
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *TierValue[T]) HasChild(value T) bool {
|
|
||||||
for _, child := range t.Children {
|
|
||||||
if child.Value == value {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if len(child.Children) > 0 {
|
|
||||||
if child.HasChild(value) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
17
utils.go
17
utils.go
|
@ -4,8 +4,6 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.nobla.cn/golang/rest/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func hasToken(hack string, need string) bool {
|
func hasToken(hack string, need string) bool {
|
||||||
|
@ -45,18 +43,3 @@ func isEmpty(val any) bool {
|
||||||
return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface())
|
return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recursiveTier[T comparable](parent T, values []*types.TierValue[T]) []*types.TierValue[T] {
|
|
||||||
items := make([]*types.TierValue[T], 0, len(values)/2)
|
|
||||||
for idx, row := range values {
|
|
||||||
if row.Used {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if row.Parent == parent {
|
|
||||||
values[idx].Used = true
|
|
||||||
row.Children = recursiveTier(row.Value, values)
|
|
||||||
items = append(items, row)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return items
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue