-- 场地展位图与户外招聘会展位预定
-- 瀚高数据库 / PostgreSQL 兼容

CREATE SEQUENCE IF NOT EXISTS "cms_venue_booth_id_seq" START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
CREATE TABLE IF NOT EXISTS "cms_venue_booth" (
    "id" BIGINT NOT NULL DEFAULT nextval('"cms_venue_booth_id_seq"') 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 "cms_venue_booth" IS '场地展位图';
COMMENT ON COLUMN "cms_venue_booth"."venue_id" IS '场地ID';
COMMENT ON COLUMN "cms_venue_booth"."booth_number" IS '展位号';
COMMENT ON COLUMN "cms_venue_booth"."position_x" IS 'X坐标';
COMMENT ON COLUMN "cms_venue_booth"."position_y" IS 'Y坐标';

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

CREATE SEQUENCE IF NOT EXISTS "cms_outdoor_fair_booth_booking_id_seq" START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
CREATE TABLE IF NOT EXISTS "cms_outdoor_fair_booth_booking" (
    "id" BIGINT NOT NULL DEFAULT nextval('"cms_outdoor_fair_booth_booking_id_seq"') PRIMARY KEY,
    "fair_id" BIGINT NOT NULL,
    "booth_id" BIGINT NOT NULL,
    "booth_number" VARCHAR(50) NOT NULL,
    "company_id" BIGINT,
    "company_name" VARCHAR(200),
    "status" VARCHAR(30) DEFAULT 'reserved',
    "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 "cms_outdoor_fair_booth_booking" IS '户外招聘会展位预定';
CREATE UNIQUE INDEX IF NOT EXISTS "uk_cms_outdoor_fair_booth_booking" ON "cms_outdoor_fair_booth_booking" ("fair_id", "booth_id");
CREATE INDEX IF NOT EXISTS "idx_cms_outdoor_fair_booth_booking_fair" ON "cms_outdoor_fair_booth_booking" ("fair_id");

WITH target_venue AS (
    SELECT "id" AS venue_id
    FROM "cms_venue_info"
    WHERE "venue_name" = '石河子市人民广场户外招聘区'
    LIMIT 1
),
seed AS (
    SELECT
        target_venue.venue_id,
        'KJ' || LPAD(gs::text, 2, '0') AS booth_number,
        40 + ((gs - 1) % 20) * 54 AS position_x,
        40 + ((gs - 1) / 20) * 54 AS position_y
    FROM target_venue
    CROSS JOIN generate_series(1, 80) gs
)
INSERT INTO "cms_venue_booth" ("venue_id", "booth_number", "position_x", "position_y", "create_by", "create_time", "del_flag")
SELECT seed.venue_id, seed.booth_number, seed.position_x, seed.position_y, 'system', NOW(), '0'
FROM seed
WHERE NOT EXISTS (
    SELECT 1
    FROM "cms_venue_booth" existing
    WHERE existing."venue_id" = seed.venue_id
      AND existing."booth_number" = seed.booth_number
);

UPDATE "cms_venue_info" venue
SET "floor_count" = booth_counts.total
FROM (
    SELECT "venue_id", COUNT(*) AS total
    FROM "cms_venue_booth"
    GROUP BY "venue_id"
) booth_counts
WHERE venue."id" = booth_counts."venue_id";

UPDATE "cms_outdoor_fair" fair
SET "booth_count" = venue."floor_count"
FROM "cms_venue_info" venue
WHERE fair."venue_id" = venue."id";
