Files
inkreach-prefect/flows/sds/order_sync.py
T

77 lines
2.4 KiB
Python

"""SDS 订单同步:按下单时间窗口(+筛选)拉取订单与商品。
对应 sds-order-sync。主时间维度 payDate(下单时间)。
"""
from __future__ import annotations
from typing import Any
from prefect import flow, task
from flows.sds._pipeline import sync_sds_records
from flows.shared import (
SyncDB, create_sds_client, fetch_sds_all_pages, get_channel_type_map, refresh_channel_type_map,
)
@task(retries=3, retry_delay_seconds=[3, 6, 9])
def extract_orders(params: dict[str, Any]) -> list:
client = create_sds_client()
try:
return fetch_sds_all_pages(client, params)
finally:
client.close()
@task
def upsert_records(raw_orders: list) -> dict:
client = create_sds_client()
try:
channel_map = get_channel_type_map(client)
return sync_sds_records(raw_orders, channel_map, SyncDB(),
channel_refresh=lambda: refresh_channel_type_map(client))
finally:
client.close()
@flow(name="sds-order-sync", log_prints=True)
def sds_order_sync(
pay_date_begin: str,
pay_date_end: str,
order_status_code: str | None = None,
factory_ids: list[int] | None = None,
merchant_ids: list[int] | None = None,
logistics_ids: list[int] | None = None,
issuing_bay_area_id: int | None = None,
keywords: str | None = None,
page_size: int = 500,
) -> dict[str, Any]:
params: dict[str, Any] = {
"pay_date_begin": pay_date_begin,
"pay_date_end": pay_date_end,
"size": page_size,
}
if order_status_code is not None:
params["order_status_code"] = order_status_code
if factory_ids:
params["factory_ids"] = factory_ids
if merchant_ids:
params["merchant_ids"] = merchant_ids
if logistics_ids:
params["logistics_ids"] = logistics_ids
if issuing_bay_area_id is not None:
params["issuing_bay_area_id"] = issuing_bay_area_id
if keywords:
params["keywords"] = keywords
print(f"SDS 订单同步 {pay_date_begin} ~ {pay_date_end}")
raw = extract_orders(params)
stats = upsert_records(raw)
print(f"完成: 订单 {stats['orderCount']} / 商品 {stats['goodsCount']} / 跳过待付款 {stats.get('skippedUnpaid',0)}")
return {"syncRange": {"from": pay_date_begin, "to": pay_date_end}, **stats}
if __name__ == "__main__":
sds_order_sync.serve(name="sds-order-sync")