# 消息实体设计 **本文档引用的文件** - [ChatMessage.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.IM/LINGYUN/Abp/IM/Messages/ChatMessage.cs) - [MessageType.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.IM/LINGYUN/Abp/IM/Messages/MessageType.cs) - [MessageStore.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.MessageService.Domain/LINGYUN/Abp/MessageService/Chat/MessageStore.cs) - [IMessageStore.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.IM/LINGYUN/Abp/IM/Messages/IMessageStore.cs) - [IDistributedIdGenerator.cs](file://aspnet-core/framework/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/IDistributedIdGenerator.cs) - [SnowflakeIdGenerator.cs](file://aspnet-core/framework/common/LINGYUN.Abp.IdGenerator/LINGYUN/Abp/IdGenerator/Snowflake/SnowflakeIdGenerator.cs) - [MessageState.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.IM/LINGYUN/Abp/IM/Messages/MessageState.cs) - [MessageSourceType.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.IM/LINGYUN/Abp/IM/Messages/MessageSourceType.cs) - [Initial-Realtime-Message.cs](file://aspnet-core/migrations/LY.MicroService.RealtimeMessage.EntityFrameworkCore/Migrations/20230110063428_Initial-Realtime-Message.cs) ## 目录 1. [引言](#引言) 2. [消息实体字段定义](#消息实体字段定义) 3. [消息内容存储策略](#消息内容存储策略) 4. [消息ID生成策略](#消息id生成策略) 5. [时间戳处理机制](#时间戳处理机制) 6. [消息类型枚举设计](#消息类型枚举设计) 7. [消息分片与索引优化](#消息分片与索引优化) 8. [消息状态管理](#消息状态管理) 9. [消息存储实现](#消息存储实现) 10. [实体关系分析](#实体关系分析) 11. [扩展指导](#扩展指导) ## 引言 本文档详细说明了实时消息模块中消息实体的设计与实现。文档涵盖了消息实体的字段定义、数据类型、约束条件和业务规则,解释了消息内容存储策略、消息分片机制和索引优化方案。同时,文档还介绍了消息ID生成策略、时间戳处理、消息类型枚举设计等关键技术实现,并为开发人员提供了消息实体扩展指导。 ## 消息实体字段定义 消息实体(ChatMessage)包含以下核心字段: | 字段名称 | 数据类型 | 约束条件 | 业务规则 | |---------|--------|---------|---------| | **TenantId** | Guid? | 可为空 | 租户标识,支持多租户架构 | | **GroupId** | string | 可为空 | 群组标识,为空时表示私聊消息 | | **MessageId** | string | 非空 | 消息唯一标识,由系统自动生成 | | **FormUserId** | Guid | 非空 | 发送者用户标识 | | **FormUserName** | string | 非空 | 发送者用户名 | | **ToUserId** | Guid? | 可为空 | 接收者用户标识,为空时表示群聊消息 | | **Content** | string | 非空 | 消息内容,最大长度1048576字符 | | **SendTime** | DateTime | 非空 | 消息发送时间,使用系统时钟 | | **IsAnonymous** | bool | 非空 | 是否匿名发送,存储在扩展属性中 | | **MessageType** | MessageType | 非空 | 消息类型,枚举值,默认为文本消息 | | **Source** | MessageSourceType | 非空 | 消息来源,用户或系统,默认为用户 | | **ExtraProperties** | ExtraPropertyDictionary | 非空 | 扩展属性字典,用于存储额外信息 | **Section sources** - [ChatMessage.cs](file://aspnet-core/modules/realtime-message/LINGYUN.Abp.IM/LINGYUN/Abp/IM/Messages/ChatMessage.cs#L11-L94) ## 消息内容存储策略 消息内容存储采用分层策略,根据