feat(accounts): 统一可换绑时间并加本地 7 天锁

- 新增官方时间、本地 bind_ok、7 天锁到期、检查时间和来源字段
- role_change_available_at 改为官方时间与本地锁取较晚者
- 绑定成功和官方探测统一写有效时间
- 分配过滤增加最近 elite bind_ok 的数据库兜底
- 迁移 0129 从 account_events 回填历史账号
- 补充字段/回填过滤测试和生命周期文档
This commit is contained in:
yml
2026-11-24 17:37:15 +08:00
parent a1530b7e74
commit 2e6303361c
11 changed files with 415 additions and 9 deletions
+27
View File
@@ -285,6 +285,33 @@ def role_change_available_at(
return None
#: 本地规则:同一斗鱼账号 7 天内只能绑定一个精英角色。
ROLE_BIND_LOCK = timedelta(days=7)
def role_bind_lock_until(last_bind_ok_at: datetime | None) -> datetime | None:
if last_bind_ok_at is None:
return None
return last_bind_ok_at + ROLE_BIND_LOCK
def effective_role_change_available_at(
*,
official_at: datetime | None,
last_bind_ok_at: datetime | None,
) -> datetime | None:
"""官方可换绑时间与本地 7 天锁取较晚者。
分配/人工选号只看这个结果;官方时间早于本地锁时,本地锁优先。
"""
candidates = [
value
for value in (official_at, role_bind_lock_until(last_bind_ok_at))
if value is not None
]
return max(candidates) if candidates else None
BindCooldownKind = Literal["platform", "customer"]
+26 -3
View File
@@ -9,6 +9,7 @@ from sqlalchemy import and_, func, or_, select
from ..models import (
Account,
AccountCommodityState,
AccountEvent,
AccountLease,
DouyuExternalOrder,
LoginTask,
@@ -121,10 +122,31 @@ def active_collab_order_exists():
)
def recent_elite_bind_lock_exists(now: datetime):
"""本地 7 天规则:最近 7 天有精英 bind_ok 的账号不能再接新角色。
这是 `role_change_available_at` 的数据库兜底:即使字段回填/写入有遗漏,
分配前也不会选中一个刚绑定过的账号。
"""
return (
select(AccountEvent.id)
.where(
AccountEvent.account_id == Account.id,
AccountEvent.event_type == "bind_ok",
AccountEvent.subject_type == "elite_role",
AccountEvent.occurred_at > now - timedelta(days=7),
)
.exists()
)
def role_change_ready_filter(now: datetime):
return or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= now,
return and_(
or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= now,
),
~recent_elite_bind_lock_exists(now),
)
@@ -167,6 +189,7 @@ __all__ = [
"fresh_ck_cutoff",
"fresh_eligible_state_filter",
"pool_owner_ids",
"recent_elite_bind_lock_exists",
"role_change_ready_filter",
"valid_ck_filter",
]
+19 -2
View File
@@ -30,13 +30,17 @@
from __future__ import annotations
from datetime import datetime
from datetime import UTC, datetime
from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
from ..models import Account
from .account_binding import (
effective_role_change_available_at,
role_bind_lock_until,
)
from .account_events_service import (
CATEGORY_ACTIVITY,
CATEGORY_BUSINESS,
@@ -467,7 +471,7 @@ def record_bind(
title += f":{role_name}"
if weak:
title += "(未回查确认)"
return emit(
emitted = emit(
db,
account_id=account.id,
event_type=EVENT_BIND_OK,
@@ -488,6 +492,19 @@ def record_bind(
actor_id=actor_id,
source_ref=task_ref(task_id),
)
if scope == "elite":
# 本地 7 天锁:绑定事实发生即生效,不能等下一次官方查询。
moment = occurred_at or datetime.now(UTC)
if account.last_bind_ok_at is None or moment > account.last_bind_ok_at:
account.last_bind_ok_at = moment
account.role_bind_lock_until = role_bind_lock_until(moment)
account.role_change_available_at = effective_role_change_available_at(
official_at=account.role_change_official_at,
last_bind_ok_at=account.last_bind_ok_at,
)
account.role_change_checked_at = datetime.now(UTC)
account.role_change_source = "local_bind"
return emitted
def record_official_exchange(
+8 -2
View File
@@ -32,6 +32,7 @@ from .account_binding import (
binding_pending_decision,
binding_snapshot,
derive_from_bind_info,
effective_role_change_available_at,
is_bound_act,
role_change_available_at,
set_bind_status,
@@ -1011,9 +1012,14 @@ def _apply_role_result(
account.game_name = role_name
account.game_channel = channel
account.change_role_wait_time = _to_int(role.get("change_role_wait_time"))
account.role_change_available_at = role_change_available_at(
role.get("bind_info")
official_at = role_change_available_at(role.get("bind_info"))
account.role_change_official_at = official_at
account.role_change_checked_at = datetime.now(UTC)
account.role_change_available_at = effective_role_change_available_at(
official_at=official_at,
last_bind_ok_at=account.last_bind_ok_at,
)
account.role_change_source = "official"
elif scope == SCOPE_ESPORTS:
account.esports_game_name = role_name
account.esports_game_channel = channel
+9 -1
View File
@@ -22,6 +22,7 @@ from .account_binding import (
binding_pending_decision,
binding_snapshot,
classify_bind_cooldown,
effective_role_change_available_at,
is_bound_act,
is_pending_binding_role,
is_role_change_cooling,
@@ -408,7 +409,14 @@ class BindMixin:
snapshot["game_channel"].replace(" / ", " · ") or account.game_channel
)
account.change_role_wait_time = snapshot["change_role_wait_time"]
account.role_change_available_at = role_change_available_at(bind_info)
official_at = role_change_available_at(bind_info)
account.role_change_official_at = official_at
account.role_change_checked_at = datetime.now(UTC)
account.role_change_available_at = effective_role_change_available_at(
official_at=official_at,
last_bind_ok_at=account.last_bind_ok_at,
)
account.role_change_source = "official"
if state is not None:
set_bind_status(account, SCOPE_ELITE, state)
account.updated_at = datetime.now(UTC)