-- ============================================================
-- 湛江市智慧人社运营运维项目
-- 修复见习岗位管理和公益性岗位管理导出按钮不显示问题
--
-- 根本原因：
--   后端 SysPermissionController 构建 codeList 时过滤 status='1'
--   但 V20260603_5/V20260603_7 Flyway脚本插入的按钮权限 status=NULL
--   导致权限码不被返回，前端 v-auth 指令隐藏导出按钮
--
-- 修复内容：
--   1. 修复 internship_post 和 welfare_post 所有按钮权限 status=1
--   2. 确保导出权限已授权给admin角色
--
-- 数据库：达梦数据库 (DM8) 兼容
-- 创建日期：2026-06-11
-- ============================================================

-- ============================================================
-- 第一部分：修复见习岗位管理按钮权限 status
-- ============================================================
UPDATE sys_permission
SET status = 1
WHERE perms IN (
    'internship_post:add',
    'internship_post:edit',
    'internship_post:delete',
    'internship_post:deleteBatch',
    'internship_post:exportXls',
    'internship_post:importExcel',
    'internship_post:publish',
    'internship_post:unpublish',
    'internship_post:updateApplyStatus',
    'internship_post:copy'
)
  AND (status IS NULL OR status != 1);

-- ============================================================
-- 第二部分：修复公益性岗位管理按钮权限 status
-- ============================================================
UPDATE sys_permission
SET status = 1
WHERE perms IN (
    'welfare_post:add',
    'welfare_post:edit',
    'welfare_post:delete',
    'welfare_post:deleteBatch',
    'welfare_post:exportXls',
    'welfare_post:importExcel',
    'welfare_post:publish',
    'welfare_post:unpublish',
    'welfare_post:updatePostStatus'
)
  AND (status IS NULL OR status != 1);

-- ============================================================
-- 第三部分：确保导出权限已授权给admin角色
-- admin角色ID: f6817f48af4fb3af11b9e8bf182f618b
-- ============================================================

-- 3.1 见习岗位导出权限授权（权限ID: 1780601000000315）
INSERT INTO sys_role_permission (id, role_id, permission_id, data_rule_ids, operate_date, operate_ip)
SELECT '1780601000000322', 'f6817f48af4fb3af11b9e8bf182f618b', '1780601000000315', NULL, '2026-06-11 10:00:00', '127.0.0.1'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_role_permission
    WHERE role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
      AND permission_id = '1780601000000315'
);

-- 3.2 公益性岗位导出权限授权（权限ID: 1780601000000415）
INSERT INTO sys_role_permission (id, role_id, permission_id, data_rule_ids, operate_date, operate_ip)
SELECT '1780601000000422', 'f6817f48af4fb3af11b9e8bf182f618b', '1780601000000415', NULL, '2026-06-11 10:00:00', '127.0.0.1'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_role_permission
    WHERE role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
      AND permission_id = '1780601000000415'
);

-- ============================================================
-- 第四部分：验证修复结果
-- ============================================================

-- 4.1 检查按钮权限status是否已修复
SELECT '见习岗位权限status' AS 检查项, id, name, perms, status
FROM sys_permission
WHERE perms LIKE 'internship_post:%' AND menu_type = 2
ORDER BY perms;

-- 4.2 检查公益性岗位权限status
SELECT '公益性岗位权限status' AS 检查项, id, name, perms, status
FROM sys_permission
WHERE perms LIKE 'welfare_post:%' AND menu_type = 2
ORDER BY perms;

-- 4.3 检查导出权限角色授权
SELECT '角色授权检查' AS 检查项, p.perms, rp.role_id
FROM sys_permission p
LEFT JOIN sys_role_permission rp ON rp.permission_id = p.id
  AND rp.role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
WHERE p.perms IN ('internship_post:exportXls', 'welfare_post:exportXls');
