-- V30: 添加缺失的邀请和通知权限
-- 修复InviteController和NotificationController中使用的权限码
-- H2兼容版本

-- ==================== 邀请管理权限 ====================
-- 添加缺失的邀请操作权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 228, 'user:invite:create', '创建邀请', 'user', 'invite', 'create', 'ALL', '创建用户邀请', 228, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user:invite:create')
ON CONFLICT (id) DO NOTHING;

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 229, 'user:invite:resend', '重发邀请', 'user', 'invite', 'resend', 'ALL', '重发邀请邮件', 229, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user:invite:resend')
ON CONFLICT (id) DO NOTHING;

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 230, 'user:invite:expire', '设置邀请过期', 'user', 'invite', 'expire', 'ALL', '手动设置邀请过期', 230, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user:invite:expire')
ON CONFLICT (id) DO NOTHING;

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 231, 'user:invite:delete', '删除邀请', 'user', 'invite', 'delete', 'ALL', '删除邀请记录', 231, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user:invite:delete')
ON CONFLICT (id) DO NOTHING;

-- ==================== 通知管理权限 ====================
-- 添加系统通知模块权限 (用于NotificationController)
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 232, 'system:notification:view', '查看系统通知', 'system', 'notification', 'view', 'ALL', '查看系统通知列表', 232, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system:notification:view')
ON CONFLICT (id) DO NOTHING;

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 233, 'system:notification:create', '创建系统通知', 'system', 'notification', 'create', 'ALL', '创建系统通知', 233, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system:notification:create')
ON CONFLICT (id) DO NOTHING;

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 234, 'system:notification:update', '更新系统通知', 'system', 'notification', 'update', 'ALL', '更新系统通知状态', 234, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system:notification:update')
ON CONFLICT (id) DO NOTHING;

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 235, 'system:notification:delete', '删除系统通知', 'system', 'notification', 'delete', 'ALL', '删除系统通知', 235, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system:notification:delete')
ON CONFLICT (id) DO NOTHING;

-- ==================== 角色权限映射 ====================
-- 为超级管理员添加所有新权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code IN ('user:invite:create', 'user:invite:resend', 'user:invite:expire', 'user:invite:delete', 'system:notification:view', 'system:notification:create', 'system:notification:update', 'system:notification:delete')
ON CONFLICT (role_id, permission_id) DO NOTHING;

-- 为系统管理员添加所有新权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code IN ('user:invite:create', 'user:invite:resend', 'user:invite:expire', 'user:invite:delete', 'system:notification:view', 'system:notification:create', 'system:notification:update', 'system:notification:delete')
ON CONFLICT (role_id, permission_id) DO NOTHING;

-- 为运营总监添加邀请权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 3, id FROM sys_permission WHERE permission_code IN ('user:invite:create', 'user:invite:resend', 'user:invite:expire', 'user:invite:delete', 'system:notification:view', 'system:notification:create', 'system:notification:update')
ON CONFLICT (role_id, permission_id) DO NOTHING;

-- 为运营经理添加邀请权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 4, id FROM sys_permission WHERE permission_code IN ('user:invite:create', 'user:invite:resend', 'user:invite:expire', 'user:invite:delete', 'system:notification:view', 'system:notification:create', 'system:notification:update')
ON CONFLICT (role_id, permission_id) DO NOTHING;

-- 为客服主管添加通知权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 9, id FROM sys_permission WHERE permission_code IN ('system:notification:view', 'system:notification:create', 'system:notification:update')
ON CONFLICT (role_id, permission_id) DO NOTHING;
