-- V34: 添加Controller使用的缺失权限码
-- 解决@RequirePermission注解中的权限码在DB中无法命中的问题

-- 活动管理模块：添加Controller直接使用的权限码
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 345, 'activity:view', '查看活动', 'activity', 'index', 'view', 'ALL', '查看活动列表和详情', 345, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'activity: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 346, 'activity:stats', '查看活动统计', 'activity', 'stats', 'view', 'ALL', '查看活动统计', 346, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'activity:stats')
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 347, 'activity:approve', '审批活动', 'activity', 'approval', 'approve', 'ALL', '审批活动', 347, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'activity:approve')
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 348, 'activity:submit', '提交活动审批', 'activity', 'approval', 'submit', 'ALL', '提交活动审批', 348, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'activity:submit')
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 349, 'reward:apply', '申请奖励', 'reward', 'index', 'apply', 'ALL', '申请奖励', 349, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'reward:apply')
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 350, 'reward:cancel', '取消奖励', 'reward', 'index', 'cancel', 'ALL', '取消奖励申请', 350, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'reward:cancel')
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 351, 'reward:reconcile', '对账奖励', 'reward', 'index', 'reconcile', 'ALL', '奖励对账', 351, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'reward:reconcile')
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 352, 'approval:view', '查看审批', 'approval', 'index', 'view', 'ALL', '查看审批列表', 352, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval: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 353, 'approval:handle', '处理审批', 'approval', 'index', 'handle', 'ALL', '处理审批', 353, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval:handle')
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 354, 'user:certify', '认证用户', 'user', 'index', 'certify', 'ALL', '认证用户', 354, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user:certify')
ON CONFLICT (id) DO NOTHING;

-- 为超级管理员和系统管理员添加新权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code IN ('activity:view', 'activity:stats', 'activity:approve', 'activity:submit', 'reward:apply', 'reward:cancel', 'reward:reconcile', 'approval:view', 'approval:handle', 'user:certify')
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 ('activity:view', 'activity:stats', 'activity:approve', 'activity:submit', 'reward:apply', 'reward:cancel', 'reward:reconcile', 'approval:view', 'approval:handle', 'user:certify')
ON CONFLICT (role_id, permission_id) DO NOTHING;
