fix(fulfillment): 仅同步已映射订单
This commit is contained in:
@@ -148,6 +148,10 @@ class TestUrlNormalization:
|
||||
url = "postgresql://u:p@h:5432/db"
|
||||
assert src._normalize_url(url) == "postgresql+psycopg://u:p@h:5432/db"
|
||||
|
||||
def test_bare_postgres_url_gets_driver(self):
|
||||
url = "postgres://u:p@h:5432/db"
|
||||
assert src._normalize_url(url) == "postgresql+psycopg://u:p@h:5432/db"
|
||||
|
||||
def test_url_with_driver_is_untouched(self):
|
||||
url = "postgresql+psycopg://u:p@h:5432/db"
|
||||
assert src._normalize_url(url) == url
|
||||
@@ -171,6 +175,19 @@ class TestDegradation:
|
||||
with pytest.raises(src.OrderLookupUnavailable):
|
||||
src.lookup_order(" ")
|
||||
|
||||
def test_empty_pending_sku_filter_skips_external_lookup(self, monkeypatch):
|
||||
monkeypatch.setenv("ORDER_SITE_DATABASE_URL", "postgresql://u:p@h:5432/db")
|
||||
monkeypatch.setenv("ORDER_SITE_LOOKUP_ENABLED", "true")
|
||||
monkeypatch.setattr(
|
||||
src,
|
||||
"_get_engine",
|
||||
lambda: (_ for _ in ()).throw(
|
||||
AssertionError("没有 SKU 白名单时不应连接 order_site")
|
||||
),
|
||||
)
|
||||
|
||||
assert src.list_pending_manual_fulfillments(internal_sku_keys=[]) == []
|
||||
|
||||
|
||||
class TestCacheRoundTrip:
|
||||
"""缓存读写:命中未过期快照就不查外部。"""
|
||||
|
||||
@@ -148,46 +148,32 @@ class _Base:
|
||||
|
||||
|
||||
class TestSyncAndMapping(_Base):
|
||||
def test_sync_creates_pending_mapping_task(self, monkeypatch):
|
||||
def test_sync_skips_external_orders_when_no_mapping_is_configured(
|
||||
self, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"list_pending_manual_fulfillments",
|
||||
lambda limit: [
|
||||
{
|
||||
"source_system": "order_site",
|
||||
"source_order_id": "100",
|
||||
"source_order_item_id": "101",
|
||||
"source_fulfillment_id": "201",
|
||||
"platform_order_id": "ORDER-101",
|
||||
"provider": "91kaquan",
|
||||
"shop_id": "shop-1",
|
||||
"shop_name": "测试店",
|
||||
"sku_code": INTERNAL_SKU,
|
||||
"sku_name": INTERNAL_SKU,
|
||||
"internal_sku_key": INTERNAL_SKU,
|
||||
"quantity": 1,
|
||||
"revenue_amount_fen": 9900,
|
||||
"total_amount": 9900,
|
||||
"paid_at": NOW,
|
||||
"item_updated_at": NOW,
|
||||
}
|
||||
],
|
||||
lambda **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("没有映射时不应查询 order_site")
|
||||
),
|
||||
)
|
||||
|
||||
result = service.sync_pending_tasks(self.session, limit=10)
|
||||
|
||||
assert result["created"] == 1
|
||||
task = self.session.query(PlatformFulfillmentTask).one()
|
||||
assert task.execution_status == "pending_mapping"
|
||||
assert task.internal_sku_key == INTERNAL_SKU
|
||||
assert task.order_amount_fen == 9900
|
||||
assert result["fetched"] == 0
|
||||
assert result["created"] == 0
|
||||
assert result["configuredSkuCount"] == 0
|
||||
assert self.session.query(PlatformFulfillmentTask).count() == 0
|
||||
|
||||
def test_sync_is_idempotent_and_applies_mapping(self, monkeypatch):
|
||||
self.mapping()
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"list_pending_manual_fulfillments",
|
||||
lambda limit: [
|
||||
captured_keys: list[str] = []
|
||||
|
||||
def _rows(*, limit: int, internal_sku_keys: list[str]):
|
||||
del limit
|
||||
captured_keys.extend(internal_sku_keys)
|
||||
return [
|
||||
{
|
||||
"source_order_id": "100",
|
||||
"source_order_item_id": "101",
|
||||
@@ -205,7 +191,12 @@ class TestSyncAndMapping(_Base):
|
||||
"paid_at": NOW,
|
||||
"item_updated_at": NOW,
|
||||
}
|
||||
],
|
||||
]
|
||||
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"list_pending_manual_fulfillments",
|
||||
_rows,
|
||||
)
|
||||
|
||||
first = service.sync_pending_tasks(self.session, limit=10)
|
||||
@@ -213,11 +204,50 @@ class TestSyncAndMapping(_Base):
|
||||
|
||||
assert first["created"] == 1
|
||||
assert second["created"] == 0
|
||||
assert captured_keys == [INTERNAL_SKU, INTERNAL_SKU]
|
||||
assert self.session.query(PlatformFulfillmentTask).count() == 1
|
||||
task = self.session.query(PlatformFulfillmentTask).one()
|
||||
assert task.execution_status == "ready"
|
||||
assert task.commodity_id == COMMODITY
|
||||
|
||||
def test_list_only_returns_tasks_that_have_a_commodity_mapping(self):
|
||||
legacy = self.task(mapped=False)
|
||||
mapping = self.mapping()
|
||||
mapped = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="102",
|
||||
source_order_item_id="103",
|
||||
source_fulfillment_id="203",
|
||||
platform_order_id="ORDER-103",
|
||||
provider="91kaquan",
|
||||
shop_id="shop-1",
|
||||
shop_name="测试店",
|
||||
sku_code=INTERNAL_SKU,
|
||||
sku_name=INTERNAL_SKU,
|
||||
internal_sku_key=INTERNAL_SKU,
|
||||
quantity=1,
|
||||
order_amount_fen=9900,
|
||||
target_platform="douyu",
|
||||
scope="elite",
|
||||
commodity_mapping_id=mapping.id,
|
||||
commodity_id=COMMODITY,
|
||||
required_points=1280,
|
||||
binding_status="not_started",
|
||||
execution_status="ready",
|
||||
exchange_status="not_started",
|
||||
source_snapshot_json={"order_no": "ORDER-103"},
|
||||
created_at=NOW,
|
||||
updated_at=NOW,
|
||||
)
|
||||
self.session.add(mapped)
|
||||
self.session.commit()
|
||||
|
||||
listed = service.list_tasks(self.session)
|
||||
|
||||
assert [item["id"] for item in listed["items"]] == [mapped.id]
|
||||
assert listed["pagination"]["total"] == 1
|
||||
assert legacy.id != mapped.id
|
||||
|
||||
def test_upsert_mapping_advances_existing_task(self):
|
||||
task = self.task(mapped=False)
|
||||
self.session.add(
|
||||
|
||||
@@ -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