perf(fulfillment): 优化本地能力选号查询与索引
This commit is contained in:
@@ -64,8 +64,38 @@ class AccountCandidate:
|
||||
class CapabilityCandidate:
|
||||
"""一个新鲜能力快照及其对应账号,供本地零外部请求选号。"""
|
||||
|
||||
account: Account
|
||||
state: AccountCommodityState
|
||||
account: CapabilityAccount
|
||||
state: CapabilityState
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CapabilityAccount:
|
||||
"""能力选号实际需要的账号字段,不加载 CK / JSON 等大列。"""
|
||||
|
||||
id: int
|
||||
username: str
|
||||
nickname: str
|
||||
uid: str
|
||||
points: int | None
|
||||
gold_balance: int | None
|
||||
points_expire_at: datetime | None
|
||||
role_change_available_at: datetime | None
|
||||
elite_opened: bool | None
|
||||
role_bind_status: str
|
||||
ck_check_status: str
|
||||
game_name: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CapabilityState:
|
||||
"""选号排序之后才需要的能力快照详情。"""
|
||||
|
||||
account_eligibility: str
|
||||
offer_state: str
|
||||
observed_at: datetime | None
|
||||
available_from: datetime | None
|
||||
valid_until: datetime | None
|
||||
blocking_reason: str
|
||||
|
||||
|
||||
def _now(now: datetime | None = None) -> datetime:
|
||||
@@ -251,7 +281,24 @@ def _capability_candidate_query(
|
||||
allow_before_open=allow_before_open,
|
||||
)
|
||||
return (
|
||||
db.query(Account, AccountCommodityState)
|
||||
db.query(
|
||||
Account.id.label("account_id"),
|
||||
Account.username.label("account_username"),
|
||||
Account.nickname.label("account_nickname"),
|
||||
Account.uid.label("account_uid"),
|
||||
Account.points.label("account_points"),
|
||||
Account.gold_balance.label("account_gold_balance"),
|
||||
Account.points_expire_at.label("account_points_expire_at"),
|
||||
Account.role_change_available_at.label(
|
||||
"account_role_change_available_at"
|
||||
),
|
||||
Account.elite_opened.label("account_elite_opened"),
|
||||
Account.role_bind_status.label("account_role_bind_status"),
|
||||
Account.ck_check_status.label("account_ck_check_status"),
|
||||
Account.game_name.label("account_game_name"),
|
||||
AccountCommodityState.offer_state.label("state_offer_state"),
|
||||
AccountCommodityState.observed_at.label("state_observed_at"),
|
||||
)
|
||||
.join(
|
||||
AccountCommodityState,
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
@@ -315,10 +362,67 @@ def capability_candidate_rows(
|
||||
)
|
||||
if query is None:
|
||||
return []
|
||||
return [
|
||||
CapabilityCandidate(account=account, state=state)
|
||||
for account, state in query.all()
|
||||
]
|
||||
rows = query.all()
|
||||
if not rows:
|
||||
return []
|
||||
account_ids = [int(row.account_id) for row in rows]
|
||||
state_rows = (
|
||||
db.query(
|
||||
AccountCommodityState.account_id,
|
||||
AccountCommodityState.account_eligibility,
|
||||
AccountCommodityState.offer_state,
|
||||
AccountCommodityState.observed_at,
|
||||
AccountCommodityState.available_from,
|
||||
AccountCommodityState.valid_until,
|
||||
AccountCommodityState.blocking_reason,
|
||||
)
|
||||
.filter(
|
||||
AccountCommodityState.account_id.in_(account_ids),
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id.strip(),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
states = {
|
||||
int(row.account_id): CapabilityState(
|
||||
account_eligibility=str(row.account_eligibility or ""),
|
||||
offer_state=str(row.offer_state or ""),
|
||||
observed_at=row.observed_at,
|
||||
available_from=row.available_from,
|
||||
valid_until=row.valid_until,
|
||||
blocking_reason=str(row.blocking_reason or ""),
|
||||
)
|
||||
for row in state_rows
|
||||
}
|
||||
candidates: list[CapabilityCandidate] = []
|
||||
for row in rows:
|
||||
account_id = int(row.account_id)
|
||||
state = states.get(account_id)
|
||||
if state is None:
|
||||
continue
|
||||
candidates.append(
|
||||
CapabilityCandidate(
|
||||
account=CapabilityAccount(
|
||||
id=account_id,
|
||||
username=str(row.account_username or ""),
|
||||
nickname=str(row.account_nickname or ""),
|
||||
uid=str(row.account_uid or ""),
|
||||
points=row.account_points,
|
||||
gold_balance=row.account_gold_balance,
|
||||
points_expire_at=row.account_points_expire_at,
|
||||
role_change_available_at=(
|
||||
row.account_role_change_available_at
|
||||
),
|
||||
elite_opened=row.account_elite_opened,
|
||||
role_bind_status=str(row.account_role_bind_status or ""),
|
||||
ck_check_status=str(row.account_ck_check_status or ""),
|
||||
game_name=str(row.account_game_name or ""),
|
||||
),
|
||||
state=state,
|
||||
)
|
||||
)
|
||||
return candidates
|
||||
|
||||
|
||||
def capability_candidate_accounts(
|
||||
@@ -330,7 +434,7 @@ def capability_candidate_accounts(
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_before_open: bool = False,
|
||||
) -> list[Account]:
|
||||
) -> list[CapabilityAccount]:
|
||||
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
||||
return [
|
||||
candidate.account
|
||||
@@ -680,6 +784,7 @@ __all__ = [
|
||||
"EXCHANGE_COOLDOWN_DAYS",
|
||||
"VALIDATION_LEASE_MINUTES",
|
||||
"AccountCandidate",
|
||||
"CapabilityAccount",
|
||||
"acquire_account_lease",
|
||||
"active_lease_for_account",
|
||||
"capability_candidate_accounts",
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -185,7 +185,12 @@ def reset_task_binding(
|
||||
|
||||
|
||||
def refresh_exchange_eligibility(
|
||||
db: Session, *, task_id: int, current: User
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
current: User,
|
||||
automatic: bool = False,
|
||||
mark_failed_on_error: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
task = _load_task(db, task_id)
|
||||
if task.binding_status != "bound":
|
||||
@@ -231,20 +236,28 @@ def refresh_exchange_eligibility(
|
||||
detail={"error": str(exc)[:1000], "commodity_id": task.commodity_id},
|
||||
actor_id=current.id,
|
||||
)
|
||||
task.execution_status = "manual_required"
|
||||
task.exchange_status = "check_failed"
|
||||
task.issue_code = "eligibility_check_failed"
|
||||
task.last_error = str(exc)
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"eligibility_check_failed",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={"error": str(exc)},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
if mark_failed_on_error:
|
||||
task.execution_status = "manual_required"
|
||||
task.exchange_status = "check_failed"
|
||||
task.issue_code = "eligibility_check_failed"
|
||||
task.last_error = str(exc)
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"eligibility_check_failed",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={"error": str(exc)},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
else:
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"eligibility_auto_refresh_failed",
|
||||
detail={"error": str(exc), "automatic": True},
|
||||
)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
@@ -366,11 +379,12 @@ def refresh_exchange_eligibility(
|
||||
db,
|
||||
task.id,
|
||||
"exchange_eligibility_checked",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
actor_type="system" if automatic else "user",
|
||||
actor_id=None if automatic else current.id,
|
||||
detail={
|
||||
"exchangeStatus": task.exchange_status,
|
||||
"limitedCount": len(limited_ids),
|
||||
"automatic": automatic,
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
@@ -378,6 +392,58 @@ def refresh_exchange_eligibility(
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def refresh_active_exchange_eligibility_for_account(
|
||||
db: Session,
|
||||
*,
|
||||
account_id: int,
|
||||
scope: str,
|
||||
actor_id: int | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""账号资产变化后,自动刷新当前活动履约任务的兑换投影。
|
||||
|
||||
自动刷新失败只记录时间线,不把已绑定任务打成人工;运营仍可手动重试。
|
||||
"""
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.join(AccountLease, AccountLease.id == PlatformFulfillmentTask.lease_id)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.account_id == int(account_id),
|
||||
PlatformFulfillmentTask.scope == str(scope or SCOPE_ELITE),
|
||||
PlatformFulfillmentTask.binding_status == "bound",
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
("completed", "cancelled")
|
||||
),
|
||||
AccountLease.status == "active",
|
||||
)
|
||||
.order_by(
|
||||
PlatformFulfillmentTask.updated_at.desc(),
|
||||
PlatformFulfillmentTask.id.desc(),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if task is None:
|
||||
return None
|
||||
actor = db.get(User, int(actor_id)) if actor_id is not None else None
|
||||
if actor is None and task.operator_id is not None:
|
||||
actor = db.get(User, int(task.operator_id))
|
||||
if actor is None:
|
||||
actor = (
|
||||
db.query(User)
|
||||
.filter(User.role == "super_admin", User.is_active.is_(True))
|
||||
.order_by(User.id.asc())
|
||||
.first()
|
||||
)
|
||||
if actor is None:
|
||||
return None
|
||||
return refresh_exchange_eligibility(
|
||||
db,
|
||||
task_id=int(task.id),
|
||||
current=actor,
|
||||
automatic=True,
|
||||
mark_failed_on_error=False,
|
||||
)
|
||||
|
||||
|
||||
def complete_manual_exchange(
|
||||
db: Session,
|
||||
*,
|
||||
|
||||
@@ -31,8 +31,10 @@ from .binding import ( # noqa: F401
|
||||
confirm_binding_sync,
|
||||
reconcile_binding_state_outbox,
|
||||
refresh_binding,
|
||||
requires_account_binding_sync,
|
||||
start_binding,
|
||||
start_binding_sync,
|
||||
sync_active_task_from_account_binding,
|
||||
sync_task_from_binding_runner,
|
||||
)
|
||||
from .binding_status import ( # noqa: F401
|
||||
@@ -68,6 +70,7 @@ from .lifecycle import (
|
||||
complete_manual_exchange,
|
||||
mark_manual_required,
|
||||
prepare_platform_task_for_collab_takeover,
|
||||
refresh_active_exchange_eligibility_for_account,
|
||||
refresh_exchange_eligibility,
|
||||
release_task_account,
|
||||
release_task_to_manual,
|
||||
@@ -155,15 +158,18 @@ __all__ = [
|
||||
"reconcile_binding_state_outbox",
|
||||
"record_douyu_operation_result",
|
||||
"record_douyu_operation_started",
|
||||
"refresh_active_exchange_eligibility_for_account",
|
||||
"refresh_binding",
|
||||
"refresh_exchange_eligibility",
|
||||
"release_task_account",
|
||||
"release_task_to_manual",
|
||||
"requires_account_binding_sync",
|
||||
"reset_task_binding",
|
||||
"restore_cancelled_task",
|
||||
"restore_mapping",
|
||||
"set_mapping_enabled",
|
||||
"start_binding",
|
||||
"sync_active_task_from_account_binding",
|
||||
"sync_order_item_ids",
|
||||
"sync_order_snapshot",
|
||||
"sync_pending_tasks",
|
||||
|
||||
@@ -362,6 +362,9 @@ def _task_out(
|
||||
)
|
||||
collab_order = resolved.collab_orders.get(int(task.id))
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
account_binding_confirmed = bool(
|
||||
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)
|
||||
exchange_task_id = task.exchange_task_id
|
||||
@@ -377,7 +380,11 @@ def _task_out(
|
||||
.scalar()
|
||||
)
|
||||
# 外层只消费共享绑定规则,不再自己比较角色名或 bindAct。
|
||||
pending_role = pending_binding_role_from_result(bind_result)
|
||||
pending_role = (
|
||||
""
|
||||
if account_binding_confirmed or task.binding_status == "bound"
|
||||
else pending_binding_role_from_result(bind_result)
|
||||
)
|
||||
sale_mode = classification.normalize_sale_mode(task.sale_mode)
|
||||
availability = classification.availability_state(
|
||||
sale_mode=sale_mode,
|
||||
@@ -477,12 +484,19 @@ def _task_out(
|
||||
"status": bind_task.status,
|
||||
"message": bind_task.message,
|
||||
"qrUrl": str(bind_result.get("url") or "") if include_qr else "",
|
||||
"roleName": binding_role_from_result(bind_result),
|
||||
"roleName": (
|
||||
str(account.game_name or "")
|
||||
if account_binding_confirmed and account is not None
|
||||
else binding_role_from_result(bind_result)
|
||||
),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindPhase": str(bind_result.get("bind_phase") or ""),
|
||||
"bindReadyForConfirm": bool(pending_role),
|
||||
"bindConfirmed": bool(bind_result.get("bind_confirmed")),
|
||||
"bindConfirmed": bool(
|
||||
account_binding_confirmed
|
||||
or bind_result.get("bind_confirmed")
|
||||
),
|
||||
}
|
||||
if bind_task
|
||||
else None
|
||||
@@ -620,7 +634,15 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
else None
|
||||
)
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
pending_role = pending_binding_role_from_result(bind_result)
|
||||
account = db.get(Account, int(task.account_id)) if task.account_id else None
|
||||
account_binding_confirmed = bool(
|
||||
account is not None and account.role_bind_status == "bound"
|
||||
)
|
||||
pending_role = (
|
||||
""
|
||||
if account_binding_confirmed or task.binding_status == "bound"
|
||||
else pending_binding_role_from_result(bind_result)
|
||||
)
|
||||
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
||||
lease = (
|
||||
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
@@ -657,11 +679,18 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"qrUrl": str(bind_result.get("url") or ""),
|
||||
"qrGeneratedAt": str(bind_result.get("qr_generated_at") or ""),
|
||||
"qrExpiresAt": str(bind_result.get("qr_expires_at") or ""),
|
||||
"roleName": binding_role_from_result(bind_result),
|
||||
"roleName": (
|
||||
str(account.game_name or "")
|
||||
if account_binding_confirmed and account is not None
|
||||
else binding_role_from_result(bind_result)
|
||||
),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindReadyForConfirm": (bool(pending_role) and not binding_failed),
|
||||
"bindConfirmed": bool(bind_result.get("bind_confirmed")) and binding_confirmed,
|
||||
"bindConfirmed": bool(
|
||||
binding_confirmed
|
||||
and (account_binding_confirmed or bind_result.get("bind_confirmed"))
|
||||
),
|
||||
"queueState": queue_state,
|
||||
"retryAfterMs": retry_after_ms,
|
||||
"queueLane": bind_task.queue_lane if bind_task else "",
|
||||
|
||||
Reference in New Issue
Block a user