-- iMeeting latest seed initialization script
-- 用途:
--   1. 面向“已执行最新全量建表脚本”的全新部署
--   2. 仅初始化系统基础数据，不包含历史会议、附件、任务、知识库等业务数据
--   3. 尽量支持重复执行（幂等），便于新库初始化或回放
--
-- 重要前提:
--   1. 已先执行 imeeting-schema-latest.sql
--   2. 当前库已存在以下最新结构:
--      sys_roles / sys_users / sys_menus / sys_role_menu_permissions
--      prompts(desc,is_system) / prompt_config
--      sys_system_parameters / llm_model_config / audio_model_config
--      hot_word_group / hot_word_item / sys_dict_data
--
-- 默认账号:
--   admin / admin123
--   demo  / 123456
--
-- 说明:
--   1. 模型 API Key 默认留空，优先走环境变量配置
--   2. ASR 默认绑定“默认热词组”，首次执行后仍可在后台继续同步词表

SET NAMES utf8mb4;

START TRANSACTION;

-- =====================================================================
-- 1. 基础角色
-- =====================================================================
INSERT INTO `sys_roles` (`role_id`, `role_name`, `created_at`)
VALUES
  (1, '平台管理员', NOW()),
  (2, '普通用户', NOW())
ON DUPLICATE KEY UPDATE
  `role_name` = VALUES(`role_name`);

-- =====================================================================
-- 2. 基础用户
-- 密码说明:
--   admin  -> admin123
--   demo   -> 123456
-- =====================================================================
INSERT INTO `sys_users`
(`user_id`, `username`, `caption`, `email`, `avatar_url`, `password_hash`, `role_id`, `created_at`)
VALUES
  (1, 'admin', '系统管理员', 'admin@imeeting.local', NULL, '240be518fabd2724ddb6f04eeb1da5967448d7e831c08c8fa822809f74c720a9', 1, NOW()),
  (2, 'demo',  '演示用户',   'demo@imeeting.local',  NULL, '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92', 2, NOW())
ON DUPLICATE KEY UPDATE
  `username` = VALUES(`username`),
  `caption` = VALUES(`caption`),
  `email` = VALUES(`email`),
  `avatar_url` = VALUES(`avatar_url`),
  `password_hash` = VALUES(`password_hash`),
  `role_id` = VALUES(`role_id`);

-- =====================================================================
-- 3. 系统默认提示词模版
-- =====================================================================
INSERT INTO `prompts`
(`id`, `name`, `task_type`, `content`, `desc`, `is_default`, `is_active`, `creator_id`, `is_system`, `created_at`)
VALUES
  (
    1,
    '系统默认会议总结',
    'MEETING_TASK',
    '你是一名专业的会议纪要助手。请基于会议转写内容输出结构化 Markdown，总结以下内容：

## 会议概览
- 说明会议主题、时间、参与方、目标

## 核心讨论
- 按议题归纳讨论重点，不按逐句流水账整理

## 决策事项
- 明确已经形成共识的决定、口径与结论

## 待办事项
- 使用表格列出事项、责任人、时间要求、当前状态

## 风险与建议
- 提醒未决问题、依赖项、潜在风险，并给出后续建议

输出要求：
1. 仅基于原文，不臆测未提及内容
2. 信息缺失时明确标注“未提及”
3. 语言简洁、可直接归档共享',
    '会议转写默认总结模板',
    1,
    1,
    1,
    1,
    NOW()
  ),
  (
    2,
    '系统默认知识整理',
    'KNOWLEDGE_TASK',
    '你是一名专业的知识整理助手。请基于输入材料生成一篇适合归档到知识库的 Markdown 文档，包含以下部分：

## 标题
- 产出准确、可检索的标题

## 摘要
- 用 3 至 5 句话概括核心内容

## 关键事实
- 提炼重要信息、规则、约束和背景

## 操作步骤或处理方案
- 如果材料中包含流程或做法，请按步骤梳理

## 风险与注意事项
- 标记边界条件、限制项和容易出错的点

## 相关名词与标签
- 提炼适合检索和分类的关键词

输出要求：
1. 保持事实准确，避免编造
2. 优先结构化、可复用、可沉淀
3. 文风专业、简洁、便于知识检索',
    '知识库整理默认模板',
    1,
    1,
    1,
    1,
    NOW()
  )
