-- ============================================================
-- saas-admin「任务管理」菜单 — 租户库纯新增（自增 ID）
--
-- 【重要】执行前请在 Navicat 左侧选中目标租户库（如 fs_tenant_cs1），
--         再运行本脚本。不要依赖 USE 切换库，避免数据写到别的库。
-- 目标表: 当前库下的 sys_menu、sys_role_menu
-- 说明: 不指定 menu_id，由数据库自增；可重复执行（按 path/perms 判重）
-- ============================================================

-- 勿取消下行注释；若必须写死库名，请改成你的租户库，例如 fs_tenant_cs1
-- USE `fs_tenant_cs1`;

-- ---------- 0. 顶级目录：任务管理 ----------

INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    '任务管理', 0, 3, 'taskManage', NULL,
    'M', '0', '0', NULL, 'el-icon-s-operation', 1, 0,
    'admin', NOW(), 'CID/外呼运营模块'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = '任务管理' AND parent_id = 0 AND path = 'taskManage'
);

SET @task_manage_id = (
    SELECT menu_id FROM sys_menu
    WHERE menu_name = '任务管理' AND parent_id = 0 AND path = 'taskManage'
    LIMIT 1
);

-- ---------- 1. 页面菜单 ----------

-- 1.1 外呼任务管理
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '外呼任务管理', @task_manage_id, 1, 'companyVoiceRobotic', 'company/companyVoiceRobotic/index',
    'C', '0', '0', 'company:companyVoiceRobotic:list', 'el-icon-phone', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyVoiceRobotic'
);

SET @m_voice_robotic = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyVoiceRobotic' LIMIT 1
);

-- 1.2 转人工通话
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '转人工通话', @task_manage_id, 2, 'handleManualAnswered', 'company/companyVoiceRobotic/handleManualAnswered',
    'C', '1', '0', 'company:handleManualAnswered:query', 'el-icon-service', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'handleManualAnswered'
);

SET @m_manual = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'handleManualAnswered' LIMIT 1
);

-- 1.3 任务模板管理
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '任务模板管理', @task_manage_id, 3, 'companyWorkflowManage', 'company/companyWorkflowManage/index',
    'C', '0', '0', 'company:companyWorkflowManage:list', 'el-icon-document', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyWorkflowManage'
);

SET @m_workflow = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyWorkflowManage' LIMIT 1
);

-- 1.4 外呼黑名单
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '外呼黑名单', @task_manage_id, 4, 'companyVoiceRoboticCallBlacklist', 'company/companyVoiceRoboticCallBlacklist/companyVoiceRoboticCallBlacklist',
    'C', '0', '0', 'company:voiceRoboticCallBlacklist:list', 'el-icon-warning', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyVoiceRoboticCallBlacklist'
);

SET @m_blacklist = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyVoiceRoboticCallBlacklist' LIMIT 1
);

-- 1.5 加微话术
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '加微话术', @task_manage_id, 5, 'companyVoiceDialog', 'company/companyVoiceDialog/index',
    'C', '0', '0', 'system:companyVoiceDialog:list', 'el-icon-chat-line-square', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyVoiceDialog'
);

SET @m_dialog = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyVoiceDialog' LIMIT 1
);

-- 1.6 声音克隆
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '声音克隆', @task_manage_id, 6, 'voiceClone', 'company/voiceClone/index',
    'C', '0', '0', 'company:voiceCloneRef:list', 'el-icon-microphone', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'voiceClone'
);

SET @m_clone = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'voiceClone' LIMIT 1
);

-- 1.7 呼入大模型
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '呼入大模型', @task_manage_id, 7, 'inboundCallManage', 'company/aiModel/inboundCallManage/index',
    'C', '0', '0', 'inboundCallManage:list', 'el-icon-cpu', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'inboundCallManage'
);

SET @m_inbound = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'inboundCallManage' LIMIT 1
);

-- 1.8 个微账号
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '个微账号', @task_manage_id, 8, 'wxAccount', 'company/wxAccount/index',
    'C', '0', '0', 'company:companyWx:list', 'el-icon-user', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxAccount'
);

SET @m_wx_account = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxAccount' LIMIT 1
);

-- 1.9 个微用户
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '个微用户', @task_manage_id, 9, 'wxUser', 'company/wxUser/index',
    'C', '0', '0', 'company:wxUser:list', 'el-icon-s-custom', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxUser'
);

