-- 将 AI配置 菜单挂载到：开发专用 -> AI配置
-- 说明：本系统菜单来源于 sys_resource，执行本脚本后请在“角色授权”里给对应角色勾选新菜单。

-- 1) 定位“开发专用”一级菜单
SET @dev_parent_id := (
  SELECT id
  FROM sys_resource
  WHERE name = '开发专用' AND level = 1
  ORDER BY id
  LIMIT 1
);

-- 2) 新增二级菜单：AI配置（页面）
INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/llm_config.html', 'AI配置', @dev_parent_id, 2, 999, 1
FROM dual
WHERE @dev_parent_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/llm_config.html' AND level = 2
  );

-- 3) 新增三级按钮权限：查看（用于角色细粒度授权）
SET @ai_cfg_id := (
  SELECT id
  FROM sys_resource
  WHERE code = 'ai/llm_config.html' AND level = 2
  ORDER BY id
  LIMIT 1
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/llm_config.html#view', '查看', @ai_cfg_id, 3, 1, 1
FROM dual
WHERE @ai_cfg_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/llm_config.html#view' AND level = 3
  );

-- 可选检查
SELECT id, code, name, resource_id, level, sort, status
FROM sys_resource
WHERE code IN ('ai/llm_config.html', 'ai/llm_config.html#view');