ON DUPLICATE KEY UPDATE
  `name` = VALUES(`name`),
  `task_type` = VALUES(`task_type`),
  `content` = VALUES(`content`),
  `desc` = VALUES(`desc`),
  `is_default` = VALUES(`is_default`),
  `is_active` = VALUES(`is_active`),
  `creator_id` = VALUES(`creator_id`),
  `is_system` = VALUES(`is_system`);

-- 为初始化账号补充默认模版启用配置
INSERT INTO `prompt_config`
(`user_id`, `task_type`, `prompt_id`, `is_enabled`, `sort_order`, `created_at`, `updated_at`)
VALUES
  (1, 'MEETING_TASK',   1, 1, 1, NOW(), NOW()),
  (1, 'KNOWLEDGE_TASK', 2, 1, 1, NOW(), NOW()),
  (2, 'MEETING_TASK',   1, 1, 1, NOW(), NOW()),
  (2, 'KNOWLEDGE_TASK', 2, 1, 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE
  `is_enabled` = VALUES(`is_enabled`),
  `sort_order` = VALUES(`sort_order`),
  `updated_at` = VALUES(`updated_at`);

-- =====================================================================
-- 4. 系统参数
-- =====================================================================
INSERT INTO `sys_system_parameters`
(`param_key`, `param_name`, `param_value`, `value_type`, `category`, `description`, `is_active`, `created_at`, `updated_at`)
VALUES
  ('token_expire_days',      'Token过期时间',         '7',        'number', 'system', '控制登录 token 的过期时间，单位：天。', 1, NOW(), NOW()),
  ('default_reset_password', '默认重置密码',         '123456',   'string', 'system', '管理员重置用户密码时使用的默认密码。', 1, NOW(), NOW()),
  ('page_size',              '系统分页大小',         '10',       'number', 'public', '系统通用分页数量。',                 1, NOW(), NOW()),
  ('max_audio_size',         '音频上传大小限制',     '100',      'number', 'public', '音频上传大小限制，单位：MB。',         1, NOW(), NOW()),
  ('max_image_size',         '图片上传大小限制',     '10',       'number', 'public', '图片上传大小限制，单位：MB。',         1, NOW(), NOW()),
  ('app_name',               '系统名称',             'iMeeting', 'string', 'public', '前端应用标题。',                     1, NOW(), NOW())
ON DUPLICATE KEY UPDATE
  `param_name` = VALUES(`param_name`),
  `param_value` = VALUES(`param_value`),
  `value_type` = VALUES(`value_type`),
  `category` = VALUES(`category`),
  `description` = VALUES(`description`),
  `is_active` = VALUES(`is_active`),
  `updated_at` = VALUES(`updated_at`);

-- =====================================================================
-- 5. 默认热词组与热词
-- =====================================================================
INSERT INTO `hot_word_group` (`name`, `description`, `status`, `create_time`, `update_time`)
SELECT '默认热词组', '系统初始化热词组，供默认 ASR 模型绑定使用。', 1, NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `hot_word_group` WHERE `name` = '默认热词组'
);

UPDATE `hot_word_group`
SET
  `description` = '系统初始化热词组，供默认 ASR 模型绑定使用。',
  `status` = 1,
  `update_time` = NOW()
WHERE `name` = '默认热词组';

SET @default_hot_word_group_id := (
  SELECT `id`
  FROM `hot_word_group`
  WHERE `name` = '默认热词组'
  ORDER BY `id`
  LIMIT 1
);

INSERT INTO `hot_word_item`
(`group_id`, `text`, `weight`, `lang`, `status`, `create_time`, `update_time`)
SELECT @default_hot_word_group_id, 'iMeeting', 8, 'zh', 1, NOW(), NOW()
FROM DUAL
WHERE @default_hot_word_group_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `weight` = VALUES(`weight`),
  `lang` = VALUES(`lang`),
  `status` = VALUES(`status`),
  `update_time` = VALUES(`update_time`);

