"""用户游戏角色的 7 天绑定锁。 斗鱼的绑定限制是**双向**的: - 一个斗鱼账号 7 天内只能绑定一个游戏角色; - 一个游戏角色 7 天内也只能绑定一个斗鱼账号。 两个限制约束的都是「账号 + 角色」这一对。履约如果给同一个角色换一个新账号, 就会撞上角色侧限制;精英/协作之所以正常,是因为它们在**已经绑定该角色的那个 账号**上操作,没有产生新的对。 这里只做一件事:在角色已知(二维码识别出 pending)时,查这个角色最近 7 天是否 已经绑在**另一个账号**上。命中就不调 ``actBind``,直接把订单转人工,并把原账号 和可复用时间带给客服。 """ from __future__ import annotations from dataclasses import dataclass from datetime import UTC, datetime, timedelta from zoneinfo import ZoneInfo from sqlalchemy.orm import Session from ...models import Account, AccountEvent, AccountLease #: 新的履约 issue code:用户角色 7 天内已绑定其他斗鱼账号。 ROLE_LOCK_ISSUE = "customer_role_bound_elsewhere" #: 新的履约 issue code:当前分配的斗鱼账号 7 天内已绑定其他角色。 ACCOUNT_LOCK_ISSUE = "account_role_bound_elsewhere" #: 官方规则窗口。 ROLE_LOCK_WINDOW = timedelta(days=7) _BEIJING = ZoneInfo("Asia/Shanghai") _LEASE_PURPOSE_LABELS = { "douyu_collab": "协作单", "platform_fulfillment": "履约单", "exchange": "兑换单", } @dataclass(frozen=True) class AccountLock: role_name: str account_id: int account_name: str bound_at: datetime reusable_at: datetime def message(self) -> str: return ( f"该斗鱼账号 7 天内已绑定角色({self.role_name})," f"绑定时间 {_beijing_text(self.bound_at)}," f"预计 {_beijing_text(self.reusable_at)} 后可再次换绑;" "如需绑定新角色请换号或等待。" ) @dataclass(frozen=True) class RoleLock: role_name: str account_id: int account_name: str bound_at: datetime reusable_at: datetime lease_purpose: str = "" lease_batch_id: str = "" @property def lease_label(self) -> str: return _LEASE_PURPOSE_LABELS.get(self.lease_purpose, "其他业务") def message(self) -> str: bound_at = _beijing_text(self.bound_at) reusable_at = _beijing_text(self.reusable_at) if not self.lease_purpose: lease_text = "原账号当前空闲,可直接复用" else: lease_text = f"原账号当前被{self.lease_label}占用" return ( f"该游戏角色({self.role_name})在 7 天内已绑定斗鱼账号" f"({self.account_name},{lease_text})," f"绑定时间 {bound_at},预计 {reusable_at} 后可再次绑定;" "请复用原账号或等待。" ) def _beijing_text(value: datetime) -> str: moment = value if value.tzinfo is not None else value.replace(tzinfo=UTC) return moment.astimezone(_BEIJING).strftime("%Y-%m-%d %H:%M") def find_role_lock( db: Session, role_name: str, *, current_account_id: int | None, now: datetime | None = None, ) -> RoleLock | None: """返回该角色最近 7 天绑定到的其他账号;同账号/超过 7 天返回 None。""" role = str(role_name or "").strip() if not role: return None moment = now or datetime.now(UTC) since = moment - ROLE_LOCK_WINDOW # 命中 ix_account_events_event_type;bind_ok 只有一万多条,按二维码 pending # 的频率(每个订单一次)足够。后续量上来再加 (event_type, subject_key, # occurred_at) 复合索引。 event = ( db.query(AccountEvent) .filter( AccountEvent.event_type == "bind_ok", AccountEvent.subject_type == "elite_role", AccountEvent.subject_key == role, AccountEvent.occurred_at >= since, AccountEvent.occurred_at <= moment, ) .order_by(AccountEvent.occurred_at.desc(), AccountEvent.id.desc()) .first() ) if event is None: return None if ( current_account_id is not None and int(event.account_id) == int(current_account_id) ): # 角色已经绑在本单当前账号上,是同一对,不拦。 return None account = db.get(Account, int(event.account_id)) if account is not None: account_name = ( str(account.nickname or "").strip() or str(account.username or "").strip() or f"账号#{account.id}" ) else: account_name = f"账号#{event.account_id}" lease = ( db.query(AccountLease) .filter( AccountLease.account_id == int(event.account_id), AccountLease.status == "active", ) .order_by(AccountLease.leased_at.desc(), AccountLease.id.desc()) .first() ) bound_at = ( event.occurred_at if event.occurred_at.tzinfo is not None else event.occurred_at.replace(tzinfo=UTC) ) return RoleLock( role_name=role, account_id=int(event.account_id), account_name=account_name, bound_at=bound_at, reusable_at=bound_at + ROLE_LOCK_WINDOW, lease_purpose=str(lease.purpose or "") if lease is not None else "", lease_batch_id=str(lease.batch_id or "") if lease is not None else "", ) def find_account_lock( db: Session, account_id: int | None, scanned_role: str, *, now: datetime | None = None, ) -> AccountLock | None: """当前分配的斗鱼账号 7 天内是否已经绑定过别的角色。 同一角色(同一对)不算锁:复用已有绑定即可。 """ role = str(scanned_role or "").strip() if account_id is None or not role: return None moment = now or datetime.now(UTC) since = moment - ROLE_LOCK_WINDOW event = ( db.query(AccountEvent) .filter( AccountEvent.event_type == "bind_ok", AccountEvent.subject_type == "elite_role", AccountEvent.account_id == int(account_id), AccountEvent.occurred_at >= since, AccountEvent.occurred_at <= moment, ) .order_by(AccountEvent.occurred_at.desc(), AccountEvent.id.desc()) .first() ) if event is None: return None bound_role = str(event.subject_key or "").strip() if not bound_role or bound_role == role: return None account = db.get(Account, int(account_id)) account_name = ( ( str(account.nickname or "").strip() or str(account.username or "").strip() or f"账号#{account.id}" ) if account is not None else f"账号#{account_id}" ) bound_at = ( event.occurred_at if event.occurred_at.tzinfo is not None else event.occurred_at.replace(tzinfo=UTC) ) return AccountLock( role_name=bound_role, account_id=int(account_id), account_name=account_name, bound_at=bound_at, reusable_at=bound_at + ROLE_LOCK_WINDOW, ) __all__ = [ "ROLE_LOCK_ISSUE", "ROLE_LOCK_WINDOW", "AccountLock", "RoleLock", "find_account_lock", "find_role_lock", ]