feat: label-ocr 默认 GB+US 近7天; refresh_pending 合并无面单订单(近7天)

This commit is contained in:
yeuimu
2026-07-31 01:57:52 +00:00
parent 3e075fd366
commit a742f4169e
4 changed files with 63 additions and 25 deletions
+27 -7
View File
@@ -8,7 +8,7 @@
from __future__ import annotations
from datetime import datetime
from datetime import datetime, timedelta
from typing import Any
from psycopg2.extras import execute_values
@@ -101,12 +101,16 @@ class SyncDB:
# ---------- OCR 面单 enrichment ----------
def get_ocr_pending(
self, limit: int = 0, destination_country: str | None = None, courier_source: str | None = None,
self, limit: int = 0,
destination_countries: list[str] | None = None,
courier_source: str | None = None,
days: int = 0,
) -> list[tuple[str, datetime, str, str | None]]:
"""查待 OCR 订单(所有系统):(orderNo, createdAt, labelPdfUrl, destinationCountry)。
条件:有 labelPdfUrl、ocr_layout 未处理(Null)。
limit=0 表示不限。可选按 destinationCountry / courierSource 筛选。
条件:有 labelPdfUrl、ocrLayout 未处理(Null)。
limit=0 表示不限。可选按 destinationCountries / courierSource 筛选。
days>0 时只取近 N 天下单的订单。
"""
sql = (
'SELECT "orderNo","createdAt","labelPdfUrl","destinationCountry" '
@@ -114,12 +118,15 @@ class SyncDB:
'WHERE "labelPdfUrl" IS NOT NULL AND "ocrLayout" IS NULL'
)
params: list = []
if destination_country:
sql += ' AND "destinationCountry"=%s'
params.append(destination_country)
if destination_countries:
sql += ' AND "destinationCountry"=ANY(%s)'
params.append(destination_countries)
if courier_source:
sql += ' AND "courierSource"=%s'
params.append(courier_source)
if days > 0:
sql += ' AND "orderedAt" >= %s'
params.append(datetime.now() - timedelta(days=days))
sql += ' ORDER BY "orderedAt" DESC'
if limit > 0:
sql += " LIMIT %s"
@@ -129,6 +136,19 @@ class SyncDB:
cur.execute(sql, params)
return [(r[0], r[1], r[2], r[3]) for r in cur.fetchall()]
def get_orders_without_label(self, system_name: str, days: int = 7) -> list[str]:
"""查近 N 天下单但无 labelPdfUrl 的订单号。"""
cutoff = datetime.now() - timedelta(days=days)
with self._conn() as conn:
with conn.cursor() as cur:
cur.execute(
'SELECT DISTINCT "orderNo" FROM "Orders" '
'WHERE "systemName"=%s AND "labelPdfUrl" IS NULL AND "orderedAt" >= %s',
(system_name, cutoff),
)
rows = cur.fetchall()
return [r[0] for r in rows]
def update_ocr_fields(
self, order_no: str, created_at: datetime, fields: dict[str, str | None]
) -> None: