init: Prefect ETL sync project (RIIN/SDS → PG) with 12 sync flows
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
"""RIIN 同步公共管道:映射订单 → 并发拉商品 → upsert 到 PG。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from flows.shared import fetch_goods_concurrent
|
||||
from flows.shared.db import SyncDB
|
||||
from flows.shared.mappers import map_riin_goods, map_riin_order
|
||||
from api import RiinDisClient
|
||||
|
||||
|
||||
def sync_riin_records(raw_orders: list[Any], client: RiinDisClient, db: SyncDB) -> dict[str, int]:
|
||||
order_inputs: list[Any] = []
|
||||
order_meta: dict[str, tuple[str, Any]] = {} # raw.id -> (orderNo, createdAt)
|
||||
goods_failed = 0
|
||||
|
||||
for raw in raw_orders:
|
||||
try:
|
||||
o = map_riin_order(raw)
|
||||
except Exception as err: # noqa: BLE001
|
||||
print(f"[RIIN] 映射订单失败 {getattr(raw, 'platform_oid', '?')}: {err}")
|
||||
continue
|
||||
order_inputs.append(o)
|
||||
if raw.id:
|
||||
order_meta[raw.id] = (o.orderNo, o.createdAt)
|
||||
|
||||
goods_inputs: list[Any] = []
|
||||
if order_meta:
|
||||
goods_map = fetch_goods_concurrent(client, list(order_meta.keys()))
|
||||
for oid, (order_no, created_at) in order_meta.items():
|
||||
items = goods_map.get(oid, [])
|
||||
if not items:
|
||||
goods_failed += 1
|
||||
for g in items:
|
||||
try:
|
||||
goods_inputs.append(map_riin_goods(g, order_no, created_at))
|
||||
except Exception as err: # noqa: BLE001
|
||||
print(f"[RIIN] 映射商品失败 order={order_no}: {err}")
|
||||
|
||||
stats = db.sync_orders_and_goods(order_inputs, goods_inputs)
|
||||
stats["orderCount"] = len(order_inputs)
|
||||
stats["goodsCount"] = len(goods_inputs)
|
||||
stats["goodsFetchFailed"] = goods_failed
|
||||
return stats
|
||||
Reference in New Issue
Block a user