2025-12-29 10:03:27 +08:00
|
|
|
|
package schema
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Wei-Shaw/sub2api/ent/schema/mixins"
|
2026-01-30 16:45:04 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/domain"
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
"entgo.io/ent"
|
|
|
|
|
|
"entgo.io/ent/dialect"
|
|
|
|
|
|
"entgo.io/ent/dialect/entsql"
|
|
|
|
|
|
"entgo.io/ent/schema"
|
|
|
|
|
|
"entgo.io/ent/schema/edge"
|
|
|
|
|
|
"entgo.io/ent/schema/field"
|
|
|
|
|
|
"entgo.io/ent/schema/index"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// UserSubscription holds the schema definition for the UserSubscription entity.
|
|
|
|
|
|
type UserSubscription struct {
|
|
|
|
|
|
ent.Schema
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (UserSubscription) Annotations() []schema.Annotation {
|
|
|
|
|
|
return []schema.Annotation{
|
|
|
|
|
|
entsql.Annotation{Table: "user_subscriptions"},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (UserSubscription) Mixin() []ent.Mixin {
|
|
|
|
|
|
return []ent.Mixin{
|
|
|
|
|
|
mixins.TimeMixin{},
|
2025-12-31 14:11:57 +08:00
|
|
|
|
mixins.SoftDeleteMixin{},
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (UserSubscription) Fields() []ent.Field {
|
|
|
|
|
|
return []ent.Field{
|
|
|
|
|
|
field.Int64("user_id"),
|
|
|
|
|
|
field.Int64("group_id"),
|
|
|
|
|
|
|
|
|
|
|
|
field.Time("starts_at").
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
|
|
|
|
|
field.Time("expires_at").
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
|
|
|
|
|
field.String("status").
|
|
|
|
|
|
MaxLen(20).
|
2026-01-30 16:45:04 +08:00
|
|
|
|
Default(domain.SubscriptionStatusActive),
|
2025-12-29 10:03:27 +08:00
|
|
|
|
|
|
|
|
|
|
field.Time("daily_window_start").
|
|
|
|
|
|
Optional().
|
|
|
|
|
|
Nillable().
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
|
|
|
|
|
field.Time("weekly_window_start").
|
|
|
|
|
|
Optional().
|
|
|
|
|
|
Nillable().
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
|
|
|
|
|
field.Time("monthly_window_start").
|
|
|
|
|
|
Optional().
|
|
|
|
|
|
Nillable().
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
|
|
|
|
|
|
|
|
|
|
|
field.Float("daily_usage_usd").
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}).
|
|
|
|
|
|
Default(0),
|
|
|
|
|
|
field.Float("weekly_usage_usd").
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}).
|
|
|
|
|
|
Default(0),
|
|
|
|
|
|
field.Float("monthly_usage_usd").
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "decimal(20,10)"}).
|
|
|
|
|
|
Default(0),
|
|
|
|
|
|
|
|
|
|
|
|
field.Int64("assigned_by").
|
|
|
|
|
|
Optional().
|
|
|
|
|
|
Nillable(),
|
|
|
|
|
|
field.Time("assigned_at").
|
|
|
|
|
|
Default(time.Now).
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "timestamptz"}),
|
|
|
|
|
|
field.String("notes").
|
|
|
|
|
|
Optional().
|
|
|
|
|
|
Nillable().
|
|
|
|
|
|
SchemaType(map[string]string{dialect.Postgres: "text"}),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (UserSubscription) Edges() []ent.Edge {
|
|
|
|
|
|
return []ent.Edge{
|
|
|
|
|
|
edge.From("user", User.Type).
|
|
|
|
|
|
Ref("subscriptions").
|
|
|
|
|
|
Field("user_id").
|
|
|
|
|
|
Unique().
|
|
|
|
|
|
Required(),
|
|
|
|
|
|
edge.From("group", Group.Type).
|
|
|
|
|
|
Ref("subscriptions").
|
|
|
|
|
|
Field("group_id").
|
|
|
|
|
|
Unique().
|
|
|
|
|
|
Required(),
|
|
|
|
|
|
edge.From("assigned_by_user", User.Type).
|
|
|
|
|
|
Ref("assigned_subscriptions").
|
|
|
|
|
|
Field("assigned_by").
|
|
|
|
|
|
Unique(),
|
2025-12-31 14:11:57 +08:00
|
|
|
|
edge.To("usage_logs", UsageLog.Type),
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (UserSubscription) Indexes() []ent.Index {
|
|
|
|
|
|
return []ent.Index{
|
|
|
|
|
|
index.Fields("user_id"),
|
|
|
|
|
|
index.Fields("group_id"),
|
|
|
|
|
|
index.Fields("status"),
|
|
|
|
|
|
index.Fields("expires_at"),
|
2026-02-28 15:01:20 +08:00
|
|
|
|
// 活跃订阅查询复合索引(线上由 SQL 迁移创建部分索引,schema 仅用于模型可读性对齐)
|
|
|
|
|
|
index.Fields("user_id", "status", "expires_at"),
|
2025-12-29 10:03:27 +08:00
|
|
|
|
index.Fields("assigned_by"),
|
2025-12-31 16:37:18 +08:00
|
|
|
|
// 唯一约束通过部分索引实现(WHERE deleted_at IS NULL),支持软删除后重新订阅
|
|
|
|
|
|
// 见迁移文件 016_soft_delete_partial_unique_indexes.sql
|
|
|
|
|
|
index.Fields("user_id", "group_id"),
|
2025-12-31 14:11:57 +08:00
|
|
|
|
index.Fields("deleted_at"),
|
2025-12-29 10:03:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|