-- 户外招聘会微信小程序码按钮权限
-- 瀚高数据库 / PostgreSQL 兼容；可重复执行

INSERT INTO "sys_menu" (
    "menu_id", "menu_name", "parent_id", "order_num", "path", "component", "query", "route_name",
    "is_frame", "is_cache", "menu_type", "visible", "status", "perms", "icon",
    "create_by", "create_time", "update_by", "update_time", "remark"
)
SELECT
    (SELECT COALESCE(MAX("menu_id"), 0) + 1 FROM "sys_menu"),
    '招聘会小程序码',
    list_menu."parent_id",
    3,
    '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:outdoorFair:qrcode', '#',
    'system', CURRENT_TIMESTAMP, '', NULL, '户外招聘会列表-查看微信小程序码'
FROM "sys_menu" list_menu
WHERE list_menu."perms" = 'cms:outdoorFair:list'
  AND NOT EXISTS (
      SELECT 1 FROM "sys_menu" WHERE "perms" = 'cms:outdoorFair:qrcode'
  )
LIMIT 1;

-- 仅授予超级管理员；其他后台角色需要时可由管理员显式分配。
INSERT INTO "sys_role_menu" ("role_id", "menu_id")
SELECT role."role_id", menu."menu_id"
FROM "sys_role" role
CROSS JOIN "sys_menu" menu
WHERE role."role_key" = 'admin'
  AND menu."perms" = 'cms:outdoorFair:qrcode'
  AND NOT EXISTS (
      SELECT 1
      FROM "sys_role_menu" role_menu
      WHERE role_menu."role_id" = role."role_id"
        AND role_menu."menu_id" = menu."menu_id"
  );
