-- =====================================================
-- XL监狱综合管理平台 - 问卷代填权限SQL
-- 生成时间: 2026-01-26
-- 功能: 添加问卷代填按钮权限
-- =====================================================

-- =====================================================
-- 方法1: 自动查找并插入（推荐）
-- =====================================================

-- 1. 查找问卷记录管理(assessment-record)的父菜单ID
SELECT @parentId := parent_id
FROM system_menu
WHERE name = '测评记录管理' AND type = 2 AND deleted = 0
LIMIT 1;

-- 如果找不到，尝试查找 questionnaire-record
SELECT @parentId := id
FROM system_menu
WHERE path = 'assessment-record' AND type = 2 AND deleted = 0
LIMIT 1;

-- 2. 插入代填权限
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
SELECT '代为填写', 'prison:questionnaire-record:agent-fill', 3, 8, @parentId, '', '', '', 0
WHERE @parentId IS NOT NULL;

-- 3. 验证插入结果
SELECT * FROM system_menu
WHERE permission = 'prison:questionnaire-record:agent-fill';

-- =====================================================
-- 方法2: 手动指定父菜单ID插入
-- =====================================================
-- 请将 5047 替换为实际的测评记录管理父菜单ID

-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
-- VALUES ('代为填写', 'prison:questionnaire-record:agent-fill', 3, 8, 5047, '', '', '', 0);

-- =====================================================
-- 方法3: 完整的查找和插入脚本（包含错误处理）
-- =====================================================

-- 设置临时变量
SET @parentId := NULL;

-- 查找问卷记录管理菜单的父ID
SELECT parent_id INTO @parentId
FROM system_menu
WHERE name = '测评记录管理'
  AND type = 2
  AND deleted = 0
ORDER BY id DESC
LIMIT 1;

-- 如果没找到，尝试通过path查找
IF @parentId IS NULL THEN
    SELECT parent_id INTO @parentId
    FROM system_menu
    WHERE path = 'assessment-record'
      AND type = 2
      AND deleted = 0
    ORDER BY id DESC
    LIMIT 1;
END IF;

-- 如果还是没找到，输出提示
SELECT
    CASE
        WHEN @parentId IS NULL THEN '未找到测评记录管理菜单，请手动检查！'
        ELSE CONCAT('找到父菜单ID: ', @parentId, '，即将插入代填权限...')
    END AS result;

-- 插入代填权限（使用 INSERT IGNORE 避免重复插入）
INSERT IGNORE INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
VALUES (
    '代为填写',
    'prison:questionnaire-record:agent-fill',
    3,          -- type=3 表示按钮
    8,          -- sort=8 排在最后（根据现有排序调整）
    @parentId,  -- 父菜单ID
    '',         -- path
    '',         -- icon
    '',         -- component
    0,          -- status
    b'1',       -- visible
    b'0',       -- keep_alive
    b'1',       -- always_show
    'system',
    NOW(),
    '',
    NOW(),
    b'0'
);

-- =====================================================
-- 验证结果
-- =====================================================

-- 查看所有 questionnaire-record 相关的权限
SELECT id, name, permission, type, sort, parent_id
FROM system_menu
WHERE (permission LIKE 'prison:questionnaire-record:%'
   OR permission LIKE 'prison:assessment-record:%')
  AND deleted = 0
ORDER BY parent_id, sort;

-- 查看插入的代填权限
SELECT * FROM system_menu
WHERE permission = 'prison:questionnaire-record:agent-fill';
