优化(直播履约): 即时选号并收敛订单快照同步

This commit is contained in:
yml
2026-11-18 16:58:08 +08:00
parent b7b09c90bd
commit dc95d83b32
13 changed files with 933 additions and 70 deletions
+91 -36
View File
@@ -257,43 +257,98 @@ def list_pending_manual_fulfillments(
)
if not sku_keys:
return []
result: list[dict[str, Any]] = []
for row in _fetch_pending_manual_fulfillments(
limit=limit,
internal_sku_keys=sku_keys,
paid_at_from=paid_at_from,
):
internal_sku_key = str(
row.get("manual_sku_key")
or row.get("sku_name")
or row.get("sku_code")
or ""
).strip()
result.append(
{
"source_system": PLATFORM_FULFILLMENT_SOURCE,
"source_order_id": str(row.get("source_order_id") or ""),
"source_order_item_id": str(row.get("source_order_item_id") or ""),
"source_fulfillment_id": str(row.get("source_fulfillment_id") or ""),
"platform_order_id": str(row.get("platform_order_id") or ""),
"provider": str(row.get("provider") or ""),
"platform": str(row.get("platform") or ""),
"shop_id": str(row.get("shop_id") or ""),
"shop_name": str(row.get("shop_name") or ""),
"order_status": str(row.get("order_status") or ""),
"pay_status": str(row.get("pay_status") or ""),
"total_amount": int(row.get("total_amount") or 0),
"revenue_amount_fen": int(row.get("revenue_amount_fen") or 0),
"paid_at": row.get("paid_at"),
"sku_code": str(row.get("sku_code") or ""),
"sku_name": str(row.get("sku_name") or ""),
"internal_sku_key": internal_sku_key,
"quantity": int(row.get("quantity") or 1),
"spec_json": row.get("spec_json"),
"item_updated_at": row.get("item_updated_at"),
}
return [
_normalize_pending_row(row)
for row in _fetch_pending_manual_fulfillments(
limit=limit,
internal_sku_keys=sku_keys,
paid_at_from=paid_at_from,
)
return result
]
def _normalize_pending_row(row: dict[str, Any]) -> dict[str, Any]:
internal_sku_key = str(
row.get("manual_sku_key") or row.get("sku_name") or row.get("sku_code") or ""
).strip()
return {
"source_system": PLATFORM_FULFILLMENT_SOURCE,
"source_order_id": str(row.get("source_order_id") or ""),
"source_order_item_id": str(row.get("source_order_item_id") or ""),
"source_fulfillment_id": str(row.get("source_fulfillment_id") or ""),
"platform_order_id": str(row.get("platform_order_id") or ""),
"provider": str(row.get("provider") or ""),
"platform": str(row.get("platform") or ""),
"shop_id": str(row.get("shop_id") or ""),
"shop_name": str(row.get("shop_name") or ""),
"order_status": str(row.get("order_status") or ""),
"pay_status": str(row.get("pay_status") or ""),
"total_amount": int(row.get("total_amount") or 0),
"revenue_amount_fen": int(row.get("revenue_amount_fen") or 0),
"paid_at": row.get("paid_at"),
"sku_code": str(row.get("sku_code") or ""),
"sku_name": str(row.get("sku_name") or ""),
"internal_sku_key": internal_sku_key,
"quantity": int(row.get("quantity") or 1),
"spec_json": row.get("spec_json"),
"item_updated_at": row.get("item_updated_at"),
}
def lookup_pending_manual_fulfillment(
source_order_item_id: str,
) -> dict[str, Any] | None:
"""按订单项定向读取旧版事件缺失的快照,不再拉取最近 500 条。"""
item_id = str(source_order_item_id or "").strip()
if not item_id:
return None
if not _enabled():
raise OrderLookupUnavailable("外部订单查询未启用")
sql = text(
"""
SELECT o.id AS source_order_id,
o.platform_order_id,
o.provider,
o.platform,
o.shop_id,
o.shop_name,
o.order_status,
o.pay_status,
o.total_amount,
o.paid_at,
o.updated_at AS order_updated_at,
oi.id AS source_order_item_id,
oi.sku_code,
oi.sku_name,
oi.quantity,
oi.revenue_amount_fen,
oi.manual_sku_key,
oi.spec_json,
oi.updated_at AS item_updated_at,
f.id AS source_fulfillment_id,
f.channel AS fulfillment_channel,
f.status AS fulfillment_status,
f.issue_code AS fulfillment_issue_code
FROM orders o
JOIN order_items oi ON oi.order_id = o.id
JOIN fulfillments f ON f.order_item_id = oi.id
WHERE oi.id = :source_order_item_id
AND f.channel = 'manual'
LIMIT 1
"""
)
try:
with _get_engine().connect() as conn:
row = (
conn.execute(sql, {"source_order_item_id": int(item_id)})
.mappings()
.first()
)
except Exception as exc:
logger.warning(f"定向查询 order_site 订单项失败({item_id}):{exc}")
raise
return _normalize_pending_row(dict(row)) if row is not None else None
def _route_of(rows: list[dict[str, Any]]) -> str: