-- 场地信息维护表、字典、菜单
-- 瀚高数据库 / PostgreSQL 兼容
-- 可重复执行。

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

CREATE TABLE IF NOT EXISTS "cms_venue_info" (
    "id" BIGINT NOT NULL DEFAULT nextval('"cms_venue_info_id_seq"') PRIMARY KEY,
    "venue_type" VARCHAR(100) NOT NULL,
    "venue_name" VARCHAR(200) NOT NULL,
    "venue_area" NUMERIC(12, 2),
    "floor_count" INTEGER,
    "venue_address" VARCHAR(500) NOT NULL,
    "contact_phone" VARCHAR(50),
    "route_line_ids" VARCHAR(500),
    "route_line_names" VARCHAR(1000),
    "handle_date" DATE,
    "handle_org" VARCHAR(200),
    "handler" VARCHAR(100),
    "create_by" VARCHAR(64) DEFAULT '',
    "create_time" TIMESTAMP(0),
    "update_by" VARCHAR(64) DEFAULT '',
    "update_time" TIMESTAMP(0),
    "remark" VARCHAR(100),
    "del_flag" CHAR(1) DEFAULT '0'
);

COMMENT ON TABLE "cms_venue_info" IS '场地信息维护表';
COMMENT ON COLUMN "cms_venue_info"."id" IS '主键ID';
COMMENT ON COLUMN "cms_venue_info"."venue_type" IS '场地类型';
COMMENT ON COLUMN "cms_venue_info"."venue_name" IS '场地名称';
COMMENT ON COLUMN "cms_venue_info"."venue_area" IS '场地面积';
COMMENT ON COLUMN "cms_venue_info"."floor_count" IS '展位数量';
COMMENT ON COLUMN "cms_venue_info"."venue_address" IS '场地地址';
COMMENT ON COLUMN "cms_venue_info"."contact_phone" IS '联系电话';
COMMENT ON COLUMN "cms_venue_info"."route_line_ids" IS '乘坐线路ID，多个用英文逗号分隔';
COMMENT ON COLUMN "cms_venue_info"."route_line_names" IS '乘坐线路名称，多个用英文逗号分隔';
COMMENT ON COLUMN "cms_venue_info"."handle_date" IS '经办日期';
COMMENT ON COLUMN "cms_venue_info"."handle_org" IS '经办机构';
COMMENT ON COLUMN "cms_venue_info"."handler" IS '经办人';
COMMENT ON COLUMN "cms_venue_info"."remark" IS '备注';
COMMENT ON COLUMN "cms_venue_info"."del_flag" IS '删除标志（0代表存在 2代表删除）';

CREATE INDEX IF NOT EXISTS "idx_cms_venue_info_type" ON "cms_venue_info" ("venue_type");
CREATE INDEX IF NOT EXISTS "idx_cms_venue_info_name" ON "cms_venue_info" ("venue_name");
CREATE INDEX IF NOT EXISTS "idx_cms_venue_info_del_flag" ON "cms_venue_info" ("del_flag");

INSERT INTO "sys_dict_type" (
    "dict_id", "dict_name", "dict_type", "status", "create_by", "create_time", "remark"
)
SELECT
    (SELECT COALESCE(MAX("dict_id"), 0) + 1 FROM "sys_dict_type"),
    '场地类型',
    'venue_info_type',
    '0',
    'system',
    NOW(),
    '场地信息维护使用'
WHERE NOT EXISTS (
    SELECT 1 FROM "sys_dict_type" WHERE "dict_type" = 'venue_info_type'
);

