Files
meijiaka-zy/python-api/app/models/point_transaction.py
T
小鱼开发 fbef15ba7e chore(alembic): 重建单一初始迁移脚本
- 合并为单一初始全量迁移,覆盖 users/user_devices/user_points/point_batches/point_transactions/point_recharge_orders
- 去掉所有 ForeignKey 约束(业务层软删除,不依赖数据库级联)
- 去掉不必要的索引,仅保留 Unique 约束自带索引
- 修复 mjk_users.id 为 UUID 类型(非 String(36))
- 修复 user_device.last_active_at 时区类型一致性(添加 timezone=True)
2026-05-08 14:40:22 +08:00

81 lines
1.9 KiB
Python

"""
积分流水表
==========
记录每一笔积分变动,用于审计和对账。
变动类型:
- recharge:充值
- consume:消费(AI 调用)
- expire:过期
- refund:返还(预扣后失败或实际少于预估)
"""
import uuid
from sqlalchemy import BigInteger, String, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import BaseModelBigInt
class PointTransaction(BaseModelBigInt):
"""积分流水"""
__tablename__ = "mjk_point_transactions"
user_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
nullable=False,
comment="用户 ID",
)
type: Mapped[str] = mapped_column(
String(20),
nullable=False,
comment="变动类型:recharge / consume / expire / refund",
)
amount: Mapped[int] = mapped_column(
default=0,
nullable=False,
comment="变动数量(正数)",
)
balance_before: Mapped[int] = mapped_column(
default=0,
nullable=False,
comment="变动前总余额",
)
balance_after: Mapped[int] = mapped_column(
default=0,
nullable=False,
comment="变动后总余额",
)
source_type: Mapped[str | None] = mapped_column(
String(32),
nullable=True,
comment="消费来源类型:script / polish / voice_clone / tts / video",
)
source_id: Mapped[str | None] = mapped_column(
String(64),
nullable=True,
comment="关联的任务 ID 或订单 ID",
)
batch_id: Mapped[int | None] = mapped_column(
BigInteger,
nullable=True,
comment="关联的积分批次 ID(消费时记录从哪个批次扣)",
)
description: Mapped[str | None] = mapped_column(
Text,
nullable=True,
comment="描述",
)