USE cattletrade;

-- 插入销售概览菜单
-- 菜单类型：0-目录，1-菜单，2-按钮
-- 排序：数字越小越靠前

-- 1. 插入销售概览主菜单（type=1，菜单）
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
VALUES (
    0,  -- parent_id: 0表示顶级菜单
    1,  -- type: 1-菜单
    'el-icon-data-line',  -- icon: 数据统计图标
    '销售概览',  -- name: 菜单名称
    50,  -- sort: 排序（可以根据实际情况调整）
    NULL,  -- route_url: 后端路由地址（菜单不需要）
    '/salesOverview/list',  -- page_url: 前端路由地址
    2,  -- org_type: 2-企业端（根据实际情况调整）
    'salesoverview:list',  -- authority: 权限字符串
    NOW(),  -- create_time
    NOW(),  -- update_time
    0   -- is_delete: 0-未删除
);

-- 2. 插入销售概览相关按钮权限（type=2，按钮）
-- 使用子查询获取刚插入的菜单ID作为parent_id

-- 2.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 
    (SELECT id FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0 LIMIT 1) AS parent_id,
    2 AS type,
    NULL AS icon,
    '新增' AS name,
    2 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    2 AS org_type,
    'salesoverview:add' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0);

-- 2.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 
    (SELECT id FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0 LIMIT 1) AS parent_id,
    2 AS type,
    NULL AS icon,
    '编辑' AS name,
    3 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    2 AS org_type,
    'salesoverview:edit' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0);

-- 2.3 删除权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    (SELECT id FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0 LIMIT 1) AS parent_id,
    2 AS type,
    NULL AS icon,
    '删除' AS name,
    4 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    2 AS org_type,
    'salesoverview:delete' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0);

-- 2.4 计算统计数据权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT 
    (SELECT id FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0 LIMIT 1) AS parent_id,
    2 AS type,
    NULL AS icon,
    '计算统计数据' AS name,
    5 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    2 AS org_type,
    'salesoverview:calculate' AS authority,
    NOW() AS create_time,
    NOW() AS update_time,
    0 AS is_delete
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE name = '销售概览' AND type = 1 AND is_delete = 0);

