-- 供水设施 Excel 批量导入按钮权限（KingbaseES / PostgreSQL 兼容）
-- 可重复执行；只补充导入按钮权限，不修改页面菜单和既有权限。

DO $$
BEGIN
    INSERT INTO app_user.sys_menu (
        menu_name, parent_id, order_num, path, component, is_frame, is_cache,
        menu_type, visible, status, perms, icon, create_by, create_time, remark
    )
    SELECT
        v.menu_name, page_menu.menu_id, 30, '', '', '1', '0',
        'F', '0', '0', v.perms, '#', 'admin', now(), '供水设施 Excel 批量导入'
    FROM (VALUES
        ('水源地导入', 'waterSupply/facility/source', 'waterSupply:waterSource:import'),
        ('水厂导入', 'waterSupply/facility/plant', 'waterSupply:waterPlant:import'),
        ('泵站导入', 'waterSupply/facility/pumpStation', 'waterSupply:waterPumpStation:import'),
        ('管网导入', 'waterSupply/facility/pipe', 'waterSupply:waterPipe:import'),
        ('用水户导入', 'waterSupply/facility/user', 'waterSupply:waterUser:import')
    ) AS v(menu_name, parent_component, perms)
    JOIN app_user.sys_menu page_menu ON page_menu.component = v.parent_component
    WHERE NOT EXISTS (
        SELECT 1 FROM app_user.sys_menu existing WHERE existing.perms = v.perms
    );
END $$;
