-- OCR模块完整菜单SQL
-- 包含OCR管理、测试、配置和网络诊断功能

-- 获取系统工具菜单ID（通常是3）
SET @parentMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = '系统工具' AND parent_id = 0 LIMIT 1);

-- 如果没有系统工具菜单，则使用系统管理（parent_id=1）
SET @parentMenuId = IFNULL(@parentMenuId, 1);

-- 插入OCR管理菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 'OCR管理', @parentMenuId, 9, 'ocr', '', 1, 0, 'M', '0', '0', '', 'camera', 'admin', NOW(), '', NULL, 'OCR服务管理菜单'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = 'OCR管理' AND perms = ''
);

-- 获取OCR管理菜单ID
SET @ocrParentMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = 'OCR管理' LIMIT 1);

-- 插入OCR测试子菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 'OCR测试', @ocrParentMenuId, 1, 'ocr', 'system/ocr/index', 1, 0, 'C', '0', '0', 'system:ocr:test', 'eye-open', 'admin', NOW(), '', NULL, 'OCR图像识别测试页面'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = 'OCR测试' AND perms = 'system:ocr:test'
);

-- 插入OCR配置子菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 'OCR配置', @ocrParentMenuId, 2, 'config', 'system/ocr/config', 1, 0, 'C', '0', '0', 'system:ocr:config', 'setting', 'admin', NOW(), '', NULL, 'OCR服务配置页面'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = 'OCR配置' AND perms = 'system:ocr:config'
);

-- 插入OCR识别权限按钮
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 'OCR识别', (SELECT menu_id FROM sys_menu WHERE perms = 'system:ocr:test' LIMIT 1), 1, '#', '', 1, 0, 'F', '0', '0', 'system:ocr:recognize', '#', 'admin', NOW(), '', NULL, 'OCR图像识别权限'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE perms = 'system:ocr:recognize'
);

-- 插入OCR服务提供商查询权限按钮
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 'OCR服务商查询', (SELECT menu_id FROM sys_menu WHERE perms = 'system:ocr:test' LIMIT 1), 2, '#', '', 1, 0, 'F', '0', '0', 'system:ocr:providers', '#', 'admin', NOW(), '', NULL, 'OCR服务提供商查询权限'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE perms = 'system:ocr:providers'
);

-- 插入网络诊断菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '网络诊断', @parentMenuId, 10, 'diag', '', 1, 0, 'M', '0', '0', '', 'link', 'admin', NOW(), '', NULL, '网络诊断管理菜单'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = '网络诊断' AND perms = ''
);

-- 插入OCR网络诊断子菜单
SET @diagParentMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = '网络诊断' LIMIT 1);
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 'OCR连接诊断', @diagParentMenuId, 1, 'ocrConnection', 'system/diag/ocrConnection', 1, 0, 'C', '0', '0', 'system:diag:ocr', 'monitor', 'admin', NOW(), '', NULL, 'OCR服务连接诊断页面'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = 'OCR连接诊断' AND perms = 'system:diag:ocr'
);

-- 查询结果
SELECT 
    menu_id,
    menu_name,
    parent_id,
    path,
    component,
    perms,
    icon,
    '菜单添加成功' AS status
FROM sys_menu 
WHERE menu_name IN ('OCR管理', 'OCR测试', 'OCR配置', 'OCR识别', 'OCR服务商查询', '网络诊断', 'OCR连接诊断')
ORDER BY parent_id, order_num;
