SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- 修复服务管理菜单（最终版）
-- 1. 删除消息管理菜单
-- 2. 确保只有一个服务管理父菜单
-- ----------------------------
BEGIN;

-- 删除消息管理菜单（2069）及其权限关联
DELETE FROM system_role_menu WHERE menu_id = 2069;
DELETE FROM system_menu WHERE id = 2069;

-- 删除可能重复的服务管理菜单（保留 2065）
DELETE FROM system_role_menu WHERE menu_id IN (SELECT id FROM (SELECT id FROM system_menu WHERE name = '服务管理' AND id != 2065) AS t);
DELETE FROM system_menu WHERE name = '服务管理' AND id != 2065;

-- 确保服务管理菜单（2065）正确配置
UPDATE system_menu SET 
    `name` = '服务管理',
    `permission` = '',
    `type` = 1,
    `sort` = 33,
    `parent_id` = 0,
    `path` = '/service',
    `icon` = 'ep:service',
    `component` = NULL,
    `component_name` = NULL,
    `status` = 0,
    `visible` = b'1',
    `keep_alive` = b'1',
    `always_show` = b'1',
    `updater` = 'admin',
    `update_time` = NOW(),
    `deleted` = b'0'
WHERE id = 2065;

-- 确保子菜单正确配置
UPDATE system_menu SET 
    `name` = 'APP意见反馈',
    `permission` = 'service:feedback:query',
    `parent_id` = 2065,
    `path` = 'feedback',
    `component` = 'service/feedback/index',
    `component_name` = 'ServiceFeedback',
    `updater` = 'admin',
    `update_time` = NOW()
WHERE id = 2066;

UPDATE system_menu SET 
    `name` = '服务评价管理',
    `permission` = 'service:evaluation:query',
    `parent_id` = 2065,
    `path` = 'evaluation',
    `component` = 'service/evaluation/index',
    `component_name` = 'ServiceEvaluation',
    `updater` = 'admin',
    `update_time` = NOW()
WHERE id = 2067;

UPDATE system_menu SET 
    `name` = '维保管理',
    `permission` = 'service:maintenance:query',
    `parent_id` = 2065,
    `path` = 'maintenance',
    `component` = 'service/maintenance/index',
    `component_name` = 'ServiceMaintenance',
    `updater` = 'admin',
    `update_time` = NOW()
WHERE id = 2068;

-- 确保权限分配正确（只包含 2065, 2066, 2067, 2068）
DELETE FROM system_role_menu WHERE role_id = 1 AND menu_id = 2069;
INSERT INTO system_role_menu (role_id, menu_id) 
SELECT 1, id FROM system_menu WHERE id IN (2065, 2066, 2067, 2068)
AND id NOT IN (SELECT menu_id FROM system_role_menu WHERE role_id = 1);

COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

