-- ================================================================
-- 修复重复规则的SQL脚本
-- 用于清理数据库中重复的空间规则定义
-- ================================================================

-- 1. 查找重复的规则（相同的规则名称）
SELECT 
    rule_name,
    COUNT(*) as rule_count,
    array_agg(rule_id ORDER BY rule_id) as rule_ids,
    array_agg(spatial_object_id ORDER BY rule_id) as spatial_object_ids
FROM spatial_rules 
GROUP BY rule_name 
HAVING COUNT(*) > 1
ORDER BY rule_name;

-- 2. 查找具体的重复规则详情
WITH duplicate_rules AS (
    SELECT rule_name, COUNT(*) as cnt
    FROM spatial_rules 
    GROUP BY rule_name 
    HAVING COUNT(*) > 1
)
SELECT 
    sr.rule_id,
    sr.rule_name,
    sr.description,
    sr.rule_category,
    sr.status,
    sr.priority,
    sr.spatial_object_type,
    sr.spatial_object_id,
    sr.alert_level,
    sr.rule_parameters,
    sr.created_by,
    sr.created_at
FROM spatial_rules sr
INNER JOIN duplicate_rules dr ON sr.rule_name = dr.rule_name
ORDER BY sr.rule_name, sr.created_at;

-- 3. 显示重复规则对应的车辆类型
WITH duplicate_rules AS (
    SELECT rule_name, COUNT(*) as cnt
    FROM spatial_rules 
    GROUP BY rule_name 
    HAVING COUNT(*) > 1
)
SELECT 
    sr.rule_id,
    sr.rule_name,
    sr.spatial_object_id,
    array_agg(vt.vehicle_type ORDER BY vt.vehicle_type) as vehicle_types
FROM spatial_rules sr
INNER JOIN duplicate_rules dr ON sr.rule_name = dr.rule_name
LEFT JOIN spatial_rule_vehicle_types vt ON sr.rule_id = vt.rule_id
GROUP BY sr.rule_id, sr.rule_name, sr.spatial_object_id
ORDER BY sr.rule_name, sr.rule_id;

-- 4. 查找停机坪限速规则的重复情况
SELECT 
    rule_id,
    rule_name,
    spatial_object_id,
    rule_parameters,
    status,
    created_at,
    created_by
FROM spatial_rules 
WHERE rule_name LIKE '%停机坪限速%' OR rule_name LIKE '%APRON%'
ORDER BY created_at;

-- 5. 删除重复的规则（保留最早创建的规则）
-- 注意：在执行删除之前，请确认要删除的规则ID

-- 5.1 删除重复规则的车辆类型关联（如果存在）
-- DELETE FROM spatial_rule_vehicle_types 
-- WHERE rule_id IN (
--     SELECT rule_id 
--     FROM spatial_rules 
--     WHERE rule_name = '停机坪限速规则' 
--     AND rule_id != (
--         SELECT rule_id 
--         FROM spatial_rules 
--         WHERE rule_name = '停机坪限速规则' 
--         ORDER BY created_at ASC 
--         LIMIT 1
--     )
-- );

-- 5.2 删除重复的规则记录（保留最早的）
-- DELETE FROM spatial_rules 
-- WHERE rule_name = '停机坪限速规则' 
-- AND rule_id != (
--     SELECT rule_id 
--     FROM spatial_rules 
--     WHERE rule_name = '停机坪限速规则' 
--     ORDER BY created_at ASC 
--     LIMIT 1
-- );

-- 6. 验证清理结果
-- SELECT 
--     rule_name,
--     COUNT(*) as rule_count
-- FROM spatial_rules 
-- GROUP BY rule_name 
-- HAVING COUNT(*) > 1;

-- 7. 检查最终的规则状态
SELECT 
    r.rule_id,
    r.rule_name,
    r.rule_category,
    r.spatial_object_id,
    r.status,
    r.priority,
    r.rule_parameters,
    array_agg(vt.vehicle_type ORDER BY vt.vehicle_type) as allowed_vehicle_types
FROM spatial_rules r
LEFT JOIN spatial_rule_vehicle_types vt ON r.rule_id = vt.rule_id
WHERE r.status = 'ACTIVE'
GROUP BY r.rule_id, r.rule_name, r.rule_category, r.spatial_object_id, r.status, r.priority, r.rule_parameters
ORDER BY r.rule_name;

-- 使用说明：
-- 1. 首先运行步骤1-4来查看重复规则的情况
-- 2. 确认需要删除的重复规则后，取消注释步骤5中的DELETE语句并执行
-- 3. 运行步骤6-7验证清理结果