"""好友邀请关系表(注册即生效)。 一行 = 一次成功的邀请绑定。`invitee_user_id` 唯一 = 一个被邀请人只能被归因一次 (天然幂等键,防重复发奖,仿 ad_reward.trans_id 思路)。`channel` 记归因来源 (clipboard 自动 / manual 手动填),便于后续分析"剪贴板自动捕获率"。 邀请码本身是 `user.invite_code`(一人一稳定码,懒生成),不在这张表里。 """ from __future__ import annotations from datetime import datetime from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, false, func from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base class InviteRelation(Base): __tablename__ = "invite_relation" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) inviter_user_id: Mapped[int] = mapped_column( Integer, ForeignKey("user.id"), index=True, nullable=False ) # 被邀请人唯一:一个人只能被邀一次(幂等键,防重复发奖) invitee_user_id: Mapped[int] = mapped_column( Integer, ForeignKey("user.id"), unique=True, index=True, nullable=False ) # 归因来源:clipboard(剪贴板自动) / manual(手动填码) channel: Mapped[str] = mapped_column(String(16), nullable=False, default="clipboard") # 状态:effective(注册即生效)。预留:以后改"完成首单才生效"时用 pending/effective status: Mapped[str] = mapped_column(String(16), nullable=False, default="effective") # 本次给邀请人 / 被邀请人各发的金币(记账留痕,便于对账) inviter_coin: Mapped[int] = mapped_column(Integer, nullable=False, default=0) invitee_coin: Mapped[int] = mapped_column(Integer, nullable=False, default=0) # ===== v2 比价发奖追踪(好友"下载+登录+比价一次"→ 给邀请人发邀请奖励金)===== # 是否已因"好友完成比价"发过奖:好友比价多次只发一次(防重复发,与 invitee 唯一约束双保险) compare_reward_granted: Mapped[bool] = mapped_column( Boolean, nullable=False, default=False, server_default=false() ) # 实发给邀请人的邀请奖励金(分);未发为 0 compare_reward_cents: Mapped[int] = mapped_column( Integer, nullable=False, default=0, server_default="0" ) # 发奖时间(未发为 None) compare_rewarded_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), index=True, nullable=False ) def __repr__(self) -> str: # pragma: no cover return ( f"" )