-- 评查点分组正式迁移前巡检脚本
-- 用途：
-- 1. 在测试库 / 正式库执行前，先盘点当前分组树、规则集绑定和入口映射风险
-- 2. 本脚本只做查询，不修改任何数据
--
-- 推荐执行方式：
-- psql "$DATABASE_URL" -f scripts/创建sql/precheck_rule_group_migration.sql

-- =========================================================
-- 0. 文档类型与入口模块映射总览
-- =========================================================
SELECT
    dt.id,
    dt.code,
    dt.name,
    dt.entry_module_id,
    em.name AS entry_module_name,
    dt.sort_order,
    dt.is_enabled
FROM leaudit_document_types dt
LEFT JOIN leaudit_entry_modules em
  ON em.id = dt.entry_module_id
WHERE dt.deleted_at IS NULL
ORDER BY dt.sort_order, dt.id;

-- =========================================================
-- 1. 旧一级根检查：一级直接挂具体文档类型
-- =========================================================
SELECT
    id,
    code,
    name,
    document_type_id,
    entry_module_id,
    sort_order,
    is_enabled
FROM leaudit_evaluation_point_groups
WHERE deleted_at IS NULL
  AND COALESCE(pid, 0) = 0
  AND document_type_id IS NOT NULL
ORDER BY id;

-- =========================================================
-- 2. 新一级业务大类根检查：一级不直接挂文档类型
-- =========================================================
SELECT
    id,
    code,
    name,
    entry_module_id,
    sort_order,
    is_enabled
FROM leaudit_evaluation_point_groups
WHERE deleted_at IS NULL
  AND COALESCE(pid, 0) = 0
  AND document_type_id IS NULL
ORDER BY sort_order, id;

-- =========================================================
-- 3. 二级分组全量清单
-- =========================================================
SELECT
    child.id,
    child.pid,
    parent.name AS parent_name,
    child.code,
    child.name,
    child.document_type_id,
    dt.name AS document_type_name,
    COALESCE(child.entry_module_id, parent.entry_module_id, dt.entry_module_id) AS resolved_entry_module_id,
    em.name AS resolved_entry_module_name,
    child.sort_order,
    child.is_enabled
FROM leaudit_evaluation_point_groups child
LEFT JOIN leaudit_evaluation_point_groups parent
  ON parent.id = child.pid
 AND parent.deleted_at IS NULL
LEFT JOIN leaudit_document_types dt
  ON dt.id = child.document_type_id
LEFT JOIN leaudit_entry_modules em
  ON em.id = COALESCE(child.entry_module_id, parent.entry_module_id, dt.entry_module_id)
WHERE child.deleted_at IS NULL
  AND COALESCE(child.pid, 0) <> 0
ORDER BY child.pid, child.sort_order, child.id;

-- =========================================================
-- 4. 二级分组唯一性检查：同一文档类型是否被多个二级分组承接
-- =========================================================
SELECT
    document_type_id,
    COUNT(*) AS child_count,
    STRING_AGG(name, ' / ' ORDER BY id) AS child_names
FROM leaudit_evaluation_point_groups
WHERE deleted_at IS NULL
  AND COALESCE(pid, 0) <> 0
  AND document_type_id IS NOT NULL
GROUP BY document_type_id
HAVING COUNT(*) > 1
ORDER BY document_type_id;

-- =========================================================
-- 5. 旧默认子级检查：通用 / *.default
-- =========================================================
SELECT
    child.id,
    child.pid,
    parent.name AS parent_name,
    child.name,
    child.code,
    child.document_type_id
FROM leaudit_evaluation_point_groups child
JOIN leaudit_evaluation_point_groups parent
  ON parent.id = child.pid
 AND parent.deleted_at IS NULL
WHERE child.deleted_at IS NULL
  AND COALESCE(child.pid, 0) <> 0
  AND (
      child.name = '通用'
      OR child.code LIKE '%.default'
  )
ORDER BY child.id;

-- =========================================================
-- 6. 规则集绑定全量清单：按分组树展开
-- =========================================================
SELECT
    rgb.id AS binding_id,
    rgb.group_id,
    root.id AS root_id,
    root.name AS root_name,
    child.id AS child_id,
    child.name AS child_name,
    child.document_type_id,
    dt.name AS document_type_name,
    rgb.rule_set_id,
    rs.rule_name,
    rs.rule_type,
    rgb.priority,
    rgb.is_active,
    rgb.note
