-- 虚拟支付订单管理菜单（可重复执行）
-- 默认放在现有“支付订单”菜单的同级目录下；若未找到，则作为一级菜单显示。

SET @virtual_order_parent_id := COALESCE(
    (SELECT parent_id FROM sys_menu WHERE perms = 'app:order:list' LIMIT 1),
    0
);

SET @virtual_order_order_num := COALESCE(
    (SELECT MAX(order_num) + 1 FROM sys_menu WHERE parent_id = @virtual_order_parent_id),
    1
);

INSERT INTO sys_menu
    (menu_name, parent_id, order_num, path, component, query, is_frame, is_cache,
     menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT
    '虚拟支付订单', @virtual_order_parent_id, @virtual_order_order_num,
    'virtualOrder', 'app/virtualOrder/index', '', 1, 0,
    'C', '0', '0', 'app:virtualOrder:list', 'money', 'admin', NOW(), '', NULL,
    '虚拟支付订单只读查询菜单'
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE perms = 'app:virtualOrder:list'
);

SET @virtual_order_menu_id := (
    SELECT menu_id FROM sys_menu WHERE perms = 'app:virtualOrder:list' LIMIT 1
);

INSERT INTO sys_menu
    (menu_name, parent_id, order_num, path, component, query, is_frame, is_cache,
     menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT
    '虚拟订单查询', @virtual_order_menu_id, 1, '', '', '', 1, 0,
    'F', '0', '0', 'app:virtualOrder:query', '#', 'admin', NOW(), '', NULL, ''
WHERE @virtual_order_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1 FROM sys_menu WHERE perms = 'app:virtualOrder:query'
  );

INSERT INTO sys_menu
    (menu_name, parent_id, order_num, path, component, query, is_frame, is_cache,
     menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT
    '虚拟订单导出', @virtual_order_menu_id, 2, '', '', '', 1, 0,
    'F', '0', '0', 'app:virtualOrder:export', '#', 'admin', NOW(), '', NULL, ''
WHERE @virtual_order_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1 FROM sys_menu WHERE perms = 'app:virtualOrder:export'
  );
