feat(fulfillment): 新增直播账号履约工作台

This commit is contained in:
yml
2026-11-17 14:44:28 +08:00
parent f8a35ec224
commit 1ca768eba5
30 changed files with 3729 additions and 27 deletions
@@ -32,6 +32,7 @@ CACHE_TTL = timedelta(hours=24)
ROUTE_UNHANDLED = "unhandled"
ROUTE_AUTO_FULFILLED = "auto_fulfilled"
ROUTE_WORKER_PLATFORM = "worker_platform"
PLATFORM_FULFILLMENT_SOURCE = "order_site"
# 发货方内部行:同一订单号下与真实店铺行成对出现,其 sku_code 是内部编码
# (如 suit_shadow_gothic),不能用来回显给买家。
@@ -151,6 +152,102 @@ def _fetch_rows(order_no: str) -> list[dict[str, Any]]:
raise
def _fetch_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]:
"""读取最近的人工渠道待履约订单项。
是否由直播账号履约由 live-hub 本地商品映射决定,不修改 order_site 配置。
"""
engine = _get_engine()
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 f.channel = 'manual'
AND f.status = 'assigned'
AND o.pay_status = 'paid'
AND o.order_status NOT IN ('refunded', 'cancelled', 'closed')
AND oi.quantity = 1
AND COALESCE(f.issue_code, '') NOT IN ('user_refund', 'platform_cancelled')
ORDER BY oi.updated_at DESC NULLS LAST, oi.id DESC
LIMIT :limit
"""
)
try:
with engine.connect() as conn:
return [
dict(row)
for row in conn.execute(
sql, {"limit": max(1, min(limit, 500))}
).mappings()
]
except Exception as exc:
logger.warning(f"查询 order_site 人工待履约订单失败:{exc}")
raise
def list_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]:
"""返回最近的人工渠道待履约订单项快照。"""
if not _enabled():
raise OrderLookupUnavailable("外部订单查询未启用")
result: list[dict[str, Any]] = []
for row in _fetch_pending_manual_fulfillments(limit=limit):
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 result
def _route_of(rows: list[dict[str, Any]]) -> str:
"""订单号维度的聚合判定:任一行已处理即算已处理。"""
if any(row["has_fulfillment"] for row in rows):