fix(chat): 固定资产优化本人查询明细资产
This commit is contained in:
1 parent
c01ddb8b23
commit
5d5127f5e8
1 file changed
+62
-12
@@ -114,6 +114,14 @@ class Text2SQLService:
|
|||||||
_ASSET_NAME_HINT_PATTERN = re.compile(
|
_ASSET_NAME_HINT_PATTERN = re.compile(
|
||||||
r"(?:机|设备|系统|软件|电脑|打印机|服务器|模具|工装|平台|车辆|空调|机床|产线|生产线)$"
|
r"(?:机|设备|系统|软件|电脑|打印机|服务器|模具|工装|平台|车辆|空调|机床|产线|生产线)$"
|
||||||
)
|
)
|
||||||
|
_ASSET_SELF_QUERY_MARKERS = (
|
||||||
|
"我",
|
||||||
|
"本人",
|
||||||
|
"自己",
|
||||||
|
"当前用户",
|
||||||
|
"当前登录用户",
|
||||||
|
"登录用户",
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _backend_dir() -> str:
|
def _backend_dir() -> str:
|
||||||
@@ -483,6 +491,8 @@ class Text2SQLService:
|
|||||||
text = cls._strip_asset_query_prefix(query)
|
text = cls._strip_asset_query_prefix(query)
|
||||||
if not text:
|
if not text:
|
||||||
return ""
|
return ""
|
||||||
|
if cls._is_self_asset_query(text):
|
||||||
|
return ""
|
||||||
match = cls._ASSET_PERSON_PATTERN.search(text)
|
match = cls._ASSET_PERSON_PATTERN.search(text)
|
||||||
if match:
|
if match:
|
||||||
candidate = cls._clean_asset_scope_value(match.group(1))
|
candidate = cls._clean_asset_scope_value(match.group(1))
|
||||||
@@ -500,7 +510,7 @@ class Text2SQLService:
|
|||||||
candidate = cls._clean_asset_scope_value(match.group(1))
|
candidate = cls._clean_asset_scope_value(match.group(1))
|
||||||
if not candidate:
|
if not candidate:
|
||||||
continue
|
continue
|
||||||
if candidate in {"我", "本人", "自己", "当前登录用户", "当前用户", "登录用户", "公司"}:
|
if candidate in {*cls._ASSET_SELF_QUERY_MARKERS, "公司"}:
|
||||||
continue
|
continue
|
||||||
if any(marker in candidate for marker in ("有哪些", "有多少", "多少", "固定资产", "固资", "资产")):
|
if any(marker in candidate for marker in ("有哪些", "有多少", "多少", "固定资产", "固资", "资产")):
|
||||||
continue
|
continue
|
||||||
@@ -526,8 +536,7 @@ class Text2SQLService:
|
|||||||
text = cls._strip_asset_query_prefix(query)
|
text = cls._strip_asset_query_prefix(query)
|
||||||
if not text:
|
if not text:
|
||||||
return False
|
return False
|
||||||
self_markers = ("我", "本人", "自己", "当前登录用户", "当前用户", "登录用户")
|
if not any(marker in text for marker in cls._ASSET_SELF_QUERY_MARKERS):
|
||||||
if not any(marker in text for marker in self_markers):
|
|
||||||
return False
|
return False
|
||||||
return bool(re.search(r"(固定资产|固资|资产)", text))
|
return bool(re.search(r"(固定资产|固资|资产)", text))
|
||||||
|
|
||||||
@@ -571,7 +580,7 @@ class Text2SQLService:
|
|||||||
continue
|
continue
|
||||||
if cls._ASSET_SUFFIX_DEPARTMENT_PATTERN.fullmatch(keyword):
|
if cls._ASSET_SUFFIX_DEPARTMENT_PATTERN.fullmatch(keyword):
|
||||||
continue
|
continue
|
||||||
if keyword in {"我", "本人", "自己", "当前登录用户", "当前用户", "登录用户"}:
|
if keyword in cls._ASSET_SELF_QUERY_MARKERS:
|
||||||
continue
|
continue
|
||||||
if cls._looks_like_person_name(keyword):
|
if cls._looks_like_person_name(keyword):
|
||||||
continue
|
continue
|
||||||
@@ -617,6 +626,42 @@ class Text2SQLService:
|
|||||||
")"
|
")"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _build_self_asset_condition(cls, base_alias: str, query: str, user_context: dict | None = None) -> str:
|
||||||
|
if not cls._is_self_asset_query(query):
|
||||||
|
return ""
|
||||||
|
normalized_user = user_context or {}
|
||||||
|
username = str(normalized_user.get("username") or "").strip()
|
||||||
|
employee_id = str(normalized_user.get("employee_id") or "").strip()
|
||||||
|
real_name = str(normalized_user.get("real_name") or "").strip()
|
||||||
|
|
||||||
|
id_candidates: list[str] = []
|
||||||
|
for raw_value in (employee_id, username):
|
||||||
|
value = str(raw_value or "").strip()
|
||||||
|
if value and value not in id_candidates:
|
||||||
|
id_candidates.append(value)
|
||||||
|
|
||||||
|
predicates: list[str] = []
|
||||||
|
if id_candidates:
|
||||||
|
literals = ", ".join(f"'{cls._escape_sql_literal(value)}'" for value in id_candidates)
|
||||||
|
predicates.append(f"TRIM(NVL({base_alias}.faj19, '')) IN ({literals})")
|
||||||
|
if real_name:
|
||||||
|
literal = cls._escape_sql_literal(re.sub(r"\s+", "", real_name))
|
||||||
|
predicates.append(
|
||||||
|
"EXISTS ("
|
||||||
|
"SELECT 1 FROM ERP_VIEW_USER.ERP_GEN_FILE_V gen_self "
|
||||||
|
f"WHERE gen_self.gen01 = {base_alias}.faj19 "
|
||||||
|
f"AND gen_self.site = {base_alias}.site "
|
||||||
|
f"AND REGEXP_REPLACE(TRIM(gen_self.gen02), '\\s+', '') = '{literal}'"
|
||||||
|
")"
|
||||||
|
)
|
||||||
|
|
||||||
|
if not predicates:
|
||||||
|
return ""
|
||||||
|
if len(predicates) == 1:
|
||||||
|
return predicates[0]
|
||||||
|
return "(" + " OR ".join(predicates) + ")"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _build_asset_department_condition(cls, base_alias: str, query: str) -> str:
|
def _build_asset_department_condition(cls, base_alias: str, query: str) -> str:
|
||||||
explicit_department = cls._extract_explicit_asset_department(query)
|
explicit_department = cls._extract_explicit_asset_department(query)
|
||||||
@@ -754,7 +799,9 @@ ORDER BY faj.faj02
|
|||||||
f"- 必须联表 ERP_VIEW_USER.ERP_GEM_FILE_V `gem`,并通过 `gem.gem01 = faj.faj20 AND gem.site = faj.site AND TRIM(gem.gem02) = '{explicit_department}'` 过滤。"
|
f"- 必须联表 ERP_VIEW_USER.ERP_GEM_FILE_V `gem`,并通过 `gem.gem01 = faj.faj20 AND gem.site = faj.site AND TRIM(gem.gem02) = '{explicit_department}'` 过滤。"
|
||||||
)
|
)
|
||||||
elif self_query:
|
elif self_query:
|
||||||
filters.append("- 用户当前是以“我 / 本人 / 当前用户”口径查询固定资产,本次必须只查询当前登录用户相关固资。")
|
filters.append("- 用户当前是以“我 / 本人 / 自己 / 当前用户 / 当前登录用户”口径查询固定资产,本次必须只查询当前登录用户相关固资。")
|
||||||
|
if username := normalized_user.get("username"):
|
||||||
|
filters.append(f"- 当前登录用户账号:{username}。如资产编码字段使用登录账号存储,需一并纳入过滤。")
|
||||||
if employee_id:
|
if employee_id:
|
||||||
filters.append(f"- 当前登录用户工号:{employee_id}。优先直接使用 `faj19 = '{employee_id}'` 过滤。")
|
filters.append(f"- 当前登录用户工号:{employee_id}。优先直接使用 `faj19 = '{employee_id}'` 过滤。")
|
||||||
if real_name:
|
if real_name:
|
||||||
@@ -1000,13 +1047,16 @@ ORDER BY faj.faj02
|
|||||||
return cls._append_sql_predicate(sql, predicate)
|
return cls._append_sql_predicate(sql, predicate)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _ensure_asset_scope_filter(cls, sql: str, base_alias: str, query: str) -> str:
|
def _ensure_asset_scope_filter(cls, sql: str, base_alias: str, query: str, user_context: dict | None = None) -> str:
|
||||||
department_predicate = cls._build_asset_department_condition(base_alias, query)
|
department_predicate = cls._build_asset_department_condition(base_alias, query)
|
||||||
if department_predicate:
|
if department_predicate:
|
||||||
return cls._append_sql_predicate(sql, department_predicate)
|
return cls._append_sql_predicate(sql, department_predicate)
|
||||||
person_predicate = cls._build_asset_person_condition(base_alias, query)
|
person_predicate = cls._build_asset_person_condition(base_alias, query)
|
||||||
if person_predicate:
|
if person_predicate:
|
||||||
return cls._append_sql_predicate(sql, person_predicate)
|
return cls._append_sql_predicate(sql, person_predicate)
|
||||||
|
self_predicate = cls._build_self_asset_condition(base_alias, query, user_context=user_context)
|
||||||
|
if self_predicate:
|
||||||
|
return cls._append_sql_predicate(sql, self_predicate)
|
||||||
return sql
|
return sql
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -1033,7 +1083,7 @@ ORDER BY faj.faj02
|
|||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _rewrite_asset_sql(cls, sql: str, query: str, agent=None) -> str:
|
def _rewrite_asset_sql(cls, sql: str, query: str, agent=None, user_context: dict | None = None) -> str:
|
||||||
if not cls._looks_like_asset_query(query, sql) and not cls._is_asset_management_agent(agent):
|
if not cls._looks_like_asset_query(query, sql) and not cls._is_asset_management_agent(agent):
|
||||||
return sql
|
return sql
|
||||||
|
|
||||||
@@ -1045,7 +1095,7 @@ ORDER BY faj.faj02
|
|||||||
for table_name in ("ERP_GEN_FILE_V", "ERP_GEM_FILE_V", "ERP_FAF_FILE_V", "ERP_FAB_FILE_V"):
|
for table_name in ("ERP_GEN_FILE_V", "ERP_GEM_FILE_V", "ERP_FAF_FILE_V", "ERP_FAB_FILE_V"):
|
||||||
normalized_sql = cls._ensure_asset_join_site(normalized_sql, base_alias, table_name)
|
normalized_sql = cls._ensure_asset_join_site(normalized_sql, base_alias, table_name)
|
||||||
|
|
||||||
normalized_sql = cls._ensure_asset_scope_filter(normalized_sql, base_alias, query)
|
normalized_sql = cls._ensure_asset_scope_filter(normalized_sql, base_alias, query, user_context=user_context)
|
||||||
normalized_sql = cls._ensure_asset_nature_filter(normalized_sql, base_alias, query)
|
normalized_sql = cls._ensure_asset_nature_filter(normalized_sql, base_alias, query)
|
||||||
normalized_sql = cls._ensure_asset_operating_center_filter(normalized_sql, base_alias)
|
normalized_sql = cls._ensure_asset_operating_center_filter(normalized_sql, base_alias)
|
||||||
normalized_sql = cls._ensure_asset_datetime_precision(normalized_sql)
|
normalized_sql = cls._ensure_asset_datetime_precision(normalized_sql)
|
||||||
@@ -1121,7 +1171,7 @@ ORDER BY faj.faj02
|
|||||||
def generate_sql(cls, query: str, user_context: dict | None = None, agent=None) -> str:
|
def generate_sql(cls, query: str, user_context: dict | None = None, agent=None) -> str:
|
||||||
special_sql = cls._build_special_asset_sql(query, agent=agent)
|
special_sql = cls._build_special_asset_sql(query, agent=agent)
|
||||||
if special_sql:
|
if special_sql:
|
||||||
return cls._rewrite_asset_sql(special_sql, query, agent=agent)
|
return cls._rewrite_asset_sql(special_sql, query, agent=agent, user_context=user_context)
|
||||||
|
|
||||||
schema = cls.get_schema()
|
schema = cls.get_schema()
|
||||||
asset_constraints = cls._build_asset_prompt_constraints(query, user_context, agent)
|
asset_constraints = cls._build_asset_prompt_constraints(query, user_context, agent)
|
||||||
@@ -1148,7 +1198,7 @@ ORDER BY faj.faj02
|
|||||||
user_prompt = f"用户问题:{query}"
|
user_prompt = f"用户问题:{query}"
|
||||||
sql = cls._chat_completion(system_prompt, user_prompt, temperature=0)
|
sql = cls._chat_completion(system_prompt, user_prompt, temperature=0)
|
||||||
sql = sql.rstrip(";")
|
sql = sql.rstrip(";")
|
||||||
sql = cls._rewrite_asset_sql(sql, query, agent=agent)
|
sql = cls._rewrite_asset_sql(sql, query, agent=agent, user_context=user_context)
|
||||||
|
|
||||||
if not cls._is_safe_query(sql):
|
if not cls._is_safe_query(sql):
|
||||||
return "SELECT 'Generated SQL was blocked because it is not a safe read-only SELECT query' AS error FROM DUAL"
|
return "SELECT 'Generated SQL was blocked because it is not a safe read-only SELECT query' AS error FROM DUAL"
|
||||||
@@ -1161,7 +1211,7 @@ ORDER BY faj.faj02
|
|||||||
def repair_sql(cls, query: str, failed_sql: str, error_message: str, user_context: dict | None = None, agent=None) -> str:
|
def repair_sql(cls, query: str, failed_sql: str, error_message: str, user_context: dict | None = None, agent=None) -> str:
|
||||||
special_sql = cls._build_special_asset_sql(query, agent=agent)
|
special_sql = cls._build_special_asset_sql(query, agent=agent)
|
||||||
if special_sql:
|
if special_sql:
|
||||||
return cls._rewrite_asset_sql(special_sql, query, agent=agent)
|
return cls._rewrite_asset_sql(special_sql, query, agent=agent, user_context=user_context)
|
||||||
|
|
||||||
schema = cls.get_schema()
|
schema = cls.get_schema()
|
||||||
invalid_identifier = cls._extract_invalid_identifier(error_message)
|
invalid_identifier = cls._extract_invalid_identifier(error_message)
|
||||||
@@ -1196,7 +1246,7 @@ ORDER BY faj.faj02
|
|||||||
try:
|
try:
|
||||||
sql = cls._chat_completion(system_prompt, user_prompt, temperature=0)
|
sql = cls._chat_completion(system_prompt, user_prompt, temperature=0)
|
||||||
sql = sql.rstrip(";")
|
sql = sql.rstrip(";")
|
||||||
sql = cls._rewrite_asset_sql(sql, query, agent=agent)
|
sql = cls._rewrite_asset_sql(sql, query, agent=agent, user_context=user_context)
|
||||||
if not cls._is_safe_query(sql):
|
if not cls._is_safe_query(sql):
|
||||||
return "SELECT 'Generated SQL was blocked because it is not a safe read-only SELECT query' AS error FROM DUAL"
|
return "SELECT 'Generated SQL was blocked because it is not a safe read-only SELECT query' AS error FROM DUAL"
|
||||||
return sql
|
return sql
|
||||||
|
|||||||
Reference in new issue
Block a user