-- 待办中心菜单与基础权限
-- 执行效果：
-- 1. 新增可访问 /system/todo 的顶级菜单。
-- 2. 为所有正常角色授予列表、详情和已读权限；接口始终只返回当前登录用户数据。
-- 3. 完成和取消权限默认不授予普通角色，需按业务职责显式分配。

SET @todoMenuId := (
  SELECT menu_id
  FROM sys_menu
  WHERE path = 'system/todo'
    AND menu_type = 'C'
  LIMIT 1
);

INSERT INTO sys_menu (
  menu_name, parent_id, order_num, path, component, is_frame, is_cache,
  menu_type, visible, status, perms, icon, create_by, create_time, remark
)
SELECT
  '待办中心', 0, 8, 'system/todo', 'system/todo/index', 1, 0,
  'C', '0', '0', 'system:todo:list', 'list', 'system', sysdate(), '当前登录用户待办中心'
FROM DUAL
WHERE @todoMenuId IS NULL;

SET @todoMenuId := COALESCE(@todoMenuId, LAST_INSERT_ID());

SET @todoQueryMenuId := (
  SELECT menu_id FROM sys_menu
  WHERE parent_id = @todoMenuId AND perms = 'system:todo:query'
  LIMIT 1
);

INSERT INTO sys_menu (
  menu_name, parent_id, order_num, path, component, is_frame, is_cache,
  menu_type, visible, status, perms, icon, create_by, create_time, remark
)
SELECT
  '待办查询', @todoMenuId, 1, '#', '', 1, 0,
  'F', '0', '0', 'system:todo:query', '#', 'system', sysdate(), ''
FROM DUAL
WHERE @todoQueryMenuId IS NULL;

SET @todoQueryMenuId := COALESCE(@todoQueryMenuId, LAST_INSERT_ID());

SET @todoReadMenuId := (
  SELECT menu_id FROM sys_menu
  WHERE parent_id = @todoMenuId AND perms = 'system:todo:read'
  LIMIT 1
);

INSERT INTO sys_menu (
  menu_name, parent_id, order_num, path, component, is_frame, is_cache,
  menu_type, visible, status, perms, icon, create_by, create_time, remark
)
SELECT
  '待办已读', @todoMenuId, 2, '#', '', 1, 0,
  'F', '0', '0', 'system:todo:read', '#', 'system', sysdate(), ''
FROM DUAL
WHERE @todoReadMenuId IS NULL;

SET @todoReadMenuId := COALESCE(@todoReadMenuId, LAST_INSERT_ID());

SET @todoCompleteMenuId := (
  SELECT menu_id FROM sys_menu
  WHERE parent_id = @todoMenuId AND perms = 'system:todo:complete'
  LIMIT 1
);

INSERT INTO sys_menu (
  menu_name, parent_id, order_num, path, component, is_frame, is_cache,
  menu_type, visible, status, perms, icon, create_by, create_time, remark
)
SELECT
  '待办完成', @todoMenuId, 3, '#', '', 1, 0,
  'F', '0', '0', 'system:todo:complete', '#', 'system', sysdate(), '仅分配给具备对应业务处理权限的角色'
FROM DUAL
WHERE @todoCompleteMenuId IS NULL;

SET @todoCompleteMenuId := COALESCE(@todoCompleteMenuId, LAST_INSERT_ID());

SET @todoCancelMenuId := (
  SELECT menu_id FROM sys_menu
  WHERE parent_id = @todoMenuId AND perms = 'system:todo:cancel'
  LIMIT 1
);

INSERT INTO sys_menu (
  menu_name, parent_id, order_num, path, component, is_frame, is_cache,
  menu_type, visible, status, perms, icon, create_by, create_time, remark
)
SELECT
  '待办取消', @todoMenuId, 4, '#', '', 1, 0,
  'F', '0', '0', 'system:todo:cancel', '#', 'system', sysdate(), '仅分配给具备关闭任务权限的角色'
FROM DUAL
WHERE @todoCancelMenuId IS NULL;

SET @todoCancelMenuId := COALESCE(@todoCancelMenuId, LAST_INSERT_ID());

-- 所有正常角色均可查看、查询并标记本人的待办。
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT role_id, @todoMenuId
FROM sys_role role_info
WHERE role_info.status = '0'
  AND role_info.del_flag = '0'
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_menu relation
    WHERE relation.role_id = role_info.role_id
      AND relation.menu_id = @todoMenuId
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT role_id, @todoQueryMenuId
FROM sys_role role_info
WHERE role_info.status = '0'
  AND role_info.del_flag = '0'
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_menu relation
    WHERE relation.role_id = role_info.role_id
      AND relation.menu_id = @todoQueryMenuId
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT role_id, @todoReadMenuId
FROM sys_role role_info
WHERE role_info.status = '0'
  AND role_info.del_flag = '0'
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_menu relation
    WHERE relation.role_id = role_info.role_id
      AND relation.menu_id = @todoReadMenuId
  );

-- 完成/取消权限按业务角色手工授权示例：
-- INSERT INTO sys_role_menu (role_id, menu_id) VALUES (<业务处理角色ID>, @todoCompleteMenuId);
-- INSERT INTO sys_role_menu (role_id, menu_id) VALUES (<任务关闭角色ID>, @todoCancelMenuId);

-- 管理端异常任务处理权限，仅默认授予 role_key = 'admin' 的系统管理员。
INSERT INTO sys_menu (
  menu_name, parent_id, order_num, path, component, is_frame, is_cache,
  menu_type, visible, status, perms, icon, create_by, create_time, remark
)
SELECT permission_info.menu_name, @todoMenuId, permission_info.order_num, '#', '', 1, 0,
       'F', '0', '0', permission_info.perms, '#', 'system', sysdate(), permission_info.remark
FROM (
  SELECT '待办管理查询' AS menu_name, 11 AS order_num, 'system:todo:admin:list' AS perms, '查看全量待办' AS remark
  UNION ALL SELECT '待办人工创建', 12, 'system:todo:admin:create', '人工补发异常待办'
  UNION ALL SELECT '待办重新分配', 13, 'system:todo:admin:assign', '重新分配有效待办'
  UNION ALL SELECT '待办管理取消', 14, 'system:todo:admin:cancel', '管理员关闭异常待办'
  UNION ALL SELECT '待办逻辑隐藏', 15, 'system:todo:admin:remove', '隐藏终态待办记录'
) permission_info
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu existing_menu
  WHERE existing_menu.parent_id = @todoMenuId
    AND existing_menu.perms = permission_info.perms
);

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT role_info.role_id, menu_info.menu_id
FROM sys_role role_info
JOIN sys_menu menu_info
  ON menu_info.parent_id = @todoMenuId
 AND menu_info.perms IN (
   'system:todo:admin:list',
   'system:todo:admin:create',
   'system:todo:admin:assign',
   'system:todo:admin:cancel',
   'system:todo:admin:remove'
 )
WHERE role_info.role_key = 'admin'
  AND role_info.status = '0'
  AND role_info.del_flag = '0'
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_menu relation
    WHERE relation.role_id = role_info.role_id
      AND relation.menu_id = menu_info.menu_id
  );

SELECT
  @todoMenuId AS todo_menu_id,
  @todoQueryMenuId AS todo_query_menu_id,
  @todoReadMenuId AS todo_read_menu_id,
  @todoCompleteMenuId AS todo_complete_menu_id,
  @todoCancelMenuId AS todo_cancel_menu_id;
