feat: migrate rule bindings to group-based flow
This commit is contained in:
@@ -1128,7 +1128,6 @@ class DocumentServiceImpl(IDocumentService):
|
||||
)
|
||||
).scalar_one()
|
||||
await self._syncRuleBindings(Session, int(row), Body.ruleSetIds, "default")
|
||||
await sync_group_bindings_from_doc_type(Session, int(row), Body.ruleSetIds)
|
||||
await Session.commit()
|
||||
|
||||
return await self.GetDocumentType(int(row))
|
||||
@@ -1171,7 +1170,6 @@ class DocumentServiceImpl(IDocumentService):
|
||||
|
||||
if "ruleSetIds" in providedFields and Body.ruleSetIds is not None:
|
||||
await self._syncRuleBindings(Session, Id, Body.ruleSetIds, "default")
|
||||
await sync_group_bindings_from_doc_type(Session, Id, Body.ruleSetIds)
|
||||
|
||||
await Session.commit()
|
||||
return await self.GetDocumentType(Id)
|
||||
@@ -1334,17 +1332,57 @@ class DocumentServiceImpl(IDocumentService):
|
||||
await Session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT doc_type_id, rule_set_id
|
||||
FROM leaudit_rule_type_bindings
|
||||
WHERE doc_type_id = ANY(:ids) AND deleted_at IS NULL AND is_active = true
|
||||
ORDER BY priority DESC
|
||||
SELECT
|
||||
child.document_type_id AS doc_type_id,
|
||||
rgb.rule_set_id,
|
||||
child.sort_order AS child_sort_order,
|
||||
rgb.priority,
|
||||
rgb.id
|
||||
FROM leaudit_evaluation_point_groups child
|
||||
JOIN leaudit_rule_group_bindings rgb
|
||||
ON rgb.group_id = child.id
|
||||
AND rgb.deleted_at IS NULL
|
||||
AND rgb.is_active = TRUE
|
||||
WHERE child.document_type_id = ANY(:ids)
|
||||
AND child.deleted_at IS NULL
|
||||
AND COALESCE(child.pid, 0) <> 0
|
||||
ORDER BY
|
||||
child.document_type_id ASC,
|
||||
COALESCE(child.sort_order, 0) ASC,
|
||||
COALESCE(rgb.priority, 0) DESC,
|
||||
rgb.id ASC
|
||||
"""
|
||||
),
|
||||
{"ids": allIds},
|
||||
)
|
||||
).fetchall()
|
||||
for b in bindingRows:
|
||||
bindingsMap.setdefault(int(b[0]), []).append(int(b[1]))
|
||||
for docTypeId, ruleSetId, *_ in bindingRows:
|
||||
current = bindingsMap.setdefault(int(docTypeId), [])
|
||||
normalizedRuleSetId = int(ruleSetId)
|
||||
if normalizedRuleSetId not in current:
|
||||
current.append(normalizedRuleSetId)
|
||||
|
||||
# 兼容历史数据:若某些文档类型尚未完成分组绑定迁移,再回退读取旧绑定表。
|
||||
missingDocTypeIds = [docTypeId for docTypeId, ruleSetIds in bindingsMap.items() if len(ruleSetIds) == 0]
|
||||
if missingDocTypeIds:
|
||||
legacyBindingRows = (
|
||||
await Session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT doc_type_id, rule_set_id
|
||||
FROM leaudit_rule_type_bindings
|
||||
WHERE doc_type_id = ANY(:ids) AND deleted_at IS NULL AND is_active = true
|
||||
ORDER BY priority DESC
|
||||
"""
|
||||
),
|
||||
{"ids": missingDocTypeIds},
|
||||
)
|
||||
).fetchall()
|
||||
for docTypeId, ruleSetId in legacyBindingRows:
|
||||
current = bindingsMap.setdefault(int(docTypeId), [])
|
||||
normalizedRuleSetId = int(ruleSetId)
|
||||
if normalizedRuleSetId not in current:
|
||||
current.append(normalizedRuleSetId)
|
||||
return rows, bindingsMap
|
||||
|
||||
async def _queryDocumentTypeRoots(self, Session, Ids: list[int] | None = None, EntryModuleId: int | None = None):
|
||||
@@ -2564,21 +2602,8 @@ class DocumentServiceImpl(IDocumentService):
|
||||
return str(Row.get("rule_name") or Row.get("rule_id") or "")
|
||||
|
||||
async def _syncRuleBindings(self, Session, DocTypeId: int, RuleSetIds: list[int], Region: str = "default") -> None:
|
||||
"""全量替换规则绑定。"""
|
||||
await Session.execute(
|
||||
text("UPDATE leaudit_rule_type_bindings SET deleted_at = NOW() WHERE doc_type_id = :id AND deleted_at IS NULL"),
|
||||
{"id": DocTypeId},
|
||||
)
|
||||
for idx, ruleSetId in enumerate(RuleSetIds):
|
||||
await Session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO leaudit_rule_type_bindings (doc_type_id, rule_set_id, binding_mode, priority, region, is_active, created_at, updated_at)
|
||||
VALUES (:doc_type_id, :rule_set_id, 'explicit', :priority, :region, true, NOW(), NOW())
|
||||
"""
|
||||
),
|
||||
{"doc_type_id": DocTypeId, "rule_set_id": ruleSetId, "priority": 100 - idx, "region": Region},
|
||||
)
|
||||
"""全量替换规则绑定,仅写入新分组绑定。"""
|
||||
await sync_group_bindings_from_doc_type(Session, DocTypeId, RuleSetIds)
|
||||
|
||||
|
||||
async def _find_latest_version_candidate(
|
||||
|
||||
Reference in New Issue
Block a user