perf(chat): 使用预聚合快照优化客服会话统计
This commit is contained in:
@@ -6,19 +6,68 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"hfb_sys/backend/internal/tenancy"
|
"hfb_sys/backend/internal/tenancy"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const adminChatCountsCacheTTL = 5 * time.Second
|
const (
|
||||||
|
adminChatCountsCacheTTL = 30 * time.Minute
|
||||||
|
adminChatCountsFreshFor = 15 * time.Second
|
||||||
|
adminChatCountsRefreshTTL = 20 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type adminChatCountsSnapshot struct {
|
||||||
|
Counts *AdminConversationCountsDTO `json:"counts"`
|
||||||
|
GeneratedAt time.Time `json:"generated_at"`
|
||||||
|
Generation int64 `json:"generation"`
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Repository) adminChatCountsCacheKey(ctx context.Context, principal Principal, filter, stage, keyword string) string {
|
func (r *Repository) adminChatCountsCacheKey(ctx context.Context, principal Principal, filter, stage, keyword string) string {
|
||||||
sum := sha256.Sum256([]byte(keyword))
|
sum := sha256.Sum256([]byte(keyword))
|
||||||
return fmt.Sprintf("admin-chat:counts:v3:%s:%d:%s:%s:%s:%s", principal.Type, principal.ID, tenancy.ScopeKey(ctx), filter, stage, hex.EncodeToString(sum[:8]))
|
return fmt.Sprintf("admin-chat:counts:v4:%s:%d:%s:%s:%s:%s", principal.Type, principal.ID, tenancy.ScopeKey(ctx), filter, stage, hex.EncodeToString(sum[:8]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) loadAdminChatCountsCache(ctx context.Context, principal Principal, filter, stage, keyword string) *AdminConversationCountsDTO {
|
func adminChatCountsGenerationKey(scope string) string {
|
||||||
|
return "admin-chat:counts:generation:" + scope
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) adminChatCountsGeneration(ctx context.Context) int64 {
|
||||||
|
if r.redis == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
raw, err := r.redis.Get(ctx, adminChatCountsGenerationKey(tenancy.ScopeKey(ctx))).Result()
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
value, _ := strconv.ParseInt(raw, 10, 64)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) invalidateAdminChatCounts(ctx context.Context, tenantIDs []uint64) {
|
||||||
|
if r.redis == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen := make(map[uint64]struct{}, len(tenantIDs))
|
||||||
|
for _, tenantID := range tenantIDs {
|
||||||
|
if tenantID == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[tenantID]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[tenantID] = struct{}{}
|
||||||
|
_ = r.redis.Incr(ctx, adminChatCountsGenerationKey(fmt.Sprintf("tenant:%d", tenantID))).Err()
|
||||||
|
}
|
||||||
|
if len(seen) == 0 {
|
||||||
|
_ = r.redis.Incr(ctx, adminChatCountsGenerationKey(tenancy.ScopeKey(ctx))).Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) loadAdminChatCountsSnapshot(ctx context.Context, principal Principal, filter, stage, keyword string) *adminChatCountsSnapshot {
|
||||||
if r.redis == nil {
|
if r.redis == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -26,20 +75,73 @@ func (r *Repository) loadAdminChatCountsCache(ctx context.Context, principal Pri
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var value AdminConversationCountsDTO
|
var value adminChatCountsSnapshot
|
||||||
if err := json.Unmarshal(raw, &value); err != nil {
|
if err := json.Unmarshal(raw, &value); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if value.Counts == nil || value.GeneratedAt.IsZero() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &value
|
return &value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) storeAdminChatCountsCache(ctx context.Context, principal Principal, filter, stage, keyword string, value *AdminConversationCountsDTO) {
|
func (r *Repository) storeAdminChatCountsSnapshot(ctx context.Context, principal Principal, filter, stage, keyword string, value *AdminConversationCountsDTO, generation int64) {
|
||||||
if r.redis == nil || value == nil {
|
if r.redis == nil || value == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
raw, err := json.Marshal(value)
|
raw, err := json.Marshal(adminChatCountsSnapshot{
|
||||||
|
Counts: value,
|
||||||
|
GeneratedAt: time.Now(),
|
||||||
|
Generation: generation,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = r.redis.Set(ctx, r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword), raw, adminChatCountsCacheTTL).Err()
|
_ = r.redis.Set(ctx, r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword), raw, adminChatCountsCacheTTL).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repository) refreshAdminChatCounts(ctx context.Context, principal Principal, filter, stage, keyword string) (*AdminConversationCountsDTO, error) {
|
||||||
|
key := r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword)
|
||||||
|
value, err, _ := r.countsGroup.Do(key, func() (any, error) {
|
||||||
|
generation := r.adminChatCountsGeneration(ctx)
|
||||||
|
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r.storeAdminChatCountsSnapshot(ctx, principal, filter, stage, keyword, result.DTO, generation)
|
||||||
|
return result.DTO, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
counts, ok := value.(*AdminConversationCountsDTO)
|
||||||
|
if !ok || counts == nil {
|
||||||
|
return nil, fmt.Errorf("invalid admin chat counts snapshot")
|
||||||
|
}
|
||||||
|
return counts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) refreshAdminChatCountsAsync(ctx context.Context, principal Principal, filter, stage, keyword string) {
|
||||||
|
if r.redis == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key := r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword)
|
||||||
|
ch := r.countsGroup.DoChan(key, func() (any, error) {
|
||||||
|
refreshCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), adminChatCountsRefreshTTL)
|
||||||
|
defer cancel()
|
||||||
|
generation := r.adminChatCountsGeneration(refreshCtx)
|
||||||
|
result, err := r.adminConversationCounts(refreshCtx, principal, filter, stage, keyword)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r.storeAdminChatCountsSnapshot(refreshCtx, principal, filter, stage, keyword, result.DTO, generation)
|
||||||
|
return result.DTO, nil
|
||||||
|
})
|
||||||
|
select {
|
||||||
|
case result := <-ch:
|
||||||
|
if result.Err != nil && r.logger != nil {
|
||||||
|
r.logger.Warn("后台客服统计刷新失败", zap.Error(result.Err))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hfb_sys/backend/internal/model"
|
||||||
|
"hfb_sys/backend/internal/tenancy"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAdminConversationCountsUsesFreshSnapshot(t *testing.T) {
|
||||||
|
db := setupAdminListTestDB(t)
|
||||||
|
client := redis.NewClient(&redis.Options{Addr: "127.0.0.1:16379", DB: 15})
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_ = client.Close()
|
||||||
|
})
|
||||||
|
if err := client.Ping(t.Context()).Err(); err != nil {
|
||||||
|
t.Skipf("Redis 测试服务不可用: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
admin := model.AdminUser{Username: "counts-cache", Nickname: "缓存测试", Status: "active"}
|
||||||
|
if err := db.Create(&admin).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
conversation := model.ChatConversation{
|
||||||
|
TenantID: 1,
|
||||||
|
Type: ConversationTypeGeneralSupport,
|
||||||
|
Title: "缓存测试会话",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
if err := db.Create(&conversation).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
participant := model.ChatParticipant{
|
||||||
|
ConversationID: conversation.ID,
|
||||||
|
ParticipantType: "admin",
|
||||||
|
ParticipantID: admin.ID,
|
||||||
|
Role: "support",
|
||||||
|
JoinedAt: time.Now(),
|
||||||
|
}
|
||||||
|
if err := db.Create(&participant).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo := NewRepository(db, nil, client)
|
||||||
|
ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: 1})
|
||||||
|
principal := Principal{Type: "admin", ID: admin.ID}
|
||||||
|
cacheKey := repo.adminChatCountsCacheKey(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_ = client.Del(t.Context(), cacheKey).Err()
|
||||||
|
})
|
||||||
|
|
||||||
|
first, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if first.Ownership[adminChatFilterMine] != 1 {
|
||||||
|
t.Fatalf("首次统计 mine = %d, want 1", first.Ownership[adminChatFilterMine])
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Delete(&participant).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cached, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if cached.Ownership[adminChatFilterMine] != 1 {
|
||||||
|
t.Fatalf("15 秒内应读取快照,mine = %d, want 1", cached.Ownership[adminChatFilterMine])
|
||||||
|
}
|
||||||
|
|
||||||
|
repo.invalidateAdminChatCounts(ctx, []uint64{1})
|
||||||
|
|
||||||
|
stale, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !stale.Refreshing {
|
||||||
|
t.Fatal("过期快照应标记 refreshing")
|
||||||
|
}
|
||||||
|
|
||||||
|
raw, err := client.Get(t.Context(), cacheKey).Bytes()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var snapshot adminChatCountsSnapshot
|
||||||
|
deadline := time.Now().Add(3 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
raw, err = client.Get(t.Context(), cacheKey).Bytes()
|
||||||
|
if err == nil && json.Unmarshal(raw, &snapshot) == nil && snapshot.Counts.Ownership[adminChatFilterMine] == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Fatal("过期快照未在超时前完成后台刷新")
|
||||||
|
}
|
||||||
@@ -186,6 +186,7 @@ func (r *Repository) SendMessage(ctx context.Context, principal Principal, conve
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
tenantScopedEvent(r.db.WithContext(ctx), event)
|
tenantScopedEvent(r.db.WithContext(ctx), event)
|
||||||
|
r.invalidateAdminChatCounts(ctx, event.TenantIDs)
|
||||||
r.hub.NotifyConversation(conversationID, event)
|
r.hub.NotifyConversation(conversationID, event)
|
||||||
// 同时广播给所有在线客服,使「未分配 / 全部」视图实时刷新;
|
// 同时广播给所有在线客服,使「未分配 / 全部」视图实时刷新;
|
||||||
// 参与者客服会重复收到,由前端按消息 id 去重。
|
// 参与者客服会重复收到,由前端按消息 id 去重。
|
||||||
|
|||||||
@@ -805,6 +805,7 @@ func (r *Repository) notifyDeliveredQrCodeMessages(messages []model.ChatMessage)
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
tenantScopedEvent(r.db, event)
|
tenantScopedEvent(r.db, event)
|
||||||
|
r.invalidateAdminChatCounts(context.Background(), event.TenantIDs)
|
||||||
r.hub.NotifyConversation(msg.ConversationID, event)
|
r.hub.NotifyConversation(msg.ConversationID, event)
|
||||||
r.hub.NotifyAllAdmins(event)
|
r.hub.NotifyAllAdmins(event)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ import (
|
|||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"golang.org/x/sync/singleflight"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
|
|
||||||
"hfb_sys/backend/internal/model"
|
"hfb_sys/backend/internal/model"
|
||||||
"hfb_sys/backend/internal/modules/chathub"
|
"hfb_sys/backend/internal/modules/chathub"
|
||||||
)
|
)
|
||||||
@@ -24,6 +26,7 @@ type Repository struct {
|
|||||||
ocrGate *ocrRequestGate
|
ocrGate *ocrRequestGate
|
||||||
ocrSleep func(context.Context, time.Duration) error
|
ocrSleep func(context.Context, time.Duration) error
|
||||||
ocrServiceURL string
|
ocrServiceURL string
|
||||||
|
countsGroup singleflight.Group
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -177,14 +180,15 @@ func EnsureOrderConversation(tx *gorm.DB, order model.RentalOrder) (*model.ChatC
|
|||||||
return &conversation, nil
|
return &conversation, nil
|
||||||
}
|
}
|
||||||
func (r *Repository) NotifyNewConversation(conversationID uint64) {
|
func (r *Repository) NotifyNewConversation(conversationID uint64) {
|
||||||
if r.hub == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
event := &chathub.ChatEvent{
|
event := &chathub.ChatEvent{
|
||||||
Type: "conversation_updated",
|
Type: "conversation_updated",
|
||||||
ConversationID: conversationID,
|
ConversationID: conversationID,
|
||||||
}
|
}
|
||||||
tenantScopedEvent(r.db, event)
|
tenantScopedEvent(r.db, event)
|
||||||
|
r.invalidateAdminChatCounts(context.Background(), event.TenantIDs)
|
||||||
|
if r.hub == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
r.hub.NotifyConversation(conversationID, event)
|
r.hub.NotifyConversation(conversationID, event)
|
||||||
// 新会话(如未分配的平台咨询)需让所有在线客服的列表实时出现。
|
// 新会话(如未分配的平台咨询)需让所有在线客服的列表实时出现。
|
||||||
r.hub.NotifyAllAdmins(event)
|
r.hub.NotifyAllAdmins(event)
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ var adminChatStages = []string{adminChatStageAll, adminChatStagePending, adminCh
|
|||||||
type AdminConversationCountsDTO struct {
|
type AdminConversationCountsDTO struct {
|
||||||
Ownership map[string]int64 `json:"ownership"`
|
Ownership map[string]int64 `json:"ownership"`
|
||||||
Stages map[string]int64 `json:"stages"`
|
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 {
|
func (r *Repository) TransferConversation(ctx context.Context, principal Principal, conversationID uint64, toAdminID uint64) error {
|
||||||
@@ -98,11 +99,14 @@ func (r *Repository) TransferConversation(ctx context.Context, principal Princip
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err == nil && r.hub != nil {
|
if err == nil {
|
||||||
event := tenantScopedEvent(r.db.WithContext(ctx), &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
event := tenantScopedEvent(r.db.WithContext(ctx), &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
||||||
|
r.invalidateAdminChatCounts(ctx, event.TenantIDs)
|
||||||
|
if r.hub != nil {
|
||||||
r.hub.NotifyConversation(conversationID, event)
|
r.hub.NotifyConversation(conversationID, event)
|
||||||
r.hub.NotifyAllAdmins(event)
|
r.hub.NotifyAllAdmins(event)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (r *Repository) GetAvailableSupportAdmins(ctx context.Context) ([]SupportAdminDTO, error) {
|
func (r *Repository) GetAvailableSupportAdmins(ctx context.Context) ([]SupportAdminDTO, error) {
|
||||||
@@ -288,15 +292,25 @@ func (r *Repository) AdminConversationCounts(ctx context.Context, principal Prin
|
|||||||
filter = normalizeAdminChatFilter(filter)
|
filter = normalizeAdminChatFilter(filter)
|
||||||
stage = normalizeAdminChatStage(stage)
|
stage = normalizeAdminChatStage(stage)
|
||||||
keyword = strings.TrimSpace(keyword)
|
keyword = strings.TrimSpace(keyword)
|
||||||
if cached := r.loadAdminChatCountsCache(ctx, principal, filter, stage, keyword); cached != nil {
|
if r.redis == nil || keyword != "" {
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
r.storeAdminChatCountsCache(ctx, principal, filter, stage, keyword, result.DTO)
|
|
||||||
return result.DTO, nil
|
return result.DTO, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
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) {
|
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
|
StageAll, StagePending, StageUnjoined, StageHandoff, StageRenting, StageAfterSale, StageEnded int64
|
||||||
}
|
}
|
||||||
var result row
|
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 nil, err
|
||||||
}
|
}
|
||||||
return &adminConversationCountsResult{
|
return &adminConversationCountsResult{
|
||||||
@@ -409,6 +423,19 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin
|
|||||||
}, nil
|
}, 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 {
|
func normalizeAdminChatFilter(filter string) string {
|
||||||
switch filter {
|
switch filter {
|
||||||
case adminChatFilterAll, adminChatFilterMine, adminChatFilterUnassigned:
|
case adminChatFilterAll, adminChatFilterMine, adminChatFilterUnassigned:
|
||||||
|
|||||||
@@ -76,6 +76,9 @@ const remarkEditing = ref(false)
|
|||||||
const remarkValue = ref('')
|
const remarkValue = ref('')
|
||||||
let searchTimer: ReturnType<typeof setTimeout> | null = null
|
let searchTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
let countsTimer: ReturnType<typeof setTimeout> | null = null
|
let countsTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let countsRecheckTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let countsPollTimer: ReturnType<typeof setInterval> | null = null
|
||||||
|
let countsRecheckAttempts = 0
|
||||||
let countsCacheKey = ''
|
let countsCacheKey = ''
|
||||||
let countsLoadedAt = 0
|
let countsLoadedAt = 0
|
||||||
|
|
||||||
@@ -203,11 +206,16 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
keyword.value = firstQueryValue(route.query.chat_keyword)
|
keyword.value = firstQueryValue(route.query.chat_keyword)
|
||||||
await Promise.all([loadConversations(), loadCounts(true), loadQuickReplies()])
|
await Promise.all([loadConversations(), loadCounts(true), loadQuickReplies()])
|
||||||
|
countsPollTimer = setInterval(() => {
|
||||||
|
if (!document.hidden) void loadCounts(true)
|
||||||
|
}, 30_000)
|
||||||
})
|
})
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
if (searchTimer) clearTimeout(searchTimer)
|
if (searchTimer) clearTimeout(searchTimer)
|
||||||
if (countsTimer) clearTimeout(countsTimer)
|
if (countsTimer) clearTimeout(countsTimer)
|
||||||
|
if (countsRecheckTimer) clearTimeout(countsRecheckTimer)
|
||||||
|
if (countsPollTimer) clearInterval(countsPollTimer)
|
||||||
})
|
})
|
||||||
|
|
||||||
async function loadConversations(showLoading = true, resetPage = false) {
|
async function loadConversations(showLoading = true, resetPage = false) {
|
||||||
@@ -244,22 +252,37 @@ async function loadCounts(force = false) {
|
|||||||
keyword: keyword.value.trim(),
|
keyword: keyword.value.trim(),
|
||||||
}
|
}
|
||||||
const cacheKey = JSON.stringify(query)
|
const cacheKey = JSON.stringify(query)
|
||||||
if (!force && cacheKey === countsCacheKey && Date.now() - countsLoadedAt < 5000) return
|
if (!force && cacheKey === countsCacheKey && Date.now() - countsLoadedAt < 10_000) return
|
||||||
try {
|
try {
|
||||||
chatCounts.value = await fetchAdminChatCounts(query)
|
chatCounts.value = await fetchAdminChatCounts(query)
|
||||||
countsCacheKey = cacheKey
|
countsCacheKey = cacheKey
|
||||||
countsLoadedAt = Date.now()
|
countsLoadedAt = Date.now()
|
||||||
|
if (chatCounts.value.refreshing && countsRecheckAttempts < 2) {
|
||||||
|
countsRecheckAttempts++
|
||||||
|
if (countsRecheckTimer) clearTimeout(countsRecheckTimer)
|
||||||
|
countsRecheckTimer = setTimeout(() => {
|
||||||
|
countsRecheckTimer = null
|
||||||
|
void loadCounts(true)
|
||||||
|
}, 1500)
|
||||||
|
} else if (!chatCounts.value.refreshing) {
|
||||||
|
countsRecheckAttempts = 0
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
/* keep the last successful counts visible */
|
/* keep the last successful counts visible */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function scheduleCountsRefresh() {
|
function scheduleCountsRefresh() {
|
||||||
|
countsRecheckAttempts = 0
|
||||||
|
if (countsRecheckTimer) {
|
||||||
|
clearTimeout(countsRecheckTimer)
|
||||||
|
countsRecheckTimer = null
|
||||||
|
}
|
||||||
if (countsTimer) clearTimeout(countsTimer)
|
if (countsTimer) clearTimeout(countsTimer)
|
||||||
countsTimer = setTimeout(() => {
|
countsTimer = setTimeout(() => {
|
||||||
countsTimer = null
|
countsTimer = null
|
||||||
void loadCounts(true)
|
void loadCounts(true)
|
||||||
}, 1000)
|
}, 2000)
|
||||||
}
|
}
|
||||||
|
|
||||||
function handlePageChange(page: number) {
|
function handlePageChange(page: number) {
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ export function resolveSupportScene(path: string): SupportScene {
|
|||||||
export interface AdminChatCounts {
|
export interface AdminChatCounts {
|
||||||
ownership?: Record<string, number>
|
ownership?: Record<string, number>
|
||||||
stages?: Record<string, number>
|
stages?: Record<string, number>
|
||||||
|
refreshing?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AdminChatQuery {
|
export interface AdminChatQuery {
|
||||||
|
|||||||
Reference in New Issue
Block a user