-- ============================================
-- 添加缺失的权限（菜单、活动、评审等）
-- ============================================

-- 添加菜单管理权限
INSERT INTO t_auth_permission (tenant_id, name, code, resource, action, description, valid_state) VALUES
(1, '菜单创建', 'menu:create', 'menu', 'create', '创建菜单权限', 1),
(1, '菜单查询', 'menu:read', 'menu', 'read', '查询菜单权限', 1),
(1, '菜单更新', 'menu:update', 'menu', 'update', '更新菜单权限', 1),
(1, '菜单删除', 'menu:delete', 'menu', 'delete', '删除菜单权限', 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 添加活动管理权限
INSERT INTO t_auth_permission (tenant_id, name, code, resource, action, description, valid_state) VALUES
(1, '活动创建', 'contest:create', 'contest', 'create', '创建活动权限', 1),
(1, '活动查询', 'contest:read', 'contest', 'read', '查询活动权限', 1),
(1, '活动更新', 'contest:update', 'contest', 'update', '更新活动权限', 1),
(1, '活动删除', 'contest:delete', 'contest', 'delete', '删除活动权限', 1),
(1, '活动报名', 'contest:register', 'contest', 'register', '活动报名权限', 1),
(1, '活动评审', 'contest:review', 'contest', 'review', '活动评审权限', 1),
(1, '活动作品', 'contest:work', 'contest', 'work', '活动作品权限', 1),
(1, '活动评委', 'contest:judge', 'contest', 'judge', '活动评委权限', 1),
(1, '活动结果', 'contest:result', 'contest', 'result', '活动结果权限', 1),
(1, '活动规则', 'contest:rule', 'contest', 'rule', '活动规则权限', 1),
(1, '活动公告', 'contest:notice', 'contest', 'notice', '活动公告权限', 1),
(1, '活动评审分配', 'contest:review:assign', 'contest', 'review:assign', '活动评审分配权限', 1),
(1, '活动评审评分', 'contest:review:score', 'contest', 'review:score', '活动评审评分权限', 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 添加学校管理权限
INSERT INTO t_auth_permission (tenant_id, name, code, resource, action, description, valid_state) VALUES
(1, '学校查询', 'school:read', 'school', 'read', '查询学校权限', 1),
(1, '部门查询', 'department:read', 'department', 'read', '查询部门权限', 1),
(1, '年级查询', 'grade:read', 'grade', 'read', '查询年级权限', 1),
(1, '班级查询', 'class:read', 'class', 'read', '查询班级权限', 1),
(1, '教师查询', 'teacher:read', 'teacher', 'read', '查询教师权限', 1),
(1, '学生查询', 'student:read', 'student', 'read', '查询学生权限', 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 添加系统管理权限
INSERT INTO t_auth_permission (tenant_id, name, code, resource, action, description, valid_state) VALUES
(1, '字典查询', 'dict:read', 'dict', 'read', '查询字典权限', 1),
(1, '配置查询', 'config:read', 'config', 'read', '查询配置权限', 1),
(1, '日志查询', 'log:read', 'log', 'read', '查询日志权限', 1),
(1, '权限查询', 'permission:read', 'permission', 'read', '查询权限权限', 1),
(1, '权限创建', 'permission:create', 'permission', 'create', '创建权限权限', 1),
(1, '权限更新', 'permission:update', 'permission', 'update', '更新权限权限', 1),
(1, '权限删除', 'permission:delete', 'permission', 'delete', '删除权限权限', 1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);

-- 获取刚插入的权限 ID 并关联到 super_admin 角色 (id=1)
-- 菜单管理权限
INSERT INTO t_auth_role_permission (role_id, permission_id)
SELECT 1, id FROM t_auth_permission WHERE code IN ('menu:create', 'menu:read', 'menu:update', 'menu:delete')
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);

-- 活动管理权限
INSERT INTO t_auth_role_permission (role_id, permission_id)
SELECT 1, id FROM t_auth_permission WHERE code LIKE 'contest%'
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);

-- 学校管理权限
INSERT INTO t_auth_role_permission (role_id, permission_id)
SELECT 1, id FROM t_auth_permission WHERE code IN ('school:read', 'department:read', 'grade:read', 'class:read', 'teacher:read', 'student:read')
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);

-- 系统管理权限
INSERT INTO t_auth_role_permission (role_id, permission_id)
SELECT 1, id FROM t_auth_permission WHERE code IN ('dict:read', 'config:read', 'log:read', 'permission:read', 'permission:create', 'permission:update', 'permission:delete')
ON DUPLICATE KEY UPDATE `role_id` = VALUES(`role_id`);
