CREATE TABLE IF NOT EXISTS im_group_messages ( id BIGINT NOT NULL AUTO_INCREMENT, topic VARCHAR(64) NOT NULL COMMENT '群 Topic,如 grp_xxx', group_id BIGINT NOT NULL COMMENT '业务群 ID,便于分区、治理和统计', seqid BIGINT NOT NULL COMMENT '群内消息序号,由 Redis 原子发号', from_user BIGINT NOT NULL COMMENT '发送人用户 ID', client_msg_id VARCHAR(64) NULL COMMENT '客户端幂等消息 ID', msg_type VARCHAR(32) NOT NULL DEFAULT 'text' COMMENT 'text/image/file/audio/video/system 等', status VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT 'normal/recalled/deleted/blocked', content JSON NULL COMMENT '消息正文或 Drafty 内容', head JSON NULL COMMENT '消息头,包含 @、引用、客户端能力等', mentioned_user_ids JSON NULL COMMENT '@ 用户列表,便于提醒和检索', mention_all TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否 @ 全体', reply_seqid BIGINT NULL COMMENT '回复的群消息 seq', root_seqid BIGINT NULL COMMENT '线程或合并上下文根消息 seq', file_count INT NOT NULL DEFAULT 0 COMMENT '附件数量', content_size INT NOT NULL DEFAULT 0 COMMENT '正文 JSON 序列化后的字节数', has_risk TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否命中风控或内容安全', audit_status VARCHAR(32) NOT NULL DEFAULT 'pass' COMMENT 'pass/pending/reject', version INT NOT NULL DEFAULT 1 COMMENT '编辑版本', recalled_at DATETIME(3) NULL, recalled_by BIGINT NULL, deleted_at DATETIME(3) NULL, expires_at DATETIME(3) NULL COMMENT '可选保留期,便于过期归档或清理', created_at DATETIME(3) NOT NULL, updated_at DATETIME(3) NOT NULL, PRIMARY KEY (id), UNIQUE KEY uk_im_group_messages_group_seq (group_id, seqid), UNIQUE KEY uk_im_group_messages_topic_seq (topic, seqid), UNIQUE KEY uk_im_group_messages_topic_sender_client_mid (topic, from_user, client_msg_id), KEY idx_im_group_messages_group_created (group_id, created_at), KEY idx_im_group_messages_group_status_seq (group_id, status, seqid), KEY idx_im_group_messages_topic_created (topic, created_at), KEY idx_im_group_messages_topic_from_seq (topic, from_user, seqid), KEY idx_im_group_messages_audit (audit_status, created_at), KEY idx_im_group_messages_expires (expires_at) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;