-- ============================================================
-- 应用分类管理 菜单初始化（幂等，可重复执行）
-- ------------------------------------------------------------
-- 说明：
--   挂在「应用管理」目录(/app-manager)下，新增「分类管理」菜单。
--   页面路径：app-manager/category/index
-- ============================================================

SET @parent_id = (SELECT id FROM sys_menu WHERE route_path = '/app-manager' AND type = 'C' LIMIT 1);

INSERT INTO sys_menu
    (parent_id, tree_path, name, type, route_name, route_path, component,
     perm, always_show, keep_alive, visible, sort, icon, redirect,
     create_time, update_time, params)
SELECT @parent_id,
       CONCAT('0,', @parent_id),
       '分类管理', 'M', 'AppCategory', 'category', 'app-manager/category/index',
       NULL, NULL, 1, 1, 2, 'menu', NULL,
       now(), now(), NULL
FROM DUAL
WHERE @parent_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE component = 'app-manager/category/index');

-- 授权给 ADMIN 角色（role_id=2，如实际不同请修改）
SET @menu_id = (SELECT id FROM sys_menu WHERE component = 'app-manager/category/index' LIMIT 1);
INSERT IGNORE INTO sys_role_menu (role_id, menu_id)
SELECT 2, @menu_id
FROM DUAL
WHERE @menu_id IS NOT NULL;
