-- 页面菜单自动补齐 SQL
-- 目标：
-- 1. 按若依常见模型补齐目录(M)与页面(C)菜单
-- 2. 管理员无需依赖 sys_role_menu 勾选；前端已按 admin 角色默认放行
-- 3. 仅插入缺失菜单，不重复插入
--
-- 说明：
-- 1. 本脚本不删除已有数据，只补齐缺失目录/页面
-- 2. 页面 path 采用“目录 + 子页面”的常见结构，贴近若依模型
-- 3. 一些详情类页面设为隐藏 visible = '1'，保留访问能力但不在侧边栏展示

START TRANSACTION;

DROP TEMPORARY TABLE IF EXISTS tmp_menu_dir_specs;
CREATE TEMPORARY TABLE tmp_menu_dir_specs (
  path VARCHAR(128) NOT NULL,
  menu_name VARCHAR(128) NOT NULL,
  icon VARCHAR(64) NULL,
  order_num INT NOT NULL DEFAULT 1,
  remark VARCHAR(255) NULL
);

DROP TEMPORARY TABLE IF EXISTS tmp_menu_page_specs;
CREATE TEMPORARY TABLE tmp_menu_page_specs (
  parent_path VARCHAR(128) NULL,
  menu_name VARCHAR(128) NOT NULL,
  path VARCHAR(128) NOT NULL,
  component VARCHAR(255) NOT NULL,
  perms VARCHAR(128) NULL,
  icon VARCHAR(64) NULL,
  order_num INT NOT NULL DEFAULT 1,
  visible CHAR(1) NOT NULL DEFAULT '0',
  is_cache CHAR(1) NOT NULL DEFAULT '0',
  remark VARCHAR(255) NULL
);

INSERT INTO tmp_menu_dir_specs (path, menu_name, icon, order_num, remark) VALUES
('dashboard', '看板统计', 'dashboard', 1, '首页看板目录'),
('projectBank', '项目库', 'table', 2, '项目库目录'),
('monitor', '系统监控', 'monitor', 3, '监控目录'),
('system', '系统管理', 'system', 4, '系统管理目录'),
('project', '项目管理', 'table', 5, '项目目录'),
('workAppraisal', '绩效管理', 'chart', 6, '绩效目录'),
('user', '用户中心', 'user', 7, '个人中心目录');

INSERT INTO tmp_menu_page_specs (parent_path, menu_name, path, component, perms, icon, order_num, visible, is_cache, remark) VALUES
-- 根页面
(NULL, '工作日志', 'index', 'worklog/index', 'business:work:hour:list', 'dashboard', 1, '0', '0', '工作日志首页'),

-- 看板统计
('dashboard', '项目进度', 'project-execution', 'dashboard/project-execution', NULL, 'table', 1, '0', '0', '项目进度页面'),

-- 项目库
('projectBank', '项目进度(兼容)', 'projectProgress', 'dashboard/project-execution', NULL, 'table', 1, '1', '0', '兼容旧路径'),
('projectBank', '项目成员', 'projectUser', 'projectBank/projectUser', NULL, 'peoples', 2, '0', '0', '项目成员页面'),
('projectBank', '人员项目', 'userProject', 'projectBank/userProject', NULL, 'table', 3, '0', '0', '人员项目页面'),
('projectBank', '我的绩效', 'userScore', 'projectBank/userScore', NULL, 'chart', 4, '0', '0', '我的绩效页面'),
('projectBank', '绩效详情', 'userScoreDetail', 'projectBank/userScoreDetail', NULL, 'form', 5, '1', '0', '绩效详情页'),

-- 用户中心
('user', '个人中心', 'profile', 'profile/index', NULL, 'user', 1, '1', '0', '个人中心页面'),

-- 系统监控
('monitor', '缓存监控', 'cache', 'monitor/cache/index', NULL, 'cache', 1, '0', '0', '缓存监控'),
('monitor', '定时任务', 'job', 'monitor/job/index', NULL, 'job', 2, '0', '0', '定时任务'),
('monitor', '登录日志', 'logininfor', 'monitor/logininfor/index', NULL, 'user', 3, '0', '0', '登录日志'),
('monitor', '在线用户', 'online', 'monitor/online/index', NULL, 'online', 4, '0', '0', '在线用户'),
('monitor', '操作日志', 'operlog', 'monitor/operlog/index', NULL, 'form', 5, '0', '0', '操作日志'),
('monitor', '服务监控', 'server', 'monitor/server/index', NULL, 'server', 6, '0', '0', '服务监控'),
('monitor', '缓存列表', 'cacheList', 'monitor/cache-list/index', NULL, 'cache', 7, '0', '0', '缓存列表'),

-- 系统管理
('system', '用户管理', 'user', 'system/user/index', NULL, 'user', 1, '0', '0', '用户管理'),
('system', '角色管理', 'role', 'system/role/index', NULL, 'peoples', 2, '0', '0', '角色管理'),
('system', '菜单管理', 'menu', 'system/menu/index', NULL, 'tree-table', 3, '0', '0', '菜单管理'),
('system', '部门管理', 'dept', 'system/dept/index', NULL, 'tree', 4, '0', '0', '部门管理'),
('system', '字典管理', 'dict', 'system/dict/index', NULL, 'dict', 5, '0', '0', '字典管理'),
('system', '参数配置', 'config', 'system/config/index', NULL, 'edit', 6, '0', '0', '参数配置'),

