-- ============================================================
-- saas-admin CID/外呼运营模块 — 统一归入「任务管理」
-- 主库模板: ylrz_saas.tenant_sys_menu
-- 租户库直接执行请用: add_task_management_menus_tenant.sql
-- 日期: 2026-06-09
--
-- 说明:
--   1. 新建顶级目录「任务管理」(32909)，所有迁移模块挂在其下
--   2. 不再分散在通信管理(35102)、微信管理(32380)、数据统计(32368) 等
--   3. visible='0' 显示，'1' 隐藏；status='0' 正常
--   4. 请将 {TENANT_DB}、{ADMIN_ROLE_ID} 替换为实际值
-- ============================================================

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

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32909, '任务管理', 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 tenant_sys_menu WHERE menu_id = 32909);

-- 若已存在则确保为可见顶级目录
UPDATE tenant_sys_menu
SET menu_name = '任务管理',
    parent_id = 0,
    path = 'taskManage',
    menu_type = 'M',
    visible = '0',
    status = '0',
    icon = 'el-icon-s-operation'
WHERE menu_id = 32909;

-- ---------- 1. 新增页面菜单（父级 = 32909 任务管理） ----------

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

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(329101, '外呼任务查询', 32910, 1, '', '', 'F', '0', '0', 'company:companyVoiceRobotic:query', '#', 1, 0, 'admin', NOW()),
(329102, '外呼任务新增', 32910, 2, '', '', 'F', '0', '0', 'system:companyVoiceRobotic:add', '#', 1, 0, 'admin', NOW()),
(329103, '外呼任务修改', 32910, 3, '', '', 'F', '0', '0', 'company:companyVoiceRobotic:edit', '#', 1, 0, 'admin', NOW()),
(329104, '外呼任务删除', 32910, 4, '', '', 'F', '0', '0', 'system:companyVoiceRobotic:remove', '#', 1, 0, 'admin', NOW()),
(329105, '外呼任务导出', 32910, 5, '', '', 'F', '0', '0', 'company:companyVoiceRobotic:export', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 1.2 转人工通话列表（隐藏路由，侧边栏不展示）
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32920, '转人工通话', 32909, 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 tenant_sys_menu WHERE menu_id = 32920);

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(329201, '转人工查询', 32920, 1, '', '', 'F', '0', '0', 'company:handleManualAnswered:query', '#', 1, 0, 'admin', NOW()),
(329202, '转人工处理', 32920, 2, '', '', 'F', '0', '0', 'company:handleManualAnswered:handle', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 1.3 外呼黑名单
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32930, '外呼黑名单', 32909, 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 tenant_sys_menu WHERE menu_id = 32930);

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(329301, '黑名单查询', 32930, 1, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:query', '#', 1, 0, 'admin', NOW()),
(329302, '黑名单新增', 32930, 2, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:add', '#', 1, 0, 'admin', NOW()),
(329303, '黑名单修改', 32930, 3, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:edit', '#', 1, 0, 'admin', NOW()),
(329304, '黑名单删除', 32930, 4, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:remove', '#', 1, 0, 'admin', NOW()),
(329305, '黑名单导出', 32930, 5, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:export', '#', 1, 0, 'admin', NOW()),
(329306, '查看手机号', 32930, 6, '', '', 'F', '0', '0', 'company:voiceRoboticCallBlacklist:queryPhone', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 1.4 加微话术（AI外呼话术 /company/companyVoiceDialog）
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32940, '加微话术', 32909, 5, 'companyVoiceDialog', 'company/companyVoiceDialog/index',
    'C', '0', '0', 'system:companyVoiceDialog:list', 'el-icon-chat-line-square', 1, 0,
    'admin', NOW(), 'AI外呼加微话术'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM tenant_sys_menu WHERE menu_id = 32940);

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(329401, '话术查询', 32940, 1, '', '', 'F', '0', '0', 'system:companyVoiceDialog:query', '#', 1, 0, 'admin', NOW()),
(329402, '话术新增', 32940, 2, '', '', 'F', '0', '0', 'system:companyVoiceDialog:add', '#', 1, 0, 'admin', NOW()),
(329403, '话术修改', 32940, 3, '', '', 'F', '0', '0', 'system:companyVoiceDialog:edit', '#', 1, 0, 'admin', NOW()),
(329404, '话术删除', 32940, 4, '', '', 'F', '0', '0', 'system:companyVoiceDialog:remove', '#', 1, 0, 'admin', NOW()),
(329405, '话术导出', 32940, 5, '', '', 'F', '0', '0', 'system:companyVoiceDialog:export', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 1.5 声音克隆
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32950, '声音克隆', 32909, 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 tenant_sys_menu WHERE menu_id = 32950);

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(329501, '克隆查询', 32950, 1, '', '', 'F', '0', '0', 'company:voiceCloneRef:list', '#', 1, 0, 'admin', NOW()),
(329502, '克隆上传', 32950, 2, '', '', 'F', '0', '0', 'company:voiceCloneRef:add', '#', 1, 0, 'admin', NOW()),
(329503, '克隆修改', 32950, 3, '', '', 'F', '0', '0', 'company:voiceCloneRef:update', '#', 1, 0, 'admin', NOW()),
(329504, '克隆删除', 32950, 4, '', '', 'F', '0', '0', 'company:voiceCloneRef:remove', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 1.6 加微统计
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32960, '加微统计', 32909, 12, '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 tenant_sys_menu WHERE menu_id = 32960);

INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(329601, '加微统计导出', 32960, 1, '', '', 'F', '0', '0', 'company:addwxlog:export', '#', 1, 0, 'admin', NOW()),
(329602, '加微统计删除', 32960, 2, '', '', 'F', '0', '0', 'company:addwxlog:remove', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 1.7 外呼看板
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time, remark
)
SELECT
    32961, '外呼看板', 32909, 13, 'aiOutboundCallDashboard', 'taskStatistics/aiOutboundCallDashboard/index',
    'C', '0', '0', 'company:callphonelog:list', 'el-icon-data-analysis', 1, 0,
    'admin', NOW(), 'AI外呼数据看板'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM tenant_sys_menu WHERE menu_id = 32961);

-- ---------- 2. 已有菜单迁入「任务管理」(32909) ----------

UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 3  WHERE menu_id = 32471; -- 任务模板管理
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 7  WHERE menu_id = 32427; -- 呼入大模型
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 8  WHERE menu_id = 32482; -- 个微账号
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 9  WHERE menu_id = 32484; -- 个微用户
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 10 WHERE menu_id = 32485; -- 微信用户分组
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 11 WHERE menu_id = 32472; -- 企微账号
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 14 WHERE menu_id = 32834; -- 外呼统计
UPDATE tenant_sys_menu SET parent_id = 32909, order_num = 15 WHERE menu_id = 32835; -- 短信统计

-- 新增菜单若已存在但父级不对，统一纠正
UPDATE tenant_sys_menu SET parent_id = 32909 WHERE menu_id IN (
    32910, 32920, 32930, 32940, 32950, 32960, 32961
);

-- 修正 path（避免 component 下 company/* 路径冲突）
UPDATE tenant_sys_menu SET path = 'inboundCallManage'   WHERE menu_id = 32427;
UPDATE tenant_sys_menu SET path = 'companyWorkflowManage' WHERE menu_id = 32471;
UPDATE tenant_sys_menu SET path = 'companyWx'           WHERE menu_id = 32472;
UPDATE tenant_sys_menu SET path = 'wxAccount'             WHERE menu_id = 32482;
UPDATE tenant_sys_menu SET path = 'wxUser'                WHERE menu_id = 32484;
UPDATE tenant_sys_menu SET path = 'wxUserGroup'           WHERE menu_id = 32485;
UPDATE tenant_sys_menu SET path = 'callLog'               WHERE menu_id = 32834;
UPDATE tenant_sys_menu SET path = 'sendMsgLog'             WHERE menu_id = 32835;

-- ---------- 3. 已有菜单补按钮权限 ----------

-- 3.1 呼入大模型 32427
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(324271, '呼入配置查询', 32427, 1, '', '', 'F', '0', '0', 'inboundCallManage:list', '#', 1, 0, 'admin', NOW()),
(324272, '呼入配置新增', 32427, 2, '', '', 'F', '0', '0', 'inboundCallManage:add', '#', 1, 0, 'admin', NOW()),
(324273, '呼入配置修改', 32427, 3, '', '', 'F', '0', '0', 'inboundCallManage:edit', '#', 1, 0, 'admin', NOW()),
(324274, '呼入配置删除', 32427, 4, '', '', 'F', '0', '0', 'inboundCallManage:delete', '#', 1, 0, 'admin', NOW()),
(324275, '呼入记录查询', 32427, 5, '', '', 'F', '0', '0', 'inboundCallManage:inboundCallRecord:list', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

UPDATE tenant_sys_menu SET perms = 'inboundCallManage:list' WHERE menu_id = 32427 AND (perms IS NULL OR perms = '');

-- 3.2 任务模板 32471
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(324711, '模板查询', 32471, 1, '', '', 'F', '0', '0', 'company:companyWorkflowManage:list', '#', 1, 0, 'admin', NOW()),
(324712, '模板新增', 32471, 2, '', '', 'F', '0', '0', 'company:companyWorkflowManage:add', '#', 1, 0, 'admin', NOW()),
(324713, '模板修改', 32471, 3, '', '', 'F', '0', '0', 'company:companyWorkflowManage:edit', '#', 1, 0, 'admin', NOW()),
(324714, '模板删除', 32471, 4, '', '', 'F', '0', '0', 'company:companyWorkflowManage:remove', '#', 1, 0, 'admin', NOW()),
(324715, '模板导出', 32471, 5, '', '', 'F', '0', '0', 'company:companyWorkflowManage:export', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

-- 3.3 个微账号 32482
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(324821, '个微账号查询', 32482, 1, '', '', 'F', '0', '0', 'company:companyWx:query', '#', 1, 0, 'admin', NOW()),
(324822, '个微账号新增', 32482, 2, '', '', 'F', '0', '0', 'company:companyWx:add', '#', 1, 0, 'admin', NOW()),
(324823, '个微账号修改', 32482, 3, '', '', 'F', '0', '0', 'company:companyWx:edit', '#', 1, 0, 'admin', NOW()),
(324824, '个微账号删除', 32482, 4, '', '', 'F', '0', '0', 'company:companyWx:remove', '#', 1, 0, 'admin', NOW()),
(324825, '个微账号导出', 32482, 5, '', '', 'F', '0', '0', 'company:companyWx:export', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

UPDATE tenant_sys_menu SET perms = 'company:companyWx:list' WHERE menu_id = 32482 AND (perms IS NULL OR perms = '');

-- 3.4 个微用户 32484
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(324841, '个微用户查询', 32484, 1, '', '', 'F', '0', '0', 'company:wxUser:query', '#', 1, 0, 'admin', NOW()),
(324842, '个微用户导出', 32484, 2, '', '', 'F', '0', '0', 'company:wxUser:export', '#', 1, 0, 'admin', NOW()),
(324843, '个微用户新增', 32484, 3, '', '', 'F', '0', '0', 'company:wxUser:add', '#', 1, 0, 'admin', NOW()),
(324844, '个微用户修改', 32484, 4, '', '', 'F', '0', '0', 'company:wxUser:edit', '#', 1, 0, 'admin', NOW()),
(324845, '个微用户删除', 32484, 5, '', '', 'F', '0', '0', 'company:wxUser:remove', '#', 1, 0, 'admin', NOW()),
(324846, '批量添加分组', 32484, 6, '', '', 'F', '0', '0', 'company:wxUser:addGroup', '#', 1, 0, 'admin', NOW()),
(324847, '批量移除分组', 32484, 7, '', '', 'F', '0', '0', 'company:wxUser:delGroup', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

UPDATE tenant_sys_menu SET perms = 'company:wxUser:list' WHERE menu_id = 32484 AND (perms IS NULL OR perms = '');

-- 3.5 外呼统计 / 短信统计
INSERT INTO tenant_sys_menu (
    menu_id, menu_name, parent_id, order_num, path, component,
    menu_type, visible, status, perms, icon, is_frame, is_cache,
    create_by, create_time
) VALUES
(328341, '外呼统计查询', 32834, 1, '', '', 'F', '0', '0', 'company:callphonelog:list', '#', 1, 0, 'admin', NOW()),
(328342, '外呼统计导出', 32834, 2, '', '', 'F', '0', '0', 'company:callphonelog:export', '#', 1, 0, 'admin', NOW()),
(328351, '短信统计查询', 32835, 1, '', '', 'F', '0', '0', 'company:sendmsglog:list', '#', 1, 0, 'admin', NOW()),
(328352, '短信统计导出', 32835, 2, '', '', 'F', '0', '0', 'company:sendmsglog:export', '#', 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE perms = VALUES(perms);

UPDATE tenant_sys_menu SET perms = 'company:callphonelog:list' WHERE menu_id = 32834 AND (perms IS NULL OR perms = '');
UPDATE tenant_sys_menu SET perms = 'company:sendmsglog:list' WHERE menu_id = 32835 AND (perms IS NULL OR perms = '');

-- ---------- 4. 隐藏迁走后可能为空的微信子目录 ----------

UPDATE tenant_sys_menu SET visible = '1'
WHERE menu_id IN (35010, 35011, 35012, 35013)
  AND NOT EXISTS (
      SELECT 1 FROM tenant_sys_menu c
      WHERE c.parent_id = tenant_sys_menu.menu_id
        AND c.menu_type <> 'F'
        AND c.visible = '0'
  );

-- ---------- 5. 授权租户管理员（在租户库执行，需先同步菜单） ----------

-- INSERT IGNORE INTO `{TENANT_DB}`.sys_role_menu (role_id, menu_id)
-- SELECT {ADMIN_ROLE_ID}, menu_id FROM tenant_sys_menu
-- WHERE menu_id IN (
--   32909,
--   32910,329101,329102,329103,329104,329105,
--   32920,329201,329202,
--   32930,329301,329302,329303,329304,329305,329306,
--   32940,329401,329402,329403,329404,329405,
--   32950,329501,329502,329503,329504,
--   32427,324271,324272,324273,324274,324275,
--   32471,324711,324712,324713,324714,324715,
--   32472,
--   32482,324821,324822,324823,324824,324825,
--   32484,324841,324842,324843,324844,324845,324846,324847,
--   32485,
--   32834,328341,328342,
--   32835,328351,328352,
--   32960,329601,329602,
--   32961
-- );
