"""RIIN 刷新未完结:查 DB 中未完结订单号,按 platformOidList 批量(500)刷新。 对应 riin-refresh-pending。终态:已退款 / 已关闭 / 已发货。 """ from __future__ import annotations from prefect import flow, task from api.riin.models import OrderPageRequest from flows.riin._pipeline import sync_riin_records from flows.shared import SyncDB, build_stats_rows, create_riin_client, emit_sync_summary RIIN_FINISHED_STATUSES = ["已退款", "已关闭", "已发货"] ORDER_BATCH = 500 @task(retries=3, retry_delay_seconds=[3, 6, 9]) def fetch_pending(order_nos: list[str]) -> list: client = create_riin_client() try: all_raw: list = [] for i in range(0, len(order_nos), ORDER_BATCH): batch = order_nos[i : i + ORDER_BATCH] resp = client.get_orders( OrderPageRequest(platform_oid_list=",".join(batch), all=True) ) all_raw.extend(resp.data.records) return all_raw finally: client.close() @task(retries=3, retry_delay_seconds=[3, 6, 9]) def upsert_records(raw_orders: list) -> dict: client = create_riin_client() try: return sync_riin_records(raw_orders, client, SyncDB()) finally: client.close() @flow(name="riin-refresh-pending", log_prints=True) def riin_refresh_pending() -> dict: db = SyncDB() order_nos = db.get_pending_orders("RIIN", RIIN_FINISHED_STATUSES) no_label_nos = db.get_orders_without_label("RIIN", days=7) all_nos = list(set(order_nos) | set(no_label_nos)) print(f"RIIN 未完结 {len(order_nos)} + 无面单(近7天) {len(no_label_nos)} → 合并 {len(all_nos)}") if not all_nos: return {"pendingCount": 0, "matchedCount": 0} raw = fetch_pending(all_nos) stats = upsert_records(raw) print(f"完成: 匹配 {len(raw)} | {stats}") emit_sync_summary("RIIN 刷新未完结", build_stats_rows("RIIN", stats, [ ("未完结订单", len(order_nos)), ("无面单(近7天)", len(no_label_nos)), ("合并去重", len(all_nos)), ("匹配", len(raw)), ])) return {"pendingCount": len(all_nos), "matchedCount": len(raw), **stats} if __name__ == "__main__": riin_refresh_pending.serve(name="riin-refresh-pending", cron="0 */3 * * *")