fix(fulfillment): 拆分绑定事实并复用同账号角色

- 详情页增加 QYOOB / cjm / 7 天锁绑定判定面板
- 订单目标只认动作 alias pending,cjm-only 不再推进订单
- 同账号同角色直接复用已有绑定,不再等待二维码或调 actBind
- 补账号侧 7 天锁本地检查
- 补充绑定证据、同对复用和 7 天锁回归测试
This commit is contained in:
yml
2026-11-24 13:14:50 +08:00
parent ad6c31729d
commit 5b6d146f93
10 changed files with 1221 additions and 27 deletions
@@ -0,0 +1,278 @@
"""履约绑定判定的只读证据。
把一个 DouyuTask 的结果拆成互不混淆的几层事实:
- ``actionCurrent``:动作 alias(QYOOB)上已经 bindAct=1 的角色;
- ``actionPending``:动作 alias 上 bindAct=0、可确认的本次待确认角色;
- ``legacyLatest``:legacy(cjm)上的最新角色,只能提示,不能单独确认;
- ``accountCurrentRole``:账号表当前生效角色(覆盖式,不代表本次);
- ``roleLock`` / ``accountLock``:7 天锁,是“能不能绑”的资格,不是角色识别。
这个模块只读,不改变订单状态。页面用它把“新扫的 / 旧的 / cjm 的 / 7 天锁的”
分开显示,避免再把它们混成一个“待确认角色”。
"""
from __future__ import annotations
from datetime import datetime
from typing import Any
from sqlalchemy.orm import Session
from ...models import Account, DouyuTask, PlatformFulfillmentTask
from ..account_binding import pending_binding_role_from_result
from .role_lock import AccountLock, RoleLock, find_account_lock, find_role_lock
def _text(value: Any) -> str:
return str(value or "").strip()
def _bool(value: Any) -> bool:
return bool(value)
def _channel(area: str, plat: str) -> str:
return " / ".join(value for value in (area, plat) if value)
def _candidate(raw: dict[str, Any]) -> dict[str, Any]:
alias = _text(raw.get("act_alias") or raw.get("alias"))
area = _text(raw.get("area_name"))
plat = _text(raw.get("plat_name"))
return {
"alias": alias,
"roleName": _text(raw.get("role_name")),
"areaName": area,
"platName": plat,
"channel": _channel(area, plat),
"bindAct": _bool(raw.get("is_bound_act") or raw.get("bind_act")),
"bindRole": _bool(raw.get("is_bound_role") or raw.get("bind_role")),
"needBindAct": _bool(raw.get("need_bind_act")),
"actionableAlias": _bool(
raw.get("actionable_alias") or raw.get("actionableAlias")
),
"confirmable": _bool(raw.get("confirmable")),
"reason": _text(raw.get("reason")),
}
def _candidate_from_info(
info: Any,
action_aliases: list[str],
) -> dict[str, Any] | None:
if not isinstance(info, dict):
return None
raw = {
**info,
"act_alias": info.get("act_alias") or info.get("alias"),
}
candidate = _candidate(raw)
candidate["actionableAlias"] = candidate["alias"] in action_aliases
return candidate if candidate["roleName"] else None
def _lock_out(lock: RoleLock | AccountLock | None) -> dict[str, Any] | None:
if lock is None:
return None
data = {
"roleName": lock.role_name,
"accountId": lock.account_id,
"accountName": lock.account_name,
"boundAt": lock.bound_at.isoformat(),
"reusableAt": lock.reusable_at.isoformat(),
"message": lock.message(),
}
if isinstance(lock, RoleLock):
data["leasePurpose"] = lock.lease_purpose
data["leaseBatchId"] = lock.lease_batch_id
return data
def _account_current_candidate(
account: Account | None,
action_alias: str,
) -> dict[str, Any] | None:
if account is None or not _text(account.game_name):
return None
if str(account.role_bind_status or "") != "bound":
return None
channel = _text(account.game_channel)
area, _, plat = channel.partition(" · ")
return {
"alias": action_alias,
"roleName": _text(account.game_name),
"areaName": area,
"platName": plat,
"channel": channel,
"bindAct": True,
"bindRole": True,
"needBindAct": False,
"actionableAlias": bool(action_alias),
"confirmable": False,
"reason": "account_current_bound",
}
def build_binding_evidence(
db: Session,
task: PlatformFulfillmentTask,
bind_task: DouyuTask | None,
account: Account | None,
*,
now: datetime | None = None,
) -> dict[str, Any] | None:
"""构建页面用的绑定判定快照;无绑定任务时返回 None。"""
if bind_task is None:
return None
result = bind_task.result if isinstance(bind_task.result, dict) else {}
action_aliases = [
_text(value)
for value in (result.get("action_act_aliases") or [])
if _text(value)
]
candidates = [
_candidate(raw)
for raw in (result.get("bind_candidates") or [])
if isinstance(raw, dict)
]
candidates = [item for item in candidates if item["alias"]]
if not action_aliases:
action_aliases = [
item["alias"] for item in candidates if item["actionableAlias"]
]
selected_info = result.get("bind_info") or result.get("before_bind_info")
selected_info_candidate = _candidate_from_info(selected_info, action_aliases)
if not candidates and selected_info_candidate is not None:
candidates = [selected_info_candidate]
selected_alias = _text(
result.get("act_alias")
or (selected_info or {}).get("act_alias")
or (candidates[0]["alias"] if candidates else "")
)
decision = result.get("bind_decision")
decision = decision if isinstance(decision, dict) else {}
selected_actionable = bool(
decision.get("actionable_alias")
or (selected_alias and selected_alias in action_aliases)
)
action_current = next(
(
item
for item in candidates
if item["alias"] in action_aliases
and item["bindAct"]
and item["roleName"]
),
None,
)
if action_current is None:
action_current = _account_current_candidate(
account,
action_aliases[0] if action_aliases else "",
)
action_pending = next(
(
item
for item in candidates
if item["alias"] in action_aliases
and item["roleName"]
and not item["bindAct"]
),
None,
)
if (
action_pending is None
and selected_info_candidate is not None
and selected_info_candidate["alias"] in action_aliases
and selected_info_candidate["roleName"]
and not selected_info_candidate["bindAct"]
):
action_pending = selected_info_candidate
legacy_latest = next(
(
item
for item in candidates
if item["alias"] not in action_aliases and item["roleName"]
),
None,
)
if (
legacy_latest is None
and selected_info_candidate is not None
and selected_info_candidate["alias"] not in action_aliases
and selected_info_candidate["roleName"]
):
legacy_latest = selected_info_candidate
pending_role = pending_binding_role_from_result(result)
scanned_role = _text(
decision.get("role_name")
or result.get("role_name")
or (legacy_latest or {}).get("roleName")
or pending_role
)
target_role = pending_role if selected_actionable else ""
account_current_role = _text(account.game_name) if account is not None else ""
same_pair = bool(
scanned_role
and account_current_role
and scanned_role == account_current_role
and action_current is not None
and action_current.get("bindAct")
and action_current.get("roleName") == scanned_role
)
bound = str(bind_task.status or "") == "success" and bool(
result.get("bind_confirmed")
)
confirmable = bool(
target_role
and str(bind_task.status or "") == "success"
and task.binding_status == "role_ready"
and not bound
)
role_lock = (
find_role_lock(
db,
scanned_role,
current_account_id=task.account_id,
now=now,
)
if scanned_role
else None
)
account_lock = find_account_lock(
db,
task.account_id,
scanned_role,
now=now,
)
return {
"sourceTaskId": int(bind_task.id),
"sourceTaskType": str(bind_task.task_type or ""),
"sourceTaskStatus": str(bind_task.status or ""),
"actionAliases": action_aliases,
"selectedAlias": selected_alias,
"selectedActionable": selected_actionable,
"decisionReason": _text(
decision.get("reason") or result.get("bind_phase")
),
"confirmable": confirmable,
"targetRole": target_role,
"scannedRole": scanned_role,
"samePair": same_pair,
"actionCurrent": action_current,
"actionPending": action_pending,
"legacyLatest": legacy_latest,
"accountCurrentRole": account_current_role,
"accountBindStatus": (
str(account.role_bind_status or "") if account is not None else ""
),
"roleLock": _lock_out(role_lock),
"accountLock": _lock_out(account_lock),
}
__all__ = ["build_binding_evidence"]