perf(chat): 使用预聚合快照优化客服会话统计

This commit is contained in:
yml
2026-11-20 19:46:09 +08:00
parent ec6f79f196
commit e01a01c1aa
8 changed files with 282 additions and 24 deletions
+7 -3
View File
@@ -9,8 +9,10 @@ import (
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
"golang.org/x/sync/singleflight"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/chathub"
)
@@ -24,6 +26,7 @@ type Repository struct {
ocrGate *ocrRequestGate
ocrSleep func(context.Context, time.Duration) error
ocrServiceURL string
countsGroup singleflight.Group
}
const (
@@ -177,14 +180,15 @@ func EnsureOrderConversation(tx *gorm.DB, order model.RentalOrder) (*model.ChatC
return &conversation, nil
}
func (r *Repository) NotifyNewConversation(conversationID uint64) {
if r.hub == nil {
return
}
event := &chathub.ChatEvent{
Type: "conversation_updated",
ConversationID: conversationID,
}
tenantScopedEvent(r.db, event)
r.invalidateAdminChatCounts(context.Background(), event.TenantIDs)
if r.hub == nil {
return
}
r.hub.NotifyConversation(conversationID, event)
// 新会话(如未分配的平台咨询)需让所有在线客服的列表实时出现。
r.hub.NotifyAllAdmins(event)