-- ============================================================================
-- OARMS - 站内信模块 DDL
-- Database: DM8 (达梦)
-- Schema: OARMS
-- Version: V12.0.0
-- Date: 2026-06-16
-- Description: 站内信（系统消息）表，支持单人接收 + 角色广播
-- ============================================================================

-- ----------------------------------------------------------------------------
-- sys_internal_message - 站内信表
-- ----------------------------------------------------------------------------
CREATE TABLE OARMS.SYS_INTERNAL_MESSAGE (
    id                VARCHAR(50)   NOT NULL,
    pms_content       CLOB          NOT NULL,
    module_type       VARCHAR(20),
    biz_id            VARCHAR(50),
    recipient_id      VARCHAR(50),
    recipient_role    VARCHAR(50),
    unread            TINYINT       NOT NULL DEFAULT 1,
    read_time         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_SIM_RECIPIENT_ID ON OARMS.SYS_INTERNAL_MESSAGE (RECIPIENT_ID, UNREAD);
CREATE INDEX IDX_SIM_RECIPIENT_ROLE ON OARMS.SYS_INTERNAL_MESSAGE (RECIPIENT_ROLE, UNREAD);
CREATE INDEX IDX_SIM_CREATE_TIME ON OARMS.SYS_INTERNAL_MESSAGE (CREATE_TIME);

COMMENT ON TABLE OARMS.SYS_INTERNAL_MESSAGE IS '站内信';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.ID IS '主键ID';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.PMS_CONTENT IS '消息内容(HTML片段，前端DOMPurify净化)';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.MODULE_TYPE IS '业务模块类型(CW-3/CW-4/MR-1/AM-2等)';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.BIZ_ID IS '关联业务记录ID(线索ID/规则ID等)';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.RECIPIENT_ID IS '接收人用户ID(与RECIPIENT_ROLE二选一)';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.RECIPIENT_ROLE IS '接收人角色编码(角色广播，与RECIPIENT_ID二选一)';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.UNREAD IS '未读标记(1=未读,0=已读)';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.READ_TIME IS '已读时间';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.CREATE_BY IS '创建人ID';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.CREATE_TIME IS '创建时间';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.CREATE_NAME IS '创建人姓名';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.UPDATE_BY IS '更新人ID';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.UPDATE_TIME IS '更新时间';
COMMENT ON COLUMN OARMS.SYS_INTERNAL_MESSAGE.UPDATE_NAME IS '更新人姓名';
