-- ============================================================================
-- OARMS - CW-3 监测线索管理 DDL
-- Database: DM8 (达梦)
-- Schema: OARMS
-- Version: V9.0.0
-- Date: 2026-05-18
-- Description: 监测线索表 + 线索生成日志表
-- ============================================================================

-- ----------------------------------------------------------------------------
-- 1. cw_monitoring_clue - 监测线索
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.CW_MONITORING_CLUE (
    id                      VARCHAR(50)    NOT NULL,
    clue_code               VARCHAR(20)    NOT NULL,
    evidence_id             VARCHAR(50)    NOT NULL,
    screen_id               VARCHAR(50)    NOT NULL,
    screen_name             VARCHAR(100)   NOT NULL,
    screen_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),
    related_rules           CLOB           NOT NULL,
    related_law_clauses     CLOB           NOT NULL,
    video_evidence_path     VARCHAR(500)   NOT NULL,
    clip_start_time         VARCHAR(8)     NOT NULL,
    clip_end_time           VARCHAR(8)     NOT NULL,
    clip_duration           INT            NOT NULL,
    clue_status             TINYINT        NOT NULL DEFAULT 1,
    generated_by            VARCHAR(100)   NOT NULL,
    generated_at            TIMESTAMP      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    transferred_at          TIMESTAMP,
    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_CLUE_CODE_UK ON OARMS.CW_MONITORING_CLUE (CLUE_CODE);
CREATE UNIQUE INDEX IDX_CW_CLUE_EVID_UK ON OARMS.CW_MONITORING_CLUE (EVIDENCE_ID);

-- 普通索引
CREATE INDEX IDX_CW_CLUE_SCREEN ON OARMS.CW_MONITORING_CLUE (SCREEN_ID);
CREATE INDEX IDX_CW_CLUE_DISTRICT ON OARMS.CW_MONITORING_CLUE (DISTRICT);
CREATE INDEX IDX_CW_CLUE_STATUS ON OARMS.CW_MONITORING_CLUE (CLUE_STATUS);
CREATE INDEX IDX_CW_CLUE_GEN_AT ON OARMS.CW_MONITORING_CLUE (GENERATED_AT);

-- 表注释
COMMENT ON TABLE OARMS.CW_MONITORING_CLUE IS '监测线索';

-- 列注释
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.ID IS '主键ID';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CLUE_CODE IS '线索编码(格式: XS-{YYYYMMDD}-{seq3})';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.EVIDENCE_ID IS '关联证据ID';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.SCREEN_ID IS '大屏ID';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.SCREEN_NAME IS '大屏名称(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.SCREEN_ADDRESS IS '大屏地址(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.DISTRICT IS '所属区域(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.OWNER_UNIT IS '产权单位(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.OWNER_CONTACT IS '产权联系人(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.OPERATOR_UNIT IS '运营单位(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.OPERATOR_CONTACT IS '运营联系人(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.ADVERTISER IS '广告主(冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.RELATED_RULES IS '关联规则(JSON数组)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.RELATED_LAW_CLAUSES IS '关联法条(JSON数组)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.VIDEO_EVIDENCE_PATH IS '视频证据路径';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CLIP_START_TIME IS '片段起始时间(HH:MM:SS,冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CLIP_END_TIME IS '片段结束时间(HH:MM:SS,冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CLIP_DURATION IS '片段时长(秒,冗余)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CLUE_STATUS IS '线索状态(1=待转办,2=已转办,3=处理中,4=已办结)';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.GENERATED_BY IS '生成人';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.GENERATED_AT IS '生成时间';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.TRANSFERRED_AT IS '转办时间';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.CW_MONITORING_CLUE.UPDATE_NAME IS '更新人姓名';

-- ----------------------------------------------------------------------------
-- 2. cw_clue_generation_log - 线索生成日志
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.CW_CLUE_GENERATION_LOG (
    id                  VARCHAR(50)    NOT NULL,
    clue_id             VARCHAR(50)    NOT NULL,
    evidence_id         VARCHAR(50)    NOT NULL,
    operation_type      VARCHAR(50)    NOT NULL,
    operation_detail    CLOB           NOT NULL,
    operation_result    VARCHAR(20)    NOT NULL,
    error_message       CLOB,
    operated_by         VARCHAR(100)   NOT NULL,
    operated_at         TIMESTAMP      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

-- 普通索引
CREATE INDEX IDX_CW_CLUE_LOG_CLUE ON OARMS.CW_CLUE_GENERATION_LOG (CLUE_ID);
CREATE INDEX IDX_CW_CLUE_LOG_EVID ON OARMS.CW_CLUE_GENERATION_LOG (EVIDENCE_ID);
CREATE INDEX IDX_CW_CLUE_LOG_TYPE ON OARMS.CW_CLUE_GENERATION_LOG (OPERATION_TYPE);
CREATE INDEX IDX_CW_CLUE_LOG_TIME ON OARMS.CW_CLUE_GENERATION_LOG (OPERATED_AT);

-- 表注释
COMMENT ON TABLE OARMS.CW_CLUE_GENERATION_LOG IS '线索生成日志';

-- 列注释
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.ID IS '主键ID';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.CLUE_ID IS '线索ID';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.EVIDENCE_ID IS '证据ID';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.OPERATION_TYPE IS '操作类型(INFO_AGGREGATE/CODE_GENERATE/VALIDATE/CONFIRM/STATUS_UPDATE)';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.OPERATION_DETAIL IS '操作详情';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.OPERATION_RESULT IS '操作结果(SUCCESS/FAILED/WARNING)';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.ERROR_MESSAGE IS '错误信息';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.OPERATED_BY IS '操作人';
COMMENT ON COLUMN OARMS.CW_CLUE_GENERATION_LOG.OPERATED_AT IS '操作时间';
