-- 工作日志菜单与按钮补录
-- 目标：
-- 1. 确保 sys_menu 中存在工作日志页面菜单（/index）
-- 2. 为工作日志页面补齐按钮权限项
-- 3. 所有插入都做不存在判断，避免重复
--
-- 对应前端逻辑：
-- - 页面路由：/index
-- - 页面组件：component 包含 worklog 即可被前端识别为工作日志页
-- - 按钮权限：
--   business:work:hour:add
--   business:work:hour:edit
--   business:work:hour:remove

START TRANSACTION;

-- 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
  '工作日志',
  0,
  1,
  'index',
  'worklog/index',
  '',
  '1',
  '0',
  'C',
  '0',
  '0',
  'business:work:hour:list',
  'dashboard',
  'codex',
  NOW(),
  '工作日志首页菜单'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1
  FROM sys_menu
  WHERE menu_type IN ('M', 'C')
    AND (
      path IN ('index', '/index')
      OR component LIKE '%worklog%'
    )
);

-- 2. 定位工作日志页面菜单
SET @worklog_menu_id := (
  SELECT menu_id
  FROM sys_menu
  WHERE menu_type IN ('M', 'C')
    AND (
      path IN ('index', '/index')
      OR component LIKE '%worklog%'
    )
  ORDER BY
    CASE
      WHEN path IN ('index', '/index') THEN 0
      ELSE 1
    END,
    menu_id
  LIMIT 1
);

-- 3. 补录按钮：新增日志
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
  '新增日志',
  @worklog_menu_id,
  1,
  '#',
  '',
  '',
  '1',
  '0',
  'F',
  '0',
  '0',
  'business:work:hour:add',
  '#',
  'codex',
  NOW(),
  '工作日志页按钮：添加日志'
FROM DUAL
WHERE @worklog_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE parent_id = @worklog_menu_id
      AND menu_type = 'F'
      AND perms = 'business:work:hour:add'
  );

-- 4. 补录按钮：编辑日志
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
  '编辑日志',
  @worklog_menu_id,
  2,
  '#',
  '',
  '',
  '1',
  '0',
  'F',
  '0',
  '0',
  'business:work:hour:edit',
  '#',
  'codex',
  NOW(),
  '工作日志页按钮：编辑/确认'
FROM DUAL
WHERE @worklog_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE parent_id = @worklog_menu_id
      AND menu_type = 'F'
      AND perms = 'business:work:hour:edit'
  );

-- 5. 补录按钮：删除日志
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
  '删除日志',
  @worklog_menu_id,
  3,
  '#',
  '',
  '',
  '1',
  '0',
  'F',
  '0',
  '0',
  'business:work:hour:remove',
  '#',
  'codex',
  NOW(),
  '工作日志页按钮：删除/取消'
FROM DUAL
WHERE @worklog_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE parent_id = @worklog_menu_id
      AND menu_type = 'F'
      AND perms = 'business:work:hour:remove'
  );

COMMIT;