INSERT INTO `hot_word_item`
(`group_id`, `text`, `weight`, `lang`, `status`, `create_time`, `update_time`)
SELECT @default_hot_word_group_id, '会议纪要', 7, 'zh', 1, NOW(), NOW()
FROM DUAL
WHERE @default_hot_word_group_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `weight` = VALUES(`weight`),
  `lang` = VALUES(`lang`),
  `status` = VALUES(`status`),
  `update_time` = VALUES(`update_time`);

INSERT INTO `hot_word_item`
(`group_id`, `text`, `weight`, `lang`, `status`, `create_time`, `update_time`)
SELECT @default_hot_word_group_id, '语音转写', 6, 'zh', 1, NOW(), NOW()
FROM DUAL
WHERE @default_hot_word_group_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `weight` = VALUES(`weight`),
  `lang` = VALUES(`lang`),
  `status` = VALUES(`status`),
  `update_time` = VALUES(`update_time`);

-- =====================================================================
-- 6. 默认模型配置
-- =====================================================================
INSERT INTO `llm_model_config`
(`model_code`, `model_name`, `model_type`, `provider`, `endpoint_url`, `api_key`, `llm_model_name`,
 `llm_timeout`, `extra_config`, `description`, `is_active`, `is_default`, `created_at`, `updated_at`)
VALUES
  (
    'llm_model',
    '默认文本模型',
    'text',
    'dashscope',
    'https://dashscope.aliyuncs.com/compatible-mode/v1',
    NULL,
    'qwen-plus',
    120,
    JSON_OBJECT(
      'temperature', 0.70,
      'top_p', 0.90,
      'max_tokens', 2048,
      'system_prompt', '你是一名专业的会议与知识整理助手，请基于输入内容给出准确、结构化、可复用的输出。'
    ),
    '系统初始化的默认 LLM 模型配置，API Key 优先使用环境变量。',
    1,
    1,
    NOW(),
    NOW()
  )
