perf(chat): 使用预聚合快照优化客服会话统计
This commit is contained in:
@@ -30,8 +30,9 @@ var adminChatFilters = []string{adminChatFilterMine, adminChatFilterAll, adminCh
|
||||
var adminChatStages = []string{adminChatStageAll, adminChatStagePending, adminChatStageUnjoined, adminChatStageHandoff, adminChatStageRenting, adminChatStageAfterSale, adminChatStageEnded}
|
||||
|
||||
type AdminConversationCountsDTO struct {
|
||||
Ownership map[string]int64 `json:"ownership"`
|
||||
Stages map[string]int64 `json:"stages"`
|
||||
Ownership map[string]int64 `json:"ownership"`
|
||||
Stages map[string]int64 `json:"stages"`
|
||||
Refreshing bool `json:"refreshing,omitempty"`
|
||||
}
|
||||
|
||||
func (r *Repository) TransferConversation(ctx context.Context, principal Principal, conversationID uint64, toAdminID uint64) error {
|
||||
@@ -98,10 +99,13 @@ func (r *Repository) TransferConversation(ctx context.Context, principal Princip
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil && r.hub != nil {
|
||||
if err == nil {
|
||||
event := tenantScopedEvent(r.db.WithContext(ctx), &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
r.invalidateAdminChatCounts(ctx, event.TenantIDs)
|
||||
if r.hub != nil {
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -288,15 +292,25 @@ func (r *Repository) AdminConversationCounts(ctx context.Context, principal Prin
|
||||
filter = normalizeAdminChatFilter(filter)
|
||||
stage = normalizeAdminChatStage(stage)
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
if cached := r.loadAdminChatCountsCache(ctx, principal, filter, stage, keyword); cached != nil {
|
||||
return cached, nil
|
||||
if r.redis == nil || keyword != "" {
|
||||
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.DTO, nil
|
||||
}
|
||||
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
generation := r.adminChatCountsGeneration(ctx)
|
||||
if cached := r.loadAdminChatCountsSnapshot(ctx, principal, filter, stage, keyword); cached != nil {
|
||||
if cached.Generation == generation && time.Since(cached.GeneratedAt) <= adminChatCountsFreshFor {
|
||||
return cached.Counts, nil
|
||||
}
|
||||
r.refreshAdminChatCountsAsync(ctx, principal, filter, stage, keyword)
|
||||
stale := *cached.Counts
|
||||
stale.Refreshing = true
|
||||
return &stale, nil
|
||||
}
|
||||
r.storeAdminChatCountsCache(ctx, principal, filter, stage, keyword, result.DTO)
|
||||
return result.DTO, nil
|
||||
return r.refreshAdminChatCounts(ctx, principal, filter, stage, keyword)
|
||||
}
|
||||
|
||||
func (r *Repository) adminConversationTotal(ctx context.Context, principal Principal, filter string, stage string, keyword string) (int64, error) {
|
||||
@@ -397,7 +411,7 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin
|
||||
StageAll, StagePending, StageUnjoined, StageHandoff, StageRenting, StageAfterSale, StageEnded int64
|
||||
}
|
||||
var result row
|
||||
if err := r.adminConversationBase(ctx, principal, keyword).Select(strings.Join(parts, ", "), args...).Scan(&result).Error; err != nil {
|
||||
if err := r.adminConversationCountsBase(ctx, principal).Select(strings.Join(parts, ", "), args...).Scan(&result).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &adminConversationCountsResult{
|
||||
@@ -409,6 +423,19 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin
|
||||
}, nil
|
||||
}
|
||||
|
||||
// adminConversationCountsBase 只保留统计条件真正使用的关联。
|
||||
// 管理端的展示关联不参与计数,避免每次统计都额外访问状态表和商品表。
|
||||
func (r *Repository) adminConversationCountsBase(ctx context.Context, principal Principal) *gorm.DB {
|
||||
db := r.db.WithContext(ctx).Table("chat_conversations AS c").
|
||||
Joins("LEFT JOIN chat_participants AS cp_me ON cp_me.conversation_id = c.id AND cp_me.participant_type = ? AND cp_me.participant_id = ?", principal.Type, principal.ID).
|
||||
Joins("LEFT JOIN rental_orders AS explicit_lo ON explicit_lo.id = c.order_id")
|
||||
scoped, err := adminConversationScope(ctx, db, principal.ID)
|
||||
if err == nil {
|
||||
return scoped
|
||||
}
|
||||
return tenantConversationScope(ctx, db)
|
||||
}
|
||||
|
||||
func normalizeAdminChatFilter(filter string) string {
|
||||
switch filter {
|
||||
case adminChatFilterAll, adminChatFilterMine, adminChatFilterUnassigned:
|
||||
|
||||
Reference in New Issue
Block a user