fix(固资): 固资查询增加HXJM/HTT/HTE及其他查询优化
This commit is contained in:
1 parent
96cc3913fc
commit
8a0a7fd6ed
1 file changed
+119
-9
@@ -10,6 +10,7 @@ class Text2SQLService:
|
||||
_oracle_client_initialized = False
|
||||
_oracle_dll_handles = []
|
||||
_ASSET_AGENT_NAMES = {"固资管理"}
|
||||
_ASSET_ALLOWED_OPERATING_CENTER_CODES = ("HXJM", "HTT", "HTE")
|
||||
_ASSET_TABLE_MARKERS = (
|
||||
"ERP_VIEW_USER.ERP_FAJ_FILE_V",
|
||||
"ERP_VIEW_USER.ERP_FAB_FILE_V",
|
||||
@@ -578,8 +579,64 @@ class Text2SQLService:
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
def _build_asset_name_detail_sql(cls, keyword: str) -> str:
|
||||
def _extract_asset_nature_code(cls, query: str) -> str:
|
||||
text = cls._strip_asset_query_prefix(query)
|
||||
if not text:
|
||||
return ""
|
||||
if "无形资产" in text:
|
||||
return "2"
|
||||
if "有形资产" in text:
|
||||
return "1"
|
||||
if "土地" in text:
|
||||
return "3"
|
||||
if "递耗资产" in text:
|
||||
return "4"
|
||||
if "费用" in text:
|
||||
return "5"
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
def _build_asset_nature_condition(cls, base_alias: str, query: str) -> str:
|
||||
nature_code = cls._extract_asset_nature_code(query)
|
||||
if not nature_code:
|
||||
return ""
|
||||
return f"TRIM(NVL({base_alias}.faj09, '')) = '{nature_code}'"
|
||||
|
||||
@classmethod
|
||||
def _build_asset_person_condition(cls, base_alias: str, query: str) -> str:
|
||||
explicit_person = cls._extract_explicit_asset_person(query)
|
||||
if not explicit_person:
|
||||
return ""
|
||||
literal = cls._escape_sql_literal(re.sub(r"\s+", "", explicit_person))
|
||||
return (
|
||||
"EXISTS ("
|
||||
"SELECT 1 FROM ERP_VIEW_USER.ERP_GEN_FILE_V gen_scope "
|
||||
f"WHERE gen_scope.gen01 = {base_alias}.faj19 "
|
||||
f"AND gen_scope.site = {base_alias}.site "
|
||||
f"AND REGEXP_REPLACE(TRIM(gen_scope.gen02), '\\s+', '') = '{literal}'"
|
||||
")"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _build_asset_department_condition(cls, base_alias: str, query: str) -> str:
|
||||
explicit_department = cls._extract_explicit_asset_department(query)
|
||||
if not explicit_department:
|
||||
return ""
|
||||
literal = cls._escape_sql_literal(re.sub(r"\s+", "", explicit_department))
|
||||
return (
|
||||
"EXISTS ("
|
||||
"SELECT 1 FROM ERP_VIEW_USER.ERP_GEM_FILE_V gem_scope "
|
||||
f"WHERE gem_scope.gem01 = {base_alias}.faj20 "
|
||||
f"AND gem_scope.site = {base_alias}.site "
|
||||
f"AND REGEXP_REPLACE(TRIM(gem_scope.gem02), '\\s+', '') = '{literal}'"
|
||||
")"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _build_asset_name_detail_sql(cls, keyword: str, query: str = "") -> str:
|
||||
literal = cls._escape_sql_literal(keyword)
|
||||
nature_condition = cls._build_asset_nature_condition("faj", query)
|
||||
nature_sql = f"\n AND {nature_condition}" if nature_condition else ""
|
||||
return f"""
|
||||
SELECT DISTINCT
|
||||
faj.faj02 AS "财产编号",
|
||||
@@ -605,8 +662,11 @@ LEFT JOIN ERP_VIEW_USER.ERP_GEM_FILE_V gem
|
||||
LEFT JOIN ERP_VIEW_USER.ERP_FAF_FILE_V faf
|
||||
ON faf.faf01 = faj.faj21
|
||||
AND faf.site = faj.site
|
||||
WHERE UPPER(NVL(faj.faj06, ' ')) LIKE '%' || UPPER('{literal}') || '%'
|
||||
OR UPPER(NVL(faj.faj061, ' ')) LIKE '%' || UPPER('{literal}') || '%'
|
||||
WHERE (
|
||||
UPPER(NVL(faj.faj06, ' ')) LIKE '%' || UPPER('{literal}') || '%'
|
||||
OR UPPER(NVL(faj.faj061, ' ')) LIKE '%' || UPPER('{literal}') || '%'
|
||||
)
|
||||
{nature_sql}
|
||||
ORDER BY faj.faj02
|
||||
""".strip()
|
||||
|
||||
@@ -622,8 +682,10 @@ ORDER BY faj.faj02
|
||||
return bool(re.search(r"(明细|列表|有哪些|有什么)", text))
|
||||
|
||||
@classmethod
|
||||
def _build_company_asset_detail_sql(cls) -> str:
|
||||
return """
|
||||
def _build_company_asset_detail_sql(cls, query: str = "") -> str:
|
||||
nature_condition = cls._build_asset_nature_condition("faj", query)
|
||||
nature_sql = f"WHERE {nature_condition}\n" if nature_condition else ""
|
||||
return f"""
|
||||
SELECT DISTINCT
|
||||
faj.faj02 AS "财产编号",
|
||||
faj.faj06 AS "中文名称",
|
||||
@@ -650,6 +712,7 @@ LEFT JOIN ERP_VIEW_USER.ERP_GEM_FILE_V gem
|
||||
LEFT JOIN ERP_VIEW_USER.ERP_FAF_FILE_V faf
|
||||
ON faf.faf01 = faj.faj21
|
||||
AND faf.site = faj.site
|
||||
{nature_sql}\
|
||||
ORDER BY faj.faj02
|
||||
""".strip()
|
||||
|
||||
@@ -658,11 +721,11 @@ ORDER BY faj.faj02
|
||||
if not (cls._is_asset_management_agent(agent) or cls._looks_like_asset_query(query)):
|
||||
return None
|
||||
if cls._is_company_asset_detail_query(query):
|
||||
return cls._build_company_asset_detail_sql()
|
||||
return cls._build_company_asset_detail_sql(query)
|
||||
keyword = cls._extract_asset_name_keyword(query)
|
||||
if not keyword:
|
||||
return None
|
||||
return cls._build_asset_name_detail_sql(keyword)
|
||||
return cls._build_asset_name_detail_sql(keyword, query)
|
||||
|
||||
@classmethod
|
||||
def _build_asset_prompt_constraints(cls, query: str, user_context: dict | None, agent) -> str:
|
||||
@@ -677,6 +740,8 @@ ORDER BY faj.faj02
|
||||
explicit_person = explicit_scope.get("person")
|
||||
explicit_department = explicit_scope.get("department")
|
||||
self_query = cls._is_self_asset_query(query)
|
||||
asset_nature_code = cls._extract_asset_nature_code(query)
|
||||
asset_nature_name = cls._ASSET_VALUE_MAPPINGS.get("资产性质", {}).get(asset_nature_code, "")
|
||||
|
||||
if explicit_person:
|
||||
filters.append(f"- 用户已明确指定查询人:{explicit_person}。本次必须以该指定人作为保管人查询固定资产,不能回退到当前登录用户。")
|
||||
@@ -696,6 +761,8 @@ ORDER BY faj.faj02
|
||||
filters.append(
|
||||
f"- 如需联表保管人姓名,请通过 ERP_VIEW_USER.ERP_GEN_FILE_V `gen`,并使用 `gen.gen01 = faj.faj19 AND gen.site = faj.site AND TRIM(gen.gen02) = '{real_name}'`。"
|
||||
)
|
||||
if asset_nature_code and asset_nature_name:
|
||||
filters.append(f"- 用户已明确指定资产性质为“{asset_nature_name}”,必须增加 `TRIM(NVL(faj.faj09, '')) = '{asset_nature_code}'` 过滤,不能返回其他资产性质。")
|
||||
|
||||
return "\n".join(
|
||||
[
|
||||
@@ -703,6 +770,7 @@ ORDER BY faj.faj02
|
||||
"- 固定资产查询优先使用 ERP_VIEW_USER.ERP_FAJ_FILE_V,表别名建议使用 `faj`。",
|
||||
"- 如需资产类型、保管人姓名、部门名称或存放位置名称,可联表 ERP_VIEW_USER.ERP_FAB_FILE_V `fab`、ERP_VIEW_USER.ERP_GEN_FILE_V `gen`、ERP_VIEW_USER.ERP_GEM_FILE_V `gem`、ERP_VIEW_USER.ERP_FAF_FILE_V `faf`。",
|
||||
"- 固资相关联表必须同时补齐 `site` 条件,例如 `gen.site = faj.site`、`gem.site = faj.site`、`faf.site = faj.site`、`fab.site = faj.site`,避免同编码跨营运中心造成重复记录。",
|
||||
"- 固资数据只允许保留 `faj22`(存放营运中心)属于 `HXJM`、`HTT`、`HTE` 的记录;生成 SQL 时必须显式加上 `UPPER(TRIM(NVL(faj.faj22, ''))) IN ('HXJM', 'HTT', 'HTE')` 过滤。",
|
||||
"- 查询固定资产明细列表时优先使用 `SELECT DISTINCT`;如果是统计资产数量,优先使用 `COUNT(DISTINCT faj.faj02)`。",
|
||||
"- 返回结果所有列必须使用中文别名,例如 `faj.faj02 AS \"财产编号\"`、`faj.faj06 AS \"资产名称\"`、`faj.faj08 AS \"规格型号\"`。",
|
||||
"- 日期时间字段展示时不要去掉时分秒;若使用 TO_CHAR 格式化,必须保留为 `YYYY-MM-DD HH24:MI:SS`,不能只输出日期。",
|
||||
@@ -774,6 +842,23 @@ ORDER BY faj.faj02
|
||||
text = str(sql or "").strip().rstrip(";")
|
||||
return re.sub(r"(?is)\border\s+by\b[\s\S]*$", "", text).strip()
|
||||
|
||||
@classmethod
|
||||
def _build_asset_operating_center_condition(cls, base_alias: str) -> str:
|
||||
allowed_codes = ", ".join(f"'{code}'" for code in cls._ASSET_ALLOWED_OPERATING_CENTER_CODES)
|
||||
return f"UPPER(TRIM(NVL({base_alias}.faj22, ''))) IN ({allowed_codes})"
|
||||
|
||||
@staticmethod
|
||||
def _append_sql_predicate(sql: str, predicate: str) -> str:
|
||||
text = str(sql or "").strip().rstrip(";")
|
||||
anchor = re.search(r"(?is)\b(group\s+by|order\s+by|having)\b", text)
|
||||
if re.search(r"(?is)\bwhere\b", text):
|
||||
if anchor:
|
||||
return f"{text[:anchor.start()].rstrip()} AND {predicate} {text[anchor.start():].lstrip()}"
|
||||
return f"{text} AND {predicate}"
|
||||
if anchor:
|
||||
return f"{text[:anchor.start()].rstrip()} WHERE {predicate} {text[anchor.start():].lstrip()}"
|
||||
return f"{text} WHERE {predicate}"
|
||||
|
||||
@classmethod
|
||||
def _build_asset_total_count_sql(cls, sql: str) -> str | None:
|
||||
if cls._is_aggregate_sql(sql):
|
||||
@@ -902,6 +987,28 @@ ORDER BY faj.faj02
|
||||
|
||||
return join_pattern.sub(_replace, sql)
|
||||
|
||||
@classmethod
|
||||
def _ensure_asset_operating_center_filter(cls, sql: str, base_alias: str) -> str:
|
||||
predicate = cls._build_asset_operating_center_condition(base_alias)
|
||||
return cls._append_sql_predicate(sql, predicate)
|
||||
|
||||
@classmethod
|
||||
def _ensure_asset_nature_filter(cls, sql: str, base_alias: str, query: str) -> str:
|
||||
predicate = cls._build_asset_nature_condition(base_alias, query)
|
||||
if not predicate:
|
||||
return sql
|
||||
return cls._append_sql_predicate(sql, predicate)
|
||||
|
||||
@classmethod
|
||||
def _ensure_asset_scope_filter(cls, sql: str, base_alias: str, query: str) -> str:
|
||||
department_predicate = cls._build_asset_department_condition(base_alias, query)
|
||||
if department_predicate:
|
||||
return cls._append_sql_predicate(sql, department_predicate)
|
||||
person_predicate = cls._build_asset_person_condition(base_alias, query)
|
||||
if person_predicate:
|
||||
return cls._append_sql_predicate(sql, person_predicate)
|
||||
return sql
|
||||
|
||||
@classmethod
|
||||
def _ensure_asset_select_distinct(cls, sql: str) -> str:
|
||||
if cls._is_aggregate_sql(sql):
|
||||
@@ -938,6 +1045,9 @@ ORDER BY faj.faj02
|
||||
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_scope_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_datetime_precision(normalized_sql)
|
||||
return cls._ensure_asset_select_distinct(normalized_sql)
|
||||
|
||||
@@ -1011,7 +1121,7 @@ ORDER BY faj.faj02
|
||||
def generate_sql(cls, query: str, user_context: dict | None = None, agent=None) -> str:
|
||||
special_sql = cls._build_special_asset_sql(query, agent=agent)
|
||||
if special_sql:
|
||||
return special_sql
|
||||
return cls._rewrite_asset_sql(special_sql, query, agent=agent)
|
||||
|
||||
schema = cls.get_schema()
|
||||
asset_constraints = cls._build_asset_prompt_constraints(query, user_context, agent)
|
||||
@@ -1051,7 +1161,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:
|
||||
special_sql = cls._build_special_asset_sql(query, agent=agent)
|
||||
if special_sql:
|
||||
return special_sql
|
||||
return cls._rewrite_asset_sql(special_sql, query, agent=agent)
|
||||
|
||||
schema = cls.get_schema()
|
||||
invalid_identifier = cls._extract_invalid_identifier(error_message)
|
||||
|
||||
Reference in new issue
Block a user