修复(履约与分配): 封堵工作台换绑、预售共号与分配中心越权占用

This commit is contained in:
yml
2026-11-27 13:42:28 +08:00
parent 9100991cb1
commit 5006367217
13 changed files with 1031 additions and 34 deletions
+97 -2
View File
@@ -27,6 +27,7 @@ from core.douyu import DouyuActivityClient
from ..models import (
Account,
AccountEvent,
AccountLease,
AuditLog,
DouyuCollabGift,
@@ -52,6 +53,7 @@ from .account_query_filters import ACTIVE_COLLAB_STATUSES
from .account_sync_runner import WORK_STOREHOME_SYNC
from .account_work_queue import enqueue_account_work
from .audit_service import record_account_action, record_audit
from .binding_ownership import role_names_match
from .business_events import record_account_query
from .collab_common import (
count_archived,
@@ -1030,6 +1032,7 @@ def update_order(
account_id=int(account.id),
force_active=order.status in COLLAB_LEASE_STATUSES,
takeover_platform_lease=takeover_platform_lease,
incoming_role=_text(probe.get("bind_game_role")),
)
apply_probe_snapshot(
db,
@@ -1134,6 +1137,7 @@ def set_account(
account_id=int(account.id),
force_active=order.status in COLLAB_LEASE_STATUSES,
takeover_platform_lease=takeover_platform_lease,
incoming_role=_text(probe.get("bind_game_role")),
)
apply_probe_snapshot(
db,
@@ -1232,7 +1236,68 @@ def _status_audit_target(order: DouyuExternalOrder) -> str:
return f"douyu_external_order:{order.id}"
def _collab_order_role_mismatch_reason(
db: Session,
order: DouyuExternalOrder,
account: Account | None = None,
*,
records: Sequence[Mapping[str, Any]] = (),
) -> str | None:
"""校验订单目标角色与账号当前实际绑定角色、兑换记录角色是否一致。"""
expected = _text(order.bind_game_role)
if not expected:
return None
acc = account or account_by_id(db, order.account_id) or account_by_username(
db, order.account_username
)
if acc is not None:
current_role = _text(acc.game_name)
if current_role and not role_names_match(expected, current_role):
return (
f"账号当前绑定角色({current_role})"
f"与订单目标角色({expected})不一致"
)
if str(acc.role_bind_status or "") == "unbound":
return f"账号当前未绑定任何游戏角色,与订单目标角色({expected})不一致"
if isinstance(order.bind_snapshot_at, datetime):
rebind_event = (
db.query(AccountEvent)
.filter(
AccountEvent.account_id == int(acc.id),
AccountEvent.event_type == "bind_ok",
AccountEvent.subject_type == "elite_role",
AccountEvent.occurred_at > order.bind_snapshot_at,
)
.order_by(AccountEvent.occurred_at.desc(), AccountEvent.id.desc())
.first()
)
if (
rebind_event is not None
and _text(rebind_event.subject_key)
and not role_names_match(expected, rebind_event.subject_key)
):
return (
f"账号在快照后已换绑为({_text(rebind_event.subject_key)}),"
f"与订单目标角色({expected})不一致"
)
for record in records:
if not isinstance(record, Mapping):
continue
record_role = _text(
record.get("roleName")
or record.get("role_name")
or record.get("bindRoleName")
)
if record_role and not role_names_match(expected, record_role):
return (
f"兑换记录角色({record_role})"
f"与订单目标角色({expected})不一致"
)
return None
def _assert_ready_for_recharge(
db: Session,
order: DouyuExternalOrder,
*,
entering_waiting: bool,
@@ -1253,6 +1318,13 @@ def _assert_ready_for_recharge(
f"{action}还缺:{'、'.join(missing)}",
code="recharge_precondition",
)
mismatch = _collab_order_role_mismatch_reason(db, order)
if mismatch:
raise CollabError(
f"{mismatch},禁止推进或完成订单,请先核实角色或切换为账号角色异常",
status_code=409,
code="role_mismatch",
)
def _restore_void_status(
@@ -1277,6 +1349,7 @@ def _restore_void_status(
target_index = STAGE_INDEX.get(target)
if target_index is not None and target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
_assert_ready_for_recharge(
db,
order,
entering_waiting=target == STATUS_TO_RECHARGE,
allow_missing_role=target == STATUS_RECHARGED,
@@ -1462,6 +1535,7 @@ def set_status(
raise CollabError("回退状态必须显式确认", code="rollback_required")
if target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
_assert_ready_for_recharge(
db,
order,
entering_waiting=target == STATUS_TO_RECHARGE,
allow_missing_role=target == STATUS_RECHARGED,
@@ -1738,6 +1812,7 @@ def query_activity(
matched = 0
matched_records: list[dict[str, Any]] = []
role_mismatch: str | None = None
now = _now()
if not include_records:
verdict = VERDICT_UNKNOWN
@@ -1752,8 +1827,18 @@ def query_activity(
matched_records = _matching_exchange_records(loaded_records, order)
matched = len(matched_records)
if matched:
verdict = VERDICT_SUCCESS
message = f"兑换记录命中「{order.gift_name}」{matched} 条"
role_mismatch = _collab_order_role_mismatch_reason(
db, order, account, records=matched_records
)
if role_mismatch:
verdict = VERDICT_FAILED
message = (
f"兑换记录命中「{order.gift_name}」{matched} 条,"
f"但{role_mismatch},禁止自动标记成功"
)
else:
verdict = VERDICT_SUCCESS
message = f"兑换记录命中「{order.gift_name}」{matched} 条"
elif order.status not in (STATUS_SCRIPTED, STATUS_FAILED):
verdict = VERDICT_UNKNOWN
message = "订单当前状态不再由兑换记录自动判定"
@@ -1844,6 +1929,16 @@ def query_activity(
expected_version=None,
commit=False,
)
elif order.status in (STATUS_SCRIPTED, STATUS_FAILED) and role_mismatch:
set_status(
db,
current,
order,
status=STATUS_ROLE_ERROR,
reason=message,
expected_version=None,
commit=False,
)
elif order.status == STATUS_SCRIPTED and verdict == VERDICT_FAILED:
set_status(
db,