67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
"""模拟交易网关写侧(PRD FR-1;FRAMEWORK §3 例外条款)。
|
||
|
||
仅本类可 INSERT jinrong_core.core_trade;core_ro 仍只读。生产环境由真实
|
||
交易系统回调替代,本类随 app/gateway/ 退役。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from decimal import Decimal
|
||
from typing import Any
|
||
|
||
from sqlalchemy import create_engine, text
|
||
from sqlalchemy.engine import Engine
|
||
|
||
from app.config.settings import settings
|
||
|
||
|
||
class GatewayRepository:
|
||
"""core_trade 唯一写入口;仅 INSERT,不改不删(模拟网关语义)。"""
|
||
|
||
def __init__(self, engine: Engine | None = None) -> None:
|
||
self._engine = engine or self._default_engine()
|
||
|
||
@staticmethod
|
||
def _default_engine() -> Engine:
|
||
pwd = settings.mysql_password
|
||
auth = f"{settings.mysql_user}:{pwd}" if pwd else settings.mysql_user
|
||
url = (
|
||
f"mysql+pymysql://{auth}@{settings.mysql_host}:{settings.mysql_port}"
|
||
f"/{settings.mysql_core_database}?charset=utf8mb4"
|
||
)
|
||
return create_engine(url, pool_pre_ping=True)
|
||
|
||
def insert_trade(
|
||
self,
|
||
trade_id: str,
|
||
customer_id: str,
|
||
product_id: str,
|
||
trade_type: str,
|
||
amount: Decimal,
|
||
traded_at: datetime,
|
||
trade_status: str = "confirmed",
|
||
) -> None:
|
||
sql = text(
|
||
"""
|
||
INSERT INTO core_trade
|
||
(trade_id, customer_id, product_id, trade_type, amount,
|
||
trade_status, traded_at)
|
||
VALUES (:tid, :cid, :pid, :ttype, :amount, :status, :at)
|
||
"""
|
||
)
|
||
with self._engine.begin() as conn:
|
||
conn.execute(
|
||
sql,
|
||
{
|
||
"tid": trade_id,
|
||
"cid": customer_id,
|
||
"pid": product_id,
|
||
"ttype": trade_type,
|
||
# str 无损传递:MySQL DECIMAL 隐式转换;sqlite text SQL 不支持 Decimal 绑定
|
||
"amount": str(amount),
|
||
"status": trade_status,
|
||
"at": traded_at,
|
||
},
|
||
)
|