-- =====================================================
-- adminUI 总后台 sys_menu：定时任务菜单（归属「系统配置」分组）
-- 可重复执行；固定 menu_id 区间 2613-2620
-- 若首次执行失败过，先清理孤儿按钮再执行本脚本
-- =====================================================

-- 0. 诊断：查看当前库里「系统配置」在哪（可选执行）
-- SELECT menu_id, parent_id, menu_name, menu_type, path, component
-- FROM sys_menu
-- WHERE menu_name LIKE '%系统配置%' OR menu_id IN (2000, 2600)
-- ORDER BY menu_id;

-- 1. 清理上次失败留下的孤儿数据（按钮先写入但页面未写入时）
DELETE FROM sys_role_menu WHERE menu_id BETWEEN 2613 AND 2620;
DELETE FROM sys_menu WHERE menu_id BETWEEN 2613 AND 2620;

-- 2. 解析父菜单「系统配置」（多种环境兼容）
SET @parent_id := (
    SELECT menu_id FROM sys_menu
    WHERE menu_id = 2600
    LIMIT 1
);
SET @parent_id := COALESCE(@parent_id, (
    SELECT menu_id FROM sys_menu
    WHERE menu_name = '系统配置' AND menu_type = 'M'
    ORDER BY menu_id
    LIMIT 1
));
SET @parent_id := COALESCE(@parent_id, (
    SELECT menu_id FROM sys_menu
    WHERE menu_name = '系统配置'
    ORDER BY menu_id
    LIMIT 1
));

-- 3. 若仍未找到，且已有「总后台」2000，则自动补建「系统配置」分组
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, icon, visible, status, is_frame, is_cache, create_by, create_time, remark)
SELECT 2600, '系统配置', 2000, 6, '', '', 'M', 'el-icon-s-tools', '0', '0', 0, 0, 'admin', NOW(), '顶部导航分组'
FROM DUAL
WHERE @parent_id IS NULL
  AND EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2000)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2600);

SET @parent_id := COALESCE(@parent_id, (
    SELECT menu_id FROM sys_menu WHERE menu_id = 2600 LIMIT 1
));

-- 4. 定时任务（页面）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, icon, visible, status, is_frame, is_cache, perms, create_by, create_time, remark)
SELECT 2613, '定时任务', @parent_id, 13, 'job', 'monitor/job/index', 'C', 'job', '0', '0', 0, 0, 'monitor:job:list', 'admin', NOW(), 'Quartz 调度任务管理'
FROM DUAL
WHERE @parent_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613);

-- 5. 调度日志（页面）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, icon, visible, status, is_frame, is_cache, perms, create_by, create_time, remark)
SELECT 2614, '调度日志', @parent_id, 14, 'jobLog', 'monitor/job/log', 'C', 'log', '0', '0', 0, 0, 'monitor:job:list', 'admin', NOW(), 'Quartz 调度日志'
FROM DUAL
WHERE @parent_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2614);

-- 6. 按钮权限（必须在 2613 存在后才写入）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, is_frame, is_cache, perms, create_by, create_time)
SELECT 2615, '任务查询', 2613, 1, '#', '', 'F', '0', '0', 0, 0, 'monitor:job:query', 'admin', NOW()
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2615);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, is_frame, is_cache, perms, create_by, create_time)
SELECT 2616, '任务新增', 2613, 2, '#', '', 'F', '0', '0', 0, 0, 'monitor:job:add', 'admin', NOW()
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2616);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, is_frame, is_cache, perms, create_by, create_time)
SELECT 2617, '任务修改', 2613, 3, '#', '', 'F', '0', '0', 0, 0, 'monitor:job:edit', 'admin', NOW()
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2617);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, is_frame, is_cache, perms, create_by, create_time)
SELECT 2618, '任务删除', 2613, 4, '#', '', 'F', '0', '0', 0, 0, 'monitor:job:remove', 'admin', NOW()
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2618);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, is_frame, is_cache, perms, create_by, create_time)
SELECT 2619, '状态修改', 2613, 5, '#', '', 'F', '0', '0', 0, 0, 'monitor:job:changeStatus', 'admin', NOW()
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2619);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, menu_type, visible, status, is_frame, is_cache, perms, create_by, create_time)
SELECT 2620, '任务导出', 2613, 6, '#', '', 'F', '0', '0', 0, 0, 'monitor:job:export', 'admin', NOW()
FROM DUAL
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2613)
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2620);

-- 7. 为超级管理员角色（role_id=1）授权
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, m.menu_id
FROM sys_menu m
WHERE m.menu_id BETWEEN 2613 AND 2620
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_menu rm WHERE rm.role_id = 1 AND rm.menu_id = m.menu_id
  );

-- 8. 执行结果检查（应看到 2613/定时任务 和 2614/调度日志）
SELECT menu_id, parent_id, menu_name, component, perms
FROM sys_menu
WHERE menu_id BETWEEN 2613 AND 2620
ORDER BY menu_id;
