-- ============================================
-- 强制发布权限更新脚本
-- 用于更新现有数据库，添加文章和课程的强制发布权限
-- 执行时间：2026-01-14
-- ============================================

USE school_news;

-- 1. 插入新的权限数据
INSERT INTO `tb_sys_permission` (id, permission_id, name, code, description, module_id, creator, create_time) VALUES
('1101', 'perm_admin_article_force_publish', '文章强制发布', 'admin:article:force-publish', '强制发布文章权限（跳过敏感词校验）', 'module_news', '1', now()),
('2201', 'perm_admin_course_force_publish', '课程强制发布', 'admin:course:force-publish', '强制发布课程权限（跳过敏感词校验）', 'module_study', '1', now())
ON DUPLICATE KEY UPDATE 
    name = VALUES(name),
    code = VALUES(code),
    description = VALUES(description),
    update_time = now();

-- 2. 为超级管理员角色添加强制发布权限
INSERT INTO `tb_sys_role_permission` (id, role_id, permission_id, creator, create_time) VALUES
('46', 'superadmin', 'perm_admin_article_force_publish', '1', now()),
('47', 'superadmin', 'perm_admin_course_force_publish', '1', now())
ON DUPLICATE KEY UPDATE update_time = now();

-- 3. 为管理员角色添加强制发布权限
INSERT INTO `tb_sys_role_permission` (id, role_id, permission_id, creator, create_time) VALUES
('140', 'admin', 'perm_admin_article_force_publish', '1', now()),
('141', 'admin', 'perm_admin_course_force_publish', '1', now())
ON DUPLICATE KEY UPDATE update_time = now();

-- 验证插入结果
SELECT '=== 新增权限 ===' AS info;
SELECT permission_id, name, code, description FROM tb_sys_permission 
WHERE permission_id IN ('perm_admin_article_force_publish', 'perm_admin_course_force_publish');

SELECT '=== 角色权限关联 ===' AS info;
SELECT r.name AS role_name, p.name AS permission_name, p.code 
FROM tb_sys_role_permission rp
JOIN tb_sys_role r ON r.role_id = rp.role_id
JOIN tb_sys_permission p ON p.permission_id = rp.permission_id
WHERE p.permission_id IN ('perm_admin_article_force_publish', 'perm_admin_course_force_publish');
