Files
2026-08-14 15:23:46 +08:00

69 lines
3.5 KiB
Python

from __future__ import annotations
from datetime import datetime
from decimal import Decimal
from sqlalchemy import DateTime, ForeignKey, Index, Numeric, String
from sqlalchemy.orm import Mapped, mapped_column
from app.enums.credit_balance import CreditScope
from app.models.base import Base, TimestampMixin
class CreditRecordAllocation(Base, TimestampMixin):
__tablename__ = "credit_record_allocations"
__table_args__ = (
Index("ix_credit_record_allocations_record", "credit_record_id", "id"),
Index("ix_credit_record_allocations_balance", "credit_balance_id", "created_at"),
Index("ix_credit_record_allocations_user_time", "user_id", "created_at"),
Index("ix_credit_record_allocations_source_allocation", "source_allocation_id"),
Index("ix_credit_record_allocations_team_time", "team_id_snapshot", "created_at", "id"),
Index(
"ix_credit_record_allocations_team_manager_time",
"team_id_snapshot", "team_manager_id_snapshot", "created_at", "id",
),
Index(
"ix_credit_record_allocations_team_period_user",
"subscription_period_id_snapshot", "user_id", "allocation_action",
),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True)
credit_record_id: Mapped[str] = mapped_column(
String(32), ForeignKey("credit_records.id", ondelete="CASCADE"), nullable=False
)
credit_balance_id: Mapped[str] = mapped_column(
String(32), ForeignKey("user_credit_balances.id", ondelete="RESTRICT"), nullable=False
)
user_id: Mapped[str] = mapped_column(
String(32), ForeignKey("users.id", ondelete="CASCADE"), nullable=False,
comment="真实业务消费者;发放类记录为资金所有人",
)
source_allocation_id: Mapped[str | None] = mapped_column(
String(32), ForeignKey("credit_record_allocations.id", ondelete="SET NULL"), nullable=True
)
allocation_action: Mapped[str] = mapped_column(String(48), nullable=False, index=True)
amount: Mapped[Decimal] = mapped_column(Numeric(20, 2), nullable=False)
request_time: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
credit_level_snapshot: Mapped[str] = mapped_column(String(32), nullable=False)
credit_scope_snapshot: Mapped[str] = mapped_column(
String(16), nullable=False, default=CreditScope.PERSONAL.value,
server_default=CreditScope.PERSONAL.value,
)
source_type_snapshot: Mapped[str] = mapped_column(String(48), nullable=False)
source_id_snapshot: Mapped[str | None] = mapped_column(String(64), nullable=True)
team_id_snapshot: Mapped[str | None] = mapped_column(String(32), nullable=True)
team_manager_id_snapshot: Mapped[str | None] = mapped_column(String(32), nullable=True)
subscription_id_snapshot: Mapped[str | None] = mapped_column(String(32), nullable=True)
subscription_period_id_snapshot: Mapped[str | None] = mapped_column(String(32), nullable=True)
seat_id_snapshot: Mapped[str | None] = mapped_column(String(32), nullable=True)
valid_from_snapshot: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
expires_at_snapshot: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
unspent_before: Mapped[Decimal] = mapped_column(Numeric(20, 2), nullable=False)
unspent_after: Mapped[Decimal] = mapped_column(Numeric(20, 2), nullable=False)
consumed_before: Mapped[Decimal] = mapped_column(Numeric(20, 2), nullable=False)
consumed_after: Mapped[Decimal] = mapped_column(Numeric(20, 2), nullable=False)