AutoRpa/rpa_oms/app/outbound/oracle_repo.py

55 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""接口 2:从表 2 读取 push_status=0。不更新推送状态。"""
from __future__ import annotations
import os
from collections.abc import Callable
from ..inbound.oracle_repo import _ensure_client
from ..inbound.repo import RepoUnavailable
_LIST_UNPUSHED_SQL = """
SELECT
fid, operate_type, oms_id, platform_type, supplier_name, bar_code,
purchase_price, purchase_qty, purchase_total, status, fail_reason,
pay_order_no, pay_order_dtl_id, org_code, push_status
FROM rpa_cart_settlement_goods_data
WHERE push_status = 0
ORDER BY oms_id, fid
"""
class OracleCartSettlementRepo:
def __init__(self, connect: Callable):
self._connect = connect
@classmethod
def from_env(cls) -> "OracleCartSettlementRepo":
user = os.getenv("RPA_ORACLE_USER", "")
password = os.getenv("RPA_ORACLE_PASSWORD", "")
host = os.getenv("RPA_ORACLE_HOST", "")
if not user or not password or not host:
raise RepoUnavailable("未配置 RPA_ORACLE_HOST / RPA_ORACLE_USER / RPA_ORACLE_PASSWORD")
port = os.getenv("RPA_ORACLE_PORT", "1521")
service = os.getenv("RPA_ORACLE_SERVICE", "rpa")
dsn = f"{host}:{port}/{service}"
def connect():
_ensure_client()
import oracledb
return oracledb.connect(user=user, password=password, dsn=dsn)
return cls(connect=connect)
def list_unpushed(self) -> list[dict]:
conn = self._connect()
try:
with conn.cursor() as cur:
cur.execute(_LIST_UNPUSHED_SQL)
cols = [item[0].lower() for item in cur.description]
return [dict(zip(cols, row)) for row in cur.fetchall()]
except Exception as exc:
raise RepoUnavailable(str(exc)) from exc
finally:
conn.close()