291 lines
10 KiB
Python
291 lines
10 KiB
Python
"""绑定任务的归属判定:一条绑定任务到底属于哪个履约订单。
|
||
|
||
## 为什么需要这个模块
|
||
|
||
精英宝典工作台只关心「这条任务自己的结果」,所以任务和账号之间不需要归属关系。
|
||
直播账号履约不一样:一个订单会占用一个账号一段时间,期间账号上可能出现多条
|
||
绑定任务(订单自己创建的、运营从精英宝典工作台创建的、上一个订单遗留下来的)。
|
||
以前履约层靠「账号 + 最新 id + 租约 active」去猜一条任务属于哪张订单,猜错就会
|
||
把别人的绑定结果投影到当前订单上。
|
||
|
||
这里把归属变成显式事实:
|
||
|
||
1. 履约订单自己创建的绑定任务在创建时就带 ``platform_task_id``;
|
||
2. 运营从精英宝典工作台为**已租给履约订单**的账号创建绑码/确认任务时,
|
||
创建入口调用 :func:`active_fulfillment_binding_task_id` 把订单 id 盖到任务上;
|
||
3. 只有带 ``platform_task_id`` 的任务才是订单的权威证据;
|
||
4. 没有归属的历史任务只能走 ``binding.py`` 里受严格限制的兼容路径。
|
||
|
||
归属一确定,剩下的事情就简单了:阶段、租约、角色目标都是可验证的,不需要
|
||
再猜「最新任务是不是本次扫码」。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import UTC, datetime, timedelta
|
||
|
||
from sqlalchemy import or_
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..models import (
|
||
Account,
|
||
AccountLease,
|
||
DouyuExternalOrder,
|
||
DouyuTask,
|
||
PlatformFulfillmentTask,
|
||
)
|
||
from .account_query_filters import ACTIVE_COLLAB_STATUSES
|
||
|
||
#: 允许把工作台任务挂到订单上的绑定阶段。已 bound / manual_required 的订单
|
||
#: 不再接受自动归属,避免工作台的一次手工操作改写已完成订单。
|
||
BINDING_LINKABLE_STATUSES = frozenset(
|
||
{
|
||
"not_started",
|
||
"allocated",
|
||
"preparing",
|
||
"qr_pending",
|
||
"waiting_bind",
|
||
"role_ready",
|
||
}
|
||
)
|
||
|
||
#: 绑定任务类型。查询类任务(query_game_name / query_change_bind_time)故意
|
||
#: 不在此列:查询只能证明账号当前状态,不能证明某个订单的绑定已经确认。
|
||
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 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(
|
||
db: Session,
|
||
account_id: int,
|
||
) -> int | None:
|
||
"""账号当前租约对应的、仍可接收绑定证据的履约任务 id。
|
||
|
||
只用于任务创建入口主动打归属;``None`` 表示这是一次与履约无关的工作台操作。
|
||
"""
|
||
lease = active_fulfillment_lease(db, account_id)
|
||
if lease is None:
|
||
return None
|
||
task = (
|
||
db.query(PlatformFulfillmentTask)
|
||
.filter(
|
||
PlatformFulfillmentTask.lease_id == int(lease.id),
|
||
PlatformFulfillmentTask.account_id == int(account_id),
|
||
PlatformFulfillmentTask.scope == "elite",
|
||
PlatformFulfillmentTask.execution_status.notin_(
|
||
("completed", "cancelled", "manual_required")
|
||
),
|
||
PlatformFulfillmentTask.binding_status.in_(BINDING_LINKABLE_STATUSES),
|
||
)
|
||
.order_by(
|
||
PlatformFulfillmentTask.updated_at.desc(),
|
||
PlatformFulfillmentTask.id.desc(),
|
||
)
|
||
.first()
|
||
)
|
||
return int(task.id) if task is not None else None
|
||
|
||
|
||
def fulfillment_lease_for_task(
|
||
db: Session,
|
||
task: PlatformFulfillmentTask,
|
||
) -> AccountLease | None:
|
||
"""订单自己的租约(不按账号重新查,避免订单换号后拿到别的账号租约)。"""
|
||
if not task.lease_id:
|
||
return None
|
||
return db.query(AccountLease).filter(AccountLease.id == int(task.lease_id)).first()
|
||
|
||
|
||
def lease_is_active(lease: AccountLease | None) -> bool:
|
||
if lease is None or lease.status != "active":
|
||
return False
|
||
expires_at = lease.expires_at
|
||
return not (
|
||
isinstance(expires_at, datetime) and expires_at <= datetime.now(UTC)
|
||
)
|
||
|
||
|
||
def source_created_within_lease(
|
||
lease: AccountLease | None,
|
||
source: DouyuTask | None,
|
||
) -> bool:
|
||
"""任务是否创建于本次租约期间。
|
||
|
||
这是防止「上一个订单的迟到任务结算当前订单」的硬约束。
|
||
"""
|
||
if lease is None or source is None:
|
||
return False
|
||
created_at = source.created_at
|
||
leased_at = lease.leased_at
|
||
if not isinstance(created_at, datetime) or not isinstance(leased_at, datetime):
|
||
return False
|
||
return created_at >= leased_at - LEASE_CLOCK_TOLERANCE
|
||
|
||
|
||
def normalize_role_name(value: object) -> str:
|
||
return str(value or "").strip().casefold()
|
||
|
||
|
||
def role_names_match(left: object, right: object) -> bool:
|
||
left_key = normalize_role_name(left)
|
||
right_key = normalize_role_name(right)
|
||
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",
|
||
"lease_is_active",
|
||
"normalize_role_name",
|
||
"role_names_match",
|
||
"source_created_within_lease",
|
||
"workbench_task_occupation_error",
|
||
]
|