Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
|
3b130c9d14 | |
|
8409716217 | |
|
368a9e868c | |
|
206932038e | |
|
5c09f1eefc |
|
@ -84,9 +84,14 @@ func BuildConditions(ctx context.Context, r *http.Request, query *Query, schemas
|
|||
}
|
||||
//如果是多选的话,直接使用IN操作
|
||||
columnName := row.Column + "[]"
|
||||
if qs.Has(columnName) && len(qs[columnName]) > 1 {
|
||||
if qs.Has(columnName) {
|
||||
if len(qs[columnName]) > 1 {
|
||||
query.AndFilterWhere(newConditionWithOperator("IN", row.Column, qs[columnName]))
|
||||
continue
|
||||
} else if len(qs[columnName]) == 1 {
|
||||
query.AndFilterWhere(newCondition(row.Column, qs[columnName][0]))
|
||||
continue
|
||||
}
|
||||
}
|
||||
formValue = qs.Get(row.Column)
|
||||
switch row.Format {
|
||||
|
|
43
rest.go
43
rest.go
|
@ -687,6 +687,49 @@ func ModelTypes[T any](ctx context.Context, db *gorm.DB, model any, domainName,
|
|||
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 获取模型某个字段的值
|
||||
func GetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string) any {
|
||||
var (
|
||||
|
|
|
@ -124,6 +124,15 @@ type (
|
|||
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[T any] struct {
|
||||
Label string `json:"label"`
|
||||
|
@ -240,3 +249,17 @@ type (
|
|||
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,6 +4,8 @@ import (
|
|||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.nobla.cn/golang/rest/types"
|
||||
)
|
||||
|
||||
func hasToken(hack string, need string) bool {
|
||||
|
@ -43,3 +45,18 @@ func isEmpty(val any) bool {
|
|||
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