feat: add tenant commands

This commit is contained in:
Rogee
2024-12-07 18:48:05 +08:00
parent e15d8d9bb2
commit 8f88929575
25 changed files with 271 additions and 48 deletions

View File

@@ -208,3 +208,44 @@ func (svc *Service) GetTenantIDBySlug(ctx context.Context, slug string) (int64,
}
return id, nil
}
// CreateTenant
func (svc *Service) CreateTenant(ctx context.Context, name, slug string) error {
log := svc.log.WithField("method", "CreateTenant")
expireAt := time.Now().Add(time.Hour * 24 * 366)
// 仅保留天数
expireAt = time.Date(expireAt.Year(), expireAt.Month(), expireAt.Day(), 0, 0, 0, 0, expireAt.Location())
tbl := table.Tenants
stmt := tbl.
INSERT(tbl.Name, tbl.Slug, tbl.ExpireAt).
VALUES(String(name), String(slug), TimestampT(expireAt)).
ON_CONFLICT(tbl.Slug).
DO_NOTHING()
log.Debug(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return errors.Wrapf(err, "create tenant: %s(%s)", name, slug)
}
return nil
}
// SetTenantExpireAtBySlug
func (svc *Service) SetTenantExpireAtBySlug(ctx context.Context, slug string, expire time.Time) error {
log := svc.log.WithField("method", "SetTenantExpireAtBySlug")
tbl := table.Tenants
stmt := tbl.
UPDATE(tbl.ExpireAt).
SET(TimestampT(expire)).
WHERE(tbl.Slug.EQ(String(slug)))
log.Debug(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return errors.Wrapf(err, "renew tenant: %s expire at %s", slug, expire)
}
return nil
}