-- 车辆管理模块权限配置SQL
-- 在sys_menu表中添加车辆管理的菜单权限

-- 1. 插入车辆管理主菜单
INSERT INTO `sys_menu` (
    `name`, 
    `parent_id`, 
    `sort`, 
    `route_url`, 
    `page_url`, 
    `type`, 
    `authority`, 
    `create_time`, 
    `update_time`
) VALUES (
    '车辆管理', 
    (SELECT id FROM (SELECT id FROM sys_menu WHERE name = '用户管理' AND parent_id = 0 LIMIT 1) AS t), 
    3,  
    '/vehicle', 
    '/userManage/vehicle', 
    1, 
    'vehicle:query', 
    NOW(), 
    NOW()
) ON DUPLICATE KEY UPDATE 
    `name` = VALUES(`name`),
    `page_url` = VALUES(`page_url`);

-- 2. 获取刚插入的车辆管理菜单ID
SET @vehicle_menu_id = LAST_INSERT_ID();
SELECT @vehicle_menu_id := id FROM sys_menu WHERE page_url = '/userManage/vehicle' LIMIT 1;

-- 3. 插入新增车辆权限按钮
INSERT INTO `sys_menu` (
    `name`, 
    `parent_id`, 
    `sort`, 
    `type`, 
    `authority`, 
    `create_time`, 
    `update_time`
) VALUES (
    '新增车辆', 
    @vehicle_menu_id, 
    1, 
    2, 
    'vehicle:add', 
    NOW(), 
    NOW()
) ON DUPLICATE KEY UPDATE 
    `name` = VALUES(`name`),
    `authority` = VALUES(`authority`);

-- 4. 插入编辑车辆权限按钮
INSERT INTO `sys_menu` (
    `name`, 
    `parent_id`, 
    `sort`, 
    `type`, 
    `authority`, 
    `create_time`, 
    `update_time`
) VALUES (
    '编辑车辆', 
    @vehicle_menu_id, 
    2, 
    2, 
    'vehicle:edit', 
    NOW(), 
    NOW()
) ON DUPLICATE KEY UPDATE 
    `name` = VALUES(`name`),
    `authority` = VALUES(`authority`);

-- 5. 插入删除车辆权限按钮
INSERT INTO `sys_menu` (
    `name`, 
    `parent_id`, 
    `sort`, 
    `type`, 
    `authority`, 
    `create_time`, 
    `update_time`
) VALUES (
    '删除车辆', 
    @vehicle_menu_id, 
    3, 
    2, 
    'vehicle:delete', 
    NOW(), 
    NOW()
) ON DUPLICATE KEY UPDATE 
    `name` = VALUES(`name`),
    `authority` = VALUES(`authority`);

-- 6. 将车辆管理相关权限分配给超级管理员角色（role_id = 1）
INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, `id` FROM `sys_menu` WHERE `authority` IN ('vehicle:query', 'vehicle:add', 'vehicle:edit', 'vehicle:delete')
ON DUPLICATE KEY UPDATE `role_id` = `role_id`;
