USE cattletrade;

-- 安全插入中转仓管理菜单（可重复执行，不会重复插入）
-- 菜单类型：0-目录，1-菜单，2-按钮
-- 排序：数字越小越靠前

-- ============================================
-- 1. 插入一级菜单：中转仓管理（type=0，目录）
-- ============================================
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,
    0 AS type,
    'el-icon-office-building' AS icon,
    '中转仓管理' AS name,
    60 AS sort,
    NULL AS route_url,
    NULL AS page_url,
    2 AS org_type,
    NULL 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 = 0 AND is_delete = 0
);

-- 获取一级菜单ID（如果已存在则查询，否则使用刚插入的ID）
SET @warehouse_parent_id = COALESCE(
    (SELECT id FROM sys_menu WHERE name = '中转仓管理' AND type = 0 AND is_delete = 0 LIMIT 1),
    LAST_INSERT_ID()
);

-- ============================================
-- 2. 插入二级菜单：中转仓管理（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)
SELECT 
    @warehouse_parent_id AS parent_id,
    1 AS type,
    'el-icon-office-building' AS icon,
    '中转仓管理' AS name,
    1 AS sort,
    'list' AS route_url,
    'warehouse/warehouse' AS page_url,
    2 AS org_type,
    'warehouse:list' 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 = @warehouse_parent_id 
      AND name = '中转仓管理' 
      AND type = 1 
      AND is_delete = 0
)
AND @warehouse_parent_id IS NOT NULL;

SET @warehouse_list_menu_id = COALESCE(
    (SELECT id FROM sys_menu WHERE parent_id = @warehouse_parent_id AND name = '中转仓管理' AND type = 1 AND is_delete = 0 LIMIT 1),
    LAST_INSERT_ID()
);

-- 中转仓管理按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT @warehouse_list_menu_id, 2, NULL, '新增', 1, NULL, NULL, 2, 'warehouse:add', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_list_menu_id AND name = '新增' AND type = 2 AND authority = 'warehouse:add' AND is_delete = 0
)
AND @warehouse_list_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 @warehouse_list_menu_id, 2, NULL, '编辑', 2, NULL, NULL, 2, 'warehouse:edit', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_list_menu_id AND name = '编辑' AND type = 2 AND authority = 'warehouse:edit' AND is_delete = 0
)
AND @warehouse_list_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 @warehouse_list_menu_id, 2, NULL, '删除', 3, NULL, NULL, 2, 'warehouse:delete', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_list_menu_id AND name = '删除' AND type = 2 AND authority = 'warehouse:delete' AND is_delete = 0
)
AND @warehouse_list_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 @warehouse_list_menu_id, 2, NULL, '查询', 4, NULL, NULL, 2, 'warehouse:query', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_list_menu_id AND name = '查询' AND type = 2 AND authority = 'warehouse:query' AND is_delete = 0
)
AND @warehouse_list_menu_id IS NOT NULL;

-- ============================================
-- 3. 插入二级菜单：进仓管理（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)
SELECT 
    @warehouse_parent_id AS parent_id,
    1 AS type,
    'el-icon-upload' AS icon,
    '进仓管理' AS name,
    2 AS sort,
    'in' AS route_url,
    'warehouse/warehouseIn' AS page_url,
    2 AS org_type,
    'warehousein:list' 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 = @warehouse_parent_id 
      AND name = '进仓管理' 
      AND type = 1 
      AND is_delete = 0
)
AND @warehouse_parent_id IS NOT NULL;

SET @warehouse_in_menu_id = COALESCE(
    (SELECT id FROM sys_menu WHERE parent_id = @warehouse_parent_id AND name = '进仓管理' AND type = 1 AND is_delete = 0 LIMIT 1),
    LAST_INSERT_ID()
);

-- 进仓管理按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT @warehouse_in_menu_id, 2, NULL, '新增', 1, NULL, NULL, 2, 'warehousein:add', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_in_menu_id AND name = '新增' AND type = 2 AND authority = 'warehousein:add' AND is_delete = 0
)
AND @warehouse_in_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 @warehouse_in_menu_id, 2, NULL, '编辑', 2, NULL, NULL, 2, 'warehousein:edit', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_in_menu_id AND name = '编辑' AND type = 2 AND authority = 'warehousein:edit' AND is_delete = 0
)
AND @warehouse_in_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 @warehouse_in_menu_id, 2, NULL, '删除', 3, NULL, NULL, 2, 'warehousein:delete', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_in_menu_id AND name = '删除' AND type = 2 AND authority = 'warehousein:delete' AND is_delete = 0
)
AND @warehouse_in_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 @warehouse_in_menu_id, 2, NULL, '查询', 4, NULL, NULL, 2, 'warehousein:query', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_in_menu_id AND name = '查询' AND type = 2 AND authority = 'warehousein:query' AND is_delete = 0
)
AND @warehouse_in_menu_id IS NOT NULL;

-- ============================================
-- 4. 插入二级菜单：出仓管理（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)
SELECT 
    @warehouse_parent_id AS parent_id,
    1 AS type,
    'el-icon-download' AS icon,
    '出仓管理' AS name,
    3 AS sort,
    'out' AS route_url,
    'warehouse/warehouseOut' AS page_url,
    2 AS org_type,
    'warehouseout:list' 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 = @warehouse_parent_id 
      AND name = '出仓管理' 
      AND type = 1 
      AND is_delete = 0
)
AND @warehouse_parent_id IS NOT NULL;

SET @warehouse_out_menu_id = COALESCE(
    (SELECT id FROM sys_menu WHERE parent_id = @warehouse_parent_id AND name = '出仓管理' AND type = 1 AND is_delete = 0 LIMIT 1),
    LAST_INSERT_ID()
);

-- 出仓管理按钮权限
INSERT INTO sys_menu (parent_id, type, icon, name, sort, route_url, page_url, org_type, authority, create_time, update_time, is_delete)
SELECT @warehouse_out_menu_id, 2, NULL, '新增', 1, NULL, NULL, 2, 'warehouseout:add', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_out_menu_id AND name = '新增' AND type = 2 AND authority = 'warehouseout:add' AND is_delete = 0
)
AND @warehouse_out_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 @warehouse_out_menu_id, 2, NULL, '编辑', 2, NULL, NULL, 2, 'warehouseout:edit', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_out_menu_id AND name = '编辑' AND type = 2 AND authority = 'warehouseout:edit' AND is_delete = 0
)
AND @warehouse_out_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 @warehouse_out_menu_id, 2, NULL, '删除', 3, NULL, NULL, 2, 'warehouseout:delete', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_out_menu_id AND name = '删除' AND type = 2 AND authority = 'warehouseout:delete' AND is_delete = 0
)
AND @warehouse_out_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 @warehouse_out_menu_id, 2, NULL, '查询', 4, NULL, NULL, 2, 'warehouseout:query', NOW(), NOW(), 0
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE parent_id = @warehouse_out_menu_id AND name = '查询' AND type = 2 AND authority = 'warehouseout:query' AND is_delete = 0
)
AND @warehouse_out_menu_id IS NOT NULL;

