perf(fulfillment): 优化本地能力选号查询与索引

This commit is contained in:
yml
2026-11-23 18:53:06 +08:00
parent d5422b0ecd
commit 7e0d27927c
23 changed files with 1624 additions and 95 deletions
@@ -44,6 +44,20 @@ from .runtime import (
)
from .views import get_task_detail
BINDING_SYNC_TASK_TYPES = frozenset(
{
"get_bind_qr",
"query_game_name",
"confirm_bind",
"prepare_esports_bind",
"query_esports_game_name",
"confirm_esports_bind",
"prepare_xpd_bind",
"query_xpd_game_name",
"confirm_xpd_bind",
}
)
def start_binding_sync(
db: Session,
@@ -277,7 +291,86 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
]
for task_id in task_ids:
refresh_binding(db, task_id=task_id)
return len(task_ids)
douyu_task = db.get(DouyuTask, int(douyu_task_id))
if douyu_task is None or douyu_task.task_type not in BINDING_SYNC_TASK_TYPES:
return len(task_ids)
return len(task_ids) + sync_active_task_from_account_binding(
db,
account_id=int(douyu_task.account_id),
scope=str(douyu_task.handbook_scope or ""),
source_task_id=int(douyu_task.id),
)
def sync_active_task_from_account_binding(
db: Session,
*,
account_id: int,
scope: str,
source_task_id: int | None = None,
) -> int:
"""账号权威绑定已确认时,把当前活跃履约任务同步到 bound。
精英宝典工作台发出的确认任务不挂在履约任务上,因此不能只看
``PlatformFulfillmentTask.bind_task_id``。账号上的绑定状态才是最终事实。
只在任务已经识别出待确认角色后同步,避免把账号历史绑定误当成当前订单结果。
"""
if scope != "elite":
return 0
account = db.get(Account, int(account_id))
if account is None or account.role_bind_status != "bound":
return 0
task = (
db.query(PlatformFulfillmentTask)
.join(AccountLease, AccountLease.id == PlatformFulfillmentTask.lease_id)
.filter(
PlatformFulfillmentTask.account_id == int(account_id),
PlatformFulfillmentTask.scope == "elite",
PlatformFulfillmentTask.binding_status.in_(("role_ready", "confirming")),
PlatformFulfillmentTask.execution_status.notin_(
("completed", "cancelled")
),
AccountLease.status == "active",
)
.order_by(
PlatformFulfillmentTask.updated_at.desc(),
PlatformFulfillmentTask.id.desc(),
)
.with_for_update()
.first()
)
if task is None:
return 0
task.binding_status = "bound"
task.execution_status = "bound"
task.issue_code = ""
task.last_error = ""
task.updated_at = _now()
_append_event(
db,
task.id,
"binding_synced_from_account",
detail={
"accountId": int(account.id),
"accountName": account.nickname or account.username,
"roleName": str(account.game_name or ""),
"gameChannel": str(account.game_channel or ""),
"roleChangeAvailableAt": (
account.role_change_available_at.isoformat()
if account.role_change_available_at
else None
),
"sourceTaskId": source_task_id,
},
)
_apply_task_lease_policy(db, task)
_emit_order_site_binding_state(db, task.id)
db.commit()
return 1
def requires_account_binding_sync(task_type: str) -> bool:
return str(task_type or "") in BINDING_SYNC_TASK_TYPES
def reconcile_binding_state_outbox(