fix(fulfillment): 仅同步已映射订单

This commit is contained in:
yml
2026-11-18 12:05:28 +08:00
parent 509a6eecb3
commit 27da489b79
4 changed files with 171 additions and 42 deletions
+44 -6
View File
@@ -61,11 +61,13 @@ def _enabled() -> bool:
def _normalize_url(url: str) -> str:
"""把 `postgresql://` 归一成带驱动的 `postgresql+psycopg://`。
"""把 PostgreSQL URL 归一成带驱动的 `postgresql+psycopg://`。
`.env` 里写的是不带驱动的裸 URL,SQLAlchemy 2 会去猜默认驱动;显式指定
能避免容器里驱动缺失时报出含糊的错误。
"""
if url.startswith("postgres://"):
return "postgresql+psycopg://" + url[len("postgres://") :]
if url.startswith("postgresql://"):
return "postgresql+psycopg://" + url[len("postgresql://") :]
return url
@@ -152,11 +154,23 @@ def _fetch_rows(order_no: str) -> list[dict[str, Any]]:
raise
def _fetch_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]:
def _fetch_pending_manual_fulfillments(
limit: int = 100,
*,
internal_sku_keys: list[str] | None = None,
) -> list[dict[str, Any]]:
"""读取最近的人工渠道待履约订单项。
是否由直播账号履约由 live-hub 本地商品映射决定,不修改 order_site 配置。
是否由直播账号履约由 live-hub 本地商品映射决定;SKU 白名单在 order_site
查询阶段生效,避免无关人工单挤占 limit 并生成大量待映射任务。
"""
sku_keys = list(
dict.fromkeys(
value.strip() for value in internal_sku_keys or [] if value.strip()
)
)
if not sku_keys:
return []
engine = _get_engine()
sql = text(
"""
@@ -192,6 +206,12 @@ def _fetch_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]
AND o.order_status NOT IN ('refunded', 'cancelled', 'closed')
AND oi.quantity = 1
AND COALESCE(f.issue_code, '') NOT IN ('user_refund', 'platform_cancelled')
AND COALESCE(
NULLIF(BTRIM(oi.manual_sku_key), ''),
NULLIF(BTRIM(oi.sku_name), ''),
NULLIF(BTRIM(oi.sku_code), ''),
''
) = ANY(CAST(:internal_sku_keys AS text[]))
ORDER BY oi.updated_at DESC NULLS LAST, oi.id DESC
LIMIT :limit
"""
@@ -201,7 +221,11 @@ def _fetch_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]
return [
dict(row)
for row in conn.execute(
sql, {"limit": max(1, min(limit, 500))}
sql,
{
"limit": max(1, min(limit, 500)),
"internal_sku_keys": sku_keys,
},
).mappings()
]
except Exception as exc:
@@ -209,12 +233,26 @@ def _fetch_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]
raise
def list_pending_manual_fulfillments(limit: int = 100) -> list[dict[str, Any]]:
def list_pending_manual_fulfillments(
limit: int = 100,
*,
internal_sku_keys: list[str] | None = None,
) -> list[dict[str, Any]]:
"""返回最近的人工渠道待履约订单项快照。"""
if not _enabled():
raise OrderLookupUnavailable("外部订单查询未启用")
sku_keys = list(
dict.fromkeys(
value.strip() for value in internal_sku_keys or [] if value.strip()
)
)
if not sku_keys:
return []
result: list[dict[str, Any]] = []
for row in _fetch_pending_manual_fulfillments(limit=limit):
for row in _fetch_pending_manual_fulfillments(
limit=limit,
internal_sku_keys=sku_keys,
):
internal_sku_key = str(
row.get("manual_sku_key")
or row.get("sku_name")