SET @m_wx_user = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxUser' LIMIT 1
);

-- 1.10 微信用户分组
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '微信用户分组', @task_manage_id, 10, 'wxUserGroup', 'company/wxUserGroup/index',
    'C', '0', '0', 'company:wxUserGroup:list', 'el-icon-folder', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxUserGroup'
);

SET @m_wx_group = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxUserGroup' LIMIT 1
);

-- 1.11 企微账号
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '企微账号', @task_manage_id, 11, 'companyWx', 'company/companyWx/index',
    'C', '0', '0', 'company:companyWx:list', 'el-icon-chat-dot-round', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyWx'
);

SET @m_company_wx = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'companyWx' LIMIT 1
);

-- 1.12 外呼统计
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '外呼统计', @task_manage_id, 12, 'callLog', 'taskStatistics/callLog/index',
    'C', '0', '0', 'company:callphonelog:list', 'el-icon-data-line', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'callLog'
);

SET @m_call_log = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'callLog' LIMIT 1
);

-- 1.13 短信统计
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '短信统计', @task_manage_id, 13, 'sendMsgLog', 'taskStatistics/sendMsgLog/index',
    'C', '0', '0', 'company:sendmsglog:list', 'el-icon-message', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'sendMsgLog'
);

SET @m_send_msg = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'sendMsgLog' LIMIT 1
);

-- 1.14 加微统计
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '加微统计', @task_manage_id, 14, 'wxClient', 'taskStatistics/wxClient/index',
    'C', '0', '0', 'company:addwxlog:list', 'el-icon-user-solid', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxClient'
);

SET @m_wx_client = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'wxClient' LIMIT 1
);

-- 1.15 外呼看板
INSERT INTO sys_menu (
    menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
)
SELECT
    '外呼看板', @task_manage_id, 15, 'aiOutboundCallDashboard', 'taskStatistics/aiOutboundCallDashboard/index',
    'C', '0', '0', 'company:callphonelog:list', 'el-icon-data-analysis', 1, 0,
    'admin', NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'aiOutboundCallDashboard'
);

SET @m_dashboard = (
    SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id AND path = 'aiOutboundCallDashboard' LIMIT 1
);

