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

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
+108 -4
View File
@@ -8,6 +8,7 @@ from typing import Any
from sqlalchemy.orm import Session
from ..models import (
Account,
AccountLease,
DouyuExternalOrder,
DouyuTask,
@@ -21,11 +22,15 @@ from .account_events_service import (
emit,
)
from .audit_service import record_audit
from .binding_ownership import role_names_match
from .douyu_collab_common import (
COLLAB_LEASE_STATUSES,
LEASE_PURPOSE_DOUYU_COLLAB,
PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES,
SOURCE_TYPE_PLATFORM_FULFILLMENT,
STATUS_FAILED,
STATUS_POINTS_ERROR,
STATUS_ROLE_ERROR,
CollabError,
)
from .platform_fulfillment.account_selector import (
@@ -43,12 +48,12 @@ def _order_occupies_account(order: DouyuExternalOrder) -> bool:
)
def _active_collab_order(
def _active_collab_orders(
db: Session,
account_id: int,
*,
exclude_order_id: int | None = None,
) -> DouyuExternalOrder | None:
) -> list[DouyuExternalOrder]:
query = db.query(DouyuExternalOrder).filter(
DouyuExternalOrder.account_id == int(account_id),
DouyuExternalOrder.status.in_(COLLAB_LEASE_STATUSES),
@@ -56,7 +61,88 @@ def _active_collab_order(
)
if exclude_order_id is not None:
query = query.filter(DouyuExternalOrder.id != int(exclude_order_id))
return query.order_by(DouyuExternalOrder.updated_at.desc()).first()
return query.order_by(DouyuExternalOrder.updated_at.desc()).all()
def _active_collab_order(
db: Session,
account_id: int,
*,
exclude_order_id: int | None = None,
) -> DouyuExternalOrder | None:
orders = _active_collab_orders(
db,
account_id,
exclude_order_id=exclude_order_id,
)
return orders[0] if orders else None
def _collab_order_expected_role(
db: Session,
order: DouyuExternalOrder,
*,
incoming_role: str = "",
) -> str:
explicit = str(incoming_role or order.bind_game_role or "").strip()
if explicit:
return explicit
if (
order.source_type == SOURCE_TYPE_PLATFORM_FULFILLMENT
and order.source_task_id
):
task = db.get(PlatformFulfillmentTask, int(order.source_task_id))
if task is not None and task.customer_role_name:
return str(task.customer_role_name or "").strip()
return ""
def _assert_collab_account_role_compatible(
db: Session,
order: DouyuExternalOrder,
target_account_id: int,
*,
incoming_role: str = "",
) -> None:
"""防止不同游戏角色/客户的预售单共享同一个斗鱼账号,或复用已换绑的账号。"""
exclude_id = int(order.id) if order.id else None
candidate_role = _collab_order_expected_role(
db,
order,
incoming_role=incoming_role,
)
existing_orders = _active_collab_orders(
db,
target_account_id,
exclude_order_id=exclude_id,
)
if candidate_role:
for existing in existing_orders:
existing_role = _collab_order_expected_role(db, existing)
if existing_role and not role_names_match(existing_role, candidate_role):
raise CollabError(
f"该账号已有进行中的预售单 {existing.order_no}"
f"(绑定角色:{existing_role}),与当前角色({candidate_role})"
"不一致,禁止共用同一账号",
status_code=409,
code="collab_role_conflict",
)
if (
not str(incoming_role or "").strip()
and str(order.bind_game_role or "").strip()
and str(order.status or "")
not in {STATUS_FAILED, STATUS_POINTS_ERROR, STATUS_ROLE_ERROR}
):
account = db.get(Account, int(target_account_id))
current_role = str(account.game_name or "").strip() if account else ""
order_bound_role = str(order.bind_game_role or "").strip()
if current_role and not role_names_match(current_role, order_bound_role):
raise CollabError(
f"账号当前绑定角色({current_role})与预售单绑定角色"
f"({order_bound_role})不一致,禁止继续占用该账号",
status_code=409,
code="collab_role_conflict",
)
def _platform_lease_matches_source(
@@ -256,6 +342,7 @@ def _ensure_order_account_lease(
account_id: int | None = None,
force_active: bool = False,
takeover_platform_lease: bool = False,
incoming_role: str = "",
commit: bool = False,
) -> AccountLease | None:
target_account_id = int(account_id or order.account_id or 0)
@@ -264,6 +351,13 @@ def _ensure_order_account_lease(
if not force_active and not _order_occupies_account(order):
return None
_assert_collab_account_role_compatible(
db,
order,
target_account_id,
incoming_role=incoming_role,
)
lease = _lock_active_lease_for_account(db, target_account_id)
if lease is not None:
if lease.purpose == LEASE_PURPOSE_DOUYU_COLLAB:
@@ -368,11 +462,20 @@ def _sync_order_lease(
reason: str,
force_active: bool = False,
reuse_active_lease: bool = False,
incoming_role: str = "",
commit: bool = False,
) -> AccountLease | None:
if force_active or _order_occupies_account(order):
if reuse_active_lease:
lease = _lock_active_lease_for_account(db, int(order.account_id or 0))
target_account_id = int(order.account_id or 0)
if target_account_id:
_assert_collab_account_role_compatible(
db,
order,
target_account_id,
incoming_role=incoming_role,
)
lease = _lock_active_lease_for_account(db, target_account_id)
if lease is not None:
if lease.purpose == LEASE_PURPOSE_DOUYU_COLLAB:
return lease
@@ -394,6 +497,7 @@ def _sync_order_lease(
current,
order,
force_active=force_active,
incoming_role=incoming_role,
commit=commit,
)
_release_order_account_lease(