85 lines
5.0 KiB
Python
85 lines
5.0 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from decimal import Decimal
|
|
|
|
from sqlalchemy import Boolean, CheckConstraint, DateTime, Index, JSON, Numeric, String
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.enums.credit_balance import CreditLevel
|
|
from app.enums.credit_product import CreditProductType
|
|
from app.models.base import Base, TimestampMixin
|
|
|
|
|
|
class CreditProduct(Base, TimestampMixin):
|
|
__tablename__ = "credit_products"
|
|
__table_args__ = (
|
|
Index("uq_credit_products_code", "product_code", unique=True),
|
|
Index("ix_credit_products_public", "product_type", "is_active", "sort_order"),
|
|
CheckConstraint("price >= 0", name="ck_credit_products_price_nonnegative"),
|
|
CheckConstraint("first_purchase_price IS NULL OR first_purchase_price >= 0", name="ck_credit_products_first_price_nonnegative"),
|
|
CheckConstraint("regular_price IS NULL OR regular_price >= 0", name="ck_credit_products_regular_price_nonnegative"),
|
|
CheckConstraint("activity_price IS NULL OR activity_price >= 0", name="ck_credit_products_activity_price_nonnegative"),
|
|
CheckConstraint("monthly_grant_credits IS NULL OR monthly_grant_credits > 0", name="ck_credit_products_monthly_grant_positive"),
|
|
CheckConstraint("grant_credits IS NULL OR grant_credits > 0", name="ck_credit_products_grant_positive"),
|
|
CheckConstraint(
|
|
"(product_type = 'subscription' AND tier_code IS NOT NULL AND tier_rank IS NOT NULL "
|
|
"AND billing_cycle IS NOT NULL AND monthly_grant_credits IS NOT NULL "
|
|
"AND first_purchase_price IS NOT NULL AND regular_price IS NOT NULL "
|
|
"AND grant_credits IS NULL AND validity_months IS NULL) "
|
|
"OR (product_type = 'credit_addon' AND grant_credits IS NOT NULL "
|
|
"AND validity_months BETWEEN 1 AND 36 AND tier_code IS NULL AND tier_rank IS NULL "
|
|
"AND billing_cycle IS NULL AND monthly_grant_credits IS NULL "
|
|
"AND first_purchase_price IS NULL AND regular_price IS NULL "
|
|
"AND activity_price IS NULL AND activity_start_at IS NULL AND activity_end_at IS NULL)",
|
|
name="ck_credit_products_type_required_fields",
|
|
),
|
|
CheckConstraint(
|
|
"(activity_price IS NULL AND activity_start_at IS NULL AND activity_end_at IS NULL) "
|
|
"OR (activity_price IS NOT NULL AND activity_start_at IS NOT NULL "
|
|
"AND activity_end_at IS NOT NULL AND activity_end_at > activity_start_at)",
|
|
name="ck_credit_products_activity_window",
|
|
),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
|
product_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
product_type: Mapped[str] = mapped_column(String(24), nullable=False, index=True)
|
|
name: Mapped[str] = mapped_column(String(96), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
|
features_json: Mapped[list | None] = mapped_column(JSON, nullable=True)
|
|
|
|
# 订阅套餐字段
|
|
tier_code: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
tier_rank: Mapped[int | None] = mapped_column(nullable=True)
|
|
billing_cycle: Mapped[str | None] = mapped_column(String(24), nullable=True)
|
|
monthly_grant_credits: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
|
|
first_purchase_price: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
|
|
regular_price: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
|
|
activity_price: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
|
|
activity_start_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
activity_end_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
renewal_enabled: Mapped[bool] = mapped_column(
|
|
Boolean, nullable=False, default=True, server_default="true"
|
|
)
|
|
|
|
# 积分增值包字段;有效期按自然月配置,范围1-36个月。
|
|
grant_credits: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
|
|
validity_months: Mapped[int | None] = mapped_column(nullable=True)
|
|
|
|
price: Mapped[Decimal] = mapped_column(Numeric(20, 2), nullable=False, default=Decimal("0.00"), server_default="0")
|
|
credit_level: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default=CreditLevel.GENERAL.value, server_default=CreditLevel.GENERAL.value
|
|
)
|
|
currency: Mapped[str] = mapped_column(String(8), nullable=False, default="CNY", server_default="CNY")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="true")
|
|
sort_order: Mapped[int] = mapped_column(nullable=False, default=0, server_default="0")
|
|
|
|
@property
|
|
def is_subscription(self) -> bool:
|
|
return self.product_type == CreditProductType.SUBSCRIPTION.value
|
|
|
|
@property
|
|
def is_credit_addon(self) -> bool:
|
|
return self.product_type == CreditProductType.CREDIT_ADDON.value
|