37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
|
|
from sqlalchemy import text
|
|
from sqlalchemy import text
|
|
from config.pgDb import pg_pool
|
|
from config.ssDb import mssql_pool
|
|
from sqlalchemy import text
|
|
|
|
def executeSQL(sql: str):
|
|
"""
|
|
执行 SQL 并返回结果列表,每行是 dict
|
|
"""
|
|
with mssql_pool.getConn() as conn:
|
|
result = conn.execute(text(sql))
|
|
# SQLAlchemy 2.x 返回 Row 对象,转成 dict
|
|
return [dict(row._mapping) for row in result]
|
|
|
|
def get_company_list(user_id: str):
|
|
# 1️⃣ 从 PostgreSQL 获取 tenant_id
|
|
with pg_pool.getConn() as pg_conn:
|
|
with pg_conn.cursor() as cur:
|
|
cur.execute("SELECT bbit_tenant_id FROM users WHERE id = %s", (user_id,))
|
|
row = cur.fetchone()
|
|
tenant_id = row[0] if row else None
|
|
|
|
# 2️⃣ 从 SQL Server 查询租户信息
|
|
if tenant_id:
|
|
query = text("SELECT Id, Name FROM dbo.POC_TENANTS WHERE Id = :tenant_id")
|
|
params = {"tenant_id": tenant_id}
|
|
else:
|
|
query = text("SELECT Id, Name FROM dbo.POC_TENANTS")
|
|
params = {}
|
|
|
|
with mssql_pool.getConn() as mssql_conn:
|
|
result = mssql_conn.execute(query, params)
|
|
return [{"id": str(row[0]), "name": row[1]} for row in result.fetchall()]
|
|
|
|
|