Files
yunzerwebsiteallinone/go/docs/sql/oa_ai_menus.sql
T

123 lines
5.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ============================================================
-- OA AI 办公 —— 菜单配置(yz_system_menu)
-- 幂等脚本:按 path 判重,重复执行不会产生重复菜单
-- 执行:mysql -h <host> -P <port> -u <user> -p <db> < oa_ai_menus.sql
--
-- 挂载位置:
-- OA → 办公 → AI办公(页面,/apps/oa/ai)
-- OA → 基础设置 → AI设置(页面,/basic-settings/ai)
--
-- 对应页面:
-- backend/src/views/apps/oa/ai/index.vue AI 广场(场景对话)
-- backend/src/views/basicSettings/ai/index.vue AI 设置(租户级接口 + Key)
--
-- 说明:
-- 1. AI 接入配置是租户级的(yz_backend_ai_provider.user_id = 0),
-- 由「AI设置」页统一维护,保存后全租户共用;
-- 2. 「AI办公」是 Web 页面(platforms=["web"]),移动端暂不提供;
-- 3. 租户端角色(cid=2)的 rights 是显式菜单 ID 列表,只插菜单不授权
-- 会被过滤掉,因此本脚本把菜单 ID 追加到角色 rights。
-- ============================================================
SET @oa_id := (
SELECT id FROM yz_system_menu
WHERE path = '/apps/oa' AND delete_time IS NULL
LIMIT 1
);
-- 「办公」目录:优先同名菜单,其次 /apps/oa/personal;都没有则回落 OA 根
SET @office_id := (
SELECT id FROM yz_system_menu
WHERE delete_time IS NULL
AND type = 1
AND (title = '办公' OR title = '个人办公' OR path = '/apps/oa/personal')
ORDER BY (title = '办公') DESC, (path = '/apps/oa/personal') DESC, sort, id
LIMIT 1
);
-- OA 基础设置目录(AI 设置挂这里)
SET @basic_id := (
SELECT id FROM yz_system_menu
WHERE path = '/apps/oa/normalsetting' AND delete_time IS NULL
LIMIT 1
);
SET @office_parent := IFNULL(@office_id, @oa_id);
SET @basic_parent := IFNULL(@basic_id, @office_parent);
-- ---------- AI办公(页面,挂在办公下) ----------
INSERT INTO yz_system_menu
(pid, title, path, component_path, icon, sort, status, is_visible, views, platforms, type, creater, remark)
SELECT @office_parent, 'AI办公', '/apps/oa/ai', '/apps/oa/ai/index.vue',
'MagicStick', 91, 1, 1, '[2]', '["web"]', 2, 'system',
'AI 广场:选择接入与模型进行场景对话,支持图片输入、系统提示词与多会话管理'
FROM DUAL
WHERE @office_parent IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM (SELECT id FROM yz_system_menu WHERE path = '/apps/oa/ai' AND delete_time IS NULL) x);
SET @ai_page_id := (
SELECT id FROM yz_system_menu
WHERE path = '/apps/oa/ai' AND delete_time IS NULL
LIMIT 1
);
UPDATE yz_system_menu
SET pid = IFNULL(@office_parent, pid),
sort = 91,
platforms = '["web"]',
is_visible = 1
WHERE path = '/apps/oa/ai' AND delete_time IS NULL;
-- ---------- AI设置(页面,挂在 OA 基础设置下) ----------
INSERT INTO yz_system_menu
(pid, title, path, component_path, icon, sort, status, is_visible, views, platforms, type, creater, remark)
SELECT @basic_parent, 'AI设置', '/basic-settings/ai', '/basicSettings/ai/index.vue',
'Setting', 9, 1, 1, '[2]', '["web"]', 2, 'system',
'租户级 AI 接入配置:填写接口地址与 API Key,支持 OpenAI 兼容与 Anthropic,可测试连通性'
FROM DUAL
WHERE @basic_parent IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM (SELECT id FROM yz_system_menu WHERE path = '/basic-settings/ai' AND delete_time IS NULL) x);
SET @ai_settings_id := (
SELECT id FROM yz_system_menu
WHERE path = '/basic-settings/ai' AND delete_time IS NULL
LIMIT 1
);
UPDATE yz_system_menu
SET pid = IFNULL(@basic_parent, pid),
sort = 9,
platforms = '["web"]',
is_visible = 1
WHERE path = '/basic-settings/ai' AND delete_time IS NULL;
-- ---------- 角色授权:把菜单追加到所有租户端角色的 rights ----------
-- 只处理 rights 非空(显式授权)的角色;rights 为空表示全权限,无需追加。
UPDATE yz_system_admin_role
SET rights = CAST(JSON_ARRAY_APPEND(CAST(rights AS JSON), '$', @ai_page_id) AS CHAR)
WHERE cid = 2
AND rights IS NOT NULL AND TRIM(rights) <> '' AND JSON_VALID(rights)
AND @ai_page_id IS NOT NULL
AND NOT JSON_CONTAINS(CAST(rights AS JSON), CAST(@ai_page_id AS JSON));
UPDATE yz_system_admin_role
SET rights = CAST(JSON_ARRAY_APPEND(CAST(rights AS JSON), '$', @ai_settings_id) AS CHAR)
WHERE cid = 2
AND rights IS NOT NULL AND TRIM(rights) <> '' AND JSON_VALID(rights)
AND @ai_settings_id IS NOT NULL
AND NOT JSON_CONTAINS(CAST(rights AS JSON), CAST(@ai_settings_id AS JSON));
-- ---------- 结果校验 ----------
SELECT id, pid, title, path, component_path, platforms, type, sort, is_visible
FROM yz_system_menu
WHERE delete_time IS NULL
AND (path = '/apps/oa/ai' OR path = '/basic-settings/ai')
ORDER BY pid, sort;
SELECT id, name, tenant_id,
JSON_CONTAINS(CAST(rights AS JSON), CAST(@ai_page_id AS JSON)) AS has_ai_page,
JSON_CONTAINS(CAST(rights AS JSON), CAST(@ai_settings_id AS JSON)) AS has_ai_settings
FROM yz_system_admin_role
WHERE cid = 2 AND rights IS NOT NULL AND TRIM(rights) <> ''
ORDER BY tenant_id, id;