-- 户外招聘会企业参会资料与企业只读展位图权限
-- 瀚高数据库 / PostgreSQL 兼容；可重复执行

ALTER TABLE "cms_outdoor_fair"
    ADD COLUMN IF NOT EXISTS "required_materials" TEXT;

ALTER TABLE "cms_outdoor_fair"
    ADD COLUMN IF NOT EXISTS "attendance_notes" TEXT;

COMMENT ON COLUMN "cms_outdoor_fair"."required_materials" IS '企业需携带资料';
COMMENT ON COLUMN "cms_outdoor_fair"."attendance_notes" IS '企业到场须知';

-- 为企业角色增加“只读查看展位图”按钮权限。
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"),
    '企业展位图查看',
    parent."menu_id",
    2,
    '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:outdoorFair:companyBoothMap', '#',
    'system', CURRENT_TIMESTAMP, '', NULL, '户外招聘会-企业审核通过后只读查看展位图权限'
FROM "sys_menu" parent
WHERE parent."menu_id" = (
      SELECT "parent_id"
      FROM "sys_menu"
      WHERE "perms" = 'cms:outdoorFair:list'
      LIMIT 1
  )
  AND NOT EXISTS (
      SELECT 1 FROM "sys_menu" WHERE "perms" = 'cms:outdoorFair:companyBoothMap'
  );

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" = 'view'
  AND menu."perms" = 'cms:outdoorFair:companyBoothMap'
  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"
  );
