Files
ai-agent-admin/backend-fastapi/online_dev/report_manager/engine/convert_lookup.py
T

102 lines
3.5 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.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""convertConfig 运行时 ID→名称查找缓存(对标 JNPF DataSetSwapUtil"""
from __future__ import annotations
from typing import Any, Dict, Optional
class ConvertLookupCache:
"""同步查找表;Golden 测试用 config.names,运行时可预填充。"""
def __init__(self) -> None:
self._users: Dict[str, str] = {}
self._depts: Dict[str, str] = {}
self._orgs: Dict[str, str] = {}
self._roles: Dict[str, str] = {}
self._groups: Dict[str, str] = {}
self._dicts: Dict[str, Dict[str, str]] = {}
def resolve(
self,
rtype: str,
value: Any,
config: Optional[Dict[str, Any]] = None,
) -> Any:
if value is None or value == "":
return value
config = config or {}
inline = config.get("names") or config.get("optionsMap") or {}
if isinstance(inline, dict):
key = str(value)
if key in inline:
return inline[key]
rtype = (rtype or "").lower()
key = str(value)
if rtype in ("user", "users"):
return self._users.get(key, value)
if rtype in ("department", "dep", "dept"):
return self._depts.get(key, value)
if rtype in ("organize", "org", "company"):
return self._orgs.get(key, value)
if rtype == "role":
return self._roles.get(key, value)
if rtype == "group":
return self._groups.get(key, value)
if rtype in ("dictionary", "dict", "select"):
dict_type = config.get("dictionaryType") or config.get("dictType") or ""
if dict_type and dict_type in self._dicts:
return self._dicts[dict_type].get(key, value)
return value
def put_dict(self, dict_type: str, mapping: Dict[str, str]) -> None:
self._dicts[dict_type] = mapping
def put_users(self, mapping: Dict[str, str]) -> None:
self._users.update(mapping)
def put_depts(self, mapping: Dict[str, str]) -> None:
self._depts.update(mapping)
def put_orgs(self, mapping: Dict[str, str]) -> None:
self._orgs.update(mapping)
async def build_lookup_cache_from_db(db) -> ConvertLookupCache:
"""从 core 模块批量加载常用 ID 映射(best-effort)。"""
cache = ConvertLookupCache()
try:
from sqlalchemy import select
from core.user.model import User
from core.dept.model import Dept
users = (await db.execute(select(User.id, User.name).where(User.is_deleted == False))).all()
cache.put_users({str(uid): name or "" for uid, name in users if uid})
depts = (await db.execute(select(Dept.id, Dept.name).where(Dept.is_deleted == False))).all()
cache.put_depts({str(did): name or "" for did, name in depts if did})
except Exception:
pass
try:
from sqlalchemy import select
from core.dict_item.model import DictItem
rows = (
await db.execute(
select(DictItem.dict_id, DictItem.value, DictItem.label).where(
DictItem.is_deleted == False
)
)
).all()
by_dict: Dict[str, Dict[str, str]] = {}
for dict_id, val, label in rows:
if not dict_id:
continue
by_dict.setdefault(str(dict_id), {})[str(val)] = label or str(val)
for dict_id, mapping in by_dict.items():
cache.put_dict(dict_id, mapping)
except Exception:
pass
return cache