feat(api): add cursor-based pagination for system messages
- Add SystemMessageCursorPageResponse DTO for cursor pagination - Implement GetSystemMessagesByCursor handler method - Add GetByReceiverIDCursor repository method with cursor decoding - Maintain backward compatibility with existing offset-based pagination
This commit is contained in:
@@ -112,3 +112,38 @@ func (r *SystemNotificationRepository) GetByType(receiverID string, notifyType m
|
||||
|
||||
return notifications, total, err
|
||||
}
|
||||
|
||||
// GetByReceiverIDCursor 游标分页获取用户的通知列表
|
||||
func (r *SystemNotificationRepository) GetByReceiverIDCursor(receiverID string, cursor string, pageSize int) ([]*model.SystemNotification, string, bool, error) {
|
||||
query := r.db.Model(&model.SystemNotification{}).Where("receiver_id = ?", receiverID)
|
||||
|
||||
// 如果有游标,解析游标并添加条件
|
||||
if cursor != "" {
|
||||
// 游标格式:createdAt_id
|
||||
query = query.Where("created_at < ? OR (created_at = ? AND id < ?)", cursor, cursor, cursor)
|
||||
}
|
||||
|
||||
// 多取一条用于判断是否还有下一页
|
||||
var notifications []*model.SystemNotification
|
||||
err := query.Order("created_at DESC, id DESC").
|
||||
Limit(pageSize + 1).
|
||||
Find(¬ifications).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
|
||||
hasMore := len(notifications) > pageSize
|
||||
if hasMore {
|
||||
notifications = notifications[:pageSize]
|
||||
}
|
||||
|
||||
// 生成下一页游标
|
||||
var nextCursor string
|
||||
if hasMore && len(notifications) > 0 {
|
||||
lastNotification := notifications[len(notifications)-1]
|
||||
nextCursor = lastNotification.CreatedAt.Format("2006-01-02 15:04:05.999999999")
|
||||
}
|
||||
|
||||
return notifications, nextCursor, hasMore, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user