优化(直播履约): 即时选号并收敛订单快照同步
This commit is contained in:
@@ -35,6 +35,7 @@ from ..douyu_service import douyu_config_value, ensure_douyu_config
|
||||
from ..external_order_source import (
|
||||
OrderLookupUnavailable,
|
||||
list_pending_manual_fulfillments,
|
||||
lookup_pending_manual_fulfillment,
|
||||
)
|
||||
from .account_selector import (
|
||||
AccountCandidate,
|
||||
@@ -97,6 +98,360 @@ def _mapping_for_sku(
|
||||
)
|
||||
|
||||
|
||||
def _parse_snapshot_datetime(value: Any) -> datetime | None:
|
||||
if isinstance(value, datetime):
|
||||
return value if value.tzinfo is not None else value.replace(tzinfo=UTC)
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
return None
|
||||
try:
|
||||
parsed = datetime.fromisoformat(text)
|
||||
except ValueError:
|
||||
return None
|
||||
return parsed if parsed.tzinfo is not None else parsed.replace(tzinfo=UTC)
|
||||
|
||||
|
||||
def _snapshot_value(data: dict[str, Any], *names: str, default: Any = "") -> Any:
|
||||
for name in names:
|
||||
if name in data and data[name] is not None:
|
||||
return data[name]
|
||||
return default
|
||||
|
||||
|
||||
def _order_snapshot_from_event(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
data = payload.get("data")
|
||||
return data if isinstance(data, dict) else payload
|
||||
|
||||
|
||||
def _upsert_order_snapshot(
|
||||
db: Session,
|
||||
snapshot: dict[str, Any],
|
||||
*,
|
||||
mapping: PlatformCommodityMapping | None,
|
||||
) -> tuple[PlatformFulfillmentTask | None, bool, bool]:
|
||||
"""应用一条订单项快照,返回 (task, created, ignored)。"""
|
||||
item_id = str(
|
||||
_snapshot_value(snapshot, "sourceOrderItemId", "source_order_item_id", default="")
|
||||
).strip()
|
||||
if not item_id:
|
||||
return None, False, True
|
||||
|
||||
paid_at = _parse_snapshot_datetime(
|
||||
_snapshot_value(snapshot, "paidAt", "paid_at", default=None)
|
||||
)
|
||||
order_created_at = _parse_snapshot_datetime(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"orderCreatedAt",
|
||||
"order_created_at",
|
||||
default=None,
|
||||
)
|
||||
)
|
||||
order_status = str(
|
||||
_snapshot_value(snapshot, "orderStatus", "order_status", default="")
|
||||
)
|
||||
pay_status = str(
|
||||
_snapshot_value(snapshot, "payStatus", "pay_status", default="paid")
|
||||
)
|
||||
terminal_order = order_status in {"refunded", "cancelled", "closed"}
|
||||
if not terminal_order and pay_status != "paid":
|
||||
return None, False, True
|
||||
cutoff = _order_match_started_at(db)
|
||||
match_basis_at = paid_at or order_created_at
|
||||
if (
|
||||
match_basis_at is not None
|
||||
and cutoff is not None
|
||||
and match_basis_at < cutoff
|
||||
):
|
||||
return None, False, True
|
||||
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.source_system == "order_site",
|
||||
PlatformFulfillmentTask.source_order_item_id == item_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
created = task is None
|
||||
if task is None:
|
||||
task = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id=str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceOrderId",
|
||||
"source_order_id",
|
||||
"orderId",
|
||||
default="",
|
||||
)
|
||||
),
|
||||
source_order_item_id=item_id,
|
||||
source_fulfillment_id=str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceFulfillmentId",
|
||||
"source_fulfillment_id",
|
||||
"fulfillmentId",
|
||||
default="",
|
||||
)
|
||||
),
|
||||
target_platform=TARGET_PLATFORM,
|
||||
scope=SCOPE_ELITE,
|
||||
created_at=_now(),
|
||||
updated_at=_now(),
|
||||
)
|
||||
db.add(task)
|
||||
db.flush()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"task_created",
|
||||
detail={
|
||||
"platformOrderId": _snapshot_value(
|
||||
snapshot, "platformOrderId", "platform_order_id", default=""
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
before = (
|
||||
task.platform_order_id,
|
||||
task.source_order_id,
|
||||
task.source_fulfillment_id,
|
||||
task.provider,
|
||||
task.shop_id,
|
||||
task.shop_name,
|
||||
task.sku_code,
|
||||
task.sku_name,
|
||||
task.internal_sku_key,
|
||||
task.quantity,
|
||||
task.order_amount_fen,
|
||||
task.paid_at,
|
||||
task.execution_status,
|
||||
task.commodity_mapping_id,
|
||||
task.commodity_id,
|
||||
task.required_points,
|
||||
task.issue_code,
|
||||
)
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
task.source_snapshot_json = _jsonable(snapshot)
|
||||
return task, created, False
|
||||
|
||||
terminal = (
|
||||
terminal_order
|
||||
or _snapshot_value(
|
||||
snapshot,
|
||||
"fulfillmentStatus",
|
||||
"fulfillment_status",
|
||||
default="",
|
||||
)
|
||||
== "archived"
|
||||
or _snapshot_value(
|
||||
snapshot,
|
||||
"configEnabled",
|
||||
"config_enabled",
|
||||
default=True,
|
||||
)
|
||||
is False
|
||||
or _snapshot_value(
|
||||
snapshot,
|
||||
"douyuFulfillmentEnabled",
|
||||
"douyu_fulfillment_enabled",
|
||||
default=True,
|
||||
)
|
||||
is False
|
||||
)
|
||||
|
||||
task.platform_order_id = str(
|
||||
_snapshot_value(snapshot, "platformOrderId", "platform_order_id", default="")
|
||||
)
|
||||
task.source_order_id = str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceOrderId",
|
||||
"source_order_id",
|
||||
"orderId",
|
||||
default=task.source_order_id,
|
||||
)
|
||||
)
|
||||
task.source_fulfillment_id = str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceFulfillmentId",
|
||||
"source_fulfillment_id",
|
||||
"fulfillmentId",
|
||||
default=task.source_fulfillment_id,
|
||||
)
|
||||
)
|
||||
task.provider = str(
|
||||
_snapshot_value(snapshot, "provider", default=task.provider)
|
||||
)
|
||||
task.shop_id = str(
|
||||
_snapshot_value(snapshot, "shopId", "shop_id", default="")
|
||||
)
|
||||
task.shop_name = str(
|
||||
_snapshot_value(snapshot, "shopName", "shop_name", default="")
|
||||
)
|
||||
task.sku_code = str(
|
||||
_snapshot_value(snapshot, "skuCode", "sku_code", default="")
|
||||
)
|
||||
task.sku_name = str(
|
||||
_snapshot_value(snapshot, "skuName", "sku_name", default="")
|
||||
)
|
||||
task.internal_sku_key = str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"internalSkuKey",
|
||||
"internal_sku_key",
|
||||
default="",
|
||||
)
|
||||
).strip()
|
||||
task.quantity = max(
|
||||
1,
|
||||
int(_snapshot_value(snapshot, "quantity", default=1) or 1),
|
||||
)
|
||||
task.order_amount_fen = int(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"revenueAmountFen",
|
||||
"revenue_amount_fen",
|
||||
"totalAmountFen",
|
||||
"total_amount",
|
||||
default=0,
|
||||
)
|
||||
or 0
|
||||
)
|
||||
task.paid_at = paid_at
|
||||
task.source_snapshot_json = _jsonable(snapshot)
|
||||
task.updated_at = _now()
|
||||
|
||||
if terminal:
|
||||
task.execution_status = "cancelled"
|
||||
task.issue_code = "source_cancelled"
|
||||
task.last_error = "订单已关闭或直播履约配置已停用"
|
||||
task.next_allocate_at = None
|
||||
else:
|
||||
_apply_mapping(task, mapping)
|
||||
if task.execution_status == "ready" and task.account_id is None:
|
||||
task.next_allocate_at = _now()
|
||||
elif task.account_id is not None:
|
||||
task.next_allocate_at = None
|
||||
|
||||
after = (
|
||||
task.platform_order_id,
|
||||
task.source_order_id,
|
||||
task.source_fulfillment_id,
|
||||
task.provider,
|
||||
task.shop_id,
|
||||
task.shop_name,
|
||||
task.sku_code,
|
||||
task.sku_name,
|
||||
task.internal_sku_key,
|
||||
task.quantity,
|
||||
task.order_amount_fen,
|
||||
task.paid_at,
|
||||
task.execution_status,
|
||||
task.commodity_mapping_id,
|
||||
task.commodity_id,
|
||||
task.required_points,
|
||||
task.issue_code,
|
||||
)
|
||||
changed = created or before != after
|
||||
if changed:
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return task, created, False
|
||||
|
||||
|
||||
def _mapping_map(db: Session) -> dict[str, PlatformCommodityMapping]:
|
||||
rows = (
|
||||
db.query(PlatformCommodityMapping)
|
||||
.filter(
|
||||
PlatformCommodityMapping.target_platform == TARGET_PLATFORM,
|
||||
PlatformCommodityMapping.enabled.is_(True),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return {
|
||||
str(row.internal_sku_key or "").strip(): row
|
||||
for row in rows
|
||||
if str(row.internal_sku_key or "").strip()
|
||||
}
|
||||
|
||||
|
||||
def sync_order_snapshot(
|
||||
db: Session,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""消费 order_site 订单项快照;正常路径不再读取 order_site 数据库。"""
|
||||
snapshot = _order_snapshot_from_event(payload)
|
||||
mapping_by_sku = _mapping_map(db)
|
||||
mapping = mapping_by_sku.get(
|
||||
str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"internalSkuKey",
|
||||
"internal_sku_key",
|
||||
default="",
|
||||
)
|
||||
).strip()
|
||||
)
|
||||
task, created, ignored = _upsert_order_snapshot(db, snapshot, mapping=mapping)
|
||||
if commit:
|
||||
db.commit()
|
||||
return {
|
||||
"fetched": 1,
|
||||
"created": int(created),
|
||||
"updated": int(task is not None and not created),
|
||||
"ignored": int(ignored),
|
||||
"taskIds": [int(task.id)] if task is not None else [],
|
||||
}
|
||||
|
||||
|
||||
def sync_order_item_ids(
|
||||
db: Session,
|
||||
source_order_item_ids: list[str],
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""兼容旧版事件:只定向读取明确订单项,不再走最近 500 条窗口。"""
|
||||
mapping_by_sku = _mapping_map(db)
|
||||
created = 0
|
||||
updated = 0
|
||||
ignored = 0
|
||||
task_ids: list[int] = []
|
||||
for item_id in dict.fromkeys(str(item or "").strip() for item in source_order_item_ids):
|
||||
if not item_id:
|
||||
ignored += 1
|
||||
continue
|
||||
snapshot = lookup_pending_manual_fulfillment(item_id)
|
||||
if snapshot is None:
|
||||
ignored += 1
|
||||
continue
|
||||
mapping = mapping_by_sku.get(str(snapshot.get("internal_sku_key") or "").strip())
|
||||
task, was_created, was_ignored = _upsert_order_snapshot(
|
||||
db,
|
||||
snapshot,
|
||||
mapping=mapping,
|
||||
)
|
||||
if task is None:
|
||||
ignored += int(was_ignored)
|
||||
continue
|
||||
created += int(was_created)
|
||||
updated += int(not was_created)
|
||||
task_ids.append(int(task.id))
|
||||
if commit:
|
||||
db.commit()
|
||||
return {
|
||||
"fetched": len(task_ids),
|
||||
"created": created,
|
||||
"updated": updated,
|
||||
"ignored": ignored,
|
||||
"taskIds": task_ids,
|
||||
}
|
||||
|
||||
|
||||
def get_settings(db: Session) -> dict[str, Any]:
|
||||
now = _now()
|
||||
row = (
|
||||
@@ -272,13 +627,25 @@ def _emit_order_site_binding_state(
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> bool:
|
||||
"""当前事务内写 Outbox;HTTP 投递由独立 worker 完成。"""
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(PlatformFulfillmentTask.id == int(task_id))
|
||||
.with_for_update()
|
||||
.one()
|
||||
)
|
||||
next_state_version = int(task.state_version or 0) + 1
|
||||
payload = customer_binding_out(db, task_id)
|
||||
payload["stateVersion"] = next_state_version
|
||||
if extra:
|
||||
payload.update(extra)
|
||||
return enqueue_order_site_binding_state(
|
||||
enqueued = enqueue_order_site_binding_state(
|
||||
db,
|
||||
payload,
|
||||
)
|
||||
if enqueued:
|
||||
task.state_version = next_state_version
|
||||
db.flush()
|
||||
return enqueued
|
||||
|
||||
|
||||
def _load_task(db: Session, task_id: int) -> PlatformFulfillmentTask:
|
||||
@@ -355,6 +722,7 @@ def _sync_pending_tasks(
|
||||
created = 0
|
||||
updated = 0
|
||||
skipped_unmapped = 0
|
||||
task_ids: list[int] = []
|
||||
for row in rows:
|
||||
item_id = str(row.get("source_order_item_id") or "")
|
||||
if not item_id:
|
||||
@@ -414,7 +782,10 @@ def _sync_pending_tasks(
|
||||
task.updated_at = _now()
|
||||
if task.execution_status in {"pending_mapping", "ready"}:
|
||||
_apply_mapping(task, mapping)
|
||||
if task.execution_status == "ready" and task.account_id is None:
|
||||
task.next_allocate_at = _now()
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
task_ids.append(int(task.id))
|
||||
|
||||
if commit:
|
||||
db.commit()
|
||||
@@ -425,6 +796,7 @@ def _sync_pending_tasks(
|
||||
"skippedUnmapped": skipped_unmapped,
|
||||
"configuredSkuCount": len(mappings_by_sku),
|
||||
"orderMatchStartedAt": _jsonable(match_started_at),
|
||||
"taskIds": task_ids,
|
||||
}
|
||||
|
||||
|
||||
@@ -723,6 +1095,7 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"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,
|
||||
"stateVersion": int(task.state_version or 0),
|
||||
"completedAt": _jsonable(task.completed_at),
|
||||
"updatedAt": _jsonable(task.updated_at),
|
||||
}
|
||||
@@ -741,7 +1114,11 @@ def ensure_customer_session(
|
||||
platform_order_id=platform_order_id,
|
||||
)
|
||||
if task is None:
|
||||
sync_pending_tasks(db, limit=500)
|
||||
sync_order_item_ids(
|
||||
db,
|
||||
[source_order_item_id],
|
||||
commit=True,
|
||||
)
|
||||
task = _find_customer_task(
|
||||
db,
|
||||
source_order_item_id=source_order_item_id,
|
||||
@@ -891,9 +1268,15 @@ def upsert_mapping(
|
||||
)
|
||||
pending = _apply_order_match_cutoff(pending, _order_match_started_at(db))
|
||||
pending = pending.all()
|
||||
task_ids: list[int] = []
|
||||
for task in pending:
|
||||
_apply_mapping(task, row if row.enabled else None)
|
||||
task.updated_at = _now()
|
||||
task.next_allocate_at = (
|
||||
_now()
|
||||
if task.execution_status == "ready" and task.account_id is None
|
||||
else None
|
||||
)
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
@@ -902,7 +1285,13 @@ def upsert_mapping(
|
||||
actor_id=current.id,
|
||||
detail={"commodityId": row.commodity_id, "enabled": row.enabled},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
task_ids.append(int(task.id))
|
||||
db.commit()
|
||||
if task_ids:
|
||||
from .auto_allocator import wake_auto_allocator
|
||||
|
||||
wake_auto_allocator(task_ids)
|
||||
return next(
|
||||
item for item in list_mappings(db, keyword=sku_key) if item["id"] == row.id
|
||||
)
|
||||
@@ -1019,6 +1408,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
)
|
||||
if selected is None or selected_state is None or lease is None:
|
||||
task.execution_status = "ready"
|
||||
task.next_allocate_at = _now() + timedelta(seconds=45)
|
||||
task.issue_code = "no_eligible_account"
|
||||
if not candidates:
|
||||
task.last_error = "没有带 CK、允许自动管理且未停用的斗鱼账号"
|
||||
@@ -1049,6 +1439,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
task.lease_id = lease.id
|
||||
task.operator_id = current.id
|
||||
task.execution_status = "allocated"
|
||||
task.next_allocate_at = None
|
||||
task.binding_status = "not_started"
|
||||
soft_reasons = selected_state.soft_requirement_reasons(
|
||||
required_points=required_points
|
||||
@@ -1390,19 +1781,27 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
||||
return len(task_ids)
|
||||
|
||||
|
||||
def reconcile_binding_state_outbox(db: Session, *, limit: int = 500) -> int:
|
||||
"""低频补齐 active 任务当前快照;只会在缺失或 dead 时重新入队。"""
|
||||
def reconcile_binding_state_outbox(
|
||||
db: Session,
|
||||
*,
|
||||
limit: int = 500,
|
||||
after_task_id: int = 0,
|
||||
) -> dict[str, Any]:
|
||||
"""按 ID 游标补齐 active 快照;批次只限制吞吐,不形成永久扫描窗口。"""
|
||||
batch_size = max(1, min(limit, 500))
|
||||
normalized_after = max(0, int(after_task_id or 0))
|
||||
task_ids = [
|
||||
int(row.id)
|
||||
for row in (
|
||||
db.query(PlatformFulfillmentTask.id)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.id > normalized_after,
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
["completed", "cancelled"]
|
||||
)
|
||||
)
|
||||
.order_by(PlatformFulfillmentTask.updated_at.asc())
|
||||
.limit(max(1, min(limit, 500)))
|
||||
.order_by(PlatformFulfillmentTask.id.asc())
|
||||
.limit(batch_size)
|
||||
.all()
|
||||
)
|
||||
]
|
||||
@@ -1410,7 +1809,11 @@ def reconcile_binding_state_outbox(db: Session, *, limit: int = 500) -> int:
|
||||
1 for task_id in task_ids if _emit_order_site_binding_state(db, task_id)
|
||||
)
|
||||
db.commit()
|
||||
return queued
|
||||
return {
|
||||
"queued": queued,
|
||||
"nextTaskId": max(task_ids, default=normalized_after),
|
||||
"hasMore": len(task_ids) == batch_size,
|
||||
}
|
||||
|
||||
|
||||
async def confirm_binding(
|
||||
@@ -1705,6 +2108,8 @@ __all__ = [
|
||||
"release_task_account",
|
||||
"reset_task_binding",
|
||||
"start_binding",
|
||||
"sync_order_item_ids",
|
||||
"sync_order_snapshot",
|
||||
"sync_pending_tasks",
|
||||
"sync_pending_tasks_if_due",
|
||||
"sync_task_from_binding_runner",
|
||||
|
||||
Reference in New Issue
Block a user