feat: migrate rule bindings to group-based flow

This commit is contained in:
wren
2026-05-07 17:43:20 +08:00
parent 75c2111209
commit f8eb2dc817
8 changed files with 871 additions and 361 deletions
@@ -250,7 +250,7 @@ async def sync_group_bindings_from_doc_type(session, doc_type_id: int, rule_set_
async def sync_doc_type_bindings_from_group(session, group_id: int) -> int | None:
"""Mirror one doc type's active child-group bindings into the runtime binding table."""
"""兼容空实现:新链路已直接读取分组绑定,不再反向写旧文档类型绑定表。"""
await ensure_rule_group_schema(session)
group_row = (
await session.execute(
@@ -268,94 +268,7 @@ async def sync_doc_type_bindings_from_group(session, group_id: int) -> int | Non
).mappings().first()
if not group_row or group_row.get("document_type_id") is None:
return None
doc_type_id = int(group_row["document_type_id"])
doc_type_code = str(group_row.get("document_type_code") or "") or None
binding_rows = (
await session.execute(
text(
"""
SELECT
rgb.id,
rgb.group_id,
rgb.rule_set_id,
rgb.priority,
rgb.is_active,
rgb.note,
g.sort_order AS group_sort_order
FROM leaudit_rule_group_bindings rgb
JOIN leaudit_evaluation_point_groups g ON g.id = rgb.group_id
WHERE g.document_type_id = :doc_type_id
AND g.deleted_at IS NULL
AND COALESCE(g.pid, 0) <> 0
AND rgb.deleted_at IS NULL
AND rgb.is_active = TRUE
ORDER BY
COALESCE(g.sort_order, 0) ASC,
COALESCE(rgb.priority, 0) DESC,
rgb.id ASC
"""
),
{"doc_type_id": doc_type_id},
)
).mappings().all()
deduped_rows: list[dict[str, Any]] = []
seen_rule_set_ids: set[int] = set()
for row in binding_rows:
rule_set_id = int(row["rule_set_id"])
if rule_set_id in seen_rule_set_ids:
continue
seen_rule_set_ids.add(rule_set_id)
deduped_rows.append(dict(row))
await session.execute(
text(
"UPDATE leaudit_rule_type_bindings SET deleted_at = NOW(), updated_at = NOW() WHERE doc_type_id = :doc_type_id AND deleted_at IS NULL"
),
{"doc_type_id": doc_type_id},
)
for index, row in enumerate(deduped_rows):
await session.execute(
text(
"""
INSERT INTO leaudit_rule_type_bindings (
doc_type_id,
doc_type_code,
rule_set_id,
binding_mode,
priority,
is_active,
note,
created_at,
updated_at,
region
) VALUES (
:doc_type_id,
:doc_type_code,
:rule_set_id,
'explicit',
:priority,
TRUE,
:note,
NOW(),
NOW(),
'default'
)
RETURNING id
"""
),
{
"doc_type_id": doc_type_id,
"doc_type_code": doc_type_code,
"rule_set_id": int(row["rule_set_id"]),
"priority": max(0, 1000 - index),
"note": row.get("note"),
},
)
return doc_type_id
return int(group_row["document_type_id"])
async def ensure_top_group(session, doc_type_row) -> int: