-- 户外招聘会企业签到与未参会失信统计
-- 瀚高数据库 / PostgreSQL 兼容；可重复执行。
-- 执行状态：测试环境已执行
-- 测试环境：test
-- 测试执行时间：2026-07-24 12:23:44 +0800
-- 正式环境：未执行

BEGIN;

SET LOCAL search_path = shz, public;

-- 兼容尚未执行招聘会报名审核迁移的旧库；已存在字段不会被覆盖。
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;

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

CREATE TABLE IF NOT EXISTS "shz"."cms_outdoor_fair_company_attendance" (
    "id"                BIGINT NOT NULL DEFAULT nextval('"shz"."cms_outdoor_fair_company_attendance_id_seq"'::regclass) PRIMARY KEY,
    "fair_id"           BIGINT NOT NULL,
    "company_id"        BIGINT NOT NULL,
    "company_name"      VARCHAR(255),
    "contact_person"    VARCHAR(64),
    "contact_phone"     VARCHAR(64),
    "attendance_status" CHAR(1) NOT NULL DEFAULT '0',
    "check_in_time"     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_attendance" IS '户外招聘会企业签到记录表';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."fair_id" IS '招聘会ID';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."company_id" IS '企业ID';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."company_name" IS '企业名称快照';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."contact_person" IS '联系人快照';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."contact_phone" IS '联系电话快照';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."attendance_status" IS '签到状态：0未签到，1已签到';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."check_in_time" IS '签到时间';
COMMENT ON COLUMN "shz"."cms_outdoor_fair_company_attendance"."del_flag" IS '删除标志（0代表存在 2代表删除）';

CREATE UNIQUE INDEX IF NOT EXISTS "uk_cms_outdoor_fair_company_attendance"
    ON "shz"."cms_outdoor_fair_company_attendance" ("fair_id", "company_id")
    WHERE "del_flag" = '0';
CREATE INDEX IF NOT EXISTS "idx_cms_outdoor_fair_company_attendance_fair_status"
    ON "shz"."cms_outdoor_fair_company_attendance" ("fair_id", "attendance_status");
CREATE INDEX IF NOT EXISTS "idx_cms_outdoor_fair_company_attendance_company"
    ON "shz"."cms_outdoor_fair_company_attendance" ("company_id");

-- 为历史审核通过的户外招聘会报名补齐“未签到”初始记录。
INSERT INTO "shz"."cms_outdoor_fair_company_attendance" (
    "fair_id", "company_id", "company_name", "contact_person", "contact_phone",
    "attendance_status", "create_by", "create_time", "del_flag"
)
SELECT fc."job_fair_id",
       fc."company_id",
       c."name",
       c."contact_person",
       c."contact_person_phone",
       '0',
       'system',
       CURRENT_TIMESTAMP,
       '0'
  FROM "shz"."fair_company" fc
  JOIN "shz"."company" c ON c."company_id" = fc."company_id" AND c."del_flag" = '0'
 WHERE fc."del_flag" = '0'
   AND fc."review_status" = '1'
   AND NOT EXISTS (
       SELECT 1
         FROM "shz"."cms_outdoor_fair_company_attendance" existing
        WHERE existing."fair_id" = fc."job_fair_id"
          AND existing."company_id" = fc."company_id"
          AND existing."del_flag" = '0'
   );

-- 企业签到接口使用独立按钮权限；把权限授给已有户外招聘会列表权限的角色。
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", 6, '', '', '', '',
    1, 0, 'F', '0', '0', 'cms:outdoorFair:companyCheckIn', '#', '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:companyCheckIn'
  )
ORDER BY list_menu."menu_id"
LIMIT 1;

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

COMMIT;
