package biz import "context" type AuthorityAccessRepo interface { CreateAuthority(context.Context, *Authority) error CopyAuthority(context.Context, uint, *Authority) error UpdateAuthority(context.Context, *Authority) error DeleteAuthority(context.Context, uint) error ListAuthorities(context.Context) ([]*Authority, error) SetAuthorityUsers(context.Context, uint, []uint) error AuthorityUserIDs(context.Context, uint) ([]uint, error) SetDataScope(context.Context, uint, int, []uint) error DataScopeDepartmentIDs(context.Context, uint) ([]uint, error) ResolveDataScope(context.Context, uint, uint) (DataScope, error) } type AuthorityUsecase struct{ AuthorityAccessRepo } func NewAuthorityUsecase(repo AuthorityAccessRepo) *AuthorityUsecase { return &AuthorityUsecase{AuthorityAccessRepo: repo} } func (uc *AuthorityUsecase) AuthorityTree(ctx context.Context) ([]*Authority, error) { items, err := uc.ListAuthorities(ctx) if err != nil { return nil, err } byID := make(map[uint]*Authority, len(items)) for _, item := range items { item.Children = nil byID[item.AuthorityID] = item } roots := make([]*Authority, 0) for _, item := range items { if item.ParentID != nil && *item.ParentID != 0 && byID[*item.ParentID] != nil { parent := byID[*item.ParentID] parent.Children = append(parent.Children, item) } else { roots = append(roots, item) } } return roots, nil }