-- ============================================================
-- 精修项落地：DUS 菜单翻转 + G 段按钮（900800-900899）+ 角色关联
-- ------------------------------------------------------------
-- 1) 900230 DUS 测试：component 从 _coming_soon 翻转到真实页面；
-- 2) G 段按钮挂 900230（perm module_bre:dus_test:*），ON CONFLICT 幂等；
-- 3) 补授 超级管理员 + 管理员（§5 主 WHERE 不含 900800 段，须单独补）。
-- ============================================================

-- 1) DUS 测试 页面翻转
UPDATE sys_menu
SET component_path = 'module_bre/dus/index', description = 'DUS 测试（描述符模板/测试记录/观测）'
WHERE id = 900230;

-- 2) G 段按钮（900800-900808，parent 900230）
WITH modules(seq, mod_id, perm) AS (VALUES
    (0, 900230, 'module_bre:dus_test')
),
btns(border, bname, suffix) AS (VALUES
    (1, '查看', 'detail'),
    (2, '查询', 'query'),
    (3, '新增', 'create'),
    (4, '编辑', 'update'),
    (5, '删除', 'delete'),
    (6, '导出', 'export'),
    (7, '导入', 'import'),
    (8, '下载模板', 'download')
)
INSERT INTO sys_menu
    (uuid, created_time, updated_time, id, name, type, icon, "order", permission, route_name, route_path, component_path, redirect, hidden, keep_alive, always_show, title, status, is_deleted, scope, description, parent_id, link, is_iframe, is_hide_tab, show_badge, affix, params, active_path, show_text_badge)
SELECT
    gen_random_uuid(), now(), now(),
    900800 + m.seq * 10 + b.border,
    b.bname, 3, NULL, b.border,
    m.perm || ':' || b.suffix,
    NULL, NULL, NULL, NULL, false, true, false, b.bname, 0, false, 'web', NULL,
    m.mod_id, NULL, false, false, false, false, NULL, NULL, NULL
FROM modules m
CROSS JOIN btns b
ON CONFLICT (id) DO NOTHING;

-- 3) 角色关联：G 段按钮授给 超级管理员 + 管理员（幂等）
INSERT INTO sys_role_menus (role_id, menu_id)
SELECT r.id, m.id
FROM sys_role r
CROSS JOIN sys_menu m
WHERE r.code IN ('SUPER_ADMIN', 'ADMIN')
  AND m.id BETWEEN 900800 AND 900899
ON CONFLICT DO NOTHING;
