- Introduced `TemplateService` for managing SQL templates, allowing for parameterized queries based on user input. - Added functionality to automatically reload templates upon asset creation in `analyst.py`. - Enhanced `CacheService` to support table generation bumping, ensuring cache invalidation on data changes. - Updated `RiskRepository` and `GatewayRepository` to trigger cache invalidation for relevant operations. - Expanded `analyst_schemas.py` to include new fields for template tracking in response metadata. - Created seed SQL script for populating initial templates and added unit tests for template rendering logic. This update significantly improves the efficiency of query handling by leveraging SQL templates, reducing reliance on LLM for common queries.
64 lines
1.9 KiB
Python
64 lines
1.9 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 text
|
||
from sqlalchemy.engine import Engine
|
||
|
||
from app.config.settings import settings
|
||
from app.utils.db import get_engine
|
||
|
||
|
||
class GatewayRepository:
|
||
"""core_trade 唯一写入口;仅 INSERT,不改不删(模拟网关语义)。"""
|
||
|
||
def __init__(self, engine: Engine | None = None) -> None:
|
||
self._engine = engine or get_engine(settings.mysql_core_database)
|
||
|
||
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,
|
||
},
|
||
)
|
||
try:
|
||
from app.service.analyst_cache_invalidate import bump_analyst_cache_for_trade
|
||
|
||
bump_analyst_cache_for_trade()
|
||
except Exception:
|
||
pass
|