-- 线上/户外招聘会参会企业与岗位导出权限
-- 瀚高数据库 / PostgreSQL 兼容；可重复执行。
-- 导出权限会授予已经拥有对应招聘会列表权限的角色，包含企业端角色。

BEGIN;

-- 户外岗位审核字段：历史关联按已通过处理，兼容旧数据。
ALTER TABLE "shz"."cms_outdoor_fair_job"
    ADD COLUMN IF NOT EXISTS "review_status" CHAR(1) DEFAULT '1';
ALTER TABLE "shz"."cms_outdoor_fair_job"
    ADD COLUMN IF NOT EXISTS "review_by" VARCHAR(64);
ALTER TABLE "shz"."cms_outdoor_fair_job"
    ADD COLUMN IF NOT EXISTS "review_time" TIMESTAMP;
ALTER TABLE "shz"."cms_outdoor_fair_job"
    ADD COLUMN IF NOT EXISTS "review_remark" VARCHAR(500);

UPDATE "shz"."cms_outdoor_fair_job"
SET "review_status" = '1'
WHERE "review_status" IS NULL;

-- 户外企业审核字段由 fair_company 复用；旧库没有字段时补齐。
ALTER TABLE "shz"."fair_company"
    ADD COLUMN IF NOT EXISTS "review_status" CHAR(1) DEFAULT '1';
ALTER TABLE "shz"."fair_company"
    ADD COLUMN IF NOT EXISTS "review_by" VARCHAR(64);
ALTER TABLE "shz"."fair_company"
    ADD COLUMN IF NOT EXISTS "review_time" TIMESTAMP;
ALTER TABLE "shz"."fair_company"
    ADD COLUMN IF NOT EXISTS "review_remark" VARCHAR(500);

UPDATE "shz"."fair_company"
SET "review_status" = '1'
WHERE "review_status" IS NULL;

-- 线上导出按钮。
INSERT INTO "shz"."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 "shz"."sys_menu"),
    '导出参会企业/岗位', list_menu."menu_id", 8, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:export', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '线上招聘会参会企业与岗位导出'
FROM "shz"."sys_menu" list_menu
WHERE list_menu."perms" = 'cms:publicJobFair:list'
  AND NOT EXISTS (
      SELECT 1 FROM "shz"."sys_menu" WHERE "perms" = 'cms:publicJobFair:export'
  )
ORDER BY list_menu."menu_id"
LIMIT 1;

-- 户外导出按钮。
INSERT INTO "shz"."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 "shz"."sys_menu"),
    '导出参会企业/岗位', list_menu."menu_id", 4, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:outdoorFair:export', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '户外招聘会参会企业与岗位导出'
FROM "shz"."sys_menu" list_menu
WHERE list_menu."perms" = 'cms:outdoorFair:list'
  AND NOT EXISTS (
      SELECT 1 FROM "shz"."sys_menu" WHERE "perms" = 'cms:outdoorFair:export'
  )
ORDER BY list_menu."menu_id"
LIMIT 1;

-- 将按钮权限补给已有对应列表权限的角色；权限和关联都可重复执行。
INSERT INTO "shz"."sys_role_menu" ("role_id", "menu_id")
SELECT DISTINCT role_menu."role_id", export_menu."menu_id"
FROM "shz"."sys_role_menu" role_menu
JOIN "shz"."sys_menu" list_menu
  ON list_menu."menu_id" = role_menu."menu_id"
JOIN "shz"."sys_menu" export_menu
  ON export_menu."perms" = 'cms:publicJobFair:export'
WHERE list_menu."perms" = 'cms:publicJobFair:list'
  AND NOT EXISTS (
      SELECT 1 FROM "shz"."sys_role_menu" existing
      WHERE existing."role_id" = role_menu."role_id"
        AND existing."menu_id" = export_menu."menu_id"
  );

INSERT INTO "shz"."sys_role_menu" ("role_id", "menu_id")
SELECT DISTINCT role_menu."role_id", export_menu."menu_id"
FROM "shz"."sys_role_menu" role_menu
JOIN "shz"."sys_menu" list_menu
  ON list_menu."menu_id" = role_menu."menu_id"
JOIN "shz"."sys_menu" export_menu
  ON export_menu."perms" = 'cms:outdoorFair:export'
WHERE list_menu."perms" = 'cms:outdoorFair:list'
  AND NOT EXISTS (
      SELECT 1 FROM "shz"."sys_role_menu" existing
      WHERE existing."role_id" = role_menu."role_id"
        AND existing."menu_id" = export_menu."menu_id"
  );

COMMIT;

-- 验证：
-- SELECT menu_id, menu_name, perms FROM shz.sys_menu
-- WHERE perms IN ('cms:publicJobFair:export', 'cms:outdoorFair:export');
