fix(fulfillment): 仅同步已映射订单
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -281,18 +281,50 @@ def _sync_pending_tasks(
|
||||
limit: int = 100,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""从 order_site 拉取人工待履约订单项并幂等生成本地作业。"""
|
||||
"""只拉取已配置商品映射的人工待履约订单项并生成本地作业。"""
|
||||
mappings = (
|
||||
db.query(PlatformCommodityMapping)
|
||||
.filter(
|
||||
PlatformCommodityMapping.target_platform == TARGET_PLATFORM,
|
||||
PlatformCommodityMapping.enabled.is_(True),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
mappings_by_sku = {
|
||||
str(mapping.internal_sku_key or "").strip(): mapping
|
||||
for mapping in mappings
|
||||
if str(mapping.internal_sku_key or "").strip()
|
||||
}
|
||||
if not mappings_by_sku:
|
||||
if commit:
|
||||
db.commit()
|
||||
return {
|
||||
"fetched": 0,
|
||||
"created": 0,
|
||||
"updated": 0,
|
||||
"skippedUnmapped": 0,
|
||||
"configuredSkuCount": 0,
|
||||
}
|
||||
try:
|
||||
rows = list_pending_manual_fulfillments(limit=limit)
|
||||
rows = list_pending_manual_fulfillments(
|
||||
limit=limit,
|
||||
internal_sku_keys=list(mappings_by_sku),
|
||||
)
|
||||
except OrderLookupUnavailable as exc:
|
||||
raise HTTPException(status_code=503, detail=f"订单源不可用:{exc}") from exc
|
||||
|
||||
created = 0
|
||||
updated = 0
|
||||
skipped_unmapped = 0
|
||||
for row in rows:
|
||||
item_id = str(row.get("source_order_item_id") or "")
|
||||
if not item_id:
|
||||
continue
|
||||
internal_sku_key = str(row.get("internal_sku_key") or "").strip()
|
||||
mapping = mappings_by_sku.get(internal_sku_key)
|
||||
if mapping is None:
|
||||
skipped_unmapped += 1
|
||||
continue
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
@@ -330,7 +362,7 @@ def _sync_pending_tasks(
|
||||
task.shop_name = str(row.get("shop_name") or "")
|
||||
task.sku_code = str(row.get("sku_code") or "")
|
||||
task.sku_name = str(row.get("sku_name") or "")
|
||||
task.internal_sku_key = str(row.get("internal_sku_key") or "")
|
||||
task.internal_sku_key = internal_sku_key
|
||||
task.quantity = 1
|
||||
task.order_amount_fen = int(
|
||||
row.get("revenue_amount_fen") or row.get("total_amount") or 0
|
||||
@@ -340,12 +372,18 @@ def _sync_pending_tasks(
|
||||
task.source_snapshot_json = _jsonable(row)
|
||||
task.updated_at = _now()
|
||||
if task.execution_status in {"pending_mapping", "ready"}:
|
||||
_apply_mapping(task, _mapping_for_sku(db, task.internal_sku_key))
|
||||
_apply_mapping(task, mapping)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
|
||||
if commit:
|
||||
db.commit()
|
||||
return {"fetched": len(rows), "created": created, "updated": updated}
|
||||
return {
|
||||
"fetched": len(rows),
|
||||
"created": created,
|
||||
"updated": updated,
|
||||
"skippedUnmapped": skipped_unmapped,
|
||||
"configuredSkuCount": len(mappings_by_sku),
|
||||
}
|
||||
|
||||
|
||||
def sync_pending_tasks(
|
||||
@@ -393,6 +431,12 @@ def list_tasks(
|
||||
page_size: int = 20,
|
||||
) -> dict[str, Any]:
|
||||
query = db.query(PlatformFulfillmentTask)
|
||||
query = query.filter(
|
||||
or_(
|
||||
PlatformFulfillmentTask.commodity_mapping_id.isnot(None),
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
)
|
||||
)
|
||||
if status:
|
||||
if status == "unfinished":
|
||||
query = query.filter(
|
||||
|
||||
Reference in New Issue
Block a user