-- 全面下线会议字段管理模块：权限、菜单、关联关系与数据表

-- 1) 移除角色权限绑定
DELETE rp
FROM role_permission rp
INNER JOIN permission p ON rp.permission_id = p.id
WHERE p.permission_code IN ('meeting.field.read', 'meeting.field.manage');

-- 2) 逻辑删除权限定义
-- permission 表未定义 is_deleted，直接物理删除对应权限码
DELETE FROM permission
WHERE permission_code IN ('meeting.field.read', 'meeting.field.manage');

-- 3) 移除菜单角色绑定
DELETE rm
FROM role_menu rm
INNER JOIN menu m ON rm.menu_id = m.id
WHERE m.menu_code = 'meeting_field' OR m.route_path = '/meeting-fields';

-- 4) 逻辑删除菜单
SET @menu_has_is_deleted := (
    SELECT COUNT(1)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'menu'
      AND COLUMN_NAME = 'is_deleted'
);
SET @sql := IF(
    @menu_has_is_deleted > 0,
    'UPDATE menu SET is_deleted = 1 WHERE menu_code = ''meeting_field'' OR route_path = ''/meeting-fields''',
    'DELETE FROM menu WHERE menu_code = ''meeting_field'' OR route_path = ''/meeting-fields'''
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 5) 删除业务表
DROP TABLE IF EXISTS meeting_field;
