Files
live-hub-py/web/backend/services/platform_fulfillment/role_lock.py
T
yml ad6c31729d fix(fulfillment): 角色 7 天锁命中时转人工
- 角色 pending 时查 7 天内是否已绑定其他斗鱼账号
- 命中不调 actBind,订单转 manual_required
- 时间线记录原账号、绑定时间、可复用时间和占用业务
- 确认前和启动对账补充校验,避免存量订单继续尝试
- 补充同账号、超窗口、确认前和启动对账回归测试
2026-11-24 02:10:07 +08:00

152 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""用户游戏角色的 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"
#: 官方规则窗口。
ROLE_LOCK_WINDOW = timedelta(days=7)
_BEIJING = ZoneInfo("Asia/Shanghai")
_LEASE_PURPOSE_LABELS = {
"douyu_collab": "协作单",
"platform_fulfillment": "履约单",
"exchange": "兑换单",
}
@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 "",
)
__all__ = [
"ROLE_LOCK_ISSUE",
"ROLE_LOCK_WINDOW",
"RoleLock",
"find_role_lock",
]