refactor: drop legacy rule binding fallbacks

This commit is contained in:
wren
2026-05-07 18:01:54 +08:00
parent e1adcf30d2
commit ad367ac5bf
@@ -3,7 +3,6 @@
from __future__ import annotations
import hashlib
import logging
from fastapi_common.fastapi_common_sqlalchemy.database import GetAsyncSession
from fastapi_common.fastapi_common_web.domain.responses import StatusCodeEnum
from fastapi_common.fastapi_common_web.exception.LeauditException import LeauditException
@@ -22,8 +21,6 @@ from fastapi_modules.fastapi_leaudit.services import IOssService, IRuleService
from fastapi_modules.fastapi_leaudit.services.impl.ossServiceImpl import OssServiceImpl
from fastapi_modules.fastapi_leaudit.services.impl.ruleGroupSupport import sync_doc_type_bindings_from_group
LOGGER = logging.getLogger(__name__)
class RuleServiceImpl(IRuleService):
"""规则服务实现。"""
@@ -438,8 +435,7 @@ class RuleServiceImpl(IRuleService):
)
async def ListBindings(self, RuleType: str | None = None, Region: str | None = None) -> list[RuleBindingVO]:
"""列出规则类型绑定,优先读取新分组绑定,旧表仅作为兼容兜底"""
region = Region or ""
"""列出规则类型绑定,读取新分组绑定。"""
async with GetAsyncSession() as Session:
params: dict[str, object] = {}
filters = ["rs.deleted_at IS NULL", "dt.deleted_at IS NULL", "child.deleted_at IS NULL", "rgb.deleted_at IS NULL"]
@@ -476,63 +472,11 @@ class RuleServiceImpl(IRuleService):
rows = result.mappings().all()
bindings: list[RuleBindingVO] = []
seen_doc_type_rule_pairs: set[tuple[int, int]] = set()
covered_doc_type_ids: set[int] = set()
for row in rows:
pair = (int(row["doc_type_id"]), int(row["rule_set_id"]))
if pair in seen_doc_type_rule_pairs:
continue
seen_doc_type_rule_pairs.add(pair)
covered_doc_type_ids.add(int(row["doc_type_id"]))
bindings.append(
RuleBindingVO(
id=int(row["id"]),
docTypeId=int(row["doc_type_id"]),
docTypeCode=row["doc_type_code"],
ruleSetId=int(row["rule_set_id"]),
ruleType=row["rule_type"],
ruleName=row["rule_name"],
bindingMode=row["binding_mode"],
priority=int(row["priority"]),
isActive=bool(row["is_active"]),
note=row["note"],
)
)
legacy_filters = ["rs.deleted_at IS NULL", "b.deleted_at IS NULL"]
legacy_params: dict[str, object] = {}
if RuleType:
legacy_filters.append("rs.rule_type = :rule_type")
legacy_params["rule_type"] = RuleType
if region:
legacy_filters.append("b.region = :region")
legacy_params["region"] = region
if covered_doc_type_ids:
legacy_filters.append("b.doc_type_id <> ALL(:covered_doc_type_ids)")
legacy_params["covered_doc_type_ids"] = list(covered_doc_type_ids)
legacy_where_clause = " AND ".join(legacy_filters)
legacy_result = await Session.execute(
text(
f"""
SELECT
b.id,
b.doc_type_id,
b.doc_type_code,
b.rule_set_id,
b.binding_mode,
b.priority,
b.is_active,
b.note,
rs.rule_type,
rs.rule_name
FROM leaudit_rule_type_bindings b
JOIN leaudit_rule_sets rs ON rs.id = b.rule_set_id
WHERE {legacy_where_clause}
ORDER BY rs.rule_type, b.priority DESC, b.id DESC
"""
),
legacy_params,
)
for row in legacy_result.mappings().all():
bindings.append(
RuleBindingVO(
id=int(row["id"]),
@@ -740,83 +684,9 @@ class RuleServiceImpl(IRuleService):
note=refreshed["note"],
)
Existing = await Session.execute(
text(
"""
SELECT
b.id, b.doc_type_id, b.doc_type_code, b.rule_set_id,
b.binding_mode, b.priority, b.is_active, b.note,
rs.rule_type, rs.rule_name
FROM leaudit_rule_type_bindings b
JOIN leaudit_rule_sets rs ON rs.id = b.rule_set_id
WHERE b.id = :bid
LIMIT 1
"""
),
{"bid": BindingId},
)
Row = Existing.mappings().first()
if not Row:
raise LeauditException(StatusCodeEnum.HTTP_404_NOT_FOUND, "绑定记录不存在")
LOGGER.warning(
"rule binding legacy fallback hit on update: binding_id=%s doc_type_id=%s rule_set_id=%s",
BindingId,
Row.get("doc_type_id"),
Row.get("rule_set_id"),
)
SetClauses: list[str] = []
Params: dict[str, object] = {"bid": BindingId}
if IsActive is not None:
SetClauses.append("is_active = :is_active")
Params["is_active"] = IsActive
if Priority is not None:
SetClauses.append("priority = :priority")
Params["priority"] = Priority
if BindingMode is not None:
SetClauses.append("binding_mode = :binding_mode")
Params["binding_mode"] = BindingMode
if Note is not None:
SetClauses.append("note = :note")
Params["note"] = Note
if SetClauses:
SetClauses.append("updated_at = now()")
await Session.execute(
text(f"UPDATE leaudit_rule_type_bindings SET {', '.join(SetClauses)} WHERE id = :bid"),
Params,
)
await Session.commit()
Result = await Session.execute(
text(
"""
SELECT
b.id, b.doc_type_id, b.doc_type_code, b.rule_set_id,
b.binding_mode, b.priority, b.is_active, b.note,
rs.rule_type, rs.rule_name
FROM leaudit_rule_type_bindings b
JOIN leaudit_rule_sets rs ON rs.id = b.rule_set_id
WHERE b.id = :bid
LIMIT 1
"""
),
{"bid": BindingId},
)
Row = Result.mappings().first()
return RuleBindingVO(
id=int(Row["id"]),
docTypeId=int(Row["doc_type_id"]),
docTypeCode=Row["doc_type_code"],
ruleSetId=int(Row["rule_set_id"]),
ruleType=Row["rule_type"],
ruleName=Row["rule_name"],
bindingMode=Row["binding_mode"],
priority=int(Row["priority"]),
isActive=bool(Row["is_active"]),
note=Row["note"],
raise LeauditException(
StatusCodeEnum.HTTP_404_NOT_FOUND,
"绑定记录不存在或已迁移到新分组绑定,请刷新列表后重试",
)
async def DeleteBinding(self, BindingId: int) -> None:
@@ -844,18 +714,10 @@ class RuleServiceImpl(IRuleService):
await Session.commit()
return
Result = await Session.execute(
text("DELETE FROM leaudit_rule_type_bindings WHERE id = :bid"),
{"bid": BindingId},
raise LeauditException(
StatusCodeEnum.HTTP_404_NOT_FOUND,
"绑定记录不存在或已迁移到新分组绑定,请刷新列表后重试",
)
if Result.rowcount:
LOGGER.warning(
"rule binding legacy fallback hit on delete: binding_id=%s",
BindingId,
)
await Session.commit()
if Result.rowcount == 0:
raise LeauditException(StatusCodeEnum.HTTP_404_NOT_FOUND, "绑定记录不存在")
async def _SwitchVersion(
self,