-- ============================================================================
-- sys_menu SQL for 信息采集子系统
-- 模块: 隐患管理 / 危险源管理 / 事故管理 / 月报上报 / 工程管理
-- PostgreSQL
-- ============================================================================
-- 使用说明:
--   1. 先确认信息采集要挂载在哪个父级菜单下:
--      SELECT menu_id, parent_id, menu_name FROM sys_menu WHERE menu_type = 'M' ORDER BY order_num;
--   2. 设置 parent_menu_id 为目标父级菜单ID（0=根目录）
--   3. 执行整个脚本
-- ============================================================================

DO $$
DECLARE
    parent_menu_id BIGINT := 0;  -- ★ 修改为实际的父级菜单ID ★

    coll_dir_id     BIGINT;  -- 信息采集 目录
    menu_id         BIGINT;  -- 临时变量
    max_order       INT;

    -- 隐患管理 子菜单ID
    hidd_objhidd_id BIGINT;
    hidd_inve_id    BIGINT;
    hidd_veri_id    BIGINT;
    hidd_rectimpl_id BIGINT;
    hidd_rectprog_id BIGINT;
    hidd_rectacce_id BIGINT;
    hidd_majsup_id  BIGINT;

    -- 危险源管理 子菜单ID
    haz_objhaz_id   BIGINT;
    haz_ideneva_id  BIGINT;
    haz_patrec_id   BIGINT;

    -- 事故管理 子菜单ID
    acci_objacci_id BIGINT;

    -- 工程管理 子菜单ID
    eng_base_id     BIGINT;
