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