-- ============================================
-- 权限管理功能初始化 SQL
-- 功能：添加权限管理菜单、设置超级管理员
-- 日期：2025-10-11
-- ============================================

-- ============================================
-- 1. 添加权限管理父菜单
-- ============================================
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `route_url`, `page_url`, `authority`, `icon`, `sort`, `is_delete`, `create_time`)
VALUES (0, 1, '权限管理', '/permission', null, 'permission:view', 'el-icon-lock', 20, 0, NOW());

-- 获取刚插入的权限管理父菜单ID（假设为 @permission_parent_id）
SET @permission_parent_id = LAST_INSERT_ID();

-- ============================================
-- 2. 添加子菜单：菜单权限管理
-- ============================================
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `route_url`, `page_url`, `authority`, `icon`, `sort`, `is_delete`, `create_time`)
VALUES (@permission_parent_id, 1, '菜单权限管理', '/permission/menu', 'permission/menuPermission', 'permission:menu:view', 'el-icon-menu', 1, 0, NOW());

SET @menu_permission_id = LAST_INSERT_ID();

-- 菜单权限管理的按钮权限
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`) VALUES
(@menu_permission_id, 2, '菜单查询', 'permission:menu:list', 1, 0, NOW()),
(@menu_permission_id, 2, '菜单新增', 'permission:menu:add', 2, 0, NOW()),
(@menu_permission_id, 2, '菜单编辑', 'permission:menu:edit', 3, 0, NOW()),
(@menu_permission_id, 2, '菜单删除', 'permission:menu:delete', 4, 0, NOW()),
(@menu_permission_id, 2, '角色分配', 'permission:menu:assign', 5, 0, NOW());

-- ============================================
-- 3. 添加子菜单：操作权限管理
-- ============================================
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `route_url`, `page_url`, `authority`, `icon`, `sort`, `is_delete`, `create_time`)
VALUES (@permission_parent_id, 1, '操作权限管理', '/permission/operation', 'permission/operationPermission', 'permission:operation:view', 'el-icon-setting', 2, 0, NOW());

SET @operation_permission_id = LAST_INSERT_ID();

-- 操作权限管理的按钮权限
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`) VALUES
(@operation_permission_id, 2, '权限查询', 'permission:operation:list', 1, 0, NOW()),
(@operation_permission_id, 2, '权限分配', 'permission:operation:assign', 2, 0, NOW()),
(@operation_permission_id, 2, '角色管理', 'permission:operation:role', 3, 0, NOW());

-- ============================================
-- 4. 查找或创建超级管理员角色
-- ============================================
-- 查看是否已有ID=1的角色，如果没有则插入
INSERT INTO `sys_role` (`id`, `name`, `description`, `is_delete`, `create_time`)
SELECT 1, '超级管理员', '拥有系统所有权限', 0, NOW()
WHERE NOT EXISTS (SELECT 1 FROM `sys_role` WHERE `id` = 1);

-- ============================================
-- 5. 为超级管理员角色分配所有菜单权限
-- ============================================
-- 清空超级管理员的旧权限（可选）
DELETE FROM `sys_role_menu` WHERE `role_id` = 1;

-- 分配所有菜单权限给超级管理员
INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, id FROM `sys_menu` WHERE `is_delete` = 0;

-- ============================================
-- 6. 设置15900000000账号为超级管理员
-- ============================================
-- 查找该手机号的用户
UPDATE `sys_user` 
SET `role_id` = 1 
WHERE `mobile` = '15900000000' AND `is_delete` = 0;

-- 如果用户不存在，创建超级管理员账号（密码默认为 123456，已MD5加密）
-- 注意：请根据实际的密码加密方式调整密码值
INSERT INTO `sys_user` (`mobile`, `name`, `password`, `role_id`, `status`, `user_type`, `is_delete`, `create_time`)
SELECT 
    '15900000000',
    '超级管理员',
    'e10adc3949ba59abbe56e057f20f883e', -- 123456 的MD5值
    1,
    1,
    1,
    0,
    NOW()
WHERE NOT EXISTS (
    SELECT 1 FROM `sys_user` WHERE `mobile` = '15900000000'
);

-- ============================================
-- 7. 为运送清单添加完整的权限点（如果还没有）
-- ============================================
-- 查找运送清单菜单
SET @delivery_menu_id = (SELECT id FROM `sys_menu` WHERE `route_url` LIKE '%delivery%' OR `route_url` LIKE '%loading%' LIMIT 1);

-- 如果找到了运送清单菜单，添加权限点
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '运送清单查看', 'delivery:list', 1, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:list');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '运送清单新增', 'delivery:add', 2, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:add');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '运送清单编辑', 'delivery:edit', 3, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:edit');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '运送清单删除', 'delivery:delete', 4, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:delete');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '运送清单查看详情', 'delivery:view', 5, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:view');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '运送清单核验', 'delivery:check', 6, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:check');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '设备分配', 'delivery:assign', 7, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:assign');

INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`)
SELECT @delivery_menu_id, 2, '装车操作', 'delivery:load', 8, 0, NOW()
WHERE @delivery_menu_id IS NOT NULL 
AND NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `authority` = 'delivery:load');

-- ============================================
-- 8. 验证数据
-- ============================================
-- 查看新增的权限管理菜单
SELECT * FROM `sys_menu` WHERE `name` LIKE '%权限%' OR `name` LIKE '%permission%';

-- 查看超级管理员用户
SELECT `id`, `mobile`, `name`, `role_id`, `status` FROM `sys_user` WHERE `mobile` = '15900000000';

-- 查看超级管理员的权限数量
SELECT COUNT(*) as '超级管理员权限数' FROM `sys_role_menu` WHERE `role_id` = 1;

-- ============================================
-- 执行完成
-- ============================================
SELECT '✅ 权限管理功能初始化完成！' as 'Status';
SELECT '账号: 15900000000, 密码: 123456 (如果是新创建的账号)' as '超级管理员信息';