BEGIN
    -- ========================================================================
    -- 清理（支持重复执行）
    -- ========================================================================
    DELETE FROM sys_menu WHERE perms LIKE 'hidd:%';
    DELETE FROM sys_menu WHERE perms LIKE 'haz:%';
    DELETE FROM sys_menu WHERE perms LIKE 'acci:%';
    DELETE FROM sys_menu WHERE perms LIKE 'monrep:%';
    DELETE FROM sys_menu WHERE perms LIKE 'eng:%';
    DELETE FROM sys_menu WHERE path IN ('eng-res','eng-cws','eng-dike','eng-hyst','eng-irr','eng-pust','eng-sd','eng-wadi','eng-waga','eng-othe') AND menu_type = 'C';
    DELETE FROM sys_menu WHERE perms LIKE 'eng:cws:%' OR perms LIKE 'eng:dike:%' OR perms LIKE 'eng:hyst:%' OR perms LIKE 'eng:irr:%' OR perms LIKE 'eng:pust:%' OR perms LIKE 'eng:sd:%' OR perms LIKE 'eng:wadi:%' OR perms LIKE 'eng:waga:%' OR perms LIKE 'eng:othe:%';
    DELETE FROM sys_menu WHERE path IN ('coll-dir','hidd-dir','haz-dir','acci-dir','monrep-dir','eng-dir') AND menu_type = 'M';

    PERFORM setval('sys_menu_menu_id_seq', COALESCE((SELECT MAX(menu_id) FROM sys_menu), 0));
    SELECT COALESCE(MAX(order_num), 0) INTO max_order FROM sys_menu WHERE parent_id = parent_menu_id;

    -- ============================================================================
    -- 一、信息采集 一级目录
    -- ============================================================================
    max_order := max_order + 1;
    INSERT INTO sys_menu (menu_name, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('信息采集', 11, 'coll-dir', NULL, 1, 0, 'M', '0', '0', '', 'monitor', 'admin', now())
    RETURNING menu_id INTO coll_dir_id;

    -- ============================================================================
    -- 二、隐患管理 (hidd) — 8个子页面
    -- ============================================================================
    max_order := max_order + 1;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('隐患管理', coll_dir_id, max_order, 'hidd-dir', NULL, 1, 0, 'M', '0', '0', '', 'tree', 'admin', now());

    -- 2.1 隐患对象
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('隐患对象', coll_dir_id, (SELECT order_num FROM sys_menu WHERE menu_name = '隐患管理' AND parent_id = coll_dir_id), 'hidd', 'slaj/hidd/index', 1, 0, 'C', '0', '0', 'hidd:objhidd:list', 'list', 'admin', now())
    RETURNING menu_id INTO hidd_objhidd_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('隐患新增', hidd_objhidd_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:objhidd:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('隐患修改', hidd_objhidd_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:objhidd:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('隐患删除', hidd_objhidd_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:objhidd:remove', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('隐患导出', hidd_objhidd_id, 4, '', '', 1, 0, 'F', '0', '0', 'hidd:objhidd:export', '#', 'admin', now());

    -- 2.2 隐患排查
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('隐患排查', coll_dir_id, 2, 'hidd-inve', 'slaj/hidd/components/HiddInvePanel', 1, 0, 'C', '0', '0', 'hidd:inve:list', 'search', 'admin', now())
    RETURNING menu_id INTO hidd_inve_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('排查新增', hidd_inve_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:inve:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('排查修改', hidd_inve_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:inve:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('排查删除', hidd_inve_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:inve:remove', '#', 'admin', now());

    -- 2.3 隐患验证
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('隐患验证', coll_dir_id, 3, 'hidd-veri', 'slaj/hidd/components/HiddVeriPanel', 1, 0, 'C', '0', '0', 'hidd:veri:list', 'check', 'admin', now())
    RETURNING menu_id INTO hidd_veri_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('验证新增', hidd_veri_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:veri:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('验证修改', hidd_veri_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:veri:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('验证删除', hidd_veri_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:veri:remove', '#', 'admin', now());

    -- 2.4 整改实施
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('整改实施', coll_dir_id, 4, 'hidd-rectimpl', 'slaj/hidd/components/HiddRectImplPanel', 1, 0, 'C', '0', '0', 'hidd:rectimpl:list', 'operation', 'admin', now())
    RETURNING menu_id INTO hidd_rectimpl_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('实施新增', hidd_rectimpl_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:rectimpl:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('实施修改', hidd_rectimpl_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:rectimpl:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('实施删除', hidd_rectimpl_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:rectimpl:remove', '#', 'admin', now());

    -- 2.5 整改进展
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('整改进展', coll_dir_id, 5, 'hidd-rectprog', 'slaj/hidd/components/HiddRectProgPanel', 1, 0, 'C', '0', '0', 'hidd:rectprog:list', 'trend-charts', 'admin', now())
    RETURNING menu_id INTO hidd_rectprog_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('进展新增', hidd_rectprog_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:rectprog:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('进展修改', hidd_rectprog_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:rectprog:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('进展删除', hidd_rectprog_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:rectprog:remove', '#', 'admin', now());

    -- 2.6 整改验收
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('整改验收', coll_dir_id, 6, 'hidd-rectacce', 'slaj/hidd/components/HiddRectAccePanel', 1, 0, 'C', '0', '0', 'hidd:rectacce:list', 'circle-check', 'admin', now())
    RETURNING menu_id INTO hidd_rectacce_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('验收新增', hidd_rectacce_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:rectacce:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('验收修改', hidd_rectacce_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:rectacce:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('验收删除', hidd_rectacce_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:rectacce:remove', '#', 'admin', now());

    -- 2.7 重大隐患督办
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('重大隐患督办', coll_dir_id, 7, 'hidd-majsup', 'slaj/hidd/components/HiddMajSupPanel', 1, 0, 'C', '0', '0', 'hidd:majsup:list', 'bell', 'admin', now())
    RETURNING menu_id INTO hidd_majsup_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('督办新增', hidd_majsup_id, 1, '', '', 1, 0, 'F', '0', '0', 'hidd:majsup:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('督办修改', hidd_majsup_id, 2, '', '', 1, 0, 'F', '0', '0', 'hidd:majsup:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('督办删除', hidd_majsup_id, 3, '', '', 1, 0, 'F', '0', '0', 'hidd:majsup:remove', '#', 'admin', now());

    -- ============================================================================
    -- 三、危险源管理 (haz)
    -- ============================================================================
    max_order := max_order + 1;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('危险源管理', coll_dir_id, max_order, 'haz-dir', NULL, 1, 0, 'M', '0', '0', '', 'warning', 'admin', now());

    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('危险源对象', coll_dir_id, (SELECT order_num FROM sys_menu WHERE menu_name = '危险源管理' AND parent_id = coll_dir_id), 'haz', 'slaj/haz/index', 1, 0, 'C', '0', '0', 'haz:objhaz:list', 'list', 'admin', now())
    RETURNING menu_id INTO haz_objhaz_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('危险源新增', haz_objhaz_id, 1, '', '', 1, 0, 'F', '0', '0', 'haz:objhaz:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('危险源修改', haz_objhaz_id, 2, '', '', 1, 0, 'F', '0', '0', 'haz:objhaz:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('危险源删除', haz_objhaz_id, 3, '', '', 1, 0, 'F', '0', '0', 'haz:objhaz:remove', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('危险源导出', haz_objhaz_id, 4, '', '', 1, 0, 'F', '0', '0', 'haz:objhaz:export', '#', 'admin', now());

    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('辨识评价', coll_dir_id, (SELECT order_num+1 FROM sys_menu WHERE menu_name = '危险源对象' AND parent_id = coll_dir_id), 'haz-ideneva', 'slaj/haz/components/HazIdenEvaPanel', 1, 0, 'C', '0', '0', 'haz:ideneva:list', 'data-analysis', 'admin', now())
    RETURNING menu_id INTO haz_ideneva_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('评价新增', haz_ideneva_id, 1, '', '', 1, 0, 'F', '0', '0', 'haz:ideneva:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('评价修改', haz_ideneva_id, 2, '', '', 1, 0, 'F', '0', '0', 'haz:ideneva:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('评价删除', haz_ideneva_id, 3, '', '', 1, 0, 'F', '0', '0', 'haz:ideneva:remove', '#', 'admin', now());

    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('巡查记录', coll_dir_id, (SELECT order_num+1 FROM sys_menu WHERE menu_name = '辨识评价' AND parent_id = coll_dir_id), 'haz-patrice', 'slaj/haz/components/HazPatRecPanel', 1, 0, 'C', '0', '0', 'haz:patrec:list', 'documentation', 'admin', now())
    RETURNING menu_id INTO haz_patrec_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('巡查新增', haz_patrec_id, 1, '', '', 1, 0, 'F', '0', '0', 'haz:patrec:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('巡查修改', haz_patrec_id, 2, '', '', 1, 0, 'F', '0', '0', 'haz:patrec:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('巡查删除', haz_patrec_id, 3, '', '', 1, 0, 'F', '0', '0', 'haz:patrec:remove', '#', 'admin', now());

    -- ============================================================================
    -- 四、事故管理 (acci)
    -- ============================================================================
    max_order := max_order + 1;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('事故管理', coll_dir_id, max_order, 'acci-dir', NULL, 1, 0, 'M', '0', '0', '', 'aim', 'admin', now());

    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('事故对象', coll_dir_id, (SELECT order_num FROM sys_menu WHERE menu_name = '事故管理' AND parent_id = coll_dir_id), 'acci', 'slaj/acci/index', 1, 0, 'C', '0', '0', 'acci:objacci:list', 'list', 'admin', now())
    RETURNING menu_id INTO acci_objacci_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('事故新增', acci_objacci_id, 1, '', '', 1, 0, 'F', '0', '0', 'acci:objacci:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('事故修改', acci_objacci_id, 2, '', '', 1, 0, 'F', '0', '0', 'acci:objacci:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('事故删除', acci_objacci_id, 3, '', '', 1, 0, 'F', '0', '0', 'acci:objacci:remove', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('事故导出', acci_objacci_id, 4, '', '', 1, 0, 'F', '0', '0', 'acci:objacci:export', '#', 'admin', now());

    -- ============================================================================
    -- 五、月报上报 (monrep)
    -- ============================================================================
    max_order := max_order + 1;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('月报上报', coll_dir_id, max_order, 'monrep-dir', NULL, 1, 0, 'M', '0', '0', '', 'date', 'admin', now());

    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('月报管理', coll_dir_id, (SELECT order_num FROM sys_menu WHERE menu_name = '月报上报' AND parent_id = coll_dir_id), 'monrep', 'slaj/monrep/index', 1, 0, 'C', '0', '0', 'monrep:peri:list', 'list', 'admin', now())
    RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('月报新增', menu_id, 1, '', '', 1, 0, 'F', '0', '0', 'monrep:peri:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('月报修改', menu_id, 2, '', '', 1, 0, 'F', '0', '0', 'monrep:peri:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('月报删除', menu_id, 3, '', '', 1, 0, 'F', '0', '0', 'monrep:peri:remove', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('月报导出', menu_id, 4, '', '', 1, 0, 'F', '0', '0', 'monrep:peri:export', '#', 'admin', now());

    -- ============================================================================
    -- 六、工程管理 (eng) — ATT_ENG_BASE + 10个类型属性表
    -- ============================================================================
    max_order := max_order + 1;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('工程管理', coll_dir_id, max_order, 'eng-dir', NULL, 1, 0, 'M', '0', '0', '', 'office-building', 'admin', now());

    -- 6.1 工程基础信息 (ATT_ENG_BASE)
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('工程基础信息', coll_dir_id, (SELECT order_num FROM sys_menu WHERE menu_name = '工程管理' AND parent_id = coll_dir_id), 'eng', 'slaj/eng/index', 1, 0, 'C', '0', '0', 'eng:base:list', 'list', 'admin', now())
    RETURNING menu_id INTO eng_base_id;
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('工程新增', eng_base_id, 1, '', '', 1, 0, 'F', '0', '0', 'eng:base:add', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('工程修改', eng_base_id, 2, '', '', 1, 0, 'F', '0', '0', 'eng:base:edit', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('工程删除', eng_base_id, 3, '', '', 1, 0, 'F', '0', '0', 'eng:base:remove', '#', 'admin', now());
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES ('工程导出', eng_base_id, 4, '', '', 1, 0, 'F', '0', '0', 'eng:base:export', '#', 'admin', now());

    -- 6.2 水库工程 (ATT_ENG_RES)
    INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('水库工程', coll_dir_id, 2, 'eng-res', 'slaj/eng/attengres/index', 1, 0, 'C', '0', '0', 'eng:res:list', 'coin', 'admin', now())
    RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','eng:res:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','eng:res:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','eng:res:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','eng:res:export','#','admin',now());

    -- 6.3-6.11 其余9种工程类型属性 (ATT_ENG_CWS/DIKE/HYST/IRR/OTHE/PUST/SD/WADI/WAGA)
    FOR rec IN SELECT * FROM (VALUES
        ('农村供水工程',3,'eng-cws','slaj/eng/attengcws/index','eng:cws'),
        ('堤防工程',4,'eng-dike','slaj/eng/attengdike/index','eng:dike'),
        ('水电站工程',5,'eng-hyst','slaj/eng/attenghyst/index','eng:hyst'),
        ('灌区工程',6,'eng-irr','slaj/eng/attengirr/index','eng:irr'),
        ('泵站工程',7,'eng-pust','slaj/eng/attengpust/index','eng:pust'),
        ('淤地坝工程',8,'eng-sd','slaj/eng/attengsd/index','eng:sd'),
        ('引调水工程',9,'eng-wadi','slaj/eng/attengwadi/index','eng:wadi'),
        ('水闸工程',10,'eng-waga','slaj/eng/attengwaga/index','eng:waga'),
        ('其他工程',11,'eng-othe','slaj/eng/attengothe/index','eng:othe')
    ) AS t(name, ord, path, comp, perm_prefix) LOOP
        INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
        VALUES (rec.name, coll_dir_id, rec.ord, rec.path, rec.comp, 1, 0, 'C', '0', '0', rec.perm_prefix||':list', 'link', 'admin', now()) RETURNING menu_id INTO menu_id;
        INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0',rec.perm_prefix||':add','#','admin',now());
        INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0',rec.perm_prefix||':edit','#','admin',now());
        INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0',rec.perm_prefix||':remove','#','admin',now());
        INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0',rec.perm_prefix||':export','#','admin',now());
    END LOOP;

    -- ========================================================================
    -- 完成
    -- ========================================================================
    RAISE NOTICE '========================================';
    RAISE NOTICE '信息采集子系统菜单 SQL 执行完成!';
    RAISE NOTICE 'parent_menu_id = %', parent_menu_id;
    RAISE NOTICE '如果菜单不显示，请检查:';
    RAISE NOTICE '  1. parent_menu_id 是否正确';
    RAISE NOTICE '  2. 是否为角色分配了菜单权限';
    RAISE NOTICE '  3. 前端 views/slaj/* 目录是否存在对应组件';
    RAISE NOTICE '========================================';
END $$;
