131 lines
4.0 KiB
Python
131 lines
4.0 KiB
Python
"""外部客户会话与绑定接口编排。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ...models import (
|
|
AccountLease,
|
|
PlatformFulfillmentTask,
|
|
)
|
|
from .binding import (
|
|
confirm_binding,
|
|
refresh_binding,
|
|
start_binding,
|
|
)
|
|
from .order_sync import sync_order_item_ids
|
|
from .repository import (
|
|
_find_customer_task,
|
|
_integration_actor,
|
|
_load_task,
|
|
)
|
|
from .views import customer_binding_out
|
|
|
|
|
|
def ensure_customer_session(
|
|
db: Session,
|
|
*,
|
|
source_order_item_id: str,
|
|
platform_order_id: str = "",
|
|
) -> dict[str, Any]:
|
|
"""按订单项幂等准备客户绑定会话;必要时同步订单并自动分配账号。"""
|
|
task = _find_customer_task(
|
|
db,
|
|
source_order_item_id=source_order_item_id,
|
|
platform_order_id=platform_order_id,
|
|
)
|
|
if task is None:
|
|
sync_order_item_ids(
|
|
db,
|
|
[source_order_item_id],
|
|
commit=True,
|
|
)
|
|
task = _find_customer_task(
|
|
db,
|
|
source_order_item_id=source_order_item_id,
|
|
platform_order_id=platform_order_id,
|
|
)
|
|
if task is None:
|
|
raise HTTPException(status_code=404, detail="订单尚未进入直播账号履约队列")
|
|
if task.execution_status in {"completed", "cancelled"}:
|
|
return customer_binding_out(db, task.id)
|
|
if task.execution_status in {"pending_mapping", "ready", "manual_required"}:
|
|
if task.issue_code == "mapping_missing":
|
|
return customer_binding_out(db, task.id)
|
|
from .auto_allocator import wake_auto_allocator
|
|
|
|
wake_auto_allocator([int(task.id)])
|
|
return customer_binding_out(db, task.id)
|
|
|
|
|
|
async def customer_start_binding(
|
|
db: Session,
|
|
*,
|
|
task_id: int,
|
|
force_refresh: bool = False,
|
|
) -> dict[str, Any]:
|
|
task = _load_task(db, task_id)
|
|
if task.execution_status in {"completed", "cancelled"}:
|
|
return customer_binding_out(db, task.id)
|
|
if task.issue_code == "mapping_missing":
|
|
return customer_binding_out(db, task.id)
|
|
|
|
if not task.account_id or not task.lease_id:
|
|
from .auto_allocator import wake_auto_allocator
|
|
|
|
wake_auto_allocator([int(task.id)])
|
|
payload = customer_binding_out(db, task.id)
|
|
payload["accepted"] = True
|
|
payload["queueState"] = "allocation_queued"
|
|
payload["retryAfterMs"] = 2000
|
|
return payload
|
|
|
|
lease = db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
|
if lease is None or lease.status != "active":
|
|
return customer_binding_out(db, task.id)
|
|
|
|
previous_bind_task_id = task.bind_task_id
|
|
actor = _integration_actor(db)
|
|
await start_binding(
|
|
db,
|
|
task_id=task.id,
|
|
current=actor,
|
|
force_refresh=force_refresh,
|
|
)
|
|
payload = customer_binding_out(db, task.id)
|
|
accepted = task.bind_task_id != previous_bind_task_id
|
|
payload["accepted"] = accepted
|
|
if accepted:
|
|
payload["queueState"] = "qr_queued"
|
|
payload["retryAfterMs"] = 1000
|
|
return payload
|
|
|
|
|
|
def customer_refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|
refresh_binding(db, task_id=task_id)
|
|
return customer_binding_out(db, task_id)
|
|
|
|
|
|
def customer_refresh_bindings(db: Session, *, task_ids: list[int]) -> dict[str, Any]:
|
|
items: list[dict[str, Any]] = []
|
|
for task_id in dict.fromkeys(task_ids):
|
|
task = (
|
|
db.query(PlatformFulfillmentTask)
|
|
.filter(PlatformFulfillmentTask.id == task_id)
|
|
.first()
|
|
)
|
|
if task is None:
|
|
continue
|
|
refresh_binding(db, task_id=task.id)
|
|
items.append(customer_binding_out(db, task.id))
|
|
return {"items": items}
|
|
|
|
|
|
async def customer_confirm_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|
refresh_binding(db, task_id=task_id)
|
|
await confirm_binding(db, task_id=task_id, current=_integration_actor(db))
|
|
return customer_binding_out(db, task_id)
|