fix(douyu): coordinate collab and platform account leases

Avoid automatic allocation of accounts with active external collaboration, require explicit manual takeover of platform leases, and add Douyu account identity search.
This commit is contained in:
yml
2026-11-20 00:18:28 +08:00
parent d19809b4ca
commit 85837944d0
12 changed files with 704 additions and 59 deletions
+200 -6
View File
@@ -38,6 +38,7 @@ from ..models import (
PlatformFulfillmentTask,
User,
)
from ..permissions import user_has_permission
from .account_binding import binding_channel, binding_role_from_result
from .account_events_service import (
CATEGORY_OPS,
@@ -132,6 +133,21 @@ STATUS_FLOW: tuple[str, ...] = (
)
SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment"
PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset(
{
"lock_goods",
"pay_locked_order",
"exchange_goods",
"exchange_esports_goods",
"exchange_xpd_goods",
"create_elite_qr",
"create_esports_qr",
"create_gold_qr",
"donate_elite_gift",
"donate_esports_chicken_gift",
"donate_esports_firework_gift",
}
)
STAGE_INDEX = {
STATUS_INCOMPLETE: 0,
@@ -521,6 +537,145 @@ def _platform_lease_matches_source(
)
def _lock_active_lease_for_account(
db: Session,
account_id: int,
) -> AccountLease | None:
return (
db.query(AccountLease)
.filter(
AccountLease.account_id == int(account_id),
AccountLease.status == "active",
)
.with_for_update()
.first()
)
def _platform_task_for_lease(
db: Session,
lease: AccountLease,
) -> PlatformFulfillmentTask | None:
task_id = 0
if lease.batch_id.startswith("pf:"):
try:
task_id = int(lease.batch_id.removeprefix("pf:"))
except ValueError:
task_id = 0
query = db.query(PlatformFulfillmentTask).with_for_update()
if task_id:
task = query.filter(PlatformFulfillmentTask.id == task_id).first()
if task is not None:
return task
return query.filter(PlatformFulfillmentTask.lease_id == int(lease.id)).first()
def _takeover_platform_lease_for_collab(
db: Session,
current: User,
order: DouyuExternalOrder,
lease: AccountLease,
) -> AccountLease:
"""外部协作显式接管平台租约,并在同一事务内把平台任务转人工。"""
if not user_has_permission(current, "platform_fulfillment:use"):
raise CollabError(
"没有接管平台履约账号的权限",
status_code=403,
code="platform_takeover_forbidden",
)
platform_task = _platform_task_for_lease(db, lease)
if platform_task is None:
raise CollabError(
"平台履约任务不存在,请刷新后重试",
status_code=409,
code="account_lease_conflict",
)
if platform_task.execution_status in {"completed", "cancelled"}:
raise CollabError(
f"平台履约任务 #{platform_task.id} 已结束,不能由协作单接管",
status_code=409,
code="platform_task_not_takeoverable",
)
if (
platform_task.account_id != lease.account_id
or platform_task.lease_id != lease.id
):
raise CollabError(
f"平台履约任务 #{platform_task.id} 与账号租约不一致,请先核对",
status_code=409,
code="account_lease_conflict",
)
blocking = (
db.query(DouyuTask)
.filter(
DouyuTask.platform_task_id == int(platform_task.id),
DouyuTask.status.in_(("planned", "pending", "running")),
DouyuTask.task_type.in_(PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES),
)
.order_by(DouyuTask.id.desc())
.first()
)
if blocking is not None:
raise CollabError(
f"平台履约任务 #{platform_task.id} 正在执行资金/兑换操作,请先停止任务",
status_code=409,
code="platform_task_running",
)
from .platform_fulfillment import service as platform_service
platform_service.prepare_platform_task_for_collab_takeover(
db,
task=platform_task,
current=current,
collab_order_no=order.order_no,
)
previous_task_id = int(platform_task.id)
lease.purpose = LEASE_PURPOSE_DOUYU_COLLAB
lease.batch_id = f"collab:{int(lease.account_id)}"[:64]
lease.commodity_id = ""
lease.holder_user_id = int(current.id)
lease.expires_at = None
lease.released_at = None
lease.release_reason = ""
emit(
db,
account_id=int(lease.account_id),
event_type=EVENT_LEASE_ACQUIRED,
title=f"外部协作接管平台履约账号:{order.order_no}",
category=CATEGORY_OPS,
detail={
"lease_id": lease.id,
"order_id": order.id,
"order_no": order.order_no,
"platform_task_id": previous_task_id,
"reason": "collab_manual_takeover",
},
source="lease",
subject_type="order",
subject_key=order.order_no,
actor_type="user",
actor_id=int(current.id),
source_ref=f"account_lease_collab_takeover:{lease.id}",
)
_audit_lease_action(
db,
current,
order=order,
account_id=int(lease.account_id),
action="douyu:collab:lease_takeover",
lease=lease,
detail={
"lease_id": lease.id,
"order_id": order.id,
"order_no": order.order_no,
"platform_task_id": previous_task_id,
"reason": "collab_manual_takeover",
},
)
return lease
def _audit_lease_action(
db: Session,
current: User,
@@ -556,6 +711,7 @@ def _ensure_order_account_lease(
*,
account_id: int | None = None,
force_active: bool = False,
takeover_platform_lease: bool = False,
commit: bool = False,
) -> AccountLease | None:
target_account_id = int(account_id or order.account_id or 0)
@@ -564,14 +720,27 @@ def _ensure_order_account_lease(
if not force_active and not _order_occupies_account(order):
return None
lease = active_lease_for_account(db, target_account_id)
lease = _lock_active_lease_for_account(db, target_account_id)
if lease is not None:
if lease.purpose == LEASE_PURPOSE_DOUYU_COLLAB:
return lease
if takeover_platform_lease:
return _takeover_platform_lease_for_collab(
db,
current,
order,
lease,
)
if _platform_lease_matches_source(db, lease, order):
return lease
platform_task = _platform_task_for_lease(db, lease)
task_label = (
f"平台履约任务 #{platform_task.id}"
if platform_task is not None
else "平台履约任务"
)
raise CollabError(
"该账号当前被平台履约占用,请先释放或转人工后再使用",
f"该账号当前被{task_label}占用,请确认转人工后再使用",
status_code=409,
code="account_lease_conflict",
)
@@ -659,10 +828,24 @@ def _sync_order_lease(
) -> AccountLease | None:
if force_active or _order_occupies_account(order):
if reuse_active_lease:
# 纯状态回写允许沿用平台履约或其他业务的有效占用,不抢占、不转换。
lease = active_lease_for_account(db, order.account_id)
# 纯状态回写只沿用协作租约或同一平台来源的租约;其他业务占用必须显式接管。
lease = _lock_active_lease_for_account(db, int(order.account_id or 0))
if lease is not None:
return lease
if lease.purpose == LEASE_PURPOSE_DOUYU_COLLAB:
return lease
if _platform_lease_matches_source(db, lease, order):
return lease
platform_task = _platform_task_for_lease(db, lease)
task_label = (
f"平台履约任务 #{platform_task.id}"
if platform_task is not None
else "平台履约任务"
)
raise CollabError(
f"该账号当前被{task_label}占用,请先由协作单确认接管",
status_code=409,
code="account_lease_conflict",
)
# 没有任何有效占用的历史异常行仍需补建协作租约,避免自动分配选中。
return _ensure_order_account_lease(
db,
@@ -876,6 +1059,7 @@ def create_order(
remark: str = "",
draft: bool = False,
require_verified: bool = True,
takeover_platform_lease: bool = False,
client: Any | None = None,
commit: bool = True,
) -> DouyuExternalOrder:
@@ -986,7 +1170,13 @@ def create_order(
if order.shop:
ensure_shops_registered(db, current, [order.shop])
_ensure_order_account_lease(db, current, order, force_active=True)
_ensure_order_account_lease(
db,
current,
order,
force_active=True,
takeover_platform_lease=takeover_platform_lease,
)
db.add(order)
db.flush()
record_audit(
@@ -1352,6 +1542,7 @@ def update_order(
payload: Mapping[str, Any],
*,
expected_version: int | None = None,
takeover_platform_lease: bool = False,
client: Any | None = None,
commit: bool = True,
) -> dict[str, Any]:
@@ -1426,6 +1617,7 @@ def update_order(
order,
account_id=int(account.id),
force_active=order.status in COLLAB_LEASE_STATUSES,
takeover_platform_lease=takeover_platform_lease,
)
apply_probe_snapshot(order, account, probe)
changes["account_id"] = {
@@ -1483,6 +1675,7 @@ def set_account(
username: str,
*,
expected_version: int | None = None,
takeover_platform_lease: bool = False,
client: Any | None = None,
commit: bool = True,
) -> dict[str, Any]:
@@ -1507,6 +1700,7 @@ def set_account(
order,
account_id=int(account.id),
force_active=order.status in COLLAB_LEASE_STATUSES,
takeover_platform_lease=takeover_platform_lease,
)
apply_probe_snapshot(order, account, probe)
order.updated_by = current.id