FROM leaudit_rule_group_bindings rgb
JOIN leaudit_evaluation_point_groups child
  ON child.id = rgb.group_id
 AND child.deleted_at IS NULL
LEFT JOIN leaudit_evaluation_point_groups root
  ON root.id = child.pid
 AND root.deleted_at IS NULL
LEFT JOIN leaudit_document_types dt
  ON dt.id = child.document_type_id
LEFT JOIN leaudit_rule_sets rs
  ON rs.id = rgb.rule_set_id
WHERE rgb.deleted_at IS NULL
ORDER BY root.id, child.id, rgb.priority DESC, rgb.id;

-- =========================================================
-- 7. 规则集重复挂载检查：旧一级根与默认子级是否同时挂了同一规则集
-- =========================================================
WITH target_groups AS (
    SELECT
        g.id,
        g.document_type_id,
        g.name,
        g.code,
        CASE
            WHEN COALESCE(g.pid, 0) = 0 THEN 'old_root'
            WHEN g.name = '通用' OR g.code LIKE '%.default' THEN 'default_child'
            ELSE 'other_child'
        END AS group_kind
    FROM leaudit_evaluation_point_groups g
    WHERE g.deleted_at IS NULL
      AND g.document_type_id IS NOT NULL
)
SELECT
    tg.document_type_id,
    rgb.rule_set_id,
    COUNT(*) AS binding_count,
    STRING_AGG(tg.group_kind || ':' || tg.name, ' / ' ORDER BY tg.id) AS group_sources
FROM leaudit_rule_group_bindings rgb
JOIN target_groups tg
  ON tg.id = rgb.group_id
WHERE rgb.deleted_at IS NULL
GROUP BY tg.document_type_id, rgb.rule_set_id
HAVING COUNT(*) > 1
ORDER BY tg.document_type_id, rgb.rule_set_id;

-- =========================================================
-- 8. 文档类型汇总绑定表现状：leaudit_rule_type_bindings
-- =========================================================
SELECT
    rtb.id,
    rtb.doc_type_id,
    dt.name AS document_type_name,
    rtb.doc_type_code,
    rtb.rule_set_id,
    rs.rule_name,
    rs.rule_type,
    rtb.binding_mode,
    rtb.priority,
    rtb.is_active,
    rtb.note
FROM leaudit_rule_type_bindings rtb
LEFT JOIN leaudit_document_types dt
  ON dt.id = rtb.doc_type_id
LEFT JOIN leaudit_rule_sets rs
  ON rs.id = rtb.rule_set_id
WHERE rtb.deleted_at IS NULL
ORDER BY rtb.doc_type_id, rtb.priority DESC, rtb.id;

-- =========================================================
-- 9. 规则集可运行性检查：版本占位
-- =========================================================
SELECT
    rs.id,
    rs.rule_name,
    rs.rule_type,
    rs.current_version_id,
    NULL::bigint AS fallback_version_id,
    rs.status
FROM leaudit_rule_sets rs
WHERE rs.deleted_at IS NULL
ORDER BY rs.id;

-- =========================================================
-- 10. 文档类型缺失入口模块检查
-- =========================================================
SELECT
    dt.id,
    dt.code,
    dt.name
FROM leaudit_document_types dt
WHERE dt.deleted_at IS NULL
  AND dt.entry_module_id IS NULL
ORDER BY dt.id;

-- =========================================================
-- 11. 一级根缺失入口模块检查
-- =========================================================
SELECT
    id,
    code,
    name
FROM leaudit_evaluation_point_groups
WHERE deleted_at IS NULL
  AND COALESCE(pid, 0) = 0
  AND document_type_id IS NULL
  AND entry_module_id IS NULL
ORDER BY id;

-- =========================================================
-- 12. 新链路下无法命中规则集的二级分组检查
-- =========================================================
SELECT
    child.id,
    child.name,
    child.code,
    child.document_type_id,
    dt.name AS document_type_name,
    COUNT(rgb.id) AS binding_count
FROM leaudit_evaluation_point_groups child
LEFT JOIN leaudit_rule_group_bindings rgb
  ON rgb.group_id = child.id
 AND rgb.deleted_at IS NULL
 AND rgb.is_active = TRUE
LEFT JOIN leaudit_document_types dt
  ON dt.id = child.document_type_id
WHERE child.deleted_at IS NULL
  AND COALESCE(child.pid, 0) <> 0
GROUP BY child.id, child.name, child.code, child.document_type_id, dt.name
HAVING COUNT(rgb.id) = 0
ORDER BY child.id;
