-- 场地展位图模板：只保存固定布局，不保存企业、预定状态或预定记录。
-- 瀚高数据库 / PostgreSQL 兼容；可重复执行。
-- 所有对象显式建在 shz schema，避免 psql 会话的默认 public schema 造成业务不可见。

-- 清理由本 migration 早期未设置 search_path 可能留下的空 public 表；如发现有数据则停止，绝不静默删除。
DO $$
BEGIN
    IF to_regclass('public.cms_venue_booth') IS NOT NULL THEN
        IF EXISTS (SELECT 1 FROM public.cms_venue_booth) THEN
            RAISE EXCEPTION 'public.cms_venue_booth contains data; please migrate it deliberately before running this migration';
        END IF;
        DROP TABLE public.cms_venue_booth;
    END IF;
    IF to_regclass('public.cms_venue_booth_id_seq') IS NOT NULL THEN
        DROP SEQUENCE public.cms_venue_booth_id_seq;
    END IF;
END $$;

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

CREATE TABLE IF NOT EXISTS shz.cms_venue_booth (
    "id" BIGINT NOT NULL DEFAULT nextval('shz.cms_venue_booth_id_seq'::regclass) PRIMARY KEY,
    "venue_id" BIGINT NOT NULL,
    "booth_number" VARCHAR(50) NOT NULL,
    "position_x" INTEGER NOT NULL DEFAULT 0,
    "position_y" INTEGER NOT NULL DEFAULT 0,
    "create_by" VARCHAR(64) DEFAULT '',
    "create_time" TIMESTAMP(0),
    "update_by" VARCHAR(64) DEFAULT '',
    "update_time" TIMESTAMP(0),
    "remark" VARCHAR(500),
    "del_flag" CHAR(1) DEFAULT '0'
);

COMMENT ON TABLE shz.cms_venue_booth IS '场地展位图模板';
COMMENT ON COLUMN shz.cms_venue_booth."venue_id" IS '场地ID';
COMMENT ON COLUMN shz.cms_venue_booth."booth_number" IS '展位号';
COMMENT ON COLUMN shz.cms_venue_booth."position_x" IS 'X坐标';
COMMENT ON COLUMN shz.cms_venue_booth."position_y" IS 'Y坐标';

CREATE UNIQUE INDEX IF NOT EXISTS uk_cms_venue_booth_number ON shz.cms_venue_booth ("venue_id", "booth_number");
CREATE INDEX IF NOT EXISTS idx_cms_venue_booth_venue ON shz.cms_venue_booth ("venue_id");
