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

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
+141 -14
View File
@@ -27,7 +27,14 @@ from datetime import UTC, datetime, timedelta
from sqlalchemy import or_
from sqlalchemy.orm import Session
from ..models import AccountLease, DouyuTask, PlatformFulfillmentTask
from ..models import (
Account,
AccountLease,
DouyuExternalOrder,
DouyuTask,
PlatformFulfillmentTask,
)
from .account_query_filters import ACTIVE_COLLAB_STATUSES
#: 允许把工作台任务挂到订单上的绑定阶段。已 bound / manual_required 的订单
#: 不再接受自动归属,避免工作台的一次手工操作改写已完成订单。
@@ -46,30 +53,59 @@ BINDING_LINKABLE_STATUSES = frozenset(
#: 不在此列:查询只能证明账号当前状态,不能证明某个订单的绑定已经确认。
BINDING_TASK_TYPES = frozenset({"get_bind_qr", "confirm_bind"})
#: 精英工作台会改写兑换资格或消耗积分的写操作任务类型。
WORKBENCH_GOODS_MUTATION_TYPES = frozenset(
{"lock_goods", "pay_locked_order", "exchange_goods"}
)
#: 任务创建时间与租约起点的容差。两者由不同事务写入,允许几秒偏差;
#: 不允许跨租约归属(上一个订单的任务不能算到下一张订单上)。
LEASE_CLOCK_TOLERANCE = timedelta(seconds=5)
def active_account_lease(
db: Session,
account_id: int,
*,
purpose: str | None = None,
) -> AccountLease | None:
"""账号当前生效且未过期的租约;可按 ``purpose`` 过滤。"""
query = db.query(AccountLease).filter(
AccountLease.account_id == int(account_id),
AccountLease.status == "active",
or_(
AccountLease.expires_at.is_(None),
AccountLease.expires_at > datetime.now(UTC),
),
)
if purpose is not None:
query = query.filter(AccountLease.purpose == purpose)
return query.order_by(AccountLease.leased_at.desc(), AccountLease.id.desc()).first()
def active_fulfillment_lease(
db: Session,
account_id: int,
) -> AccountLease | None:
"""账号当前生效的履约租约;没有则返回 ``None``。"""
return (
db.query(AccountLease)
.filter(
AccountLease.account_id == int(account_id),
AccountLease.status == "active",
AccountLease.purpose == "platform_fulfillment",
or_(
AccountLease.expires_at.is_(None),
AccountLease.expires_at > datetime.now(UTC),
),
)
.order_by(AccountLease.leased_at.desc(), AccountLease.id.desc())
.first()
return active_account_lease(db, account_id, purpose="platform_fulfillment")
def active_collab_order_for_account(
db: Session,
account_id: int,
*,
exclude_order_id: int | None = None,
) -> DouyuExternalOrder | None:
"""账号当前未结束、未归档的预售台账订单。"""
query = db.query(DouyuExternalOrder).filter(
DouyuExternalOrder.account_id == int(account_id),
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_STATUSES),
DouyuExternalOrder.archived_at.is_(None),
)
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()
def active_fulfillment_binding_task_id(
@@ -149,10 +185,100 @@ def role_names_match(left: object, right: object) -> bool:
return bool(left_key) and left_key == right_key
def _account_display_label(account: Account) -> str:
return (
str(account.nickname or "").strip()
or str(account.username or "").strip()
or str(account.uid or "").strip()
or f"#{account.id}"
)
def workbench_task_occupation_error(
db: Session,
account: Account,
*,
task_type: str,
handbook_scope: str,
platform_task_id: int | None,
allow_auto_link: bool = False,
) -> str | None:
"""检查工作台无归属任务是否会误改已占用账号的角色或库存。
仅拦截未绑定 ``platform_task_id`` 的精英宝典写操作:
- 绑码/换绑(``get_bind_qr`` / ``confirm_bind``):若未命中可自动归属的
待绑定履约单,禁止在任意活动租约(平台履约、预售台账等)或活跃预售单期间执行;
- 锁单/支付/兑换:账号处于平台履约租约期间,只能由履约订单带 ``platform_task_id`` 发起。
"""
if handbook_scope != "elite" or platform_task_id is not None:
return None
label = _account_display_label(account)
lease = active_account_lease(db, account.id)
if task_type in BINDING_TASK_TYPES:
if allow_auto_link and active_fulfillment_binding_task_id(db, account.id) is not None:
return None
if lease is not None:
if lease.purpose == "platform_fulfillment":
return (
f"账号 {label} 当前有已绑定、人工处理中或已完成的平台履约订单"
f"(批次 {lease.batch_id or '-'}),禁止从工作台发起绑定或换绑"
)
if lease.purpose == "douyu_collab":
return (
f"账号 {label} 当前由斗鱼预售台账占用,"
"禁止从工作台发起绑定或换绑"
)
return (
f"账号 {label} 当前已被业务租约占用({lease.purpose}),"
"禁止从工作台发起绑定或换绑"
)
collab = active_collab_order_for_account(db, account.id)
if collab is not None:
return (
f"账号 {label} 存在未完成的斗鱼预售台账订单({collab.order_no}),"
"禁止从工作台发起绑定或换绑"
)
bound_fulfillment = (
db.query(PlatformFulfillmentTask.id)
.filter(
PlatformFulfillmentTask.account_id == int(account.id),
PlatformFulfillmentTask.scope == "elite",
PlatformFulfillmentTask.execution_status.notin_(
("completed", "cancelled")
),
PlatformFulfillmentTask.binding_status.notin_(
BINDING_LINKABLE_STATUSES
),
)
.first()
)
if bound_fulfillment is not None:
return (
f"账号 {label} 当前有关联的已绑定或待人工处理平台履约单(#{bound_fulfillment[0]}),"
"禁止从工作台发起绑定或换绑"
)
return None
if (
task_type in WORKBENCH_GOODS_MUTATION_TYPES
and lease is not None
and lease.purpose == "platform_fulfillment"
):
return (
f"账号 {label} 当前由平台履约订单占用"
f"(批次 {lease.batch_id or '-'}),请从履约订单内发起锁单或兑换"
)
return None
__all__ = [
"BINDING_LINKABLE_STATUSES",
"BINDING_TASK_TYPES",
"LEASE_CLOCK_TOLERANCE",
"WORKBENCH_GOODS_MUTATION_TYPES",
"active_account_lease",
"active_collab_order_for_account",
"active_fulfillment_binding_task_id",
"active_fulfillment_lease",
"fulfillment_lease_for_task",
@@ -160,4 +286,5 @@ __all__ = [
"normalize_role_name",
"role_names_match",
"source_created_within_lease",
"workbench_task_occupation_error",
]