-- ============================================================================
-- OARMS - CW-1 固化取证管理 DDL
-- Database: DM8 (达梦)
-- Schema: OARMS
-- Version: V7.0.0
-- Date: 2026-05-18
-- Description: 固化取证记录表 + 证据状态变更历史表
-- ============================================================================

-- ----------------------------------------------------------------------------
-- 1. cw_evidence_record - 固化取证记录
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.cw_evidence_record (
    id                      VARCHAR(50)    NOT NULL,
    monitor_record_id       VARCHAR(50)    NOT NULL,
    screen_id               VARCHAR(50)    NOT NULL,
    screen_name             VARCHAR(200)   NOT NULL,
    screen_address          VARCHAR(500)   NOT NULL,
    district                VARCHAR(50)    NOT NULL,
    evidence_video_file     VARCHAR(500)   NOT NULL,
    clip_start_time         VARCHAR(8)     NOT NULL,
    clip_end_time           VARCHAR(8)     NOT NULL,
    clip_duration           INT            NOT NULL,
    evidence_status         TINYINT        NOT NULL DEFAULT 1,
    evidence_person         VARCHAR(100)   NOT NULL,
    evidenced_at            TIMESTAMP      NOT NULL,
    source_monitor_person   VARCHAR(100)   NOT NULL,
    source_monitor_time     TIMESTAMP,
    source_monitor_remark   VARCHAR(500),
    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_CW_EVIDENCE_VIDEO_UK ON OARMS.CW_EVIDENCE_RECORD (EVIDENCE_VIDEO_FILE);

-- 普通索引
CREATE INDEX IDX_CW_EVIDENCE_MONITOR ON OARMS.CW_EVIDENCE_RECORD (MONITOR_RECORD_ID);
CREATE INDEX IDX_CW_EVIDENCE_SCREEN ON OARMS.CW_EVIDENCE_RECORD (SCREEN_ID);
CREATE INDEX IDX_CW_EVIDENCE_DISTRICT ON OARMS.CW_EVIDENCE_RECORD (DISTRICT);
CREATE INDEX IDX_CW_EVIDENCE_STATUS ON OARMS.CW_EVIDENCE_RECORD (EVIDENCE_STATUS);
CREATE INDEX IDX_CW_EVIDENCE_PERSON ON OARMS.CW_EVIDENCE_RECORD (EVIDENCE_PERSON);
CREATE INDEX IDX_CW_EVIDENCE_AT ON OARMS.CW_EVIDENCE_RECORD (EVIDENCED_AT);

-- 表注释
COMMENT ON TABLE OARMS.CW_EVIDENCE_RECORD IS '固化取证记录';

-- 列注释
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.ID IS '主键ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.MONITOR_RECORD_ID IS '关联监测记录ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.SCREEN_ID IS '大屏ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.SCREEN_NAME IS '大屏名称(冗余)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.SCREEN_ADDRESS IS '大屏地址(冗余)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.DISTRICT IS '所属区域(冗余)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.EVIDENCE_VIDEO_FILE IS '取证视频文件路径';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.CLIP_START_TIME IS '片段起始时间(HH:MM:SS)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.CLIP_END_TIME IS '片段结束时间(HH:MM:SS)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.CLIP_DURATION IS '片段时长(秒,>=3)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.EVIDENCE_STATUS IS '取证状态(1=待关联规则,2=已关联规则,3=已生成线索)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.EVIDENCE_PERSON IS '取证人';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.EVIDENCED_AT IS '取证时间';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.SOURCE_MONITOR_PERSON IS '来源监测人(冗余)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.SOURCE_MONITOR_TIME IS '来源监测时间(冗余)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.SOURCE_MONITOR_REMARK IS '来源监测备注(冗余)';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_RECORD.UPDATE_NAME IS '更新人姓名';

-- ----------------------------------------------------------------------------
-- 2. cw_evidence_status_history - 证据状态变更历史
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.cw_evidence_status_history (
    id                VARCHAR(50)    NOT NULL,
    evidence_id       VARCHAR(50)    NOT NULL,
    from_status       TINYINT,
    to_status         TINYINT        NOT NULL,
    changed_at        TIMESTAMP      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    changed_by        VARCHAR(100)   NOT NULL,
    PRIMARY KEY (id)
);

-- 普通索引
CREATE INDEX IDX_CW_EVID_HIST_EVID ON OARMS.CW_EVIDENCE_STATUS_HISTORY (EVIDENCE_ID);
CREATE INDEX IDX_CW_EVID_HIST_TIME ON OARMS.CW_EVIDENCE_STATUS_HISTORY (CHANGED_AT);

-- 表注释
COMMENT ON TABLE OARMS.CW_EVIDENCE_STATUS_HISTORY IS '证据状态变更历史';

-- 列注释
COMMENT ON COLUMN OARMS.CW_EVIDENCE_STATUS_HISTORY.ID IS '主键ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_STATUS_HISTORY.EVIDENCE_ID IS '证据ID';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_STATUS_HISTORY.FROM_STATUS IS '变更前状态';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_STATUS_HISTORY.TO_STATUS IS '变更后状态';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_STATUS_HISTORY.CHANGED_AT IS '变更时间';
COMMENT ON COLUMN OARMS.CW_EVIDENCE_STATUS_HISTORY.CHANGED_BY IS '变更人';
