USE cattletrade;

-- 一键修复：确保销售概览菜单在侧边栏显示
-- 该脚本会：
-- 1. 检查菜单是否存在，不存在则插入
-- 2. 为超级管理员角色（role_id=1）分配菜单权限
-- 3. 将菜单的org_type设置为3（海关企业共用），确保所有用户都能看到

-- ========== 步骤1：确保菜单已插入 ==========
-- 如果菜单不存在，则插入
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    0 AS parent_id,
    1 AS type,
    'el-icon-data-line' AS icon,
    '销售概览' AS name,
    50 AS sort,
    NULL AS route_url,
    '/salesOverview/list' AS page_url,
    3 AS org_type,  -- 设置为3（海关企业共用），确保所有用户都能看到
    'salesoverview:list' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0
);

-- 获取销售概览菜单ID
SET @salesoverview_menu_id = (SELECT id FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0 LIMIT 1);

-- 更新菜单的org_type为3（如果已存在）
UPDATE sys_menu 
SET org_type = 3, update_time = NOW()
WHERE id = @salesoverview_menu_id AND org_type != 3;

-- ========== 步骤2：确保按钮权限已插入 ==========
-- 插入新增按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    @salesoverview_menu_id AS parent_id,
    2 AS type,
    NULL AS icon,
    '新增' AS name,
    2 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    3 AS org_type,
    'salesoverview:add' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu 
    WHERE parent_id = @salesoverview_menu_id AND name = '新增' AND type = 2 AND is_delete = 0
)
AND @salesoverview_menu_id IS NOT NULL;

-- 插入编辑按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    @salesoverview_menu_id AS parent_id,
    2 AS type,
    NULL AS icon,
    '编辑' AS name,
    3 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    3 AS org_type,
    'salesoverview:edit' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu 
    WHERE parent_id = @salesoverview_menu_id AND name = '编辑' AND type = 2 AND is_delete = 0
)
AND @salesoverview_menu_id IS NOT NULL;

-- 插入删除按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    @salesoverview_menu_id AS parent_id,
    2 AS type,
    NULL AS icon,
    '删除' AS name,
    4 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    3 AS org_type,
    'salesoverview:delete' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu 
    WHERE parent_id = @salesoverview_menu_id AND name = '删除' AND type = 2 AND is_delete = 0
)
AND @salesoverview_menu_id IS NOT NULL;

-- 插入计算统计数据按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    @salesoverview_menu_id AS parent_id,
    2 AS type,
    NULL AS icon,
    '计算统计数据' AS name,
    5 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    3 AS org_type,
    'salesoverview:calculate' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu 
    WHERE parent_id = @salesoverview_menu_id AND name = '计算统计数据' AND type = 2 AND is_delete = 0
)
AND @salesoverview_menu_id IS NOT NULL;

-- ========== 步骤3：为超级管理员角色分配权限 ==========
-- 查询所有销售概览相关的菜单ID
SET @menu_main = @salesoverview_menu_id;
SET @menu_add = (SELECT id FROM sys_menu WHERE parent_id = @salesoverview_menu_id AND name = '新增' AND type = 2 AND is_delete = 0 LIMIT 1);
SET @menu_edit = (SELECT id FROM sys_menu WHERE parent_id = @salesoverview_menu_id AND name = '编辑' AND type = 2 AND is_delete = 0 LIMIT 1);
SET @menu_delete = (SELECT id FROM sys_menu WHERE parent_id = @salesoverview_menu_id AND name = '删除' AND type = 2 AND is_delete = 0 LIMIT 1);
SET @menu_calculate = (SELECT id FROM sys_menu WHERE parent_id = @salesoverview_menu_id AND name = '计算统计数据' AND type = 2 AND is_delete = 0 LIMIT 1);

-- 为角色ID=1（超级管理员）分配主菜单权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, @menu_main
WHERE NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = @menu_main)
AND @menu_main IS NOT NULL;

-- 为角色ID=1分配新增权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, @menu_add
WHERE NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = @menu_add)
AND @menu_add IS NOT NULL;

-- 为角色ID=1分配编辑权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, @menu_edit
WHERE NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = @menu_edit)
AND @menu_edit IS NOT NULL;

-- 为角色ID=1分配删除权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, @menu_delete
WHERE NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = @menu_delete)
AND @menu_delete IS NOT NULL;

-- 为角色ID=1分配计算权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, @menu_calculate
WHERE NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = @menu_calculate)
AND @menu_calculate IS NOT NULL;

-- ========== 步骤4：验证结果 ==========
-- 显示菜单信息
SELECT 
    '菜单信息' AS info_type,
    id,
    name,
    type,
    parent_id,
    page_url,
    org_type,
    authority
FROM sys_menu 
WHERE name = '销售概览' OR parent_id = @salesoverview_menu_id
ORDER BY type, sort;

-- 显示权限分配情况
SELECT 
    '权限分配' AS info_type,
    rm.role_id,
    r.name AS role_name,
    rm.menu_id,
    m.name AS menu_name,
    m.type AS menu_type
FROM sys_role_menu rm
INNER JOIN sys_role r ON rm.role_id = r.id
INNER JOIN sys_menu m ON rm.menu_id = m.id
WHERE m.name = '销售概览' OR m.parent_id = @salesoverview_menu_id
ORDER BY rm.role_id, m.type, m.sort;

-- 提示信息
SELECT 
    '提示' AS info_type,
    CONCAT('销售概览菜单ID: ', @salesoverview_menu_id) AS message
UNION ALL
SELECT 
    '提示' AS info_type,
    '请重新登录系统以刷新菜单' AS message;

