feat: OCR v2 batch + RIIN waybill + 双系统 label-ocr flow(hourly)
This commit is contained in:
+43
-12
@@ -100,22 +100,25 @@ class SyncDB:
|
||||
|
||||
# ---------- OCR 面单 enrichment ----------
|
||||
|
||||
def get_ocr_pending(self, limit: int = 200) -> list[tuple[str, datetime, str, str | None]]:
|
||||
"""查待 OCR 的 SDS 订单:(orderNo, createdAt, labelPdfUrl, destinationCountry)。
|
||||
def get_ocr_pending(self, limit: int = 0) -> list[tuple[str, datetime, str, str | None]]:
|
||||
"""查待 OCR 订单(所有系统):(orderNo, createdAt, labelPdfUrl, destinationCountry)。
|
||||
|
||||
条件:SDS、有 labelPdfUrl、ocr_layout 未处理(Null)、已发货/已完成。
|
||||
条件:有 labelPdfUrl、ocr_layout 未处理(Null)。
|
||||
limit=0 表示不限。
|
||||
"""
|
||||
sql = (
|
||||
'SELECT "orderNo","createdAt","labelPdfUrl","destinationCountry" '
|
||||
'FROM "Orders" '
|
||||
'WHERE "labelPdfUrl" IS NOT NULL AND "ocr_layout" IS NULL '
|
||||
'ORDER BY "orderedAt" DESC'
|
||||
)
|
||||
params: list = []
|
||||
if limit > 0:
|
||||
sql += " LIMIT %s"
|
||||
params.append(limit)
|
||||
with self._conn() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'''SELECT "orderNo","createdAt","labelPdfUrl","destinationCountry"
|
||||
FROM "Orders"
|
||||
WHERE "systemName"=%s AND "labelPdfUrl" IS NOT NULL
|
||||
AND "ocr_layout" IS NULL
|
||||
AND "systemStatus"=ANY(%s)
|
||||
ORDER BY "orderedAt" DESC LIMIT %s''',
|
||||
("SDS", ["已完成", "部分发货", "备货中", "待确认"], limit),
|
||||
)
|
||||
cur.execute(sql, params)
|
||||
return [(r[0], r[1], r[2], r[3]) for r in cur.fetchall()]
|
||||
|
||||
def update_ocr_fields(
|
||||
@@ -135,6 +138,34 @@ class SyncDB:
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def update_ocr_batch(self, updates: list[tuple[str, datetime, dict[str, str | None]]]) -> int:
|
||||
"""批量更新 ocr 字段(单连接单事务)。updates: [(orderNo, createdAt, {field: value})]。
|
||||
|
||||
返回更新行数。
|
||||
"""
|
||||
if not updates:
|
||||
return 0
|
||||
rows = 0
|
||||
with self._conn() as conn:
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
for order_no, created_at, fields in updates:
|
||||
if not fields:
|
||||
continue
|
||||
assignments = ", ".join(f'"{k}"=%s' for k in fields)
|
||||
params = list(fields.values()) + [order_no, created_at]
|
||||
cur.execute(
|
||||
f'''UPDATE "Orders" SET {assignments}
|
||||
WHERE "orderNo"=%s AND "createdAt"=%s''',
|
||||
params,
|
||||
)
|
||||
rows += cur.rowcount
|
||||
conn.commit()
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
raise
|
||||
return rows
|
||||
|
||||
def count_ocr_status(self) -> dict[str, int]:
|
||||
"""统计 OCR 处理状态(done/error/unsupported/pending)。"""
|
||||
with self._conn() as conn:
|
||||
|
||||
@@ -195,7 +195,7 @@ def map_riin_order(raw: Any) -> OrderUpsertInput:
|
||||
addressLine3=raw.receiver_town,
|
||||
postalCode=raw.post_code,
|
||||
storeName=raw.platform_shop_name,
|
||||
labelPdfUrl=None,
|
||||
labelPdfUrl=raw.waybill,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user