|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package repo
-
- import (
- "fmt"
- "gim/internal/business/comm/utils"
- "gim/internal/logic/domain/message/model"
- "gim/pkg/db"
- "gim/pkg/gerrors"
- "time"
- )
-
- const messageTableNum = 1
-
- type messageRepo struct{}
-
- var MessageRepo = new(messageRepo)
-
- func (*messageRepo) tableName(userId int64) string {
- return fmt.Sprintf("message_%03d", userId%messageTableNum)
- }
-
- // Save 插入一条消息
- func (d *messageRepo) Save(message model.Message) error {
- err := db.DB.Table(d.tableName(message.UserId)).Create(&message).Error
- if err != nil {
- return gerrors.WrapError(err)
- }
- return nil
- }
-
- // ListBySeq 根据类型和id查询大于序号大于seq的消息
- func (d *messageRepo) ListBySeq(userId, seq, limit int64) ([]model.Message, bool, error) {
- DB := db.DB.Table(d.tableName(userId)).
- Where("user_id = ? and seq > ?", userId, seq)
-
- var count int64
- err := DB.Count(&count).Error
- if err != nil {
- return nil, false, gerrors.WrapError(err)
- }
- if count == 0 {
- return nil, false, nil
- }
-
- var messages []model.Message
- err = DB.Limit(limit).Find(&messages).Error
- if err != nil {
- return nil, false, gerrors.WrapError(err)
- }
- return messages, count > limit, nil
- }
-
- // GetMessage 获取消息状态
- func (d *messageRepo) GetMessage(senderId, seq int64) (*model.Message, error) {
- var message model.Message
- if err := db.DB.Table(d.tableName(senderId)).Where("sender_id = ? and seq = ?", senderId, seq).
- First(&message).Error; err != nil {
- return nil, gerrors.WrapError(err)
- }
- return &message, nil
- }
-
- // GetMessageSeqForSendTime 获取消息状态
- func (d *messageRepo) GetMessageSeqForSendTime(userId int64, sendTime time.Time) (int64, error) {
- var message model.Message
- if err := db.DB.Table(d.tableName(userId)).Where("user_id = ? and send_time = ?", userId, sendTime).
- First(&message).Error; err != nil {
- return 0, gerrors.WrapError(err)
- }
- utils.FilePutContents("GetMessageSeqForSendTime", utils.SerializeStr(map[string]interface{}{
- "userId": userId,
- "sendTime": sendTime,
- "message": message,
- "seq": message.Seq,
- }))
- return message.Seq, nil
- }
-
- // UpdateStatus 更新消息状态
- func (d *messageRepo) UpdateStatus(userId int64, sendTime time.Time, status int) (int64, error) {
- db := db.DB.Model(&model.Message{}).Table(d.tableName(userId)).Where("user_id = ? and send_time = ?", userId, sendTime).
- Update("status", status)
- if db.Error != nil {
- return 0, gerrors.WrapError(db.Error)
- }
-
- return db.RowsAffected, nil
- }
|