fix(fulfillment): 角色 7 天锁命中时转人工

- 角色 pending 时查 7 天内是否已绑定其他斗鱼账号
- 命中不调 actBind,订单转 manual_required
- 时间线记录原账号、绑定时间、可复用时间和占用业务
- 确认前和启动对账补充校验,避免存量订单继续尝试
- 补充同账号、超窗口、确认前和启动对账回归测试
This commit is contained in:
yml
2026-11-24 02:10:07 +08:00
parent efb140cf27
commit ad6c31729d
7 changed files with 519 additions and 1 deletions
@@ -51,6 +51,7 @@ from .repository import (
_load_task,
_load_task_for_update,
)
from .role_lock import ROLE_LOCK_ISSUE, RoleLock, find_role_lock
from .runtime import (
_append_event,
_apply_task_lease_policy,
@@ -335,6 +336,65 @@ def _record_binding_rejection(
)
def _mark_role_locked(
db: Session,
task: PlatformFulfillmentTask,
source: DouyuTask | None,
lock: RoleLock,
) -> bool:
"""角色 7 天内已绑定其他账号:转人工,不调 actBind。"""
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
return False
message = lock.message()
lease = _lease_for_task(db, task)
if source is not None and int(source.id) > int(task.bind_task_id or 0):
# 保留本次待确认任务,详情/时间线能看到尝试的角色;状态已转人工,
# 不会因为 bind_task_id 变化被自动推回 role_ready。
task.bind_task_id = int(source.id)
task.binding_status = "failed"
task.execution_status = "manual_required"
task.issue_code = ROLE_LOCK_ISSUE
task.last_error = message
task.updated_at = _now()
detail = {
"roleName": lock.role_name,
"roleLockAccountId": lock.account_id,
"roleLockAccountName": lock.account_name,
"roleLockBoundAt": lock.bound_at.isoformat(),
"roleLockReusableAt": lock.reusable_at.isoformat(),
"roleLockLeasePurpose": lock.lease_purpose,
"roleLockLeaseBatchId": lock.lease_batch_id,
"allocatedAccountId": task.account_id,
"message": message,
}
if source is not None:
detail["sourceTaskId"] = int(source.id)
detail["sourceTaskType"] = str(source.task_type or "")
_append_event(db, task.id, "binding_role_locked_elsewhere", detail=detail)
_apply_task_lease_policy(db, task, lease=lease)
_emit_order_site_binding_state(db, task.id)
return True
def _block_on_role_lock(
db: Session,
task: PlatformFulfillmentTask,
role_name: str,
source: DouyuTask | None = None,
) -> bool:
"""角色已绑在别的账号上时,把订单转人工;返回是否拦截。"""
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
return False
lock = find_role_lock(
db,
role_name,
current_account_id=task.account_id,
)
if lock is None:
return False
return _mark_role_locked(db, task, source, lock)
def _apply_binding_qr_display(
db: Session,
task: PlatformFulfillmentTask,
@@ -381,6 +441,9 @@ def _apply_binding_task(
"""
if _frozen(task):
return False
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
# 角色 7 天锁只能由人工重置后重新进入流程。
return False
if not task.account_id or not source.account_id:
return False
if int(source.account_id) != int(task.account_id):
@@ -550,6 +613,8 @@ def _apply_binding_task(
and not task.last_error
):
return False
if _block_on_role_lock(db, task, role_name, source):
return True
task.bind_task_id = int(source.id)
task.binding_status = "role_ready"
task.execution_status = "role_ready"
@@ -823,6 +888,16 @@ def reconcile_binding_observations(
latest_pending,
):
changed = True
if not changed and task.binding_status == "role_ready":
# 部署前已经 role_ready 的订单:启动时补一次角色 7 天锁检查。
target_role_name = _order_target_role(db, task)
if target_role_name and _block_on_role_lock(
db,
task,
target_role_name,
current,
):
changed = True
if changed:
reconciled += 1
if reconciled:
@@ -899,6 +974,10 @@ def _attach_pending_binding_task(
return False
if current_id and int(bind_task.id) < current_id:
return False
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
return False
if _block_on_role_lock(db, task, binding.pending_role_name, bind_task):
return True
previous_bind_task_id = task.bind_task_id
task.bind_task_id = int(bind_task.id)
task.binding_status = "role_ready"
@@ -1081,6 +1160,9 @@ def reconcile_binding_state_outbox(
def confirm_binding_sync(db: Session, *, task_id: int, current: User) -> dict[str, Any]:
task = _load_task_for_update(db, task_id)
task = _load_task(db, refresh_binding(db, task_id=task.id)["task"]["id"])
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
# 刷新时已经判定角色被其他账号占用:直接返回人工状态,不再创建确认任务。
return get_task_detail(db, task.id)
if task.binding_status != "role_ready":
raise HTTPException(status_code=409, detail="当前没有待确认的游戏角色")
if not task.account_id or not task.lease_id:
@@ -1095,6 +1177,16 @@ def confirm_binding_sync(db: Session, *, task_id: int, current: User) -> dict[st
else None
)
current_bind_result = (current_bind_task.result or {}) if current_bind_task else {}
# 兼容部署前已经进入 role_ready 的订单:确认前再查一次角色 7 天锁。
target_role_name = _order_target_role(db, task)
if target_role_name and _block_on_role_lock(
db,
task,
target_role_name,
current_bind_task,
):
db.commit()
return get_task_detail(db, task.id)
expected_role_name = str(current_bind_result.get("role_name") or "").strip()
baseline_role_name = str(current_bind_result.get("current_role_name") or "").strip()
batch_id = f"pfconfirm-{task.id}-{uuid.uuid4().hex[:8]}"