Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
|
3b130c9d14 | |
|
8409716217 |
2
rest.go
2
rest.go
|
@ -688,7 +688,7 @@ func ModelTypes[T any](ctx context.Context, db *gorm.DB, model any, domainName,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModelTiers 查询指定模型的层级数据
|
// ModelTiers 查询指定模型的层级数据
|
||||||
func ModelTiers[T any](ctx context.Context, db *gorm.DB, model any, domainName, parentColumn, labelColumn, valueColumn string) (values []*types.TierValue[T], err error) {
|
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)
|
tx := db.WithContext(ctx)
|
||||||
result := make([]map[string]any, 0, 10)
|
result := make([]map[string]any, 0, 10)
|
||||||
if domainName == "" {
|
if domainName == "" {
|
||||||
|
|
|
@ -125,7 +125,7 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
//TierValue 层级数据
|
//TierValue 层级数据
|
||||||
TierValue[T any] struct {
|
TierValue[T comparable] struct {
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Value T `json:"value"`
|
Value T `json:"value"`
|
||||||
Parent T `json:"-"`
|
Parent T `json:"-"`
|
||||||
|
@ -249,3 +249,17 @@ 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
|
||||||
|
}
|
||||||
|
|
9
utils.go
9
utils.go
|
@ -46,22 +46,17 @@ func isEmpty(val any) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recursiveTier[T any](parent T, values []*types.TierValue[T]) []*types.TierValue[T] {
|
func recursiveTier[T comparable](parent T, values []*types.TierValue[T]) []*types.TierValue[T] {
|
||||||
items := make([]*types.TierValue[T], 0, len(values)/2)
|
items := make([]*types.TierValue[T], 0, len(values)/2)
|
||||||
for idx, row := range values {
|
for idx, row := range values {
|
||||||
if row.Used {
|
if row.Used {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if reflect.DeepEqual(row.Parent, parent) {
|
if row.Parent == parent {
|
||||||
values[idx].Used = true
|
values[idx].Used = true
|
||||||
row.Children = recursiveTier(row.Value, values)
|
row.Children = recursiveTier(row.Value, values)
|
||||||
items = append(items, row)
|
items = append(items, row)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, row := range values {
|
|
||||||
if !row.Used {
|
|
||||||
items = append(items, row)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue