优化了消息数量,筛选和顶部部分

This commit is contained in:
yml2213
2026-06-13 19:03:29 +08:00
parent 3b4cd76e42
commit 9d7184ecde
12 changed files with 223 additions and 12 deletions
@@ -40,6 +40,18 @@ func (r *Repository) conversationQuery(ctx context.Context, principal Principal)
Joins("JOIN chat_participants AS cp ON cp.conversation_id = c.id").
Where("cp.participant_type = ? AND cp.participant_id = ?", principal.Type, principal.ID)
}
func (r *Repository) CountUnreadMessages(ctx context.Context, principal Principal) (int64, error) {
var total int64
err := r.db.WithContext(ctx).Table("chat_messages AS cm").
Joins("JOIN chat_participants AS cp ON cp.conversation_id = cm.conversation_id").
Where("cp.participant_type = ? AND cp.participant_id = ?", principal.Type, principal.ID).
Where("NOT (cm.sender_type = ? AND cm.sender_id = ?)", principal.Type, principal.ID).
Where("(cp.last_read_at IS NULL OR cm.created_at > cp.last_read_at)").
Count(&total).Error
return total, err
}
func (r *Repository) findParticipant(tx *gorm.DB, principal Principal, conversationID uint64, lock bool) (*model.ChatParticipant, error) {
var participant model.ChatParticipant
db := tx