47 lines
2.2 KiB
Python
47 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
from datetime import datetime, timedelta
|
||
from typing import Optional
|
||
|
||
from sqlalchemy import String, Column, BigInteger, ForeignKey, DateTime, Index, Text
|
||
from sqlalchemy.dialects.postgresql import JSONB
|
||
from sqlalchemy.orm import Mapped, mapped_column
|
||
|
||
from backend.common.model import snowflake_id_key, Base
|
||
|
||
|
||
class Points(Base):
|
||
__tablename__ = 'points'
|
||
|
||
id: Mapped[snowflake_id_key] = mapped_column(BigInteger, init=False, primary_key=True)
|
||
user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey('wx_user.id'), unique=True, nullable=False, comment='关联的用户ID')
|
||
balance: Mapped[int] = mapped_column(BigInteger, default=0, comment='当前积分余额')
|
||
total_earned: Mapped[int] = mapped_column(BigInteger, default=0, comment='累计获得积分')
|
||
total_spent: Mapped[int] = mapped_column(BigInteger, default=0, comment='累计消费积分')
|
||
expired_time: Mapped[datetime] = mapped_column(DateTime, default=datetime.now() + timedelta(days=30), comment="过期时间")
|
||
|
||
# 索引优化
|
||
__table_args__ = (
|
||
Index('idx_points_user', 'user_id'),
|
||
{'comment': '用户积分表'}
|
||
)
|
||
|
||
|
||
class PointsLog(Base):
|
||
__tablename__ = 'points_log'
|
||
|
||
id: Mapped[snowflake_id_key] = mapped_column(BigInteger, init=False, primary_key=True)
|
||
user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey('wx_user.id'), nullable=False, comment='用户ID')
|
||
action: Mapped[str] = mapped_column(String(32), comment='动作:earn/spend')
|
||
amount: Mapped[int] = mapped_column(BigInteger, comment='变动数量')
|
||
balance_after: Mapped[int] = mapped_column(BigInteger, comment='变动后余额')
|
||
related_id: Mapped[Optional[int]] = mapped_column(BigInteger, default=None, comment='关联ID')
|
||
details: Mapped[Optional[dict]] = mapped_column(JSONB, default=None, comment='附加信息')
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now, comment='创建时间')
|
||
|
||
# 索引优化
|
||
__table_args__ = (
|
||
Index('idx_points_log_user_action', 'user_id', 'action'),
|
||
Index('idx_points_log_user_time', 'user_id', 'created_at'),
|
||
{'comment': '积分变动日志表'}
|
||
) |