-- ============================================================================
-- OARMS - MR-1 监测规则管理 DDL
-- Database: DM8 (达梦)
-- Schema: OARMS
-- Version: V6.0.0
-- Date: 2026-05-18
-- Description: 监测规则表 + 规则-法条关联表 + 规则操作历史表
-- ============================================================================

-- ----------------------------------------------------------------------------
-- 1. mr_monitoring_rule - 监测规则
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.MR_MONITORING_RULE (
    id                VARCHAR(50)    NOT NULL,
    rule_code         VARCHAR(30)    NOT NULL,
    rule_name         VARCHAR(100)   NOT NULL,
    rule_content      VARCHAR(2000)  NOT NULL,
    scope_type        TINYINT        NOT NULL DEFAULT 1,
    scope_districts   CLOB,
    enable_status     TINYINT        NOT NULL DEFAULT 1,
    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_MR_RULE_CODE ON OARMS.MR_MONITORING_RULE (RULE_CODE);
CREATE UNIQUE INDEX IDX_MR_RULE_NAME ON OARMS.MR_MONITORING_RULE (RULE_NAME);

-- 普通索引
CREATE INDEX IDX_MR_RULE_ENABLE ON OARMS.MR_MONITORING_RULE (ENABLE_STATUS);
CREATE INDEX IDX_MR_RULE_SCOPE ON OARMS.MR_MONITORING_RULE (SCOPE_TYPE);

-- 表注释
COMMENT ON TABLE OARMS.MR_MONITORING_RULE IS '监测规则';

-- 列注释
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.ID IS '主键ID';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.RULE_CODE IS '规则编码(格式: MR-{YYYY}-{seq4})';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.RULE_NAME IS '规则名称';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.RULE_CONTENT IS '规则内容';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.SCOPE_TYPE IS '适用范围类型(1=全部大屏,2=指定区域)';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.SCOPE_DISTRICTS IS '适用区域列表(JSON数组,scope_type=1时为空)';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.ENABLE_STATUS IS '启用状态(1=已启用,2=已停用)';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.MR_MONITORING_RULE.UPDATE_NAME IS '更新人姓名';

-- ----------------------------------------------------------------------------
-- 2. mr_rule_law_clause_rel - 规则-法条关联
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.MR_RULE_LAW_CLAUSE_REL (
    id                VARCHAR(50)    NOT NULL,
    rule_id           VARCHAR(50)    NOT NULL,
    clause_id         VARCHAR(50)    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 UNIQUE INDEX IDX_MR_RULE_CLAUSE_UK ON OARMS.MR_RULE_LAW_CLAUSE_REL (RULE_ID, CLAUSE_ID);

-- 普通索引
CREATE INDEX IDX_MR_RULE_CLAUSE_RULE ON OARMS.MR_RULE_LAW_CLAUSE_REL (RULE_ID);
CREATE INDEX IDX_MR_RULE_CLAUSE_CLS ON OARMS.MR_RULE_LAW_CLAUSE_REL (CLAUSE_ID);

-- 表注释
COMMENT ON TABLE OARMS.MR_RULE_LAW_CLAUSE_REL IS '规则-法条关联';

-- 列注释
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.ID IS '主键ID';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.RULE_ID IS '规则ID';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.CLAUSE_ID IS '法条ID';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.MR_RULE_LAW_CLAUSE_REL.UPDATE_NAME IS '更新人姓名';

-- ----------------------------------------------------------------------------
-- 3. mr_rule_operation_history - 规则操作历史
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.MR_RULE_OPERATION_HISTORY (
    id                VARCHAR(50)    NOT NULL,
    rule_id           VARCHAR(50)    NOT NULL,
    operator          VARCHAR(100)   NOT NULL,
    operation_type    TINYINT        NOT NULL,
    before_change     CLOB,
    after_change      CLOB,
    operated_at       TIMESTAMP      NOT NULL DEFAULT CURRENT_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 INDEX IDX_MR_RULE_HIST_RULE ON OARMS.MR_RULE_OPERATION_HISTORY (RULE_ID);
CREATE INDEX IDX_MR_RULE_HIST_TYPE ON OARMS.MR_RULE_OPERATION_HISTORY (OPERATION_TYPE);
CREATE INDEX IDX_MR_RULE_HIST_TIME ON OARMS.MR_RULE_OPERATION_HISTORY (OPERATED_AT);

-- 表注释
COMMENT ON TABLE OARMS.MR_RULE_OPERATION_HISTORY IS '规则操作历史';

-- 列注释
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.ID IS '主键ID';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.RULE_ID IS '规则ID';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.OPERATOR IS '操作人';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.OPERATION_TYPE IS '操作类型(1=新增,2=编辑,3=删除,4=启用,5=停用)';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.BEFORE_CHANGE IS '变更前快照(JSON)';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.AFTER_CHANGE IS '变更后快照(JSON)';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.OPERATED_AT IS '操作时间';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.MR_RULE_OPERATION_HISTORY.UPDATE_NAME IS '更新人姓名';