WITH seed("dict_sort", "dict_label", "dict_value", "list_class", "is_default") AS (
    VALUES
        (1, '智能化招聘场馆', '智能化招聘场馆', 'primary', 'Y'),
        (2, '户外招聘广场', '户外招聘广场', 'success', 'N'),
        (3, '高校招聘场地', '高校招聘场地', 'processing', 'N'),
        (4, '园区招聘场地', '园区招聘场地', 'cyan', 'N'),
        (5, '临时招聘场地', '临时招聘场地', 'warning', 'N'),
        (6, '其他', '其他', 'default', 'N')
),
new_rows AS (
    SELECT
        ROW_NUMBER() OVER (ORDER BY seed."dict_sort") AS rn,
        seed.*
    FROM seed
    WHERE NOT EXISTS (
        SELECT 1
        FROM "sys_dict_data" data
        WHERE data."dict_type" = 'venue_info_type'
          AND data."dict_value" = seed."dict_value"
    )
),
base AS (
    SELECT COALESCE(MAX("dict_code"), 0) AS max_code FROM "sys_dict_data"
)
INSERT INTO "sys_dict_data" (
    "dict_code", "dict_sort", "dict_label", "dict_value", "dict_type",
    "css_class", "list_class", "is_default", "status", "create_by", "create_time", "remark"
)
SELECT
    base.max_code + new_rows.rn,
    new_rows."dict_sort",
    new_rows."dict_label",
    new_rows."dict_value",
    'venue_info_type',
    NULL,
    new_rows."list_class",
    new_rows."is_default",
    '0',
    'system',
    NOW(),
    '场地类型'
FROM new_rows
CROSS JOIN base;

WITH parent_menu AS (
    SELECT "menu_id" FROM "sys_menu" WHERE "menu_id" = 2055
),
inserted_page AS (
    INSERT INTO "sys_menu" (
        "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", "remark"
    )
    SELECT
        '场地信息维护',
        parent_menu."menu_id",
        2,
        'jobfair/venueinfo',
        'jobfair/venueinfo/index',
        NULL,
        '',
        1,
        0,
        'C',
        '0',
        '0',
        'cms:venueInfo:list',
        '#',
        'system',
        NOW(),
        '场地信息维护菜单'
    FROM parent_menu
    WHERE NOT EXISTS (
        SELECT 1 FROM "sys_menu" WHERE "parent_id" = parent_menu."menu_id" AND "menu_name" = '场地信息维护'
    )
    RETURNING "menu_id"
),
page_menu AS (
    SELECT "menu_id" FROM inserted_page
    UNION
    SELECT "menu_id" FROM "sys_menu" WHERE "parent_id" = 2055 AND "menu_name" = '场地信息维护'
),
button_seed("menu_name", "order_num", "perms") AS (
    VALUES
        ('场地信息查询', 1, 'cms:venueInfo:query'),
        ('场地信息新增', 2, 'cms:venueInfo:add'),
        ('场地信息修改', 3, 'cms:venueInfo:edit'),
        ('场地信息删除', 4, 'cms:venueInfo:remove')
)
INSERT INTO "sys_menu" (
    "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", "remark"
)
SELECT
    button_seed."menu_name",
    page_menu."menu_id",
    button_seed."order_num",
    '',
    NULL,
    NULL,
    '',
    1,
    0,
    'F',
    '0',
    '0',
    button_seed."perms",
    '#',
    'system',
    NOW(),
    '场地信息维护按钮'
FROM page_menu
CROSS JOIN button_seed
WHERE NOT EXISTS (
    SELECT 1 FROM "sys_menu" WHERE "perms" = button_seed."perms"
);

WITH outdoor_roles AS (
    SELECT DISTINCT "role_id"
    FROM "sys_role_menu"
    WHERE "menu_id" = 2111
),
venue_menus AS (
    SELECT "menu_id"
    FROM "sys_menu"
    WHERE ("parent_id" = 2055 AND "menu_name" = '场地信息维护')
       OR "perms" IN (
           'cms:venueInfo:query',
           'cms:venueInfo:add',
           'cms:venueInfo:edit',
           'cms:venueInfo:remove'
       )
),
role_menu_rows AS (
    SELECT outdoor_roles."role_id", venue_menus."menu_id"
    FROM outdoor_roles
    CROSS JOIN venue_menus
)
INSERT INTO "sys_role_menu" ("role_id", "menu_id")
SELECT role_menu_rows."role_id", role_menu_rows."menu_id"
FROM role_menu_rows
WHERE NOT EXISTS (
    SELECT 1
    FROM "sys_role_menu" existing
    WHERE existing."role_id" = role_menu_rows."role_id"
      AND existing."menu_id" = role_menu_rows."menu_id"
);
