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
@@ -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(