AutoRpa/rpa_cart/repo.py

98 lines
2.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:写支付行。"""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from db import CREATE_USER_NAME, RepoUnavailable, connect_from_env, oracle_dsn
from settle import YAOEX_PLATFORM_TYPE
_SELECT_PAY_ORDER_NOS = """
SELECT DISTINCT pay_order_no
FROM rpa_cart_settlement_goods_data
WHERE operate_type = 2
AND platform_type = :platform_type
AND pay_order_no IS NOT NULL
"""
_INSERT_PAY = """
INSERT INTO rpa_cart_settlement_goods_data (
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, push_status, create_time,
org_code, multi_platform_flag, create_user_name
) VALUES (
: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, :push_status, SYSDATE,
:org_code, :multi_platform_flag, :create_user_name
)
"""
_PAY_BIND_KEYS = (
"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",
"push_status",
"org_code",
"multi_platform_flag",
)
class EmptyPayRepo:
def load_existing_pay_order_nos(self) -> set[str]:
return set()
def save_pay_rows(self, rows: list[dict[str, Any]]) -> None:
return
class OraclePayRepo:
def __init__(self, connect: Callable):
self._connect = connect
@classmethod
def from_env(cls) -> "OraclePayRepo":
oracle_dsn()
return cls(connect=connect_from_env)
def load_existing_pay_order_nos(self) -> set[str]:
conn = self._connect()
try:
with conn.cursor() as cur:
cur.execute(_SELECT_PAY_ORDER_NOS, {"platform_type": YAOEX_PLATFORM_TYPE})
return {str(order_no) for (order_no,) in cur.fetchall() if order_no}
except Exception as exc:
raise RepoUnavailable(str(exc)) from exc
finally:
conn.close()
def save_pay_rows(self, rows: list[dict[str, Any]]) -> None:
if not rows:
return
conn = self._connect()
try:
with conn.cursor() as cur:
for row in rows:
payload = {key: row.get(key) for key in _PAY_BIND_KEYS}
payload["create_user_name"] = CREATE_USER_NAME
cur.execute(_INSERT_PAY, payload)
conn.commit()
except Exception as exc:
try:
conn.rollback()
except Exception:
pass
raise RepoUnavailable(str(exc)) from exc
finally:
conn.close()