-- ============================================
-- 外部接口清单 菜单 + 角色授权（幂等）
-- 页面: src/views/super/apiList/index.vue
-- 后端: /sys/extApi/* (ExternalApiController)
-- ============================================

SET @menu_id = REPLACE(UUID(), '-', '');

INSERT INTO sys_permission
  (id, parent_id, name, url, component, is_route, component_name, redirect,
   menu_type, perms, perms_type, sort_no, always_show, icon, is_leaf,
   keep_alive, hidden, hide_tab, description, create_by, create_time,
   update_by, update_time, del_flag, rule_flag, status, internal_or_external)
SELECT
  @menu_id, '', '外部接口清单', '/super/apiList', 'super/apiList/index', 1, NULL, NULL,
  1, NULL, 1, 100.00, 0, NULL, 1,
  0, 0, 0, '应用系统间接口清单（对外提供/对外调用），注解自动采集', 'admin', NOW(),
  'admin', NOW(), 0, 0, 1, 0
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE name = '外部接口清单' AND url = '/super/apiList');

-- 授权给 admin 角色
INSERT INTO sys_role_permission (id, role_id, permission_id, data_rule_ids)
SELECT REPLACE(UUID(), '-', ''), 'f6817f48af4fb3af11b9e8bf182f618b', @menu_id, NULL
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_permission WHERE id = @menu_id)
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission r
    WHERE r.role_id = 'f6817f48af4fb3af11b9e8bf182f618b' AND r.permission_id = @menu_id
  );
