user_platform_quota.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package schema
  2. import (
  3. "fmt"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect"
  6. "entgo.io/ent/dialect/entsql"
  7. "entgo.io/ent/schema"
  8. "entgo.io/ent/schema/edge"
  9. "entgo.io/ent/schema/field"
  10. "entgo.io/ent/schema/index"
  11. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  12. )
  13. // UserPlatformQuota holds the schema definition for per-user per-platform quota.
  14. type UserPlatformQuota struct {
  15. ent.Schema
  16. }
  17. func (UserPlatformQuota) Annotations() []schema.Annotation {
  18. return []schema.Annotation{
  19. entsql.Annotation{Table: "user_platform_quotas"},
  20. }
  21. }
  22. func (UserPlatformQuota) Mixin() []ent.Mixin {
  23. return []ent.Mixin{
  24. mixins.TimeMixin{},
  25. mixins.SoftDeleteMixin{},
  26. }
  27. }
  28. func (UserPlatformQuota) Fields() []ent.Field {
  29. return []ent.Field{
  30. field.Int64("user_id"),
  31. field.String("platform").
  32. MaxLen(32).
  33. NotEmpty().
  34. Validate(func(s string) error {
  35. // 注意:平台列表的单一权威源为 service.AllowedQuotaPlatforms;
  36. // 此处为 ent 构建期约束,需与 service.AllowedQuotaPlatforms 保持同步。
  37. switch s {
  38. case "anthropic", "openai", "gemini", "antigravity", "grok",
  39. "kimi", "zhipu", "deepseek", "minimax":
  40. return nil
  41. default:
  42. return fmt.Errorf("platform %q is not allowed", s)
  43. }
  44. }),
  45. // 日 / 周 / 月 USD 上限:
  46. // nil / not set → 无限额(完全放行)
  47. // 0 → 完全禁用(任何请求都会被拒绝,因为 usage >= 0 恒成立)
  48. // > 0 → USD 限额上限
  49. field.Float("daily_limit_usd").
  50. Optional().
  51. Nillable().
  52. SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}),
  53. field.Float("weekly_limit_usd").
  54. Optional().
  55. Nillable().
  56. SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}),
  57. field.Float("monthly_limit_usd").
  58. Optional().
  59. Nillable().
  60. SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}),
  61. // 当前窗口已用量(USD,preflight 时与 limit 比较)
  62. field.Float("daily_usage_usd").
  63. Default(0).
  64. SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}),
  65. field.Float("weekly_usage_usd").
  66. Default(0).
  67. SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}),
  68. field.Float("monthly_usage_usd").
  69. Default(0).
  70. SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}),
  71. // 窗口起点(NULL = 首次还未初始化,由 InitWindowStarts 用 COALESCE 兜底)
  72. field.Time("daily_window_start").
  73. Optional().
  74. Nillable().
  75. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  76. field.Time("weekly_window_start").
  77. Optional().
  78. Nillable().
  79. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  80. field.Time("monthly_window_start").
  81. Optional().
  82. Nillable().
  83. SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
  84. }
  85. }
  86. func (UserPlatformQuota) Edges() []ent.Edge {
  87. return []ent.Edge{
  88. edge.From("user", User.Type).
  89. Ref("platform_quotas").
  90. Field("user_id").
  91. Unique().
  92. Required(),
  93. }
  94. }
  95. func (UserPlatformQuota) Indexes() []ent.Index {
  96. return []ent.Index{
  97. // 软删除友好:只对未删记录唯一
  98. index.Fields("user_id", "platform").
  99. Unique().
  100. Annotations(entsql.IndexWhere("deleted_at IS NULL")),
  101. index.Fields("user_id"),
  102. }
  103. }