-- 权限按钮对账与补齐脚本
-- 用途：
-- 1. 根据前端当前实际使用到的权限码，对 sys_menu 中的按钮菜单做补齐
-- 2. 仅插入缺失按钮，不修改已有按钮，不重复插入
-- 3. 执行前会先输出“未找到父页面菜单”的按钮清单，便于先补页面再补按钮
--
-- 说明：
-- 1. 当前环境无法直接连库校验，这份脚本基于前端代码中的权限使用点生成
-- 2. 父页面使用 sys_menu.path 定位，个别页面补充 component 模糊匹配兜底
-- 3. 同一权限码如果在多个页面中复用，会按页面分别补按钮节点

START TRANSACTION;

DROP TEMPORARY TABLE IF EXISTS tmp_permission_button_specs;
CREATE TEMPORARY TABLE tmp_permission_button_specs (
  parent_path VARCHAR(128) NOT NULL,
  parent_component_like VARCHAR(255) NULL,
  menu_name VARCHAR(128) NOT NULL,
  perms VARCHAR(128) NOT NULL,
  order_num INT NOT NULL DEFAULT 1,
  remark VARCHAR(255) NULL
);

INSERT INTO tmp_permission_button_specs (parent_path, parent_component_like, menu_name, perms, order_num, remark) VALUES
-- 工作日志
('index', '%worklog%', '新增日志', 'business:work:hour:add', 1, 'WorkLogPage 添加日志'),
('index', '%worklog%', '编辑日志', 'business:work:hour:edit', 2, 'WorkLogPage 编辑/确认'),
('index', '%worklog%', '更新日志', 'business:work:hour:update', 3, 'WorkLogPage 兼容旧权限码'),
('index', '%worklog%', '删除日志', 'business:work:hour:remove', 4, 'WorkLogPage 删除/取消'),

-- 项目管理
('project', '%project/list%', '新增项目', 'business:project:add', 1, 'ProjectPage'),
('project', '%project/list%', '编辑项目', 'business:project:edit', 2, 'ProjectPage/ProjectDetailPage'),
('project', '%project/list%', '删除项目', 'business:project:remove', 3, 'ProjectPage'),
('project', '%project/list%', '查看需求', 'project:demand:list', 4, 'ProjectPage'),

-- 需求管理
('demandManage', NULL, '新增需求', 'project:demand:add', 1, 'DemandManagePage'),
('demandManage', NULL, '编辑需求', 'project:demand:edit', 2, 'DemandManagePage'),
('demandManage', NULL, '删除需求', 'project:demand:remove', 3, 'DemandManagePage'),
('demandManage', NULL, '新增版本', 'project:version:add', 4, 'DemandManagePage'),
('demandManage', NULL, '编辑版本', 'project:version:edit', 5, 'DemandManagePage'),
('demandManage', NULL, '删除版本', 'project:version:remove', 6, 'DemandManagePage'),

-- 任务监控
('job', NULL, '新增任务', 'monitor:job:add', 1, 'JobMonitorPage'),
('job', NULL, '编辑任务', 'monitor:job:edit', 2, 'JobMonitorPage'),
('job', NULL, '删除任务', 'monitor:job:remove', 3, 'JobMonitorPage'),
('job', NULL, '导出任务', 'monitor:job:export', 4, 'JobMonitorPage'),
('job', NULL, '查询任务', 'monitor:job:query', 5, 'JobMonitorPage'),
('job', NULL, '状态修改', 'monitor:job:changeStatus', 6, 'JobMonitorPage'),

-- 缓存列表
('cacheList', NULL, '清理缓存', 'monitor:cache:clear', 1, 'CacheListPage'),

-- 在线用户
('online', NULL, '强退用户', 'monitor:online:forceLogout', 1, 'OnlineUserPage'),

-- 登录日志
('logininfor', NULL, '删除日志', 'monitor:logininfor:remove', 1, 'LoginLogPage'),
('logininfor', NULL, '清空日志', 'monitor:logininfor:clean', 2, 'LoginLogPage'),
('logininfor', NULL, '账户解锁', 'monitor:logininfor:unlock', 3, 'LoginLogPage'),
('logininfor', NULL, '导出日志', 'monitor:logininfor:export', 4, 'LoginLogPage'),

