DO $$
DECLARE
    parent_menu_id BIGINT := 0;  -- ★ 修改为实际父级菜单ID
    ttsa_dir_id BIGINT;
    menu_id BIGINT;
    max_order INT;
BEGIN
    -- 清理已存在菜单 + 重置序列（可重复执行）
    DELETE FROM sys_menu WHERE perms LIKE 'ttsa:%';
    DELETE FROM sys_menu WHERE path = 'ttsa' 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, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
    VALUES ('三类人员考核', parent_menu_id, max_order, 'ttsa', NULL, 1, 0, 'M', '0', '0', '', 'user', 'admin', now())
    RETURNING menu_id INTO ttsa_dir_id;

    -- 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 ('三类人员信息', ttsa_dir_id, 1, 'objtts', 'slaj/ttsa/objtts/index', 1, 0, 'C', '0', '0', 'ttsa:objtts:list', 'people', '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','ttsa:objtts: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','ttsa:objtts: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','ttsa:objtts: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','ttsa:objtts:export','#','admin',now());

    -- 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 ('安全考核申请', ttsa_dir_id, 2, 'objttsa', 'slaj/ttsa/objttsa/index', 1, 0, 'C', '0', '0', 'ttsa:objttsa:list', 'form', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:objttsa:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:objttsa:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:objttsa:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:objttsa:export','#','admin',now());

    -- 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 ('批量考核申请', ttsa_dir_id, 3, 'objttsaexamappl', 'slaj/ttsa/objttsaexamappl/index', 1, 0, 'C', '0', '0', 'ttsa:objttsaexamappl:list', 'list', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:objttsaexamappl:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:objttsaexamappl:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:objttsaexamappl:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:objttsaexamappl:export','#','admin',now());

    -- 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 ('三类人员证书', ttsa_dir_id, 4, 'ttsacert', 'slaj/ttsa/ttsacert/index', 1, 0, 'C', '0', '0', 'ttsa:ttsacert:list', 'certificate', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsacert:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsacert:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsacert:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsacert:export','#','admin',now());

    -- 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 ('证书阵列', ttsa_dir_id, 5, 'ttsacertarray', 'slaj/ttsa/ttsacertarray/index', 1, 0, 'C', '0', '0', 'ttsa:ttsacertarray:list', 'grid', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsacertarray:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsacertarray:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsacertarray:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsacertarray:export','#','admin',now());

    -- 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 ('证书变更申请', ttsa_dir_id, 6, 'ttsacertchaappl', 'slaj/ttsa/ttsacertchaappl/index', 1, 0, 'C', '0', '0', 'ttsa:ttsacertchaappl:list', 'edit', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsacertchaappl:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsacertchaappl:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsacertchaappl:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsacertchaappl:export','#','admin',now());

    -- 7-14. 证书变更明细表（8张）
    FOR rec IN SELECT * FROM (VALUES
        ('单位变更',7,'ttsacertchacomp','ttsa:ttsacertchacomp'),
        ('遗失补办',8,'ttsacertchalost','ttsa:ttsacertchalost'),
        ('姓名变更',9,'ttsacertchaname','ttsa:ttsacertchaname'),
        ('人员变更',10,'ttsacertchapers','ttsa:ttsacertchapers'),
        ('证书调动',11,'ttsacertchatran','ttsa:ttsacertchatran'),
        ('补发申请',12,'ttsacertreisappl','ttsa:ttsacertreisappl'),
        ('撤销申请',13,'ttsacertrevappl','ttsa:ttsacertrevappl'),
        ('延期申请',14,'ttsaceriextapll','ttsa:ttsaceriextapll')
    ) AS t(name, ord, 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, ttsa_dir_id, rec.ord, rec.comp, 'slaj/ttsa/'||rec.comp||'/index', 1, 0, 'C', '0', '0', rec.perm_prefix||':list', 'cascader', '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;

    -- 15. 考核计划
    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 ('考核计划', ttsa_dir_id, 15, 'ttsaexamplan', 'slaj/ttsa/ttsaexamplan/index', 1, 0, 'C', '0', '0', 'ttsa:ttsaexamplan:list', 'date', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsaexamplan:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsaexamplan:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsaexamplan:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsaexamplan:export','#','admin',now());

    -- 16. 试卷
    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 ('试卷', ttsa_dir_id, 16, 'ttsapaper', 'slaj/ttsa/ttsapaper/index', 1, 0, 'C', '0', '0', 'ttsa:ttsapaper:list', 'documentation', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsapaper:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsapaper:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsapaper:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsapaper:export','#','admin',now());

    -- 17. 试题
    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 ('试题', ttsa_dir_id, 17, 'ttsatque', 'slaj/ttsa/ttsatque/index', 1, 0, 'C', '0', '0', 'ttsa:ttsatque:list', 'component', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsatque:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsatque:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsatque:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsatque:export','#','admin',now());

    -- 18. 试题选项
    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 ('试题选项', ttsa_dir_id, 18, 'ttsatqueopt', 'slaj/ttsa/ttsatqueopt/index', 1, 0, 'C', '0', '0', 'ttsa:ttsatqueopt:list', 'radio', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsatqueopt:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsatqueopt:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsatqueopt:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsatqueopt:export','#','admin',now());

    -- 19. 考试成绩
    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 ('考试成绩', ttsa_dir_id, 19, 'ttsascore', 'slaj/ttsa/ttsascore/index', 1, 0, 'C', '0', '0', 'ttsa:ttsascore:list', 'number', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsascore:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsascore:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsascore:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsascore:export','#','admin',now());

    -- 20. 考核记录
    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 ('考核记录', ttsa_dir_id, 20, 'ttsarec', 'slaj/ttsa/ttsarec/index', 1, 0, 'C', '0', '0', 'ttsa:ttsarec:list', 'log', 'admin', now()) RETURNING menu_id INTO menu_id;
    INSERT INTO sys_menu VALUES ('新增',menu_id,1,'','',1,0,'F','0','0','ttsa:ttsarec:add','#','admin',now());
    INSERT INTO sys_menu VALUES ('修改',menu_id,2,'','',1,0,'F','0','0','ttsa:ttsarec:edit','#','admin',now());
    INSERT INTO sys_menu VALUES ('删除',menu_id,3,'','',1,0,'F','0','0','ttsa:ttsarec:remove','#','admin',now());
    INSERT INTO sys_menu VALUES ('导出',menu_id,4,'','',1,0,'F','0','0','ttsa:ttsarec:export','#','admin',now());

    -- 21-27. 剩余7表
    FOR rec IN SELECT * FROM (VALUES
        ('考核培训',21,'ttsatrain','ttsa:ttsatrain'),
        ('培训人员',22,'ttsatrainpers','ttsa:ttsatrainpers'),
        ('考核指定',23,'ttsadesign','ttsa:ttsadesign'),
        ('指定明细',24,'ttsadesigndetail','ttsa:ttsadesigndetail'),
        ('指定考试关联',25,'ttsadesignexamrel','ttsa:ttsadesignexamrel'),
        ('考核企业',26,'ttsaexamente','ttsa:ttsaexamente'),
        ('申请安全元素',27,'ttsaapplwiun','ttsa:ttsaapplwiun')
    ) AS t(name, ord, 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, ttsa_dir_id, rec.ord, rec.comp, 'slaj/ttsa/'||rec.comp||'/index', 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 'TTSA menu done. Rows inserted.';
END $$;
