golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

message_repo.go 1.1 KiB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package repo
  2. import (
  3. "fmt"
  4. "gim/internal/logic/domain/message/model"
  5. "gim/pkg/db"
  6. "gim/pkg/gerrors"
  7. )
  8. const messageTableNum = 1
  9. type messageRepo struct{}
  10. var MessageRepo = new(messageRepo)
  11. func (*messageRepo) tableName(userId int64) string {
  12. return fmt.Sprintf("message_%03d", userId%messageTableNum)
  13. }
  14. // Save 插入一条消息
  15. func (d *messageRepo) Save(message model.Message) error {
  16. err := db.DB.Table(d.tableName(message.UserId)).Create(&message).Error
  17. if err != nil {
  18. return gerrors.WrapError(err)
  19. }
  20. return nil
  21. }
  22. // ListBySeq 根据类型和id查询大于序号大于seq的消息
  23. func (d *messageRepo) ListBySeq(userId, seq, limit int64) ([]model.Message, bool, error) {
  24. DB := db.DB.Table(d.tableName(userId)).
  25. Where("user_id = ? and seq > ?", userId, seq)
  26. var count int64
  27. err := DB.Count(&count).Error
  28. if err != nil {
  29. return nil, false, gerrors.WrapError(err)
  30. }
  31. if count == 0 {
  32. return nil, false, nil
  33. }
  34. var messages []model.Message
  35. err = DB.Limit(limit).Find(&messages).Error
  36. if err != nil {
  37. return nil, false, gerrors.WrapError(err)
  38. }
  39. return messages, count > limit, nil
  40. }