-- ---------- 2. 按钮权限（按 perms 判重） ----------

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼任务查询', @m_voice_robotic, 1, '', '', 'F', '0', '0', 'company:companyVoiceRobotic:query', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_voice_robotic IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_voice_robotic AND perms = 'company:companyVoiceRobotic:query');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼任务新增', @m_voice_robotic, 2, '', '', 'F', '0', '0', 'system:companyVoiceRobotic:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_voice_robotic IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_voice_robotic AND perms = 'system:companyVoiceRobotic:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼任务修改', @m_voice_robotic, 3, '', '', 'F', '0', '0', 'company:companyVoiceRobotic:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_voice_robotic IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_voice_robotic AND perms = 'company:companyVoiceRobotic:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼任务删除', @m_voice_robotic, 4, '', '', 'F', '0', '0', 'system:companyVoiceRobotic:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_voice_robotic IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_voice_robotic AND perms = 'system:companyVoiceRobotic:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼任务导出', @m_voice_robotic, 5, '', '', 'F', '0', '0', 'company:companyVoiceRobotic:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_voice_robotic IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_voice_robotic AND perms = 'company:companyVoiceRobotic:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '转人工查询', @m_manual, 1, '', '', 'F', '0', '0', 'company:handleManualAnswered:query', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_manual IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_manual AND perms = 'company:handleManualAnswered:query');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '转人工处理', @m_manual, 2, '', '', 'F', '0', '0', 'company:handleManualAnswered:handle', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_manual IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_manual AND perms = 'company:handleManualAnswered:handle');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '模板查询', @m_workflow, 1, '', '', 'F', '0', '0', 'company:companyWorkflowManage:list', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_workflow IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_workflow AND perms = 'company:companyWorkflowManage:list');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '模板新增', @m_workflow, 2, '', '', 'F', '0', '0', 'company:companyWorkflowManage:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_workflow IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_workflow AND perms = 'company:companyWorkflowManage:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '模板修改', @m_workflow, 3, '', '', 'F', '0', '0', 'company:companyWorkflowManage:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_workflow IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_workflow AND perms = 'company:companyWorkflowManage:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '模板删除', @m_workflow, 4, '', '', 'F', '0', '0', 'company:companyWorkflowManage:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_workflow IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_workflow AND perms = 'company:companyWorkflowManage:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '模板导出', @m_workflow, 5, '', '', 'F', '0', '0', 'company:companyWorkflowManage:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_workflow IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_workflow AND perms = 'company:companyWorkflowManage:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '黑名单查询', @m_blacklist, 1, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:query', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_blacklist IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_blacklist AND perms = 'company:voiceRoboticCallBlacklist:query');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '黑名单新增', @m_blacklist, 2, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_blacklist IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_blacklist AND perms = 'company:voiceRoboticCallBlacklist:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '黑名单修改', @m_blacklist, 3, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_blacklist IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_blacklist AND perms = 'company:voiceRoboticCallBlacklist:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '黑名单删除', @m_blacklist, 4, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_blacklist IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_blacklist AND perms = 'company:voiceRoboticCallBlacklist:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '黑名单导出', @m_blacklist, 5, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_blacklist IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_blacklist AND perms = 'company:voiceRoboticCallBlacklist:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '查看手机号', @m_blacklist, 6, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:queryPhone', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_blacklist IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_blacklist AND perms = 'company:voiceRoboticCallBlacklist:queryPhone');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '话术查询', @m_dialog, 1, '', '', 'F', '0', '0', 'system:companyVoiceDialog:query', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_dialog IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_dialog AND perms = 'system:companyVoiceDialog:query');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '话术新增', @m_dialog, 2, '', '', 'F', '0', '0', 'system:companyVoiceDialog:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_dialog IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_dialog AND perms = 'system:companyVoiceDialog:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '话术修改', @m_dialog, 3, '', '', 'F', '0', '0', 'system:companyVoiceDialog:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_dialog IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_dialog AND perms = 'system:companyVoiceDialog:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '话术删除', @m_dialog, 4, '', '', 'F', '0', '0', 'system:companyVoiceDialog:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_dialog IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_dialog AND perms = 'system:companyVoiceDialog:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '话术导出', @m_dialog, 5, '', '', 'F', '0', '0', 'system:companyVoiceDialog:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_dialog IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_dialog AND perms = 'system:companyVoiceDialog:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '克隆查询', @m_clone, 1, '', '', 'F', '0', '0', 'company:voiceCloneRef:list', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_clone IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_clone AND perms = 'company:voiceCloneRef:list');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '克隆上传', @m_clone, 2, '', '', 'F', '0', '0', 'company:voiceCloneRef:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_clone IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_clone AND perms = 'company:voiceCloneRef:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '克隆修改', @m_clone, 3, '', '', 'F', '0', '0', 'company:voiceCloneRef:update', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_clone IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_clone AND perms = 'company:voiceCloneRef:update');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '克隆删除', @m_clone, 4, '', '', 'F', '0', '0', 'company:voiceCloneRef:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_clone IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_clone AND perms = 'company:voiceCloneRef:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '呼入配置查询', @m_inbound, 1, '', '', 'F', '0', '0', 'inboundCallManage:list', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_inbound IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_inbound AND perms = 'inboundCallManage:list');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '呼入配置新增', @m_inbound, 2, '', '', 'F', '0', '0', 'inboundCallManage:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_inbound IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_inbound AND perms = 'inboundCallManage:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '呼入配置修改', @m_inbound, 3, '', '', 'F', '0', '0', 'inboundCallManage:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_inbound IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_inbound AND perms = 'inboundCallManage:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '呼入配置删除', @m_inbound, 4, '', '', 'F', '0', '0', 'inboundCallManage:delete', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_inbound IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_inbound AND perms = 'inboundCallManage:delete');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '呼入记录查询', @m_inbound, 5, '', '', 'F', '0', '0', 'inboundCallManage:inboundCallRecord:list', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_inbound IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_inbound AND perms = 'inboundCallManage:inboundCallRecord:list');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微账号查询', @m_wx_account, 1, '', '', 'F', '0', '0', 'company:companyWx:query', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_account IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_account AND perms = 'company:companyWx:query');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微账号新增', @m_wx_account, 2, '', '', 'F', '0', '0', 'company:companyWx:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_account IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_account AND perms = 'company:companyWx:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微账号修改', @m_wx_account, 3, '', '', 'F', '0', '0', 'company:companyWx:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_account IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_account AND perms = 'company:companyWx:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微账号删除', @m_wx_account, 4, '', '', 'F', '0', '0', 'company:companyWx:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_account IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_account AND perms = 'company:companyWx:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微账号导出', @m_wx_account, 5, '', '', 'F', '0', '0', 'company:companyWx:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_account IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_account AND perms = 'company:companyWx:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微用户查询', @m_wx_user, 1, '', '', 'F', '0', '0', 'company:wxUser:query', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:query');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微用户导出', @m_wx_user, 2, '', '', 'F', '0', '0', 'company:wxUser:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微用户新增', @m_wx_user, 3, '', '', 'F', '0', '0', 'company:wxUser:add', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:add');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微用户修改', @m_wx_user, 4, '', '', 'F', '0', '0', 'company:wxUser:edit', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:edit');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '个微用户删除', @m_wx_user, 5, '', '', 'F', '0', '0', 'company:wxUser:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:remove');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '批量添加分组', @m_wx_user, 6, '', '', 'F', '0', '0', 'company:wxUser:addGroup', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:addGroup');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '批量移除分组', @m_wx_user, 7, '', '', 'F', '0', '0', 'company:wxUser:delGroup', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_user IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_user AND perms = 'company:wxUser:delGroup');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼统计查询', @m_call_log, 1, '', '', 'F', '0', '0', 'company:callphonelog:list', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_call_log IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_call_log AND perms = 'company:callphonelog:list');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '外呼统计导出', @m_call_log, 2, '', '', 'F', '0', '0', 'company:callphonelog:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_call_log IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_call_log AND perms = 'company:callphonelog:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '短信统计查询', @m_send_msg, 1, '', '', 'F', '0', '0', 'company:sendmsglog:list', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_send_msg IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_send_msg AND perms = 'company:sendmsglog:list');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '短信统计导出', @m_send_msg, 2, '', '', 'F', '0', '0', 'company:sendmsglog:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_send_msg IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_send_msg AND perms = 'company:sendmsglog:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '加微统计导出', @m_wx_client, 1, '', '', 'F', '0', '0', 'company:addwxlog:export', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_client IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_client AND perms = 'company:addwxlog:export');

INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, menu_type, visible, status, perms, icon, is_frame, is_cache, create_by, create_time)
SELECT '加微统计删除', @m_wx_client, 2, '', '', 'F', '0', '0', 'company:addwxlog:remove', '#', 1, 0, 'admin', NOW()
FROM DUAL WHERE @m_wx_client IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE parent_id = @m_wx_client AND perms = 'company:addwxlog:remove');

-- ---------- 3. 授权租户管理员（role_id=1，整棵任务管理树） ----------

INSERT IGNORE INTO sys_role_menu (role_id, menu_id)
SELECT 1, m.menu_id
FROM sys_menu m
WHERE m.menu_id = @task_manage_id
   OR m.parent_id = @task_manage_id
   OR m.parent_id IN (
       SELECT menu_id FROM (
           SELECT menu_id FROM sys_menu WHERE parent_id = @task_manage_id
       ) AS task_pages
   );

-- ---------- 4. 执行后校验（应能看到「任务管理」及子菜单） ----------

SELECT DATABASE() AS current_database;

SELECT menu_id, menu_name, parent_id, path, component, menu_type, visible
FROM sys_menu
WHERE menu_name = '任务管理' AND parent_id = 0 AND path = 'taskManage';

SELECT m.menu_id, m.menu_name, m.parent_id, m.path, m.component, m.menu_type
FROM sys_menu p
JOIN sys_menu m ON m.parent_id = p.menu_id
WHERE p.menu_name = '任务管理' AND p.parent_id = 0 AND p.path = 'taskManage'
ORDER BY m.order_num;

SELECT COUNT(*) AS role_menu_grant_count
FROM sys_role_menu rm
JOIN sys_menu p ON p.menu_id = rm.menu_id
WHERE p.menu_name = '任务管理' AND p.parent_id = 0 AND p.path = 'taskManage'
   OR rm.menu_id IN (
       SELECT menu_id FROM sys_menu WHERE parent_id IN (
           SELECT menu_id FROM sys_menu WHERE menu_name = '任务管理' AND parent_id = 0 AND path = 'taskManage'
       )
   );
