moto/internal/organize/user.go

33 lines
907 B
Go
Raw Normal View History

2024-12-12 17:39:04 +08:00
package organize
import (
"context"
"fmt"
"git.nobla.cn/golang/moto/common/db"
"git.nobla.cn/golang/moto/models"
"git.nobla.cn/golang/rest/types"
"gorm.io/gorm"
)
func UserTypes(ctx context.Context, domainName string) []*types.TypeValue {
result, err := db.TryCache(ctx, fmt.Sprintf("domain:%s:user:types", domainName), func(tx *gorm.DB) (any, error) {
values := make([]*models.User, 0)
err := tx.Where("domain=?", domainName).Order("uid ASC").Find(&values).Error
if err != nil {
return nil, err
}
data := make([]*types.TypeValue, 0, len(values))
for _, row := range values {
data = append(data, &types.TypeValue{
Label: fmt.Sprintf("%s(%s)", row.Username, row.UID),
Value: row.UID,
})
}
return data, nil
}, db.WithDepend("SELECT max(`updated_at`) FROM `users` WHERE `domain`=?", domainName))
if err == nil {
return result.([]*types.TypeValue)
}
return nil
}