-- 操作日志
('operlog', NULL, '删除日志', 'monitor:operlog:remove', 1, 'OperationLogPage'),
('operlog', NULL, '清空日志', 'monitor:operlog:clean', 2, 'OperationLogPage'),
('operlog', NULL, '导出日志', 'monitor:operlog:export', 3, 'OperationLogPage'),

-- 用户管理
('user', NULL, '新增用户', 'system:user:add', 1, 'UserPage'),
('user', NULL, '修改用户', 'system:user:edit', 2, 'UserPage'),
('user', NULL, '删除用户', 'system:user:remove', 3, 'UserPage'),
('user', NULL, '重置密码', 'system:user:resetPwd', 4, 'UserPage'),

-- 角色管理
('role', NULL, '新增角色', 'system:role:add', 1, 'RolePage'),
('role', NULL, '修改角色', 'system:role:edit', 2, 'RolePage'),
('role', NULL, '删除角色', 'system:role:remove', 3, 'RolePage'),
('role', NULL, '导出角色', 'system:role:export', 4, 'RolePage'),
('role', NULL, '数据权限', 'system:role:dataScope', 5, 'RolePage'),
('role', NULL, '分配用户', 'system:role:authUser', 6, 'RolePage'),

-- 菜单管理
('menu', NULL, '新增菜单', 'system:menu:add', 1, 'MenuPage'),
('menu', NULL, '修改菜单', 'system:menu:edit', 2, 'MenuPage'),
('menu', NULL, '删除菜单', 'system:menu:remove', 3, 'MenuPage'),

-- 部门管理
('dept', NULL, '新增部门', 'system:dept:add', 1, 'DeptPage'),
('dept', NULL, '修改部门', 'system:dept:edit', 2, 'DeptPage'),
('dept', NULL, '删除部门', 'system:dept:remove', 3, 'DeptPage'),

-- 字典管理
('dict', NULL, '新增字典', 'system:dict:add', 1, 'DictPage'),
('dict', NULL, '修改字典', 'system:dict:edit', 2, 'DictPage'),
('dict', NULL, '删除字典', 'system:dict:remove', 3, 'DictPage'),
('dict', NULL, '导出字典', 'system:dict:export', 4, 'DictPage'),
('dict', NULL, '刷新缓存', 'system:dict:refreshCache', 5, 'DictPage'),

-- 参数配置
('config', NULL, '新增参数', 'system:config:add', 1, 'ConfigPage'),
('config', NULL, '修改参数', 'system:config:edit', 2, 'ConfigPage'),
('config', NULL, '删除参数', 'system:config:remove', 3, 'ConfigPage'),
('config', NULL, '导出参数', 'system:config:export', 4, 'ConfigPage'),
('config', NULL, '刷新缓存', 'system:config:refreshCache', 5, 'ConfigPage'),

-- 绩效任务设置
('taskSet', NULL, '新增任务', 'task:add', 1, 'TaskSetPage'),
('taskSet', NULL, '创建任务', 'task:create', 2, 'TaskSetPage 兼容旧权限码'),
('taskSet', NULL, '编辑任务', 'task:edit', 3, 'TaskSetPage'),
('taskSet', NULL, '更新任务', 'task:update', 4, 'TaskSetPage 兼容旧权限码'),
('taskSet', NULL, '删除任务', 'task:remove', 5, 'TaskSetPage'),
('taskSet', NULL, '删除任务兼容', 'task:delete', 6, 'TaskSetPage 兼容旧权限码'),
('taskSet', NULL, '指标配置', 'task:config', 7, 'TaskSetPage'),
('taskSet', NULL, '设置指标', 'task:set', 8, 'TaskSetPage 兼容旧权限码'),

-- 绩效看板
('taskModule', NULL, '删除模板', 'examine:template:remove', 1, 'AppraisalDashboardPage'),
('taskModule', NULL, '删除模板兼容', 'examine:template:delete', 2, 'AppraisalDashboardPage 兼容旧权限码'),

-- 绩效打分相关页面
('manager', NULL, '新增评分', 'examine:detail:add', 1, 'ManagerPage'),
('manager', NULL, '编辑评分', 'examine:detail:edit', 2, 'ManagerPage'),
('manager', NULL, '提交评分', 'examine:detail:submit', 3, 'ManagerPage'),

