Files
geo/server/internal/tenant/repository/workspace_repo.go
T
2026-05-20 15:37:25 +08:00

154 lines
4.7 KiB
Go

package repository
import (
"context"
"time"
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
)
type WorkspaceRecentArticle struct {
ID int64
BrandID *int64
GenerateStatus string
PublishStatus string
SourceType string
GenerationMode *string
CreatedAt time.Time
Title *string
WordCount int
SourceLabel *string
TemplateName *string
}
type WorkspacePlan struct {
PlanCode string
PlanName string
QuotaPolicyJSON []byte
StartAt time.Time
EndAt time.Time
}
type WorkspaceKolCard struct {
SubscriptionPromptID int64
GrantedAt time.Time
UpdatedAt time.Time
PromptName string
PlatformHint *string
PackageName string
PackageCover *string
KolDisplayName string
}
type WorkspaceRepository interface {
CountArticlesByTenantAndBrand(ctx context.Context, tenantID, brandID int64) (int64, error)
CountPublishedArticlesByTenantAndBrand(ctx context.Context, tenantID, brandID int64) (int64, error)
CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error)
GetRecentArticlesByBrand(ctx context.Context, tenantID, brandID int64) ([]WorkspaceRecentArticle, error)
GetQuotaSummary(ctx context.Context, tenantID int64) (int, error)
GetActivePlanForTenant(ctx context.Context, tenantID int64) (*WorkspacePlan, error)
ListKolCards(ctx context.Context, tenantID int64) ([]WorkspaceKolCard, error)
}
type workspaceRepository struct {
q generated.Querier
}
func NewWorkspaceRepository(db generated.DBTX) WorkspaceRepository {
return &workspaceRepository{q: newQuerier(db)}
}
func (r *workspaceRepository) CountArticlesByTenantAndBrand(ctx context.Context, tenantID, brandID int64) (int64, error) {
return r.q.CountArticlesByTenantAndBrand(ctx, generated.CountArticlesByTenantAndBrandParams{
TenantID: tenantID,
BrandID: brandID,
})
}
func (r *workspaceRepository) CountPublishedArticlesByTenantAndBrand(ctx context.Context, tenantID, brandID int64) (int64, error) {
return r.q.CountPublishedArticlesByTenantAndBrand(ctx, generated.CountPublishedArticlesByTenantAndBrandParams{
TenantID: tenantID,
BrandID: brandID,
})
}
func (r *workspaceRepository) CountBrandsByTenant(ctx context.Context, tenantID int64) (int64, error) {
return r.q.CountBrandsByTenant(ctx, tenantID)
}
func (r *workspaceRepository) GetRecentArticlesByBrand(ctx context.Context, tenantID, brandID int64) ([]WorkspaceRecentArticle, error) {
rows, err := r.q.GetRecentArticlesByBrand(ctx, generated.GetRecentArticlesByBrandParams{
TenantID: tenantID,
BrandID: brandID,
})
if err != nil {
return nil, err
}
articles := make([]WorkspaceRecentArticle, 0, len(rows))
for _, row := range rows {
brandID := row.BrandID
articles = append(articles, WorkspaceRecentArticle{
ID: row.ID,
BrandID: &brandID,
GenerateStatus: row.GenerateStatus,
PublishStatus: row.PublishStatus,
SourceType: row.SourceType,
GenerationMode: nullableAnyText(row.GenerationMode),
CreatedAt: timeFromTimestamp(row.CreatedAt),
Title: nullableText(row.Title),
WordCount: intFromInt4(row.WordCount),
SourceLabel: nullableText(row.SourceLabel),
TemplateName: nullableText(row.TemplateName),
})
}
return articles, nil
}
func (r *workspaceRepository) GetQuotaSummary(ctx context.Context, tenantID int64) (int, error) {
balance, err := r.q.GetQuotaSummary(ctx, tenantID)
if err != nil {
return 0, err
}
return int(balance), nil
}
func (r *workspaceRepository) GetActivePlanForTenant(ctx context.Context, tenantID int64) (*WorkspacePlan, error) {
row, err := r.q.GetActivePlanForTenant(ctx, tenantID)
if err != nil {
return nil, err
}
return &WorkspacePlan{
PlanCode: row.PlanCode,
PlanName: row.PlanName,
QuotaPolicyJSON: row.QuotaPolicyJson,
StartAt: timeFromTimestamp(row.StartAt),
EndAt: timeFromTimestamp(row.EndAt),
}, nil
}
func (r *workspaceRepository) ListKolCards(ctx context.Context, tenantID int64) ([]WorkspaceKolCard, error) {
rows, err := r.q.ListKolCardsForTenant(ctx, tenantID)
if err != nil {
return nil, err
}
cards := make([]WorkspaceKolCard, 0, len(rows))
for _, row := range rows {
cards = append(cards, WorkspaceKolCard{
SubscriptionPromptID: row.SubscriptionPromptID,
GrantedAt: timeFromTimestamp(row.GrantedAt),
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
PromptName: row.PromptName,
PlatformHint: nullableText(row.PlatformHint),
PackageName: row.PackageName,
PackageCover: nullableText(row.PackageCover),
KolDisplayName: row.KolDisplayName,
})
}
return cards, nil
}