From 8409716217c666650274ec2c2c665bd8d5a6bb23 Mon Sep 17 00:00:00 2001 From: Yavolte Date: Thu, 26 Jun 2025 14:07:30 +0800 Subject: [PATCH] add types --- rest.go | 2 +- types/types.go | 16 +++++++++++++++- utils.go | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/rest.go b/rest.go index 9ad6022..cab21ac 100644 --- a/rest.go +++ b/rest.go @@ -688,7 +688,7 @@ func ModelTypes[T any](ctx context.Context, db *gorm.DB, model any, domainName, } // 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) result := make([]map[string]any, 0, 10) if domainName == "" { diff --git a/types/types.go b/types/types.go index 5dda7eb..605cfdc 100644 --- a/types/types.go +++ b/types/types.go @@ -125,7 +125,7 @@ type ( } //TierValue 层级数据 - TierValue[T any] struct { + TierValue[T comparable] struct { Label string `json:"label"` Value T `json:"value"` Parent T `json:"-"` @@ -249,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 +} diff --git a/utils.go b/utils.go index c58ea33..eba4849 100644 --- a/utils.go +++ b/utils.go @@ -46,13 +46,13 @@ 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) for idx, row := range values { if row.Used { continue } - if reflect.DeepEqual(row.Parent, parent) { + if row.Parent == parent { values[idx].Used = true row.Children = recursiveTier(row.Value, values) items = append(items, row)