-- 户外招聘会企业退订：企业退订后移除参会关系，并保留退订快照。
-- 瀚高数据库 / PostgreSQL 兼容；可重复执行。
-- 执行状态：测试环境已执行
-- 测试环境：test
-- 测试执行时间：2026-07-24 11:46:48 +0800
-- 正式环境：未执行

BEGIN;

SET LOCAL search_path = shz, public;

CREATE SEQUENCE IF NOT EXISTS "shz"."cms_outdoor_fair_company_unsubscribe_id_seq"
    START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;

CREATE TABLE IF NOT EXISTS "shz"."cms_outdoor_fair_company_unsubscribe" (
    "id"                BIGINT NOT NULL DEFAULT nextval('"shz"."cms_outdoor_fair_company_unsubscribe_id_seq"'::regclass) PRIMARY KEY,
    "fair_id"           BIGINT NOT NULL,
    "company_id"        BIGINT NOT NULL,
    "company_name"      VARCHAR(255) NOT NULL,
    "contact_person"    VARCHAR(64),
    "contact_phone"     VARCHAR(64),
    "reason"            VARCHAR(500) NOT NULL,
    "unsubscribe_time"  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "create_by"         VARCHAR(64) DEFAULT '',
    "create_time"       TIMESTAMP,
    "update_by"         VARCHAR(64) DEFAULT '',
    "update_time"       TIMESTAMP,
    "remark"            VARCHAR(500),
    "del_flag"          CHAR(1) DEFAULT '0'
);

COMMENT ON TABLE "shz"."cms_outdoor_fair_company_unsubscribe" IS '户外招聘会企业退订记录表';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."fair_id" IS '招聘会ID';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."company_id" IS '企业ID';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."company_name" IS '企业名称快照';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."contact_person" IS '联系人快照';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."contact_phone" IS '联系电话快照';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."reason" IS '退订原因';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."unsubscribe_time" IS '退订时间';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_unsubscribe"."del_flag" IS '删除标志（0代表存在 2代表删除）';

CREATE INDEX IF NOT EXISTS "idx_cms_outdoor_fair_company_unsubscribe_fair_time"
    ON "shz"."cms_outdoor_fair_company_unsubscribe" ("fair_id", "unsubscribe_time" DESC);
CREATE INDEX IF NOT EXISTS "idx_cms_outdoor_fair_company_unsubscribe_company"
    ON "shz"."cms_outdoor_fair_company_unsubscribe" ("company_id");

-- 企业端退订按钮权限。授权给已有户外招聘会列表权限的角色，覆盖企业角色和管理角色。
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", 5, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:outdoorFair:unsubscribe', '#', 'system', 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:unsubscribe'
  )
ORDER BY list_menu."menu_id"
LIMIT 1;

INSERT INTO "shz"."sys_role_menu" ("role_id", "menu_id")
SELECT DISTINCT role_menu."role_id", unsubscribe_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" unsubscribe_menu
  ON unsubscribe_menu."perms" = 'cms:outdoorFair:unsubscribe'
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" = unsubscribe_menu."menu_id"
  );

COMMIT;
