import json from datetime import datetime from sqlalchemy import BigInteger, String, Text, Integer, TIMESTAMP from sqlalchemy.orm import DeclarativeBase, mapped_column class Base(DeclarativeBase): pass class ArchiveMessages(Base): __tablename__ = 'archive_messages' __table_args__ = { 'comment': '企微会话存档消息表' } id = mapped_column(BigInteger, primary_key=True, autoincrement=True, comment='自增主键') msgid = mapped_column(String(255), nullable=False, comment='企微消息唯一ID') seq = mapped_column(BigInteger, nullable=False, comment='存档序列号,用于增量拉取') corp_id = mapped_column(String(64), nullable=False, comment='企业ID') action = mapped_column(String(32), default='send', comment='send/recall/agree/disagree') from_user = mapped_column(String(128), nullable=False, comment='发送者userid') from_role = mapped_column(String(32), nullable=False, comment='发送者角色: INTERNAL-企业内部成员 EXTERNAL-外部联系人 SYSTEM-系统') to_user = mapped_column(String(128), nullable=True, comment='接收者userid(单聊)') tolist = mapped_column(Text, nullable=True, comment='接收者列表(群聊),JSON数组字符串') roomid = mapped_column(String(255), nullable=True, comment='群聊ID(群聊时有值)') msgtype = mapped_column(String(64), nullable=False, comment='text/image/voice/video/file/link/location/weapp/chatrecord/voip') msgtime = mapped_column(BigInteger, nullable=False, comment='消息发送时间,UTC毫秒时间戳') content = mapped_column(Text, nullable=True, comment='文本消息内容(长文本使用LONGTEXT)') media_data = mapped_column(Text, nullable=True, comment='媒体消息元数据(sdkfileid/文件大小/时长等),JSON字符串') session_id = mapped_column(String(128), nullable=True, comment='关联的会话ID(业务生成)') decrypt_status = mapped_column(Integer, default=1, comment='解密状态: 1成功 2失败') decrypt_error = mapped_column(String(512), nullable=True, comment='解密失败原因') created_at = mapped_column(TIMESTAMP, nullable=False, default=datetime.now, comment='创建时间') updated_at = mapped_column(TIMESTAMP, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间') def to_dict(self): return { 'id': self.id, 'msgid': self.msgid, 'seq': self.seq, 'corp_id': self.corp_id, 'action': self.action, 'from_user': self.from_user, 'from_role': self.from_role, 'to_user': self.to_user, 'tolist': self.tolist, 'roomid': self.roomid, 'msgtype': self.msgtype, 'msgtime': self.msgtime, 'content': self.content, 'media_data': self.media_data, 'session_id': self.session_id, 'decrypt_status': self.decrypt_status, 'decrypt_error': self.decrypt_error, 'created_at': self.created_at.isoformat() if self.created_at else None, 'updated_at': self.updated_at.isoformat() if self.updated_at else None } def __repr__(self): return json.dumps(self.to_dict())