ON DUPLICATE KEY UPDATE
  `model_name` = VALUES(`model_name`),
  `model_type` = VALUES(`model_type`),
  `provider` = VALUES(`provider`),
  `endpoint_url` = VALUES(`endpoint_url`),
  `api_key` = VALUES(`api_key`),
  `llm_model_name` = VALUES(`llm_model_name`),
  `llm_timeout` = VALUES(`llm_timeout`),
  `extra_config` = VALUES(`extra_config`),
  `description` = VALUES(`description`),
  `is_active` = VALUES(`is_active`),
  `is_default` = VALUES(`is_default`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `llm_model_config`
(`model_code`, `model_name`, `model_type`, `provider`, `endpoint_url`, `api_key`, `llm_model_name`,
 `llm_timeout`, `extra_config`, `description`, `is_active`, `is_default`, `created_at`, `updated_at`)
VALUES
  (
    'vector_dashscope_text_embedding_v4',
    '阿里百炼 向量 text-embedding-v4',
    'vector',
    'dashscope',
    'https://dashscope.aliyuncs.com/compatible-mode/v1',
    NULL,
    'text-embedding-v4',
    120,
    JSON_OBJECT('dimensions', 1024, 'encoding_format', 'float'),
    '系统初始化的默认向量模型配置，向量专属参数存储在 extra_config。',
    1,
    1,
    NOW(),
    NOW()
  )
ON DUPLICATE KEY UPDATE
  `model_name` = VALUES(`model_name`),
  `model_type` = VALUES(`model_type`),
  `provider` = VALUES(`provider`),
  `endpoint_url` = VALUES(`endpoint_url`),
  `api_key` = VALUES(`api_key`),
  `llm_model_name` = VALUES(`llm_model_name`),
  `llm_timeout` = VALUES(`llm_timeout`),
  `extra_config` = VALUES(`extra_config`),
  `description` = VALUES(`description`),
  `is_active` = VALUES(`is_active`),
  `is_default` = VALUES(`is_default`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `audio_model_config`
(`model_code`, `model_name`, `model_type`, `provider`, `endpoint_url`, `api_key`,
 `request_timeout_seconds`, `extra_config`, `description`,
 `is_active`, `is_default`, `created_at`, `updated_at`)
SELECT
  'audio_model',
  '默认语音识别模型',
  'asr',
  'dashscope',
  'https://dashscope.aliyuncs.com',
  NULL,
  300,
  JSON_OBJECT(
    'model', 'paraformer-v2',
    'vocabulary_id', g.`vocabulary_id`,
    'speaker_count', 10,
    'language_hints', JSON_ARRAY('zh', 'en'),
    'disfluency_removal_enabled', TRUE,
    'diarization_enabled', TRUE
  ),
  '系统初始化的默认 ASR 模型配置。',
  1,
  1,
  NOW(),
  NOW()
FROM `hot_word_group` g
WHERE g.`id` = @default_hot_word_group_id
ON DUPLICATE KEY UPDATE
  `model_name` = VALUES(`model_name`),
  `model_type` = VALUES(`model_type`),
  `provider` = VALUES(`provider`),
  `endpoint_url` = VALUES(`endpoint_url`),
  `api_key` = VALUES(`api_key`),
  `request_timeout_seconds` = VALUES(`request_timeout_seconds`),
  `extra_config` = VALUES(`extra_config`),
  `description` = VALUES(`description`),
  `is_active` = VALUES(`is_active`),
  `is_default` = VALUES(`is_default`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `audio_model_config`
(`model_code`, `model_name`, `model_type`, `provider`, `endpoint_url`, `api_key`,
 `request_timeout_seconds`, `extra_config`, `description`,
 `is_active`, `is_default`, `created_at`, `updated_at`)
VALUES
  (
    'voiceprint_model',
    '默认声纹配置',
    'voiceprint',
    'funasr',
    NULL,
    NULL,
    120,
    JSON_OBJECT(
      'template_text', '我正在进行声纹采集，这段语音将用于身份识别和验证。声纹技术能够准确识别每个人独特的声音特征。',
      'duration_seconds', 12,
      'sample_rate', 16000,
      'channels', 1,
      'max_size_bytes', 5242880
    ),
    '系统初始化的默认声纹采集配置。',
    1,
    1,
    NOW(),
    NOW()
  )
ON DUPLICATE KEY UPDATE
  `model_name` = VALUES(`model_name`),
  `model_type` = VALUES(`model_type`),
  `provider` = VALUES(`provider`),
  `endpoint_url` = VALUES(`endpoint_url`),
  `api_key` = VALUES(`api_key`),
  `request_timeout_seconds` = VALUES(`request_timeout_seconds`),
  `extra_config` = VALUES(`extra_config`),
  `description` = VALUES(`description`),
  `is_active` = VALUES(`is_active`),
  `is_default` = VALUES(`is_default`),
  `updated_at` = VALUES(`updated_at`);

-- =====================================================================
-- 7. 基础字典数据
-- =====================================================================
INSERT INTO `sys_dict_data`
(`dict_type`, `dict_code`, `parent_code`, `tree_path`, `label_cn`, `label_en`, `sort_order`,
 `extension_attr`, `is_default`, `status`, `create_time`, `update_time`)
VALUES
  ('client_platform', 'DESKTOP',  'ROOT',     NULL, '桌面端',     'Desktop',  1, JSON_OBJECT('icon', 'monitor'), 0, 1, NOW(), NOW()),
  ('client_platform', 'MOBILE',   'ROOT',     NULL, '移动端',     'Mobile',   2, JSON_OBJECT('icon', 'phone'),   0, 1, NOW(), NOW()),
  ('client_platform', 'TERMINAL', 'ROOT',     NULL, '专用终端',   'Terminal', 3, JSON_OBJECT('icon', 'router'),  0, 1, NOW(), NOW()),
  ('client_platform', 'WIN',      'DESKTOP',  NULL, 'Windows',    'Windows',  1, JSON_OBJECT('suffix', '.exe', 'arch_support', JSON_ARRAY('x86', 'x64')), 0, 1, NOW(), NOW()),
  ('client_platform', 'MAC',      'DESKTOP',  NULL, 'macOS',      'macOS',    2, JSON_OBJECT('suffix', '.dmg', 'arch_support', JSON_ARRAY('x64', 'arm64')), 0, 1, NOW(), NOW()),
  ('client_platform', 'LINUX',    'DESKTOP',  NULL, 'Linux',      'Linux',    3, JSON_OBJECT('suffix', '.deb', 'arch_support', JSON_ARRAY('x64', 'arm64')), 0, 1, NOW(), NOW()),
  ('client_platform', 'IOS',      'MOBILE',   NULL, '苹果iOS',    'iOS',      1, JSON_OBJECT('suffix', '.ipa', 'store_link', TRUE), 0, 1, NOW(), NOW()),
  ('client_platform', 'ANDROID',  'MOBILE',   NULL, '安卓',       'Android',  2, JSON_OBJECT('suffix', '.apk'), 0, 1, NOW(), NOW()),
  ('client_platform', 'TERM_STD', 'TERMINAL', NULL, '通用终端',   'Standard Terminal', 1, JSON_OBJECT('vendor', 'Generic', 'os', 'Android'), 1, 1, NOW(), NOW()),
  ('client_platform', 'TERM_S100','TERMINAL', NULL, '中兴 S100',  'ZTE S100', 2, JSON_OBJECT('vendor', 'ZTE', 'os', 'Android'),     0, 1, NOW(), NOW()),
  ('client_platform', 'TERM_A133','TERMINAL', NULL, '全志 A133',  'Allwinner A133', 3, JSON_OBJECT('vendor', 'Allwinner', 'os', 'Android'), 0, 1, NOW(), NOW()),

  ('terminal_type',   'TERM_STD', 'ROOT',     NULL, '通用终端',   'Standard Terminal', 1, NULL, 1, 1, NOW(), NOW()),
  ('terminal_type',   'TERM_S100','ROOT',     NULL, '中兴 S100',  'ZTE S100',          2, NULL, 0, 1, NOW(), NOW()),
  ('terminal_type',   'TERM_A133','ROOT',     NULL, '全志 A133',  'Allwinner A133',    3, NULL, 0, 1, NOW(), NOW()),

  ('task_type',       'MEETING_TASK',   'ROOT', NULL, '会议任务',   'Meeting Task',   1, NULL, 0, 1, NOW(), NOW()),
  ('task_type',       'KNOWLEDGE_TASK', 'ROOT', NULL, '知识库任务', 'Knowledge Task', 2, NULL, 0, 1, NOW(), NOW()),

  ('external_apps',   'NATIVE', 'ROOT', NULL, '原生应用', 'Native App', 1, JSON_OBJECT('protocol', 'apk'), 0, 1, NOW(), NOW()),
  ('external_apps',   'WEB',    'ROOT', NULL, 'Web应用',  'Web App',    2, JSON_OBJECT('protocol', 'https'), 0, 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE
  `parent_code` = VALUES(`parent_code`),
  `tree_path` = VALUES(`tree_path`),
  `label_cn` = VALUES(`label_cn`),
  `label_en` = VALUES(`label_en`),
  `sort_order` = VALUES(`sort_order`),
  `extension_attr` = VALUES(`extension_attr`),
  `is_default` = VALUES(`is_default`),
  `status` = VALUES(`status`),
  `update_time` = VALUES(`update_time`);

-- =====================================================================
-- 8. 菜单定义
-- 最终菜单树:
--   dashboard
--   desktop
--   meeting_manage
--     ├─ meeting_center
--     └─ prompt_config
--   platform_admin
--     ├─ hot_word_management
--     ├─ model_management
--     ├─ prompt_management
--     ├─ client_management
--     ├─ external_app_management
--     └─ terminal_management
--   system_management
--     ├─ user_management
--     ├─ permission_management
--     │   └─ permission_menu_tree (隐藏)
--     ├─ dict_management
--     └─ parameter_management
-- =====================================================================
INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
VALUES
  ('dashboard',         'Dashboard', 'DashboardOutlined', '/dashboard',                    'link', NULL, 1, NULL, 1, 1, 1, '管理员桌面',             NOW(), NOW()),
  ('desktop',           'Desktop',   'DesktopOutlined',   '/dashboard',                    'link', NULL, 1, NULL, 2, 1, 1, '普通用户桌面',           NOW(), NOW()),
  ('meeting_manage',    '会议管理',   'CalendarOutlined',  '/meetings/center',              'link', NULL, 1, NULL, 3, 1, 1, '普通用户会议功能入口',   NOW(), NOW()),
  ('platform_admin',    '平台管理',   'Shield',            '/admin/management/hot-word-management', 'link', NULL, 1, NULL, 4, 1, 1, '平台能力配置入口', NOW(), NOW()),
  ('system_management', '系统管理',   'Setting',           '/admin/management/system-overview',      'link', NULL, 1, NULL, 5, 1, 1, '系统治理入口',     NOW(), NOW())
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

SET @meeting_manage_id := (SELECT `menu_id` FROM `sys_menus` WHERE `menu_code` = 'meeting_manage' LIMIT 1);
SET @platform_admin_id := (SELECT `menu_id` FROM `sys_menus` WHERE `menu_code` = 'platform_admin' LIMIT 1);
SET @system_management_id := (SELECT `menu_id` FROM `sys_menus` WHERE `menu_code` = 'system_management' LIMIT 1);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'meeting_center', '会议中心', 'CalendarOutlined', '/meetings/center', 'link',
  @meeting_manage_id, 2, NULL, 1, 1, 1, '普通用户会议中心', NOW(), NOW()
FROM DUAL
WHERE @meeting_manage_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'prompt_config', '提示词配置', 'Book', '/prompt-config', 'link',
  @meeting_manage_id, 2, NULL, 2, 1, 1, '用户提示词启用与排序配置', NOW(), NOW()
FROM DUAL
WHERE @meeting_manage_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'hot_word_management', '热词管理', 'Text', '/admin/management/hot-word-management', 'link',
  @platform_admin_id, 2, NULL, 1, 1, 1, 'ASR 热词管理与同步', NOW(), NOW()
FROM DUAL
WHERE @platform_admin_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'model_management', '模型管理', 'Appstore', '/admin/management/model-management', 'link',
  @platform_admin_id, 2, NULL, 2, 1, 1, '音频模型与 LLM 模型配置', NOW(), NOW()
FROM DUAL
WHERE @platform_admin_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'prompt_management', '提示词库', 'BookText', '/prompt-management', 'link',
  @platform_admin_id, 2, NULL, 3, 1, 1, '系统提示词库管理', NOW(), NOW()
FROM DUAL
WHERE @platform_admin_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'client_management', '客户端管理', 'Smartphone', '/admin/management/client-management', 'link',
  @platform_admin_id, 2, NULL, 4, 1, 1, '客户端下载与版本管理', NOW(), NOW()
FROM DUAL
WHERE @platform_admin_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'external_app_management', '外部应用管理', 'AppWindow', '/admin/management/external-app-management', 'link',
  @platform_admin_id, 2, NULL, 5, 1, 1, '外部系统入口管理', NOW(), NOW()
FROM DUAL
WHERE @platform_admin_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'terminal_management', '终端管理', 'Monitor', '/admin/management/terminal-management', 'link',
  @platform_admin_id, 2, NULL, 6, 1, 1, '专用终端管理', NOW(), NOW()
FROM DUAL
WHERE @platform_admin_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'user_management', '用户管理', 'Users', '/admin/management/user-management', 'link',
  @system_management_id, 2, NULL, 1, 1, 1, '账号、角色、密码管理', NOW(), NOW()
FROM DUAL
WHERE @system_management_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'permission_management', '权限管理', 'KeyRound', '/admin/management/permission-management', 'link',
  @system_management_id, 2, NULL, 2, 1, 1, '菜单与角色授权管理', NOW(), NOW()
FROM DUAL
WHERE @system_management_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'dict_management', '字典管理', 'BookMarked', '/admin/management/dict-management', 'link',
  @system_management_id, 2, NULL, 3, 1, 1, '平台字典与码表管理', NOW(), NOW()
FROM DUAL
WHERE @system_management_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'parameter_management', '参数管理', 'Setting', '/admin/management/parameter-management', 'link',
  @system_management_id, 2, NULL, 4, 1, 1, '系统参数管理', NOW(), NOW()
FROM DUAL
WHERE @system_management_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

SET @permission_management_id := (
  SELECT `menu_id`
  FROM `sys_menus`
  WHERE `menu_code` = 'permission_management'
  LIMIT 1
);

INSERT INTO `sys_menus`
(`menu_code`, `menu_name`, `menu_icon`, `menu_url`, `menu_type`, `parent_id`, `menu_level`, `tree_path`,
 `sort_order`, `is_active`, `is_visible`, `description`, `created_at`, `updated_at`)
SELECT
  'permission_menu_tree', '菜单树维护', 'AppstoreAdd', '/admin/management/permission-management', 'link',
  @permission_management_id, 3, NULL, 20, 1, 0, '权限管理中的隐藏入口，用于菜单树维护。', NOW(), NOW()
FROM DUAL
WHERE @permission_management_id IS NOT NULL
ON DUPLICATE KEY UPDATE
  `menu_name` = VALUES(`menu_name`),
  `menu_icon` = VALUES(`menu_icon`),
  `menu_url` = VALUES(`menu_url`),
  `menu_type` = VALUES(`menu_type`),
  `parent_id` = VALUES(`parent_id`),
  `menu_level` = VALUES(`menu_level`),
  `sort_order` = VALUES(`sort_order`),
  `is_active` = VALUES(`is_active`),
  `is_visible` = VALUES(`is_visible`),
  `description` = VALUES(`description`),
  `updated_at` = VALUES(`updated_at`);

-- 回填本次初始化菜单的树结构
UPDATE `sys_menus`
SET
  `menu_level` = 1,
  `tree_path` = CONCAT('/', `menu_id`)
WHERE `menu_code` IN ('dashboard', 'desktop', 'meeting_manage', 'platform_admin', 'system_management');

UPDATE `sys_menus` c
JOIN `sys_menus` p ON c.`parent_id` = p.`menu_id`
SET
  c.`menu_level` = p.`menu_level` + 1,
  c.`tree_path` = CONCAT(p.`tree_path`, '/', c.`menu_id`)
WHERE c.`menu_code` IN (
  'meeting_center',
  'prompt_config',
  'hot_word_management',
  'model_management',
  'prompt_management',
  'client_management',
  'external_app_management',
  'terminal_management',
  'user_management',
  'permission_management',
  'dict_management',
  'parameter_management'
);

UPDATE `sys_menus` c
JOIN `sys_menus` p ON c.`parent_id` = p.`menu_id`
SET
  c.`menu_level` = p.`menu_level` + 1,
  c.`tree_path` = CONCAT(p.`tree_path`, '/', c.`menu_id`)
WHERE c.`menu_code` IN ('permission_menu_tree');

-- =====================================================================
-- 9. 角色授权
-- 管理员: 所有启用菜单
-- 普通用户: dashboard / desktop / meeting_manage / meeting_center / prompt_config
-- =====================================================================
INSERT INTO `sys_role_menu_permissions`
(`role_id`, `menu_id`, `granted_by`, `granted_at`)
SELECT
  1,
  m.`menu_id`,
  1,
  NOW()
FROM `sys_menus` m
WHERE m.`is_active` = 1
ON DUPLICATE KEY UPDATE
  `granted_by` = VALUES(`granted_by`),
  `granted_at` = VALUES(`granted_at`);

INSERT INTO `sys_role_menu_permissions`
(`role_id`, `menu_id`, `granted_by`, `granted_at`)
SELECT
  2,
  m.`menu_id`,
  1,
  NOW()
FROM `sys_menus` m
WHERE m.`is_active` = 1
  AND m.`menu_code` IN ('dashboard', 'desktop', 'meeting_manage', 'meeting_center', 'prompt_config')
ON DUPLICATE KEY UPDATE
  `granted_by` = VALUES(`granted_by`),
  `granted_at` = VALUES(`granted_at`);

-- =====================================================================
-- 10. 自增游标兜底
-- =====================================================================
COMMIT;

ALTER TABLE `sys_roles` AUTO_INCREMENT = 3;
ALTER TABLE `sys_users` AUTO_INCREMENT = 3;
ALTER TABLE `prompts` AUTO_INCREMENT = 3;
