fix(fulfillment): 修复历史账号绑定覆盖当前待确认角色

This commit is contained in:
yml
2026-11-23 19:42:38 +08:00
parent 17ec07b63a
commit 0a32f24fa8
7 changed files with 347 additions and 9 deletions
@@ -501,6 +501,10 @@
"20261123_0125_douyu_collab_shop_aliases.py": {
"revision": "20261123_0125",
"sha256": "ff11ab9c735043d95504c1d52ddfeb8db04068a8027e0e1253ec45330f227bad"
},
"20261123_0126_repair_false_binding_sync.py": {
"revision": "20261123_0126",
"sha256": "86c201d2298845f7f3eb3e733760fe46911638dc1ef046d4518c02172cfaba6f"
}
}
}
@@ -0,0 +1,112 @@
"""repair fulfillment tasks promoted by historical account bindings.
Revision ID: 20261123_0126
Revises: 20261123_0125
"""
from __future__ import annotations
import json
from datetime import UTC, datetime
import sqlalchemy as sa
from alembic import op
revision: str = "20261123_0126"
down_revision: str | None = "20261123_0125"
branch_labels: str | None = None
depends_on: str | None = None
def upgrade() -> None:
bind = op.get_bind()
rows = bind.execute(
sa.text(
"""
SELECT DISTINCT
t.id AS task_id,
a.id AS account_id,
a.username,
a.nickname,
a.game_name AS bound_role_name,
d.id AS bind_task_id,
JSON_UNQUOTE(JSON_EXTRACT(d.result, '$.role_name')) AS pending_role_name,
JSON_UNQUOTE(JSON_EXTRACT(d.result, '$.game_channel')) AS pending_channel
FROM platform_fulfillment_tasks AS t
JOIN accounts AS a ON a.id = t.account_id
JOIN douyu_tasks AS d ON d.id = t.bind_task_id
JOIN platform_fulfillment_events AS e
ON e.task_id = t.id
AND e.event_type = 'binding_synced_from_account'
WHERE t.binding_status = 'bound'
AND t.execution_status = 'bound'
AND d.task_type IN ('get_bind_qr', 'query_game_name')
AND JSON_EXTRACT(d.result, '$.bind_ready_for_confirm') = true
AND COALESCE(JSON_EXTRACT(d.result, '$.bind_confirmed'), false) = false
AND TRIM(COALESCE(a.game_name, '')) <> TRIM(
COALESCE(
JSON_UNQUOTE(JSON_EXTRACT(d.result, '$.role_name')),
''
)
)
"""
)
).mappings().all()
now = datetime.now(UTC).replace(tzinfo=None)
for row in rows:
task_id = int(row["task_id"])
bind.execute(
sa.text(
"""
UPDATE platform_fulfillment_tasks
SET binding_status = 'role_ready',
execution_status = 'role_ready',
issue_code = '',
last_error = '',
updated_at = :now
WHERE id = :task_id
"""
),
{"task_id": task_id, "now": now},
)
detail = {
"accountId": int(row["account_id"]),
"accountName": str(row["nickname"] or row["username"] or ""),
"bindTaskId": int(row["bind_task_id"]),
"previousBindingStatus": "bound",
"correctedBindingStatus": "role_ready",
"boundRoleName": str(row["bound_role_name"] or ""),
"pendingRoleName": str(row["pending_role_name"] or ""),
"pendingChannel": str(row["pending_channel"] or ""),
"reason": "historical_account_binding_not_current_order_confirmation",
}
bind.execute(
sa.text(
"""
INSERT INTO platform_fulfillment_events (
task_id,
event_type,
actor_type,
actor_id,
detail_json,
created_at
) VALUES (
:task_id,
'binding_false_confirmation_reverted',
'system',
'',
:detail_json,
:created_at
)
"""
),
{
"task_id": task_id,
"detail_json": json.dumps(detail, ensure_ascii=False),
"created_at": now,
},
)
def downgrade() -> None:
"""数据修复不回退,避免再次用历史账号绑定覆盖当前待确认角色。"""
@@ -57,6 +57,13 @@ BINDING_SYNC_TASK_TYPES = frozenset(
"confirm_xpd_bind",
}
)
ACCOUNT_CONFIRMATION_TASK_TYPES = frozenset(
{
"confirm_bind",
"confirm_esports_bind",
"confirm_xpd_bind",
}
)
def start_binding_sync(
@@ -294,6 +301,15 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
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)
result = douyu_task.result or {}
if (
douyu_task.task_type not in ACCOUNT_CONFIRMATION_TASK_TYPES
or result.get("bind_confirmed") is not True
):
# 查询/出码任务只能证明“账号历史绑定是什么”,不能证明本次订单已经确认。
# 只有明确执行成功并回查到 bind_confirmed 的确认任务,才允许把账号权威
# 绑定投影到履约任务。
return len(task_ids)
return len(task_ids) + sync_active_task_from_account_binding(
db,
account_id=int(douyu_task.account_id),
@@ -317,6 +333,17 @@ def sync_active_task_from_account_binding(
"""
if scope != "elite":
return 0
source_task = (
db.get(DouyuTask, int(source_task_id))
if source_task_id is not None
else None
)
if (
source_task is None
or source_task.task_type not in ACCOUNT_CONFIRMATION_TASK_TYPES
or (source_task.result or {}).get("bind_confirmed") is not True
):
return 0
account = db.get(Account, int(account_id))
if account is None or account.role_bind_status != "bound":
return 0
@@ -341,6 +368,7 @@ def sync_active_task_from_account_binding(
)
if task is None:
return 0
task.bind_task_id = int(source_task.id)
task.binding_status = "bound"
task.execution_status = "bound"
task.issue_code = ""
@@ -362,8 +362,11 @@ def _task_out(
)
collab_order = resolved.collab_orders.get(int(task.id))
bind_result = (bind_task.result or {}) if bind_task else {}
bind_result_confirmed = bool(bind_result.get("bind_confirmed"))
account_binding_confirmed = bool(
account is not None and account.role_bind_status == "bound"
bind_result_confirmed
and account is not None
and account.role_bind_status == "bound"
)
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
effective_last_error = _effective_task_message(task, effective_issue)
@@ -635,8 +638,11 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
)
bind_result = (bind_task.result or {}) if bind_task else {}
account = db.get(Account, int(task.account_id)) if task.account_id else None
bind_result_confirmed = bool(bind_result.get("bind_confirmed"))
account_binding_confirmed = bool(
account is not None and account.role_bind_status == "bound"
bind_result_confirmed
and account is not None
and account.role_bind_status == "bound"
)
pending_role = (
""
@@ -129,6 +129,7 @@ const EVENT_LABELS: Record<string, string> = {
binding_reset: '重置角色资料和绑定记录',
binding_stale_role_ignored: '忽略账号旧角色',
binding_synced_from_account: '同步账号最新角色绑定',
binding_false_confirmation_reverted: '纠正误判的账号历史绑定',
eligibility_check_failed: '兑换资格查询失败',
eligibility_auto_refresh_failed: '自动刷新兑换资格失败',
exchange_eligibility_checked: '刷新兑换资格',