-- 消息表模式事务消息数据库表结构

-- 业务数据表
CREATE TABLE IF NOT EXISTS business_data (
    id VARCHAR(36) PRIMARY KEY COMMENT '业务数据ID',
    data TEXT NOT NULL COMMENT '业务数据内容',
    status VARCHAR(20) NOT NULL DEFAULT 'PENDING' COMMENT '业务数据状态',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_status (status),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='业务数据表';

-- 消息表
CREATE TABLE IF NOT EXISTS message_table (
    id VARCHAR(36) PRIMARY KEY COMMENT '消息ID',
    content TEXT NOT NULL COMMENT '消息内容',
    type VARCHAR(50) NOT NULL COMMENT '消息类型',
    sender VARCHAR(100) NOT NULL COMMENT '发送者',
    status VARCHAR(20) NOT NULL DEFAULT 'PENDING' COMMENT '消息状态',
    retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
    error_message TEXT COMMENT '错误信息',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_status (status),
    INDEX idx_created_at (created_at),
    INDEX idx_updated_at (updated_at),
    INDEX idx_retry_count (retry_count)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='消息表';

-- 死信队列表
CREATE TABLE IF NOT EXISTS dead_letter_queue (
    id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '死信ID',
    message_id VARCHAR(36) NOT NULL COMMENT '原始消息ID',
    content TEXT NOT NULL COMMENT '消息内容',
    type VARCHAR(50) NOT NULL COMMENT '消息类型',
    sender VARCHAR(100) NOT NULL COMMENT '发送者',
    error_message TEXT COMMENT '错误信息',
    retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_message_id (message_id),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='死信队列表';

-- 消息发送日志表
CREATE TABLE IF NOT EXISTS message_send_log (
    id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '日志ID',
    message_id VARCHAR(36) NOT NULL COMMENT '消息ID',
    action VARCHAR(50) NOT NULL COMMENT '操作类型',
    status VARCHAR(20) NOT NULL COMMENT '操作状态',
    error_message TEXT COMMENT '错误信息',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_message_id (message_id),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='消息发送日志表';

