INSERT INTO `la_system_menu` (`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT COALESCE((SELECT `pid` FROM `la_system_menu` WHERE `perms` = 'ai.aiConfig/lists' LIMIT 1), 0), 'C', '知识库配置', '', 0, 'ai.kbConfig/lists', 'kb_config', 'ai/kb_config/index', '', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_system_menu` WHERE `perms` = 'ai.kbConfig/lists');

INSERT INTO `la_system_menu` (`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT COALESCE((SELECT `pid` FROM `la_system_menu` WHERE `perms` = 'ai.aiConfig/lists' LIMIT 1), 0), 'C', '知识文档', '', 0, 'ai.kbDocument/lists', 'kb_document', 'ai/kb_document/index', '', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_system_menu` WHERE `perms` = 'ai.kbDocument/lists');

INSERT INTO `la_system_menu` (`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT COALESCE((SELECT `pid` FROM `la_system_menu` WHERE `perms` = 'ai.aiConfig/lists' LIMIT 1), 0), 'C', '同步任务', '', 0, 'ai.kbSyncJob/lists', 'kb_sync_job', 'ai/kb_sync_job/index', '', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_system_menu` WHERE `perms` = 'ai.kbSyncJob/lists');

INSERT INTO `la_system_menu` (`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT COALESCE((SELECT `pid` FROM `la_system_menu` WHERE `perms` = 'ai.aiConfig/lists' LIMIT 1), 0), 'C', '查询日志', '', 0, 'ai.kbQueryLog/lists', 'kb_query_log', 'ai/kb_query_log/index', '', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (SELECT 1 FROM `la_system_menu` WHERE `perms` = 'ai.kbQueryLog/lists');

INSERT INTO `la_system_role_menu` (`role_id`, `menu_id`)
SELECT 1, m.id
FROM `la_system_menu` m
WHERE m.`perms` IN ('ai.kbConfig/lists', 'ai.kbDocument/lists', 'ai.kbSyncJob/lists', 'ai.kbQueryLog/lists')
  AND NOT EXISTS (
      SELECT 1
      FROM `la_system_role_menu` rm
      WHERE rm.`role_id` = 1
        AND rm.`menu_id` = m.`id`
  );
