-- =====================================================
-- 城市部件 — 菜单 + 按钮权限 SQL
-- 使用 LAST_INSERT_ID 避免ID冲突，NOT EXISTS 防止重复
-- =====================================================

-- 一级菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件', '', 1, 10, 0, '/city-component', 'ep:component', '', 'CityComponent', 0, b'1', b'0', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE name='城市部件' AND type=1 AND deleted=0);

SET @parent_id = (SELECT id FROM system_menu WHERE name='城市部件' AND type=1 AND deleted=0 LIMIT 1);

-- 城市部件类型
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件类型', '', 2, 1, @parent_id, 'city-component-type', 'ep:list', 'garden/cityComponentType/index', 'CityComponentType', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE name='城市部件类型' AND parent_id=@parent_id AND deleted=0);

-- 城市部件管理
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件管理', '', 2, 2, @parent_id, 'city-component', 'ep:grid', 'garden/cityComponent/index', 'CityComponent', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE name='城市部件管理' AND parent_id=@parent_id AND deleted=0);

SET @type_menu_id = (SELECT id FROM system_menu WHERE name='城市部件类型' AND parent_id=@parent_id AND deleted=0 LIMIT 1);
SET @comp_menu_id = (SELECT id FROM system_menu WHERE name='城市部件管理' AND parent_id=@parent_id AND deleted=0 LIMIT 1);

-- 城市部件类型按钮
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件类型查询', 'garden:city-component-type:query', 3, 1, @type_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component-type:query' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件类型新增', 'garden:city-component-type:create', 3, 2, @type_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component-type:create' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件类型修改', 'garden:city-component-type:update', 3, 3, @type_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component-type:update' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件类型删除', 'garden:city-component-type:delete', 3, 4, @type_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component-type:delete' AND deleted=0);

-- 城市部件管理按钮
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件查询', 'garden:city-component:query', 3, 1, @comp_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component:query' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件新增', 'garden:city-component:create', 3, 2, @comp_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component:create' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件修改', 'garden:city-component:update', 3, 3, @comp_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component:update' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件删除', 'garden:city-component:delete', 3, 4, @comp_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component:delete' AND deleted=0);

INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT '城市部件导出', 'garden:city-component:export', 3, 5, @comp_menu_id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='garden:city-component:export' AND deleted=0);

-- 赋权超级管理员
INSERT IGNORE INTO system_role_menu (role_id, menu_id, creator, create_time, updater, update_time)
SELECT 1, id, '1', NOW(), '1', NOW() FROM system_menu
WHERE id IN (@parent_id, @type_menu_id, @comp_menu_id) AND deleted=0;

INSERT IGNORE INTO system_role_menu (role_id, menu_id, creator, create_time, updater, update_time)
SELECT 1, id, '1', NOW(), '1', NOW() FROM system_menu
WHERE parent_id IN (@type_menu_id, @comp_menu_id) AND deleted=0;