-- 项目管理
('project', '项目列表', 'list', 'project/list', NULL, 'table', 1, '0', '0', '项目列表'),
('project', '项目详情', 'detail', 'project/detail', NULL, 'form', 2, '1', '0', '项目详情页'),
('project', '需求管理', 'demandManage', 'project/DemandManagePage', NULL, 'edit', 3, '0', '0', '需求管理'),

-- 绩效管理
('workAppraisal', '经理评分', 'manager', 'workAppraisal/manager', NULL, 'build', 1, '0', '0', '经理评分页'),
('workAppraisal', '员工评分', 'normalWorker', 'workAppraisal/normalWorker', NULL, 'chart', 2, '0', '0', '员工评分页'),
('workAppraisal', '经理评分用户', 'managerUser', 'workAppraisal/managerUser', NULL, 'peoples', 3, '1', '0', '经理评分用户页'),
('workAppraisal', '任务设置', 'taskSet', 'workAppraisal/taskSet', NULL, 'tool', 4, '0', '0', '任务设置页'),
('workAppraisal', '评分详情', 'detail', 'workAppraisal/detail', NULL, 'form', 5, '1', '0', '评分详情页'),
('workAppraisal', '绩效看板', 'taskModule', 'workAppraisal/taskModule', NULL, 'chart', 6, '0', '0', '绩效看板页'),
('workAppraisal', '绩效看板(兼容)', 'dashboard', 'workAppraisal/taskModule', NULL, 'chart', 7, '1', '0', '兼容旧路径'),
('workAppraisal', '模块详情', 'moduleDetail', 'workAppraisal/moduleDetail', NULL, 'form', 8, '1', '0', '模块详情页'),
('workAppraisal', '我的绩效(兼容)', 'myPerformance', 'projectBank/userScore', NULL, 'chart', 9, '1', '0', '兼容旧路径');

-- 1. 先补目录
INSERT INTO sys_menu (
  menu_name,
  parent_id,
  order_num,
  path,
  component,
  query,
  is_frame,
  is_cache,
  menu_type,
  visible,
  status,
  perms,
  icon,
  create_by,
  create_time,
  remark
)
SELECT
  d.menu_name,
  0,
  d.order_num,
  d.path,
  '',
  '',
  '1',
  '0',
  'M',
  '0',
  '0',
  '',
  d.icon,
  'codex',
  NOW(),
  d.remark
FROM tmp_menu_dir_specs d
WHERE NOT EXISTS (
  SELECT 1
  FROM sys_menu m
  WHERE m.menu_type IN ('M', 'C')
    AND m.parent_id = 0
    AND m.path = d.path
);

-- 2. 再补页面
INSERT INTO sys_menu (
  menu_name,
  parent_id,
  order_num,
  path,
  component,
  query,
  is_frame,
  is_cache,
  menu_type,
  visible,
  status,
  perms,
  icon,
  create_by,
  create_time,
  remark
)
SELECT
  p.menu_name,
  CASE
    WHEN p.parent_path IS NULL OR p.parent_path = '' THEN 0
    ELSE (
      SELECT m.menu_id
      FROM sys_menu m
      WHERE m.menu_type = 'M'
        AND m.path = p.parent_path
      ORDER BY m.menu_id
      LIMIT 1
    )
  END AS parent_id,
  p.order_num,
  p.path,
  p.component,
  '',
  '1',
  p.is_cache,
  'C',
  p.visible,
  '0',
  COALESCE(p.perms, ''),
  p.icon,
  'codex',
  NOW(),
  p.remark
FROM tmp_menu_page_specs p
WHERE (
  p.parent_path IS NULL
  OR EXISTS (
    SELECT 1
    FROM sys_menu m
    WHERE m.menu_type = 'M'
      AND m.path = p.parent_path
  )
)
AND NOT EXISTS (
  SELECT 1
  FROM sys_menu c
  WHERE c.menu_type IN ('M', 'C')
    AND c.path = p.path
    AND (
      c.component = p.component
      OR c.menu_name = p.menu_name
    )
    AND c.parent_id = CASE
      WHEN p.parent_path IS NULL OR p.parent_path = '' THEN 0
      ELSE (
        SELECT m.menu_id
        FROM sys_menu m
        WHERE m.menu_type = 'M'
          AND m.path = p.parent_path
        ORDER BY m.menu_id
        LIMIT 1
      )
    END
);

-- 3. 查看本次补齐结果
SELECT
  parent.menu_name AS parent_menu_name,
  child.menu_name,
  child.path,
  child.component,
  child.menu_type,
  child.visible
FROM sys_menu child
LEFT JOIN sys_menu parent ON parent.menu_id = child.parent_id
WHERE child.create_by = 'codex'
  AND child.menu_type IN ('M', 'C')
ORDER BY child.parent_id, child.order_num, child.menu_id;

COMMIT;
