Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrUserPlatformQuotaNotFound service 层 sentinel:quota 记录不存在。
|
||||
// adapter 将 repository.ErrUserPlatformQuotaNotFound 包装为此错误,
|
||||
// handler 只需引用 service 包,无需直接依赖 repository 包。
|
||||
var ErrUserPlatformQuotaNotFound = errors.New("user platform quota not found")
|
||||
|
||||
// ErrUserPlatformQuotaFKViolation service 层 sentinel:批量 snapshot UPSERT 时存在
|
||||
// user_id 不在 users 表的记录(外键违反)。adapter 负责将 repository 层同名 sentinel 包装为此错误。
|
||||
var ErrUserPlatformQuotaFKViolation = errors.New("user platform quota snapshot FK violation")
|
||||
|
||||
// UserPlatformQuotaSnapshot 是 service 层 flusher 向 DB 写入快照时使用的传输结构。
|
||||
// 字段语义与 repository.UserPlatformQuotaSnapshot 完全对应,由 adapter 负责转换。
|
||||
type UserPlatformQuotaSnapshot struct {
|
||||
UserID int64
|
||||
Platform string
|
||||
DailyUsageUSD float64
|
||||
WeeklyUsageUSD float64
|
||||
MonthlyUsageUSD float64
|
||||
DailyWindowStart time.Time
|
||||
WeeklyWindowStart time.Time
|
||||
MonthlyWindowStart time.Time
|
||||
}
|
||||
|
||||
// UserPlatformQuotaRecord service 层传输结构体(与 repository 层解耦)。
|
||||
type UserPlatformQuotaRecord struct {
|
||||
UserID int64
|
||||
Platform string
|
||||
DailyLimitUSD *float64
|
||||
WeeklyLimitUSD *float64
|
||||
MonthlyLimitUSD *float64
|
||||
DailyUsageUSD float64
|
||||
WeeklyUsageUSD float64
|
||||
MonthlyUsageUSD float64
|
||||
// 窗口起始时间(可选,用于未来 reset 校验)
|
||||
DailyWindowStart *time.Time
|
||||
WeeklyWindowStart *time.Time
|
||||
MonthlyWindowStart *time.Time
|
||||
}
|
||||
|
||||
// UserPlatformQuotaRepository 定义 service 层所需的 user × platform quota 数据访问端口。
|
||||
// repository 包的 userPlatformQuotaRepository 实现此接口。
|
||||
type UserPlatformQuotaRepository interface {
|
||||
// GetByUserPlatform 查询单条配额记录,未找到时返回 (nil, nil)。
|
||||
GetByUserPlatform(ctx context.Context, userID int64, platform string) (*UserPlatformQuotaRecord, error)
|
||||
// BulkInsertInitial 幂等批量插入初始配额记录(ON CONFLICT DO NOTHING)。
|
||||
BulkInsertInitial(ctx context.Context, records []UserPlatformQuotaRecord) error
|
||||
// IncrementUsageWithReset 原子地累加用量,若窗口已过期则先重置再累加。
|
||||
IncrementUsageWithReset(ctx context.Context, userID int64, platform string, cost float64, now time.Time) error
|
||||
// ListByUser 查询用户的所有平台配额记录。
|
||||
ListByUser(ctx context.Context, userID int64) ([]UserPlatformQuotaRecord, error)
|
||||
// UpsertForUser 全量替换该用户所有平台限额配置(事务内):
|
||||
// 1. 软删除未在 records 中出现的所有 active 行
|
||||
// 2. 对 records 中每条:UPDATE 已存在的(含重新激活软删行);UPDATE 未命中时 INSERT
|
||||
// 仅改 *_limit_usd + deleted_at + updated_at,保留 *_usage_usd / *_window_start。
|
||||
// records 为空时仅执行步骤 1。
|
||||
UpsertForUser(ctx context.Context, userID int64, records []UserPlatformQuotaRecord) error
|
||||
// ResetExpiredWindow 重置指定窗口("daily"|"weekly"|"monthly")的用量与起始时间。
|
||||
// 未命中活跃记录时返回(service-side wrapper of repository.ErrUserPlatformQuotaNotFound)。
|
||||
ResetExpiredWindow(ctx context.Context, userID int64, platform string, window string, newStart time.Time) error
|
||||
// BatchSnapshotUsage 绝对值覆盖写入整批 usage 快照。FK 违反返回 ErrUserPlatformQuotaFKViolation。
|
||||
BatchSnapshotUsage(ctx context.Context, snapshots []UserPlatformQuotaSnapshot, now time.Time) error
|
||||
}
|
||||
Reference in New Issue
Block a user