('managerUser', NULL, '新增评分', 'examine:detail:add', 1, 'ManagerUserPage'),
('managerUser', NULL, '编辑评分', 'examine:detail:edit', 2, 'ManagerUserPage'),
('managerUser', NULL, '提交评分', 'examine:detail:submit', 3, 'ManagerUserPage'),

('normalWorker', NULL, '新增评分', 'examine:detail:add', 1, 'NormalWorkerPage'),
('normalWorker', NULL, '编辑评分', 'examine:detail:edit', 2, 'NormalWorkerPage'),
('normalWorker', NULL, '提交评分', 'examine:detail:submit', 3, 'NormalWorkerPage'),

('detail', NULL, '新增评分', 'examine:detail:add', 1, 'AppraisalDetailPage'),
('detail', NULL, '编辑评分', 'examine:detail:edit', 2, 'AppraisalDetailPage'),
('detail', NULL, '提交评分', 'examine:detail:submit', 3, 'AppraisalDetailPage');

-- 先看哪些按钮找不到父页面
SELECT
  s.parent_path,
  s.menu_name,
  s.perms,
  s.remark
FROM tmp_permission_button_specs s
WHERE (
  SELECT m.menu_id
  FROM sys_menu m
  WHERE m.menu_type IN ('M', 'C')
    AND (
      m.path = s.parent_path
      OR (s.parent_component_like IS NOT NULL AND s.parent_component_like <> '' AND m.component LIKE s.parent_component_like)
    )
  ORDER BY
    CASE WHEN m.path = s.parent_path THEN 0 ELSE 1 END,
    m.menu_id
  LIMIT 1
) IS NULL
ORDER BY s.parent_path, s.order_num;

-- 补齐缺失按钮
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
  s.menu_name,
  (
    SELECT m.menu_id
    FROM sys_menu m
    WHERE m.menu_type IN ('M', 'C')
      AND (
        m.path = s.parent_path
        OR (s.parent_component_like IS NOT NULL AND s.parent_component_like <> '' AND m.component LIKE s.parent_component_like)
      )
    ORDER BY
      CASE WHEN m.path = s.parent_path THEN 0 ELSE 1 END,
      m.menu_id
    LIMIT 1
  ) AS parent_id,
  s.order_num,
  '#',
  '',
  '',
  '1',
  '0',
  'F',
  '0',
  '0',
  s.perms,
  '#',
  'codex',
  NOW(),
  s.remark
FROM tmp_permission_button_specs s
WHERE (
  SELECT m.menu_id
  FROM sys_menu m
  WHERE m.menu_type IN ('M', 'C')
    AND (
      m.path = s.parent_path
      OR (s.parent_component_like IS NOT NULL AND s.parent_component_like <> '' AND m.component LIKE s.parent_component_like)
    )
  ORDER BY
    CASE WHEN m.path = s.parent_path THEN 0 ELSE 1 END,
    m.menu_id
  LIMIT 1
) IS NOT NULL
AND NOT EXISTS (
  SELECT 1
  FROM sys_menu f
  WHERE f.menu_type = 'F'
    AND f.perms = s.perms
    AND f.parent_id = (
      SELECT m.menu_id
      FROM sys_menu m
      WHERE m.menu_type IN ('M', 'C')
        AND (
          m.path = s.parent_path
          OR (s.parent_component_like IS NOT NULL AND s.parent_component_like <> '' AND m.component LIKE s.parent_component_like)
        )
      ORDER BY
        CASE WHEN m.path = s.parent_path THEN 0 ELSE 1 END,
        m.menu_id
      LIMIT 1
    )
);

-- 补齐后可查看这一批按钮
SELECT
  p.menu_name AS parent_menu_name,
  p.path AS parent_path,
  c.menu_name,
  c.perms,
  c.order_num
FROM sys_menu c
INNER JOIN sys_menu p ON p.menu_id = c.parent_id
WHERE c.menu_type = 'F'
  AND c.create_by = 'codex'
ORDER BY p.path, c.order_num, c.menu_id;

COMMIT;
