-- 公共招聘会企业报名审核及企业岗位维护权限
--
-- 说明：
-- 1. 企业在 CMS 报名后进入 0-待审核；管理员审核为 1-通过或 2-驳回。
-- 2. 历史企业关联没有审核字段，统一按 1-已通过处理，避免历史数据突然不能维护。
-- 3. 脚本按权限标识和角色关联幂等执行，可重复执行。

BEGIN;

-- 1. 企业报名审核字段
ALTER TABLE "shz"."public_job_fair_company"
    ADD COLUMN IF NOT EXISTS "review_status" CHAR(1) DEFAULT '1';
ALTER TABLE "shz"."public_job_fair_company"
    ADD COLUMN IF NOT EXISTS "review_by" VARCHAR(64);
ALTER TABLE "shz"."public_job_fair_company"
    ADD COLUMN IF NOT EXISTS "review_time" TIMESTAMP;
ALTER TABLE "shz"."public_job_fair_company"
    ADD COLUMN IF NOT EXISTS "review_remark" VARCHAR(500);

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

COMMENT ON COLUMN "shz"."public_job_fair_company"."review_status"
    IS '审核状态 0待审核 1通过 2驳回';
COMMENT ON COLUMN "shz"."public_job_fair_company"."review_by"
    IS '审核人';
COMMENT ON COLUMN "shz"."public_job_fair_company"."review_time"
    IS '审核时间';
COMMENT ON COLUMN "shz"."public_job_fair_company"."review_remark"
    IS '审核意见';

-- 2. 新增 CMS 按钮权限。
-- 当前线上招聘会列表菜单为 cms:publicJobFair:list；找不到时兼容已知父菜单 2056。
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"),
    '企业报名公共招聘会', parent_menu."menu_id", 1, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:signup', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '企业用户报名公共招聘会'
FROM (
    SELECT COALESCE((
        SELECT "menu_id"
        FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id"
        LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:signup'
);

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"),
    '企业新增报名岗位', parent_menu."menu_id", 2, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:job:add', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '审核通过后企业新增报名岗位'
FROM (
    SELECT COALESCE((
        SELECT "menu_id" FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id" LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:job:add'
);

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"),
    '企业修改报名岗位', parent_menu."menu_id", 3, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:job:edit', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '企业修改自己的报名岗位'
FROM (
    SELECT COALESCE((
        SELECT "menu_id" FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id" LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:job:edit'
);

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"),
    '企业删除报名岗位', parent_menu."menu_id", 4, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:job:remove', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '企业删除自己的报名岗位'
FROM (
    SELECT COALESCE((
        SELECT "menu_id" FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id" LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:job:remove'
);

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"),
    '审核参会企业', parent_menu."menu_id", 5, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:review', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '管理员审核招聘会企业报名'
FROM (
    SELECT COALESCE((
        SELECT "menu_id" FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id" LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:review'
);

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"),
    '管理员添加参会企业', parent_menu."menu_id", 6, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:company:add', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '管理员向招聘会添加参会企业'
FROM (
    SELECT COALESCE((
        SELECT "menu_id" FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id" LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:company:add'
);

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"),
    '管理员移除参会企业', parent_menu."menu_id", 7, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:publicJobFair:company:remove', '#', 'admin', CURRENT_TIMESTAMP,
    '', NULL, '管理员从招聘会移除参会企业'
FROM (
    SELECT COALESCE((
        SELECT "menu_id" FROM "shz"."sys_menu"
        WHERE "perms" = 'cms:publicJobFair:list'
        ORDER BY "menu_id" LIMIT 1
    ), 2056) AS "menu_id"
) parent_menu
WHERE NOT EXISTS (
    SELECT 1 FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:company:remove'
);

-- 3. 企业角色（线上 role_id=1102）获得企业端报名和岗位维护权限。
INSERT INTO "shz"."sys_role_menu" ("role_id", "menu_id")
SELECT 1102, menu."menu_id"
FROM "shz"."sys_menu" menu
WHERE menu."perms" IN (
    'cms:publicJobFair:signup',
    'cms:publicJobFair:job:add',
    'cms:publicJobFair:job:edit',
    'cms:publicJobFair:job:remove'
)
  AND NOT EXISTS (
      SELECT 1
      FROM "shz"."sys_role_menu" existing
      WHERE existing."role_id" = 1102
        AND existing."menu_id" = menu."menu_id"
  );

-- 管理员同时获得 CMS 详情页的企业/岗位维护权限；企业角色虽有同名岗位权限，
-- 但 Service 层会拒绝其调用管理员关联接口。
INSERT INTO "shz"."sys_role_menu" ("role_id", "menu_id")
SELECT DISTINCT admin_role."role_id", menu."menu_id"
FROM (
    SELECT DISTINCT role_menu."role_id"
    FROM "shz"."sys_role_menu" role_menu
    JOIN "shz"."sys_menu" list_menu
      ON list_menu."menu_id" = role_menu."menu_id"
     AND list_menu."perms" = 'cms:publicJobFair:list'
    WHERE role_menu."role_id" <> 1102
    UNION
    SELECT "role_id"
    FROM "shz"."sys_role"
    WHERE "role_name" IN ('管理员', '超级管理员')
) admin_role
JOIN "shz"."sys_menu" menu
  ON menu."perms" IN (
      'cms:publicJobFair:company:add',
      'cms:publicJobFair:company:remove',
      'cms:publicJobFair:job:add',
      'cms:publicJobFair:job:remove'
  )
WHERE NOT EXISTS (
    SELECT 1
    FROM "shz"."sys_role_menu" existing
    WHERE existing."role_id" = admin_role."role_id"
      AND existing."menu_id" = menu."menu_id"
);

-- 5. 管理员角色获得企业审核权限：包括已有公共招聘会列表权限的非企业角色
--    （当前线上为网格员 1103）以及超级管理员。企业角色不授予审核权限。
INSERT INTO "shz"."sys_role_menu" ("role_id", "menu_id")
SELECT DISTINCT admin_role."role_id", review_menu."menu_id"
FROM (
    SELECT DISTINCT role_menu."role_id"
    FROM "shz"."sys_role_menu" role_menu
    JOIN "shz"."sys_menu" list_menu
      ON list_menu."menu_id" = role_menu."menu_id"
     AND list_menu."perms" = 'cms:publicJobFair:list'
    WHERE role_menu."role_id" <> 1102
    UNION
    SELECT "role_id"
    FROM "shz"."sys_role"
    WHERE "role_name" IN ('管理员', '超级管理员')
) admin_role
CROSS JOIN (
    SELECT "menu_id"
    FROM "shz"."sys_menu"
    WHERE "perms" = 'cms:publicJobFair:review'
    ORDER BY "menu_id"
    LIMIT 1
) review_menu
WHERE NOT EXISTS (
    SELECT 1
    FROM "shz"."sys_role_menu" existing
    WHERE existing."role_id" = admin_role."role_id"
      AND existing."menu_id" = review_menu."menu_id"
);

COMMIT;

-- 验证：
-- SELECT column_name, data_type, column_default
-- FROM information_schema.columns
-- WHERE table_schema = 'shz' AND table_name = 'public_job_fair_company'
-- ORDER BY ordinal_position;
-- SELECT menu_id, menu_name, perms
-- FROM shz.sys_menu
-- WHERE perms LIKE 'cms:publicJobFair:%'
-- ORDER BY menu_id;
