-- ============================================================================
-- OARMS - BS-1 大屏基础信息管理 DDL
-- Database: DM8 (达梦)
-- Schema: OARMS
-- Version: V1.0.0
-- Date: 2026-05-18
-- Description: 户外广告大屏核心表 + 历史版本表
-- ============================================================================

-- ----------------------------------------------------------------------------
-- 1. bs_screen - 户外广告大屏
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.bs_screen (
    id                VARCHAR(50)   NOT NULL,
    screen_code       VARCHAR(30)   NOT NULL,
    screen_name       VARCHAR(100)  NOT NULL,
    address           VARCHAR(200)  NOT NULL,
    district          VARCHAR(20)   NOT NULL,
    owner_unit        VARCHAR(100)  NOT NULL,
    owner_contact     VARCHAR(20),
    operator_unit     VARCHAR(100)  NOT NULL,
    operator_contact  VARCHAR(20),
    advertiser        VARCHAR(100),
    screen_spec       VARCHAR(50)   NOT NULL,
    ad_play_time_range VARCHAR(20)  NOT NULL,
    screen_status     TINYINT       NOT NULL DEFAULT 1,
    longitude         DECIMAL(10,6),
    latitude          DECIMAL(10,6),
    create_by         VARCHAR(50),
    create_time       TIMESTAMP,
    create_name       VARCHAR(50),
    update_by         VARCHAR(50),
    update_time       TIMESTAMP,
    update_name       VARCHAR(50),
    PRIMARY KEY (id)
);

-- 唯一约束
CREATE UNIQUE INDEX IDX_BS_SCREEN_CODE ON OARMS.BS_SCREEN (SCREEN_CODE);
CREATE UNIQUE INDEX IDX_BS_SCREEN_ADDRESS ON OARMS.BS_SCREEN (ADDRESS);

-- 普通索引
CREATE INDEX IDX_BS_SCREEN_DISTRICT ON OARMS.BS_SCREEN (DISTRICT);
CREATE INDEX IDX_BS_SCREEN_STATUS ON OARMS.BS_SCREEN (SCREEN_STATUS);

-- 表注释
COMMENT ON TABLE OARMS.BS_SCREEN IS '户外广告大屏';

-- 列注释
COMMENT ON COLUMN OARMS.BS_SCREEN.ID IS '主键ID';
COMMENT ON COLUMN OARMS.BS_SCREEN.SCREEN_CODE IS '大屏编码';
COMMENT ON COLUMN OARMS.BS_SCREEN.SCREEN_NAME IS '大屏名称';
COMMENT ON COLUMN OARMS.BS_SCREEN.ADDRESS IS '大屏地址';
COMMENT ON COLUMN OARMS.BS_SCREEN.DISTRICT IS '所属区域';
COMMENT ON COLUMN OARMS.BS_SCREEN.OWNER_UNIT IS '业主单位';
COMMENT ON COLUMN OARMS.BS_SCREEN.OWNER_CONTACT IS '业主联系方式';
COMMENT ON COLUMN OARMS.BS_SCREEN.OPERATOR_UNIT IS '运营单位';
COMMENT ON COLUMN OARMS.BS_SCREEN.OPERATOR_CONTACT IS '运营联系方式';
COMMENT ON COLUMN OARMS.BS_SCREEN.ADVERTISER IS '广告主';
COMMENT ON COLUMN OARMS.BS_SCREEN.SCREEN_SPEC IS '屏幕规格';
COMMENT ON COLUMN OARMS.BS_SCREEN.AD_PLAY_TIME_RANGE IS '广告播放时间段';
COMMENT ON COLUMN OARMS.BS_SCREEN.SCREEN_STATUS IS '状态(1=正常,2=停用,3=维修中)';
COMMENT ON COLUMN OARMS.BS_SCREEN.LONGITUDE IS '经度';
COMMENT ON COLUMN OARMS.BS_SCREEN.LATITUDE IS '纬度';
COMMENT ON COLUMN OARMS.BS_SCREEN.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.BS_SCREEN.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.BS_SCREEN.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.BS_SCREEN.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.BS_SCREEN.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.BS_SCREEN.UPDATE_NAME IS '更新人姓名';

-- ----------------------------------------------------------------------------
-- 2. bs_screen_history - 大屏历史版本
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.bs_screen_history (
    id              VARCHAR(50)   NOT NULL,
    screen_id       VARCHAR(50)   NOT NULL,
    snapshot_data   CLOB          NOT NULL,
    changed_at      TIMESTAMP     NOT NULL,
    create_by       VARCHAR(50),
    create_time     TIMESTAMP,
    create_name     VARCHAR(50),
    update_by       VARCHAR(50),
    update_time     TIMESTAMP,
    update_name     VARCHAR(50),
    PRIMARY KEY (id)
);

-- 索引
CREATE INDEX IDX_BS_SCREEN_HIST_SID ON OARMS.BS_SCREEN_HISTORY (SCREEN_ID);
CREATE INDEX IDX_BS_SCREEN_HIST_CAT ON OARMS.BS_SCREEN_HISTORY (CHANGED_AT);

-- 表注释
COMMENT ON TABLE OARMS.BS_SCREEN_HISTORY IS '大屏历史版本';

-- 列注释
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.ID IS '主键ID';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.SCREEN_ID IS '大屏ID';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.SNAPSHOT_DATA IS '快照数据(JSON)';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.CHANGED_AT IS '变更时间';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.BS_SCREEN_HISTORY.UPDATE_NAME IS '更新人姓名';
