SET NAMES utf8mb4;

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
VALUES (
  399900000000000001, 366200000000000003, 1, '查询', '', '', '',
  0, '/api/v1/chatHistory/query', 1, 0, NOW(), 1, NOW(), 1, '聊天历史查询权限'
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT
  399900000000000100 + ROW_NUMBER() OVER (ORDER BY candidate.`role_id`),
  candidate.`role_id`,
  399900000000000001
FROM (
  SELECT DISTINCT parent_grant.`role_id`
  FROM `tb_sys_role_menu` parent_grant
  WHERE parent_grant.`menu_id` = 366200000000000003
) candidate
WHERE NOT EXISTS (
  SELECT 1
  FROM `tb_sys_role_menu` existing_grant
  WHERE existing_grant.`role_id` = candidate.`role_id`
    AND existing_grant.`menu_id` = 399900000000000001
);
